-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshanghai.js
169 lines (144 loc) · 4.94 KB
/
shanghai.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
$( document ).ready(function() {
generate_header();
game_reset();
$('#cta-start').click( function() {
if ('Reset' == $(this).html()) {
$(this).html('Start');
game_reset();
} else {
$(this).html('Reset')
game_start();
}
});
function game_reset() {
game.players = [];
game.scores = [];
game.playersScores = [];
game.inProgress = false;
render_scores();
$('#scores').html('');
$('.player-row.row-0 .playername').html('');
}
function game_start() {
settings_hide();
settings_get();
game.inProgress = true;
$('#scores').html('');
var table_header = '';
table_header += '<tr class="scores-header"><th>Round</th>';
for ( var i = 0; i < game.playersTotal; i++ ) {
table_header += '<th>';
table_header += '<input data-playernum="' + i + '" class="player-name" type="text" name="player' + i + '" placeholder="Player ' + i + '" />';
table_header += '<div class="player-scores player-scores' + i + '" /></div>';
table_header += '</th>';
game.players[i] = 'Player ' + i;
}
table_header += '</tr>';
$('#scores').html(table_header);
render_scores();
$('.player-name').blur( function() {
var player_name = '' == $(this).val() ? 'Player ' + $(this).data('playernum') : $(this).val();
game.players[ parseInt( $(this).data('playernum') ) ] = player_name ;
render_scores();
} );
}
$('#del').click( function() {
if ( 'Start' == $('#cta-start').html() ) return;
game.inProgress = true;
game.scores.pop();
render_scores();
});
$('#miss').click( function() {
if ( ! game.inProgress ) return;
game.scores.push( 0 );
sfx_play( 'miss' )
render_scores();
});
$('#single').click( function() {
if ( ! game.inProgress ) return;
game.scores.push( 1 );
sfx_play( 'hit1' )
render_scores();
});
$('#double').click( function() {
if ( ! game.inProgress ) return;
game.scores.push( 2 );
sfx_play( 'hit1' )
render_scores();
});
$('#tripple').click( function() {
if ( ! game.inProgress ) return;
game.scores.push( 3 );
sfx_play( 'hit2' )
render_scores();
});
function render_scores() {
game.roundInfo = get_round_info( game.scores, game.playersTotal );
$('.current-round').html(game.roundInfo.round);
var row = '';
$('.score-row').remove();
//Create emppty scores table
for ( var r = game.roundInfo.round; r >= 1; r-- ) {
row = '<tr class="score-row row' + r + '"><td>';
row += r;
row += '</td>';
for ( var p = 1; p <= game.playersTotal; p++ ) {
row += '<td class="cell-' + r + '-' + p + '">0</td>';
}
row += '</tr>';
$('#scores').append(row);
}
//Fill scores table
game.playersScores = [];
for (var r = 0; r < game.roundInfo.round; r++) {
for (var p = 0; p < game.playersTotal; p++) {
var sum = 0;
for ( var i = 0; i <= 2; i++ ) {
var index = r * game.playersTotal * 3 + p*3+ i;
if (! is_empty( game.scores[index] ) ) {
sum += game.scores[index] * ( r + 1 );
}
}
$( '.cell-' + ( r + 1 ) + '-' + ( p + 1 ) ).html( sum );
game.playersScores[p] = is_empty( game.playersScores[p] ) ? sum : game.playersScores[p] + sum;
}
}
//Fill players total scores
for (var p = 0; p < game.playersTotal; p++) {
$( '.player-scores' + p ).html( game.playersScores[p] );
}
//Fill throws table
$('player-row.row-0 .throw').html('');
var round_zero_pos = ( game.roundInfo.round - 1 ) * game.playersTotal * 3 + ( game.roundInfo.player - 1 ) * 3;
$('.player-row.row-0 .throw1').html( game.scores[ round_zero_pos ] * game.roundInfo.round );
$('.player-row.row-0 .throw2').html( game.scores[ round_zero_pos + 1 ] * game.roundInfo.round );
$('.player-row.row-0 .throw3').html( game.scores[ round_zero_pos + 2 ] * game.roundInfo.round );
$('.player-row.row-0 .playername').html( game.players[game.roundInfo.player - 1] );
$('.player-row.row-0 .throw').each( function( ){
if ( '' == $(this).html() ) {
$(this).addClass('dart');
} else {
$(this).removeClass('dart');
}
} );
$('.player-row.row-1 .throw').html('');
$('.player-row.row-1 .playername').html('');
if ( game.roundInfo.round > 1 || game.roundInfo.player > 1 ) {
var round_previous_pos = round_zero_pos - 3;
var round_previous = game.roundInfo.round;
var player_previous = game.roundInfo.player - 2;
if ( player_previous < 0 ) {
player_previous = game.playersTotal - 1;
--round_previous;
}
$('.player-row.row-1 .throw1').html( game.scores[ round_previous_pos ] * round_previous );
$('.player-row.row-1 .throw2').html( game.scores[ round_previous_pos + 1 ] * round_previous );
$('.player-row.row-1 .throw3').html( game.scores[ round_previous_pos + 2 ] * round_previous );
$('.player-row.row-1 .playername').html( game.players[player_previous] );
}
if ( 21 <= game.roundInfo.round ) {
game.inProgress = false;
}
send_scores();
}
});