/* A simple example for a highscore online, with a php script :) */ // agk code : // Project: HighScore // Created: 21-11-23 SetErrorMode(2) // set window properties SetWindowTitle( "HighScore (agk+php)" ) SetWindowSize( 1024, 600, 0 ) // set display properties SetVirtualResolution( 1024, 600 ) // doesn't have to match the window UseNewDefaultFonts( 1 ) // create variables Global iHttp, delayHttpResponse, MyURL$ // the delay to get info from our "tchat.php" page delayHttpResponse = 2000 // your url here : MyURL$ = "YourURL.com" if MyURL$ ="YourURL.com" message("plesae, set your own url (own site for example)") endif CreateText(1,"") SetTextSize(1,20) // send the highscore pseudo$ = "Player"+str(random(0, 500)) Myscore = random(10, 5000) Score$ = str(Myscore) SendScore(Pseudo$, score$) // main loop do If GetPointerPressed() y = getpointery() StartTchaty = y -TchatY endif if GetPointerState() y = getpointery() TchatY = y -StartTchaty SetTextPosition(1,0,TchatY) endif Refresh() // Print( "Server response: " + response$ ) Sync() loop Function SendScore(Pseudo$, score$) // create an http connection : iHTTP = CreateHTTPConnection() SetHTTPHost( iHTTP, MyURL$, 0 ) // if the server takes a long time to respond this could make the app unresponsive // it's necessary to encode it (for some words) Pseudo$ = HTTPEncode(Pseudo$) // but, it seems the HTTPEncode() doesn't works for accents. Pseudo$ = ReplaceString(Pseudo$,"é","%C3%A9",-1) Pseudo$ = ReplaceString(Pseudo$,"è","%C3%A8",-1) Pseudo$ = ReplaceString(Pseudo$,"â","%C3%A2",-1) Pseudo$ = ReplaceString(Pseudo$,"ô","%C3%B4",-1) Pseudo$ = ReplaceString(Pseudo$,"à","%C3%A0",-1) Pseudo$ = ReplaceString(Pseudo$,"ê","%C3%AA",-1) Pseudo$ = ReplaceString(Pseudo$,"ù","%C3%B9",-1) Pseudo$ = ReplaceString(Pseudo$,"ç","%C3%A7",-1) // the create the string for szServerFile for SendHTTPRequest() post$ = "highscore.php?Action=Post&Pseudo="+Pseudo$+"&Score="+Score$ // we could use Asynchronous, that doesn't wait for the response, because we doesn't need it Response$= SendHTTPRequest( iHTTP, post$) // just if we want to check the status of the request() int= GetHTTPStatusCode( iHTTP ) //~ message(REsponse$) //~ message(str(int)) CloseHTTPConnection(iHTTP) DeleteHTTPConnection(iHTTP) Endfunction Function Refresh() // to refresh the connection to get the new message from the php script // you can change the delay if you want. IF delayHttpResponse > Screenfps()/2 // create a new httpconnection iHTTP = CreateHTTPConnection() SetHTTPHost( iHTTP, MyURL$, 0 ) // set the string for SendHTTPRequest url$ ="highscore.php?Action=Refresh" r$ = SendHTTPRequest( iHTTP, url$ ) // then get the response, and if it's not "", we can add it to the highscore if r$ <> "" response$ = ReplaceString(r$,"<br/>",Chr(10),-1) response$ = ReplaceString(response$,"\'","'",-1) response$ = ReplaceString(response$,"\"+chr(34),chr(34),-1) // \" SetTextString(1,response$) endif // int= GetHTTPStatusCode( iHTTP ) delayHttpResponse = 0 CloseHTTPConnection(iHTTP) DeleteHTTPConnection(iHTTP) else inc delayHttpResponse endif Endfunction /* Then the code for the php script, you have to send it to your personnal website (or webserver), via FTP for example. */ // be carefull, no verification is made. <?php if(!is_file('HighScore.txt')) { $Score=array(); file_put_contents('HighScore.txt', serialize($Score)); } if(isset($_GET['Action'])) { if($_GET['Action']=='Post' AND isset($_GET['Pseudo']) AND isset($_GET['Score'])) { $Score=unserialize(file_get_contents('HighScore.txt')); $Score[$_GET['Score']]=$_GET['Pseudo'].' : '.$_GET['Score']; file_put_contents('HighScore.txt', serialize($Score)); } elseif($_GET['Action']=='Refresh') { $Score=unserialize(file_get_contents('HighScore.txt')); if ($Score!="") { krsort($Score); foreach($Score as $key => $val) { echo $val.'<br/>'; } } } } ?>