REM ***********************************************
REM Title: Purple Token example
REM Author: Phaelax
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
 
SetWindowTitle( "purpletoken_example" )
SetWindowSize( 1024, 768, 0 )
//UseNewDefaultFonts(1)
 
 
// Used for this example only
Type ScoreUDT
    name  as string
    score as integer
    timestamp as string
EndType
 
 
// Include the Purple Token library
#include 'purpletoken.agc'
 
 
// Initialize purple token with your game key
PT_initialize("game key here")
 
 
 
 
do
 
 
	// Press ENTER key to request retrieval of game scores
    if getRawKeyPressed(13) then PT_requestScoresWithDates()
 
 
	// Press SPACE key to submit a score
    if getRawKeyPressed(32)
		s$ = "Submitting...."
		PT_submitScore("Beavis", random(999, 9999))
    endif
 
 
 
	// If score data is available, this will return 1
	if PT_scoresAvailable()
		// Once you call PT_getScores(), PT_scoresAvailable() will return 0 until a new request is made
		scores$ = PT_getScores()
		// Parse scores how you see fit
		//parseScores(scores$)
		parseScoresWithDates(scores$)
	endif
 
	// If a score is submitted this will return 1 upon completion of the http request, otherwise 0
	if PT_submitCompleted()
		// Once PT_getSubmitCode() is called, the code is wiped clear and
		// PT_submitCompleted() will again return 0 until a new submission is made
		submitCode = PT_getSubmitCode()
		if submitCode = -1 then s$ = "Error"
		s$ = _PT_Codes[submitCode]
	endif
 
 
 
	// See status of score submission
	print("Score submitted: "+s$)
	print("")
 
	// Display scores however you want
	//displayScores()
	displayScoresWithDates()
 
 
	// Listens for the http request responses
	// No sockets are left open or connections in memory if 
	// there have been no requests or submissions
	PT_listener()
 
    Sync()
loop
 
 
 
 
 
// Score format: No_Of_Entries,User1,Score1,User2,Score2,User3,Score3
function parseScores(scores as string)
	count = countStringTokens(scores, ",")
	size  = val(getStringToken(scores, ",", 1))
	dim scoreboard[size] as ScoreUDT
	j = 1
	for i = 2 to count step 2
		player$ = getStringToken(scores, ",", i)
		score   = val(getStringToken(scores, ",", i+1))
		scoreboard[j].name = player$
		scoreboard[j].score = score
		inc j
	next i
endfunction
 
 
// Score format: No_Of_Entries,User1,Score1,Date1,User2,Score2,Date2,User3,Score3,Date3
function parseScoresWithDates(scores as string)
	count = countStringTokens(scores, ",")
	size  = val(getStringToken(scores, ",", 1))
	dim scoreboard[size] as ScoreUDT
	j = 1
	for i = 2 to count step 3
		player$ = getStringToken(scores, ",", i)
		score   = val(getStringToken(scores, ",", i+1))
		t$ 		= getStringToken(scores, ",", i+2)
		scoreboard[j].name = player$
		scoreboard[j].score = score
		scoreboard[j].timestamp = t$
		inc j
	next i
endfunction
 
 
 
function displayScores()
	size = scoreboard.length
	for i = 1 to size
        print(scoreboard[i].name+" - "+str(scoreboard[i].score))
    next i
endfunction
 
 
 
function displayScoresWithDates()
	s$ = spaces(20)
	size = scoreboard.length
	for i = 1 to size
        print(left(scoreboard[i].name+s$, 20)+" - "+right(spaces(5)+str(scoreboard[i].score),5)+"  :  "+scoreboard[i].timestamp)
    next i
endfunction