Adjusting HTML5 games to save scores with SMF Arcade

Started by Chen Zhen, Jan 01, 2018, 11:23 PM

previous topic - next topic

Chen Zhen

Jan 01, 2018, 11:23 PM Last Edit: Jan 01, 2018, 11:27 PM by Chen Zhen
Adjusting HTML5 games to save scores with SMF Arcade

Code:
Code Select
function saveGameSmf(newhighscore)
{
var gameSmfFullscreen = document.getElementById("gameSmfFullscreen") != undefined ? document.getElementById("gameSmfFullscreen").value : 0;
if (parseInt(gameSmfFullscreen) == 1)
{
if (parseInt(newhighscore) > 0)
{
var sessid = document.getElementById("gameSmfToken");
var gameForm = document.getElementById("gameForm");
var vargamescore = document.createElement("INPUT");
var vargamesessid = document.createElement("INPUT");
vargamescore.type = "hidden";
vargamescore.id = "score";
vargamescore.name = "score";
vargamescore.value = parseInt(newhighscore);
gameForm.appendChild(vargamescore);
vargamesessid.type = "hidden";
vargamesessid.id = "gamesessid";
vargamesessid.name = "gamesessid";
vargamesessid.value = sessid.value;
gameForm.appendChild(vargamesessid);
setTimeout(gameForm.submit(), 1000);
throw new Error("<<< SAVING SCORE >>>");
return true;
}
else
{
var noSmfScore = document.getElementById("noSmfScore").val ? document.getElementById("noSmfScore").val : "No score to save!";
alert(noSmfScore);
return false;
}
}
else
{
if (parseInt(newhighscore) > 0)
{
var sessid = parent.document.getElementById("gameSmfToken");
var gameForm = parent.document.getElementById("gameForm");
var vargamescore = parent.document.createElement("INPUT");
var vargamesessid = parent.document.createElement("INPUT");
vargamescore.type = "hidden";
vargamescore.id = "score";
vargamescore.name = "score";
vargamescore.value = parseInt(newhighscore);
gameForm.appendChild(vargamescore);
vargamesessid.type = "hidden";
vargamesessid.id = "gamesessid";
vargamesessid.name = "gamesessid";
vargamesessid.value = sessid.value;
gameForm.appendChild(vargamesessid);
gameForm.submit();
window.open(location, '_self').close();
throw new Error("<<< SAVING SCORE >>>");
return true;
}
else
{
var noSmfScore = parent.document.getElementById("noSmfScore").val ? parent.document.getElementById("noSmfScore").val : "No score to save!";
alert(noSmfScore);
return false;
}
}

return false;
}

You either add the code shown above to a game's main javascript file or create a new file ie. smf_save.js and add the code to it.
If you create a new file you will need to include it to the html file of the game.
ie. loading your javascript file containing the code necessary to save a score:
Code Select
<script src="smf_save.js"></script>

Then you find in the game the score variable and where it attempts to save score or end the game.
Then you evoke the following function call:

let's say the score variable is "newscore"
Code Select
saveGameSmf(newscore);

Some games may require additional logic or it may even be required to add a method of scoring.
This may take some knowledge of javascript programming to accomplish. 
You have the option of requesting a game to be modified here in this board whereas someone with experience may decide to render assistance.