-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmafia_r.js
212 lines (202 loc) · 7.24 KB
/
mafia_r.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// This contains a collection of some oft used functions
function loadScript(location) { // Dynamically loads a javascript file for use
js = document.createElement("script");
js.src = location;
document.body.appendChild(js);
}
function timer(interval, callbackEnd, callPerSecond) { //Timer function
/*
Usage: timer(intervalInSeconds, functionToBeCalledWhenTimeUp, functionToBeCalledPerSecond(timeElapsed))
*/
var initialTime = Date.now();
// Interval should be in seconds
//Call per second gets the diff as well
var _timer = setInterval(function() {
var diff = Date.now() - initialTime;
callPerSecond.call(this, Math.floor(diff/1000));
if((diff/1000)>=interval) {
clearInterval(_timer);
callbackEnd.call(this);
}
}, 900);
}
function voteBankString(voteBank) {
var keys = Object.keys(voteBank);
var st = "";
for(var i=0; i!=keys.length; i++) {
st += "<br>" + keys[i] + " : " + voteBank[keys[i]];
}
return st;
}
function setupVoting(roundTime, gameState) {
console.log("Entered setupVoting");
gameState.voteState = {};
gameState.populateForm();
console.log("Past the populateForm function");
//TODO: It makes me queasy to have to touch the HTML code OUTSIDE the decorate function - definite need to refactor this bit. It's frequently used, too
var radioButtons = document.getElementsByClassName("players");
var labels = document.getElementsByClassName("players-label");
var endVote;
var sendVote = function(e){
var selected = false;
for(var i=0; i!=radioButtons.length; i++) {
if(radioButtons[i].checked==true) {
selected = radioButtons[i].value;
ws.send("#VOTE:"+selected);
if(gameState.round=="#VOTE_ANON") gameState.decorate("");
//console.log("You've clicked on " + selected);
break;
}
}
};
endVote = function() {
for(var i=0; i!=radioButtons.length; i++) {
radioButtons[i].removeEventListener("change", sendVote);
radioButtons[i].checked = false ;
}
gameState.sanitizeForm();
ws.send("#DONE_VOTING");
};
for(var i=0; i!=radioButtons.length; i++) {
radioButtons[i].addEventListener("change", sendVote);
}
console.log("starting up timer");
timer(roundTime,endVote, function(s) {timerDisplay(roundTime, s)});
}
function timerDisplay(roundTime, s) {
if(document.getElementById("timer").style.display!="inline") document.getElementById("timer").style.display = "inline";
document.getElementById("timer").lastChild.innerHTML = "" + roundTime-s;
if(roundTime-s == 0) document.getElementById("timer").style.display = "none";
}
function GameState() {
this.username = "";
this.type = ""; // Potential for an error?
this.names = {};
this.round = "";
this.voteState= {};
this.teamNames = [];
}
GameState.prototype.toString = function() {
var repr = "";
repr+="Username:" + this.username + "<br>";
repr+="Type: " + this.type + "<br>";
repr += "List of people: <br>";
var keys = Object.keys(this.names);
for(var i=0; i!=keys.length; i++) {
repr+=keys[i] + " : " + this.names[keys[i]] + "<br>";
}
repr+="Round: " + this.round + "<br>";
repr+="State of the vote: <br>";
keys = Object.keys(this.voteState);
for(var i=0; i!=keys.length; i++) {
repr+=keys[i] + " votes for " + this.voteState[keys[i]] + "<br>";
}
if(this.type!="Victim"){
repr+="List of people belonging to the team: <br>";
for(var i=0; i!=this.teamNames.length; i++) {
repr+="" + this.teamNames[i] + "<br>";
}
}
return repr;
};
GameState.prototype.initiate = function () {
//Initiate with username, type, team, detection-result
document.getElementById("username").lastChild.innerHTML = this.username;
document.getElementById("type").lastChild.innerHTML = this.type;
if(this.type=="Victim") {
document.getElementById("team").style.display = "none";
}
else {
var place = document.getElementById("team").lastChild;
place.innerHTML = "";
for(var i=0; i!=this.teamNames.length; i++) {
place.innerHTML += "<br>" + this.teamNames[i];
}
}
if(this.type!="Detective") {
document.getElementById("detection-result").style.display="none";
}
};
GameState.prototype.populateForm = function () {
this.sanitizeForm();
var form = document.getElementById("voting-form");
var nameKeys = Object.keys(this.names);
for(var i=0; i!=nameKeys.length; i++) {
if(this.names[nameKeys[i]]==false) continue;
var radioButton = document.createElement("input");
var label = document.createElement("label");
var voteSpan = document.createElement("span");
radioButton.name="players";
radioButton.type="radio";
radioButton.className="players";
radioButton.value = "" + nameKeys[i];
radioButton.id = "players-"+nameKeys[i];
voteSpan.id = "others-"+nameKeys[i];
if(i%2==0) label.className = "alter ";
label.className += "players-label"
label.appendChild(radioButton);
label.appendChild(document.createTextNode(nameKeys[i]));
label.appendChild(voteSpan);
form.appendChild(label);
//TODO: Add functionality as well as decor
}
};
GameState.prototype.sanitizeForm = function () {
document.getElementById("voting-form").innerHTML="";
};
GameState.prototype.decorate = function(status) {
//This has the task of filling up the template from the GameState Object
if(status!="") {
document.getElementById("status").lastChild.innerHTML = status;
}
//Now to add the names and the entire shebang if it is a voting round.
if(this.round == "#MAFIA_VOTE" || this.round == "#DETECTIVE_VOTE" || this.round == "#VOTE_OPEN") {
var voteKeys = Object.keys(this.voteState);
var nameKeys = Object.keys(this.names);
//Need to clear the voteSpans
for(var i=0; i!=nameKeys.length; i++) {
if(this.names[nameKeys[i]] == false) continue;
//console.log("Trying for.... " + nameKeys[i])
document.getElementById("others-"+nameKeys[i]).innerHTML = "";
}
for(var i=0; i!=voteKeys.length; i++) {
var voter = voteKeys[i];
var votee = this.voteState[voteKeys[i]];
var voteSpan = document.getElementById("others-"+votee);
if(voteSpan) voteSpan.innerHTML+=" " + voter + " ";
}
}
if(this.round == "#VOTE_ANON") {
var radioButtons = document.getElementsByClassName("players");
for(var i=0; i!=radioButtons.length; i++) {
if(radioButtons[i].checked==true) {
document.getElementById("others-" + radioButtons[i].value).innerHTML = "You";
} else {
document.getElementById("others-" + radioButtons[i].value).innerHTML = "";
}
}
}
};
GameState.prototype.splash = function (text, duration, callback) {
var sscreen = document.getElementById("splash");
sscreen.innerHTML = "";
var h1 = document.createElement("h1");
h1.appendChild(document.createTextNode(text));
sscreen.appendChild(h1);
if(duration == 0) {
var button = document.createElement("div");
button.id = "done-button";
button.appendChild(document.createTextNode("Done"));
button.onclick = function() {
sscreen.style.display = "none";
callback.call(window);
};
sscreen.appendChild(button);
} else {
timer(duration, function() {
sscreen.style.display = "none";
callback.call(window);
}, function(s){});
}
sscreen.style.display = "block";
};