HERE YOU GO: (I have to clean up the code when complete!)
This function is called when a user clicks the Leaderboard button for the first time. (Or if his/hers local Username.txt doesn't exist). It checks the database to make sure username doesn't exist and if not it will insert the new user into database.
I know the code is very sloppy but I didn't get a chance to clean it up. It does work on Android and when I learn a little more on PHP I'm gonna just use 1 script file to achieve this task.
function CreateNewUser
SetTextDepth(ShowLBStatusText, 0)
SetTextVisible(ShowLBStatusText, 1)
SetTextPosition(ShowLBStatusText, 46,77 )
myname as string
myname = temp_name$
Select NewUser_stage
case 1 //Create connection
if GetInternetState() = 1
g_Net_Connection = CreateHTTPConnection( )
rem ANDROID
rem g_Net_Connected = SetHTTPHost ( g_Net_Connection, sDomain, 0 )
rem g_Net_Connected = SetHTTPHost ( g_Net_Connection, sDomain, 0, "sbjleaderboard", "Anthony0906#" )
rem IOS
g_Net_Connected = SetHTTPHost ( g_Net_Connection, sDomain, 1, "sbjleaderboard", "Anthony0906#" )
NewUser_stage = 2
else
PlaySound(errorsound)
NewUser_stage = 0
EndIf
endcase
case 2 //Send the new username entered to see if it already exists!
SetTextColor( ShowLBStatusText, 255, 255, 255, 255 )
SetTextString ( ShowLBStatusText, "SENDING..." )
If GetFileExists( "UserCheck.dat" ) = 1 then DeleteFile( "UserCheck.dat" )
GetHTTPFile ( g_Net_Connection, sDirectory + "checkuser.php", "UserCheck.dat", "name=" + myname)
NewUser_stage = 3
endcase
case 3 //Sending name and Receiving UserCheck.dat from Server
perc#=GetHTTPFileProgress ( g_Net_Connection ) : perc=perc#
SetTextColor( ShowLBStatusText, 255, 255, 255, 255 )
SetTextString ( ShowLBStatusText, "WAITING..."+str(perc) )
if ( perc# >= 100.0 )
NewUser_stage = 4
endif
endcase
case 4 //Check UserCheck.dat to see if user exists=notnull or user doesnt exist=null
file = OpenToRead( "UserCheck.dat" )
tmp$ = ReadLine( file )
tmp$ = ReadLine( file ) //repeat twice because the php exports an empty line
CloseFile( file )
If tmp$ = ""
SetTextColor( ShowLBStatusText, 0, 255, 64, 255 )
SetTextString (ShowLBStatusText, "USERNAME IS VALID!" )
NewUser_stage = 5
Else
PlaySound(errorsound)
SetTextColor( ShowLBStatusText, 237, 28, 36, 255 )
SetTextString (ShowLBStatusText, tmp$ + " IN USE!" ) : Sync() : Sleep( 2500 )
NewUser_stage = 0
SetTextVisible(ShowLBStatusText, 0)
EndIf
endcase
case 5 //Lets Add New User
myname as string : mycountry as string : myscore as string : tempscore as integer = 0
myscore = str(tempscore)
myname = temp_name$
mycountry = temp_country$
SetTextColor( ShowLBStatusText, 255, 255, 255, 255 )
SetTextString (ShowLBStatusText, "SENDING..." )
If GetFileExists("check.dat") = 1 then DeleteFile("check.dat")
GetHTTPFile( g_Net_Connection, sDirectory + "createuser.php", "check.dat", "name=" + myname + "&score=" + myscore + "&country=" + mycountry)
NewUser_stage = 6
endcase
case 6
perc#=GetHTTPFileProgress ( g_Net_Connection ) : perc=perc#
SetTextColor( ShowLBStatusText, 255, 255, 255, 255 )
SetTextString (ShowLBStatusText, "WAITING..."+str(perc) )
if ( perc# >= 100.0 )
NewUser_stage = 7
endif
endcase
case 7
file = OpenToRead( "check.dat" )
tmp$ = ReadLine( file )
tmp$ = ReadLine( file ) //repeat twice because the php exports an empty line
tmpID$ = ReadLine( file )
CloseFile( file )
If tmp$ = "1"
SetTextColor( ShowLBStatusText, 0, 255, 64, 255 )
SetTextString (ShowLBStatusText, "SUBMITTED!" ) : Sync() : Sleep( 2500 )
rem Create new text files.
opentowrite(1,"lbID.txt", 0)
WriteString(1, tmpID$)
closefile(1)
opentowrite(1,"lbname.txt", 0)
WriteString(1, temp_name$)
closefile(1)
opentowrite(1,"lbcountry.txt", 0)
WriteString(1, temp_country$)
closefile(1)
opentowrite(1,"lbdate.txt", 0)
WriteString(1, GetCurrentDate())
closefile(1)
NewUser_stage = 8
Else
SetTextColor( ShowLBStatusText, 237, 28, 36, 255 )
SetTextString (ShowLBStatusText, "UPLOAD FAILED!" ) : Sync() : Sleep( 2500 )
NewUser_stage = 0
SetTextColor( ShowLBStatusText, 255, 255, 255, 255 )
SetTextString (ShowLBStatusText, "" )
SetTextVisible(ShowLBStatusText, 0)
EndIf
endcase
case 8
Sleep(500)
NewUser_stage = 0
CloseHTTPConnection(g_Net_Connection)
DeleteHTTPConnection(g_Net_Connection)
SetTextColor( ShowLBStatusText, 255, 255, 255, 255 )
SetTextString (ShowLBStatusText, "" )
SetTextVisible(ShowLBStatusText, 0)
rem Close LeaderBoard Setup
if allow_sound = 1 then PlaySound(clicksound)
SetSpriteDepth (46, 0)
SetSpriteVisible (46, 1 )
SetSpriteDepth (400, 0)
SetSpriteVisible (400, 1 )
SetSpriteDepth (401, 0)
SetSpriteVisible (401, 1 )
SetSpriteDepth (700, 1000)
SetSpriteVisible (700, 0 )
SetSpriteDepth (701, 1000)
SetSpriteVisible (701, 0 )
SetSpriteDepth (702, 1000)
SetSpriteVisible (702, 0 )
temp_name$ = ""
temp_country$ = ""
temp_ID$=""
enter_lbinfo = 0
DeleteEditBox(1)
DeleteEditBox(2)
endcase
endselect
endfunction
checkuser.php
<?php
header('Access-Control-Allow-Origin: *');
include 'config.php';
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$name = strip_tags($_POST['name']);
$sql="SELECT name FROM $tbl_name WHERE name = '$name' ";
$result=mysql_query($sql);
if(!result) {Die('bad query');}
while($rows=mysql_fetch_array($result)){ echo $rows['name'] . chr(10); } mysql_close();
?>
createuser.php
<?php
include 'config.php';
$dblink = mysql_connect($host,$username,$password);
$seldb = mysql_select_db($db_name);
if(isset($_POST['name']) && isset($_POST['score']) && isset($_POST['country'])){ $name = strip_tags(mysql_real_escape_string($_POST['name'])); $score = strip_tags(mysql_real_escape_string($_POST['score']));
$country = strip_tags(mysql_real_escape_string($_POST['country']));
$sql = mysql_query("INSERT INTO `$db_name`.`$tbl_name` (`id`,`name`,`score`,`country`) VALUES ('','$name','$score','$country');");
if($sql){ echo '1' . chr(10) . mysql_insert_id(); }else{ echo 'Error saving score...' . chr(10);} }else{ echo 'POST requests only'; } mysql_close($dblink);
?>
config.php
<?php $host = "sbjleaderboard.db.9860565.hostedresource.com";$username="sbjleaderboard";$password="Anthony0906#";$db_name="sbjleaderboard";$tbl_name="scores"; ?>