how to use:
Set the variable highscores to the highscores in a certain format. The format is:
Code:
"score-username-score-username"
so first you write the score, then a line (-), then the username that owns the last score. It may seem a little backwards but makes more sense to loop through the numbers and not every single last name unknowingly. Note that the username cannot have a line (-) in it because otherwise it will think the last part of the username would count as a score. So:
Code:
"100-joe-burns-200-tomy"
this would not work because after the word joe there is a line, which means burns would be a score and 200 would be a username, and tomy would be another score. To fix it, put a backslash before the line. Because of javascript escaping, we have to escape the backslash as well. So the string would look like:
Code:
"100-joe\\-burns-200-tomy"
now the code works. If ever nessecary to have a backslash in the username next to a line, we would just add another backslash, like
this would result in joe\-burns. Remember to call the getscores function.
How it works and the nitty gritty details:
It first loops through the highscores variable, checking for usernames and scores, and sorts them into an array called namearray:
Code:
namearray[0] = "100-joe\\-burns";
namearray[1] = "200-tomy";
it then loops through this halfway through array and breaks it down into another array called highscorearray, this time with the key being the score, like:
Code:
highscorearray["100"] = "joe\\-burns";
highscorearray["200"] = "tomy";
if two people had the same score, it could still handle it with a little string magic:
Code:
highscorearray["200"] = "tomy";
highscorearray["200\\\-"] = "tomysshadow";
the difficult part was this. How could i get the array key to show? I knew only one key could be allowed with the same name, so i created a function looping through each number from lowest to highest in the array, and if a number was found, it would also test for the same score with a "\\\-" on the end, to see if someone got the same score, and would keep going until all of the same score was found. It would subsequently write these to the page. So:
Code:
highscorearray["100"] = "joe\\-burns";
highscorearray["200"] = "tomy";
highscorearray["200\\\-"] = "tomysshadow";
writes this to the page:
Code:
joe-burns - 100
<br>tomy - 200
<br>tomysshadow - 200
if you prefer not to use document.write, look for this comment:
Code:
// the username is stored in the variable highscorearray['' + i + ''].replace(/\\-/g, '-') and the the score is stored in the variable key, you might want to do something else with this than write it to the page using document.write, but be aware not all the code is outputted at once, instead looping through each username
and
Code:
// the username is stored in the variable highscorearray['' + i + ''].replace(/\\-/g, '-') and the the score is stored in the variable key.replace(/\\\\-/g, ''), you might want to do something else with this than write it to the page using document.write, but be aware not all the code is outputted at once, instead looping through each username
and follow the instructions in each comment to do what you want instead, for example, you may choose to store the html in a variable instead and write all the code at once (which i admit may be slightly faster overall, but won't update the display immediately after each loop.
Downsides:
It can't handle time. Try converting times to milliseconds then back again to regular time (seconds? Minutes?)
it can't handle decimals. Try multiplying by 10, 100, or 1000 and when writing to the page, dividing again by 10, 100, or 1000.
Because a for loop checks for each number, if the highscore have a range of around 100 million it would surely cause the script to freeze. A good maximum score would be 10000000 from the lowest.
On the other hand, it's easy to learn and great for family highscores (highscores that are only on one computer because of cookies).
To clarify, it does not matter what order the highscores are in when they are inside the varaible. This would also work:
Code:
"300-tomy-100-tomysshadow"
so all you have to do is basically add:
everytime there is a new score, unless it is the first score on the list, in which case you shouldn't add the first line (-) character:
i wrote this script by myself. Feel free to edit this script to your liking, and use it without giving credit to the author! I like free stuff and don't like to bother with liscenses, and i can't wait to see what people do with it so go ahead (and if you do edit, maybe post it here for all to see)!
My main goals with scripts are to keep them safe and browser compatible. Not even the oldest of ancient browsers should be left out, because leaving them out means leaving out folks with no knowledge of computers and don't know what to do in the case of an error. And of course, with scripts made in languages such as php, safety can be a big concern. I do my best to take care of both situations in my scripts so that everyone can enjoy and be safe.