-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighscore.js
160 lines (132 loc) · 4.34 KB
/
highscore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// A score record JavaScript class to store the name and the score of a player
//
function ScoreRecord(id,name, score) {
this.id = id;
this.name = name;
this.score = score;
}
//
// This function reads the high score table from the cookies
//
function getHighScoreTable() {
var table = new Array();
for (var i = 0; i < 10; i++) {
// Contruct the cookie name
var cookieName = "player" + i;
// Get the cookie value using the cookie name
var value = getCookie(cookieName);
// If the cookie does not exist exit from the for loop
if(value == null)
break;
// Extract the name and score of the player from the cookie value
var temp = value.split("~");
var id = parseInt(temp[0])+1;
var name = temp[1];
var score = temp[2];
// Add a new score record at the end of the array
table.push(new ScoreRecord(id,name, parseInt(score)));
}
return table;
}
//
// This function stores the high score table to the cookies
//
function setHighScoreTable(table) {
for (var i = 0; i < 10; i++) {
// If i is more than the length of the high score table exit
// from the for loop
if (i >= table.length) break;
// Contruct the cookie name
var cookieName = "player";
// Store the ith record as a cookie using the cookie name
setCookie(cookieName + i.toString(), table[i].id +"~"+table[i].name + "~" + table[i].score.toString());
}
}
//
// This function adds a high score entry to the text node
//
function addHighScore(record, node) {
// Create the name text span
var name1 = svgdoc.createElementNS("http://www.w3.org/2000/svg", "tspan");
// Set the attributes and create the text
name1.setAttribute("x" , 100);
name1.setAttribute("dy" , 30);
//if current user
if(record.id == 0){
name1.setAttribute("fill", "black");
}
name1.appendChild(svgdoc.createTextNode(record.name));
// Add the name to the text node
node.appendChild(name1);
// Create the score text span
var score = svgdoc.createElementNS("http://www.w3.org/2000/svg", "tspan");
// Set the attributes and create the text
score.setAttribute("x" , 400);
//if current user
if(record.id == 0){
score.setAttribute("fill", "black");
}
score.appendChild(svgdoc.createTextNode(record.score));
// Add the name to the text node
node.appendChild(score);
}
//
// This function shows the high score table to SVG
//
function showHighScoreTable(table) {
// Show the table
var node = svgdoc.getElementById("highscoretable");
node.style.setProperty("visibility", "visible", null);
// Get the high score text node
var node = svgdoc.getElementById("highscoretext");
for (var i = 0; i < 10; i++) {
// If i is more than the length of the high score table exit
// from the for loop
if (i >= table.length) break;
// Add the record at the end of the text node
addHighScore(table[i], node);
}
}
//
// The following functions are used to handle HTML cookies
//
//
// Set a cookie
//
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
document.cookie = curCookie;
}
//
// Get a cookie
//
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}
//
// Delete a cookie
//
function deleteCookie(name, path, domain) {
if (get_cookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}