Posting High-Scores (Hacktendo)
From Hack Wars Wiki
This code provides an example of how to use a file on your HD to keep track of the high-scores of players who play your Hacktendo game. It has three parts: a watch script fired using trigger watch, the code in Hacktendo for trigger watch, and an HTTP script that prints out the high-scores.
Posting High Scores: Using Trigger Watch
I have a global parameter in Hacktendo I use to keep track of a player's score. When they beat the game I use trigger watch to report it back to my score keeping watch script.
triggerWatch("Galaxium","score",getGlobal("score")); |
Posting High Scores: The Watch Code
Trigger watch in Hacktendo uses the note on the watch to trigger the watch. In this case, I set the note to "Galaxium". We can note that trigger watch in the previous section corresponds to this.
string data=readFile("scores"); string records[]=split(data,char(10)); string name=getTargetIP(); string score=getTriggerParameter("score"); float fscore=parseFloat(score); boolean written=false; string new_data=""; for(int i=0;i<length(records);i++){ String record_data[]=split(records[i],","); String old_name=record_data[0]; String old_score=record_data[1]; float old_fscore=parseFloat(old_score); if(old_name==name){ if(fscore>old_fscore){ old_fscore=fscore; written=true; } }else{ new_data+=old_name+","+old_fscore+char(10); } } if(!written&&fscore>30.0){ written=true; new_data+=name+","+fscore+char(10); } if(written){ new_data=""; boolean inserted=false; for(int i=0;i<length(records);i++){ String record_data[]=split(records[i],","); String old_name=record_data[0]; String old_score=record_data[1]; float old_fscore=parseFloat(old_score); if(fscore>old_fscore&&!inserted){ new_data+=name+","+fscore+char(10); inserted=true; } if(old_name==name){ if(old_fscore>fscore){ new_data+=old_name+","+old_fscore+char(10); inserted=true; } }else new_data+=old_name+","+old_fscore+char(10); } if(!inserted){ new_data+=name+","+fscore+char(10); } writeFile("scores",new_data); } |
Posting High Scores: The HTTP Code
Finally we parse the same file on our website and output the high-scores.
void outputHighScores(){ string message="<table width='60%' ><tr><td><b>Player</b></td><td><b>Score</b></td></tr>"; string data=readFile("scores"); string records[]=split(data,char(10)); for(int i=0;i<length(records);i++){ String record_data[]=split(records[i],","); String old_name=record_data[0]; String old_score=record_data[1]; message+="<tr><td>"+old_name+"</td><td>"+old_score+"</td></tr>"; } message+="</table>"; rc("user1",message); } |
