REM ***********************************************
REM Title: Purple Token Library
REM Author: Phaelax
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
/*
*
* Author:       Phaelax
* Date:         Nov 13, 2023
*
* Description:  The Purple Token library was created to make it
*			    easier for users to incorporate the functionality
*               of the Purple Token high-score service from within
*			    AGK Tier 1.
*
* Score format: No_Of_Entries,User1,Score1,User2,Score2,User3,Score3
*
*/
 
 
// Internally used variables. You should never need to access
// _PT directly in your application
Type _PurpleToken
	key				as string
	submitConn   	as integer
	retrieveConn 	as integer
	scoresRetrieved as integer
	scoreSubmitted  as integer
	scores 			as string
Endtype
 
 
Global _PT as _PurpleToken
 
// This array is safe to access from your application
// and contains the return codes possible for score submission
// The exception is an error code of -1, which is an unknown error
Global _PT_Codes as string[3]
 
 
 
 
 
// Must be called before using any other PT commands
function PT_initialize(gameKey as string)
	_PT.key = gameKey
 
	_PT_Codes[1] = "Success"
	_PT_Codes[2] = "Name length exceeds 32 characters"
	_PT_Codes[3] = "Gamekey not recognized"
 
endfunction
 
 
 
// Submit a score
function PT_submitScore(player as string, score as integer)
	_PT.scoreSubmitted = 0
    _PT.submitConn = createHTTPConnection()
    r = setHTTPHost(_PT.submitConn, "purpletoken.com", 1, "", "")
 
    vars$ = "player="+player+"&score="+str(score)+"&gamekey="+_PT.key
 
    sendHTTPRequestASync(_PT.submitConn, "update/index.php", vars$)
endfunction
 
 
 
 
 
// Handles the http responses for submitting and requesting scores
// It is safe to call this every iteration of your game loop
function PT_listener()
 
	// Score retrieval
	if _PT.retrieveConn > 0
		if _PT.scoresRetrieved = 0
 
			ptResponse = getHTTPResponseReady(_PT.retrieveConn)
 
			if ptResponse = -1
				// Error
			elseif ptResponse = 1
				_PT.scores = GetHTTPResponse(_PT.retrieveConn)
				_PT.scoresRetrieved = 1
			endif
 
			if ptResponse <> 0  // If either success or failed request, end connection
				closeHTTPConnection(_PT.retrieveConn)
				deleteHTTPConnection(_PT.retrieveConn)
				_PT.retrieveConn = 0
			endif
		endif
	endif
 
	// Score submission
	if _PT.submitConn > 0
		res = getHTTPResponseReady(_PT.submitConn)
		if res = -1
			// Error
			_PT.scoreSubmitted = -1
		elseif res = 1
			_PT.scoreSubmitted = val(GetHTTPResponse(_PT.submitConn))
		endif
		if _PT.scoreSubmitted <> 0
			_PT.scoreSubmitted = 1
			closeHTTPConnection(_PT.submitConn)
			deleteHTTPConnection(_PT.submitConn)
		endif
	endif
endfunction
 
 
 
// If a score is submitted this will return '1' upon completion of the http request, otherwise 0
// Codes can be looked up using the PT_Codes[] array
function PT_submitCompleted()
	if _PT.scoreSubmitted <> 0 then exitfunction 1
endfunction 0
 
 
 
// After a score submission has finished a code is generated (for success or possible errors)
// Once this function is called the code is cleared until a new submission is made
function PT_getSubmitCode()
	code = _PT.scoreSubmitted
	_PT.scoreSubmitted = 0
endfunction code
 
 
 
// Check if new score data is available after a request is made through PT_requestScores()
// Returns 1 for true, 0 otherwise. If scores are available, command will continue to return 1
// until the data has been retrieved at least once.
function PT_scoresAvailable()
 
endfunction _PT.scoresRetrieved
 
 
 
 
// Returns the raw score data (comma delimited single-line string)
// Once this function is called, PT_scoresAvailable() will return 0 until a new request is made. 
// However, the last retrieved score data can continue to be accessed through this function
function PT_getScores()
	_PT.scoresRetrieved = 0
endfunction _PT.scores
 
 
 
 
// Initiate the connection to get list of scores
// Once the request has completed, PT_scoresAvailable() will return 1
function PT_requestScores()
	_PT.scoresRetrieved = 0
    _PT.retrieveConn = createHTTPConnection()
    r = setHTTPHost(_PT.retrieveConn, "purpletoken.com", 1, "", "")
 
    vars$ = "gamekey="+_PT.key
    sendHTTPRequestASync(_PT.retrieveConn, "update/index.php", vars$)
 
endfunction
 
 
 
// Same as above but also retrieves the submission date/time of the scores
// The returned format will then look like: No_Of_Entries,User1,Score1,Date1,User2,Score2,Date2,User3,Score3,Date3
function PT_requestScoresWithDates()
	_PT.scoresRetrieved = 0
    _PT.retrieveConn = createHTTPConnection()
    r = setHTTPHost(_PT.retrieveConn, "purpletoken.com", 1, "", "")
 
    vars$ = "dates=yes&gamekey="+_PT.key
    sendHTTPRequestASync(_PT.retrieveConn, "update/index.php", vars$)
 
endfunction