Posted: 23rd Nov 2021 14:15
Hi

I have adapted a code From Yrahen (pb code), in AGK.

It's an example to use a php page and SendHTTPRequest() which works fine .
You can do a lot of things with that kind of codes (a tchat, a simple online game (very basic, but it could work), online hi-score and a lots of things.
I have not add security test (with header for example), but it's an example to show how to use AGk and a simple PHP code :

Source :
in 2014, Yrahen has created a code (in Purebasic and PHP) to create a tchat system with no server, no DataBase, and simple.
I have adapted this code for AppGameKit .

The AppGameKit code :
+ Code Snippet
// Project: Tchat test 
// Created: 21-11-23

SetErrorMode(2)

// set window properties
SetWindowTitle( "Tchat (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"


CreateText(1,"")
SetTextSize(1,20)

CreateEditBox(0)
SetEditBoxPosition(0,0, 570)
SetEditBoxSize(0,1024,30)
SetEditBoxTextSize(0,30)


// main loop
do
	

	
	
	If GetPointerPressed()
		y = getpointery()
		StartTchaty = y -TchatY
	endif
	if GetPointerState()
		y = getpointery()
		TchatY = y -StartTchaty
		SetTextPosition(1,0,TchatY)
	endif
	
	If GetRawKeyReleased(13) // enter
		Message$ = GetEditBoxText(0)
		if message$ <> ""
			// send a message in the tchat.
			
			// 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
			// change the pseudo here :)
			pseudo$="AGK2"
			
			// it's necessary to encode it (for some words)
			Message$ = HTTPEncode(Message$)
			// but, it seems the HTTPEncode() doesn't works for accents.
			Message$ = ReplaceString(Message$,"?","%C3%A9",-1)
			Message$ = ReplaceString(Message$,"?","%C3%A8",-1)
			Message$ = ReplaceString(Message$,"?","%C3%A2",-1)
			Message$ = ReplaceString(Message$,"?","%C3%B4",-1)
			Message$ = ReplaceString(Message$,"?","%C3%A0",-1)
			Message$ = ReplaceString(Message$,"?","%C3%AA",-1)
			Message$ = ReplaceString(Message$,"?","%C3%B9",-1)
			Message$ = ReplaceString(Message$,"?","%C3%A7",-1)
			// the create the string for szServerFile for SendHTTPRequest()
			post$ = "tchat.php?Action=Post&Pseudo="+Pseudo$+"&Message="+Message$ 
			
			// 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)
			if GetDeviceBaseName()<> "android"
				SetEditBoxFocus(0,1)
				SetEditBoxText(0,"")
			endif
		endif
		SetEditBoxFocus(0,1)
	endif
		
	Refresh()
    // Print( "Server response: " + response$ )
    Sync()
loop


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$ ="tchat.php?Action=Refresh"
		r$ = SendHTTPRequest( iHTTP, url$ )
		// then get the response, and if it's not "", we can add it to the tchat.
		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 


The PHP script : should be in your website (web server), name it tchat.php (or anything you want, but don't forget to change the name in the agk code ).
+ Code Snippet
Tchat.php :

<?php
if(!is_file('Tchat.txt'))
{
	$Tchat=array();
	file_put_contents('Tchat.txt', serialize($Tchat));
}
if(isset($_GET['Action']))
{
	if($_GET['Action']=='Post' AND isset($_GET['Pseudo']) AND isset($_GET['Message']))
	{
		$Tchat=unserialize(file_get_contents('Tchat.txt'));
		$Tchat[time()]=$_GET['Pseudo'].':'.$_GET['Message'];
		file_put_contents('Tchat.txt', serialize($Tchat));		
	}
	elseif($_GET['Action']=='Refresh')
	{
		$Tchat=unserialize(file_get_contents('Tchat.txt'));
		ksort($Tchat);
		foreach($Tchat as $Time => $Message)
		{
			echo $Message.'<br/>';
		}
	}
}
?>


I hope this can be usefull
A+
Posted: 23rd Nov 2021 15:39
Nice, basic but effective, easy to bot spam tho

I have a half written tutorial showing how to use AGK/PHP/SQL to have full on persistent data (for example) user profiles and games stats, player driven market's, game event logging and replay (like clash of clans, pseudo multiplayer), its a powerful and secure system using session and api keys, I will finish it when this game comp is done.

A neat trick for using async is to send some prefix arguments that you send back (like message even codes) so the callback knows what action the response if for, I use a WinAPI style message system with a json table so you can send complex stacked request and have a WinProc style callback to process them, it expands the functionally of AppGameKit exponentially

with sync http calls like that if you hit latently the game will lock up, not good, I'd go with asynchronous every time.
Posted: 23rd Nov 2021 15:46
This is one of my test apps showing highscore tables with leaderboards, api key driven (click leaderboard text to view)
Posted: 23rd Nov 2021 15:52
Hi

Nice, basic but effective, easy to bot spam tho

Yes, it's a very simple and basic system, to show for new users who don't know php how to create a simple tchat system with agk and php .

Against the bots, this could be made in the AppGameKit client (and in the php script of course). For example :
- the player can send max 1message per 2 or 3 seconds.
- we could limit the characters which are send.
and so ^^

neat trick for using async is to send some prefix arguments that you send back (like message even codes) so the callback knows what action the response if for, I use a WinAPI style message system with a json table so you can send complex stacked request and have a WinProc style callback to process them, it expands the functionally of AppGameKit exponentially


Waho, I am very very interested by your systeme with AppGameKit + PHP

But should your script be executed on the server (for example, with "chmod +x" or other things like that), or can it be "no executed" (like the script of this example) ?
Because i only have a website (webserver), but I can't launch php script, just copy it on my site thanks to FTP ^^.


with sync http calls like that if you hit latently the game will lock up, not good, I'd go with asynchronous every time.

Yes, it's better to use an async request, I totally agree .

If I have the time, I will change the code to use an async request .
Posted: 23rd Nov 2021 15:53
https://parttimecoder.itch.io/iscore-test-app

Sorry, I have a 404 error with your link, I think it's private
Posted: 23rd Nov 2021 16:16
Yeah soz its a private link, forgot to grab a preview link

https://parttimecoder.itch.io/iscore-test-app?secret=1btdL3yU9obDQndCL2Dus1L4

But should your script be executed on the server


That example ^^ is running from a remote server, all PHP/SQL no problems : https://www.000webhost.com/

WIP Site [its broken lol, I am not good with CSS] : https://parttimesoftware.000webhostapp.com/index.php

I will finish the site soon ... the framework is done just need to sort the formatting

Edit: next year I will consolidate all my stuff, fix the site, get some nice stuff going, until now I have been lazy, time to start focus!