-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvote.js
112 lines (94 loc) · 1.93 KB
/
vote.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
//config
var voting_timeout = 5000;
var done_voting_timeout;
function move_now(){
if(phase!="vote"){
return;
}
window.clearTimeout(hidden_timeout);
hide(document.all.votes);
finish_move(true);
}
function vote_now(){
window.clearTimeout(done_voting_timeout);
done_voting();
phase = "vote";
players.forEach( function(p){
p.my_vote = null;
hide(p.vote_pic());
});
show_and_hide(document.all.votes, voting_timeout, function (){
finish_move(true);
});
//handle ai votes
active_players().forEach(function(p){
if(p.role != "human"){
p.ai.do_vote();
}
});
}
function lets_vote(){
if( phase != "vote" ) {
return
}
window.clearTimeout(hidden_timeout);
hide(document.all.vote_now);
show(document.all.vote_ip);
active_players().forEach(function(p){
if(p.role != "human"){
p.ai.vote();
}
})
}
function all_voted(){
var num_votes = 0;
players.forEach(function (p){
if(p.my_vote != null){
num_votes++;
}
});
if(num_votes>=count_players()){
//vote finished
return true;
}else{
return false;
}
}
function finish_vote(vote_value){
if(phase!="vote"){
return
}
players.forEach( function (p){
vote_value += p.my_vote;
p.set_vote_pic(pic("vote_"+p.my_vote));
} )
//alert(vote_value);
if(vote_value < 0 ){
finish_move(false);
}else{
finish_move(true);
}
players.forEach( function(p){
p.redraw();
});
done_voting_timeout = window.setTimeout(done_voting,2000);
}
function vote_check(){
var vote_value = 0;
if(all_voted()){
finish_vote(vote_value);
}
}
function done_voting(){
show(document.all.vote_now);
hide(document.all.vote_ip);
hide(document.all.votes);
}
function vote_handler(e) {
if(phase!="vote")
return;
var attrs = this.value.split(";");
var p = parseInt(attrs[0]);
var val = parseInt(attrs[1]);
players[p].vote(val);
}