I was really not expecting to go down this path, but here’s a nifty tool.
After being slightly annoyed that I couldn’t keep track of everyone’s scores in a game, this was the result. A fair bit of trial and error and some pretty painful css because tables .
Check this out
+
-
Hey! My game could use that
Want the code for yourself?
Here ya go, it’s on the house, just ping me a message if you use it and mention my name somewhere. The CSS is a bit harder to share as it’s using a bunch of root variables but nothing that ChatGPT can’t solve for you I’m sure:
copy dat
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
< table class = "scores" >
< thead >
< tr >
< th > Scoring Condition </ th >
< th > Player 1 </ th >
< th > Player 2 </ th >
< th id = "additionalPlayers" ></ th >
</ tr >
</ thead >
< tbody >
< tr id = "rowCritters" >
< td > Critters </ td >
< td >
< div class = "input-container" >
< input type = "number" id = "p1_critters" value = "0" onfocus = "showButtons(this)" oninput = "calculateTotal()" >
</ div >
</ td >
< td >
< div class = "input-container" >
< input type = "number" id = "p2_critters" value = "0" onfocus = "showButtons(this)" oninput = "calculateTotal()" >
</ div >
</ td >
< td id = "additionalCritters" ></ td >
</ tr >
< tr id = "rowConnections" >
< td > Connections </ td >
< td >
< div class = "input-container" >
< input type = "number" id = "p1_connections" value = "0" onfocus = "showButtons(this)" oninput = "calculateTotal()" >
</ div >
</ td >
< td >
< div class = "input-container" >
< input type = "number" id = "p2_connections" value = "0" onfocus = "showButtons(this)" oninput = "calculateTotal()" >
</ div >
</ td >
< td id = "additionalConnections" ></ td >
</ tr >
< tr id = "rowVolcanoes" >
< td > Volcanoes </ td >
< td >
< div class = "input-container" >
< input type = "number" id = "p1_volcanoes" value = "0" onfocus = "showButtons(this)" oninput = "calculateTotal()" >
</ div >
</ td >
< td >
< div class = "input-container" >
< input type = "number" id = "p2_volcanoes" value = "0" onfocus = "showButtons(this)" oninput = "calculateTotal()" >
</ div >
</ td >
< td id = "additionalVolcanoes" ></ td >
</ tr >
< tr id = "rowSacrifices" >
< td > Sacrifices </ td >
< td >
< div class = "input-container" >
< input type = "number" id = "p1_sacrifices" value = "0" onfocus = "showButtons(this)" oninput = "calculateTotal()" >
</ div >
</ td >
< td >
< div class = "input-container" >
< input type = "number" id = "p2_sacrifices" value = "0" onfocus = "showButtons(this)" oninput = "calculateTotal()" >
</ div >
</ td >
< td id = "additionalSacrifices" ></ td >
</ tr >
< tr id = "rowBonuses" >
< td > Bonuses </ td >
< td >
< div class = "input-container" >
< input type = "number" id = "p1_bonuses" value = "0" onfocus = "showButtons(this)" oninput = "calculateTotal()" >
</ div >
</ td >
< td >
< div class = "input-container" >
< input type = "number" id = "p2_bonuses" value = "0" onfocus = "showButtons(this)" oninput = "calculateTotal()" >
</ div >
</ td >
< td id = "additionalBonuses" ></ td >
</ tr >
< tr id = "rowTotals" >
< td > Total </ td >
< td >< span id = "p1_total" > 0 </ span ></ td >
< td >< span id = "p2_total" > 0 </ span ></ td >
< td id = "additionalTotals" ></ td >
</ tr >
</ tbody >
</ table >
< div class = "plusminus-button-container" id = "plusminusButtonContainer" style = "display: none;" >
< button class = "increment-button" onclick = "incrementScore()" >+</ button >
< button class = "decrement-button" onclick = "decrementScore()" > - </ button >
</ div >
< div class = "player-button-container" id = "playerButtonContainer" >
< button class = "remove-button" onclick = "removePlayer()" >
<!-- < img src = "/icons/minus-player.svg" alt = "Remove Player" />
Remove Player -->
< svg xmlns = "http://www.w3.org/2000/svg" width = "24" height = "24" viewBox = "0 0 24 24" fill = "none" stroke = "currentColor" stroke - width = "2" stroke - linecap = "round" stroke - linejoin = "round" class = "icon icon-tabler icons-tabler-outline icon-tabler-user-minus" >< path stroke = "none" d = "M0 0h24v24H0z" fill = "none" />< path d = "M8 7a4 4 0 1 0 8 0a4 4 0 0 0 -8 0" />< path d = "M6 21v-2a4 4 0 0 1 4 -4h4c.348 0 .686 .045 1.009 .128" />< path d = "M16 19h6" /></ svg > Remove Player
</ button >
< button class = "add-button" onclick = "addPlayer()" >
< svg xmlns = "http://www.w3.org/2000/svg" width = "24" height = "24" viewBox = "0 0 24 24" fill = "none" stroke = "currentColor" stroke - width = "2" stroke - linecap = "round" stroke - linejoin = "round" class = "icon icon-tabler icons-tabler-outline icon-tabler-user-plus" >< path stroke = "none" d = "M0 0h24v24H0z" fill = "none" />< path d = "M8 7a4 4 0 1 0 8 0a4 4 0 0 0 -8 0" />< path d = "M16 19h6" />< path d = "M19 16v6" />< path d = "M6 21v-2a4 4 0 0 1 4 -4h4" /></ svg ><!-- < img src = "/icons/plus-player.svg" alt = "Add Player" /> -->
Add Player
</ button >
</ div >
< script >
let playerCount = 2 ;
let selectedInput = null ;
function addPlayer () {
playerCount ++ ;
const playerId = ` p $ { playerCount } ` ;
// Add new player header
const th = document . createElement ( 'th' );
th . innerText = ` Player $ { playerCount } ` ;
document . querySelector ( 'thead tr' ) . appendChild ( th );
// Add new player input fields to each row
const rows = [ 'Critters' , 'Connections' , 'Volcanoes' , 'Sacrifices' , 'Bonuses' , 'Totals' ];
rows . forEach ( row => {
const td = document . createElement ( 'td' );
if ( row === 'Totals' ) {
const span = document . createElement ( 'span' );
span . id = ` $ { playerId } _total ` ;
span . innerText = 0 ;
td . appendChild ( span );
} else {
const input = document . createElement ( 'input' );
input . type = 'number' ;
input . id = ` $ { playerId } _ $ { row . toLowerCase ()} ` ;
input . value = 0 ;
input . onfocus = () => showButtons ( input );
input . oninput = () => {
console . log ( ` Input changed : $ { input . id }, New Value : $ { input . value } ` );
calculateTotal ();
};
td . appendChild ( input );
}
document . getElementById ( ` row $ { row } ` ) . appendChild ( td );
});
}
function removePlayer () {
if ( playerCount > 2 ) {
const playerId = ` p $ { playerCount } ` ;
// Remove player header
const th = document . querySelector ( 'thead tr' ) . lastChild ;
document . querySelector ( 'thead tr' ) . removeChild ( th );
// Remove player input fields from each row
const rows = [ 'Critters' , 'Connections' , 'Volcanoes' , 'Sacrifices' , 'Bonuses' , 'Totals' ];
rows . forEach ( row => {
const td = document . getElementById ( ` row $ { row } ` ) . lastChild ;
document . getElementById ( ` row $ { row } ` ) . removeChild ( td );
});
playerCount -- ;
calculateTotal ();
}
}
function showButtons ( input ) {
selectedInput = input ;
const buttonContainer = document . getElementById ( 'plusminusButtonContainer' );
buttonContainer . style . display = 'block' ;
buttonContainer . style . position = 'absolute' ;
buttonContainer . style . top = ` $ { input . getBoundingClientRect () . top + window . scrollY + input . offsetHeight } px ` ;
buttonContainer . style . left = ` $ { input . getBoundingClientRect () . left + window . scrollX } px ` ;
}
function hideButtons () {
const buttonContainer = document . getElementById ( 'plusminusButtonContainer' );
buttonContainer . style . display = 'none' ;
}
function incrementScore () {
if ( selectedInput ) {
selectedInput . value = parseInt ( selectedInput . value ) + 1 ;
calculateTotal ();
}
}
function decrementScore () {
if ( selectedInput ) {
selectedInput . value = parseInt ( selectedInput . value ) - 1 ;
calculateTotal ();
}
}
function calculateTotal () {
console . log ( 'Calculating total...' );
let highestTotal = 0 ;
let highestPlayerId = null ;
for ( let i = 1 ; i <= playerCount ; i ++ ) {
const playerId = ` p $ { i } ` ;
const critters = parseInt ( document . getElementById ( ` $ { playerId } _critters ` ) . value ) || 0 ;
const connections = parseInt ( document . getElementById ( ` $ { playerId } _connections ` ) . value ) || 0 ;
const volcanoes = parseInt ( document . getElementById ( ` $ { playerId } _volcanoes ` ) . value ) || 0 ;
const sacrifices = parseInt ( document . getElementById ( ` $ { playerId } _sacrifices ` ) . value ) || 0 ;
const bonuses = parseInt ( document . getElementById ( ` $ { playerId } _bonuses ` ) . value ) || 0 ;
console . log ( ` Player $ { i } - Critters : $ { critters }, Connections : $ { connections }, Volcanoes : $ { volcanoes }, Sacrifices : $ { sacrifices }, Bonuses : $ { bonuses } ` );
const total = critters + connections + volcanoes + sacrifices + bonuses ;
document . getElementById ( ` $ { playerId } _total ` ) . innerText = total ;
if ( total > highestTotal ) {
highestTotal = total ;
highestPlayerId = playerId ;
}
}
highlightHighestTotal ( highestPlayerId );
}
function highlightHighestTotal ( playerId ) {
// Remove previous highlights
for ( let i = 1 ; i <= playerCount ; i ++ ) {
const totalCell = document . getElementById ( ` p $ { i } _total ` ) . parentElement ;
totalCell . style . backgroundColor = '' ;
totalCell . style . color = '' ;
}
// Find the highest total and check for ties
let highestTotal = 0 ;
let highestPlayerIds = [];
for ( let i = 1 ; i <= playerCount ; i ++ ) {
const total = parseInt ( document . getElementById ( ` p $ { i } _total ` ) . innerText ) || 0 ;
if ( total > highestTotal ) {
highestTotal = total ;
highestPlayerIds = [ ` p $ { i } ` ];
} else if ( total === highestTotal ) {
highestPlayerIds . push ( ` p $ { i } ` );
}
}
// Highlight the highest total ( s ) if there 's no tie
if ( highestPlayerIds . length === 1 ) {
const highestTotalCell = document . getElementById ( ` $ { highestPlayerIds [ 0 ]} _total ` ) . parentElement ;
highestTotalCell . style . backgroundColor = 'lightgreen' ;
highestTotalCell . style . color = 'black' ;
}
}
// Add event listener to hide buttons when clicking outside the input box
document . addEventListener ( 'click' , ( event ) => {
if ( selectedInput && ! selectedInput . contains ( event . target ) && ! document . getElementById ( 'plusminusButtonContainer' ) . contains ( event . target )) {
hideButtons ();
}
});
Lava your comments here