/
jeopardy-player.js
169 lines (104 loc) · 2.53 KB
/
jeopardy-player.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
161
162
163
164
165
166
167
168
169
/*
Copyright © 2010-2012 Jesse McCarthy <http://jessemccarthy.net/>
This file is part of the Jeopardy Scorekeeper software. "JEOPARDY!"
is a trademark of Jeopardy Productions, Inc. This software is not
endorsed by, sponsored by, or affiliated with Jeopardy Productions,
Inc.
This software may be used under the MIT (aka X11) license or
Simplified BSD (aka FreeBSD) license. See LICENSE.
*/
/**
* Player constructor.
*
* @param object player Initial player data.
*
* @return void
*/
Jeopardy.Player = function ( player ) {
this.id = player.id;
this.name = player.name;
this.set_score( player.score );
return;
};
// Jeopardy.Player
/// number ID, unique within the game.
Jeopardy.Player.prototype.id;
/// string Name.
Jeopardy.Player.prototype.name;
/// number Score.
Jeopardy.Player.prototype.score = 0;
/// boolean Has control of the board.
Jeopardy.Player.prototype.has_control = false;
/**
* Sanitize a score value. E.g. to remove formatting added for the UI
* or entered by the user.
*
* @param mixed score May be undefined, number, string, etc.
*
* @return number Sanitized score.
*/
Jeopardy.Player.prototype.sanitize_score = function ( score ) {
if ( score === undefined ) {
score = 0;
}
// if
score = Number( score.toString().replace( /[^0-9.-]/g, '' ) );
if ( isNaN( score ) ) {
score = 0;
}
// if
return score;
};
// Player.sanitize_score
/**
* Set player's score.
*
* @param mixed score Maybe undefined, number, string, etc.
*
* @param boolean append Append score rather than overwrite.
*
* @return void
*/
Jeopardy.Player.prototype.set_score = function ( score, append ) {
score = this.sanitize_score( score );
if ( append ) {
score += this.score;
}
// if
this.score = Number( score.toFixed( 0 ) );
return;
};
// Player.set_score
/**
* Update player's score.
*
* @param mixed score May be undefined, number, string, etc.
*
* @return number New score.
*/
Jeopardy.Player.prototype.update_score = function ( score ) {
this.set_score( score, true );
return this.score;
};
// Player.update_score
/**
* Get player's score.
*
* @return number Score.
*/
Jeopardy.Player.prototype.get_score = function () {
return this.score;
};
// Player.get_score
/**
* Grant or revoke the player's control of the board.
*
* @param boolean has_control Has control of the board.
*
* @return void
*/
Jeopardy.Player.prototype.set_has_control = function ( has_control ) {
this.has_control = has_control;
return;
};
// Player.set_has_control