Skip to content

Commit 0110a56

Browse files
its-clawdiaits-clawdia
andauthored
improve: show all players stats in room (closes #27) (#46)
* improve(i9): show all players' stats in room (closes #27) * chore: add screenshot --------- Co-authored-by: its-clawdia <clawdia@mailbox.org>
1 parent 30d884d commit 0110a56

5 files changed

Lines changed: 40 additions & 2 deletions

File tree

client/css/main.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,19 @@ svg pattern path { stroke-width: 2; }
376376
padding: 2px 6px;
377377
}
378378
#tutorialClose:hover { color: #000; }
379+
380+
/* ── Player stats bar (multiplayer) ───────────────────── */
381+
#playerStats {
382+
display: flex;
383+
gap: 12px;
384+
justify-content: center;
385+
flex-wrap: wrap;
386+
padding: 6px 12px;
387+
font-size: 0.8em;
388+
color: #555;
389+
}
390+
#playerStats .pstat {
391+
background: #f0f0f0;
392+
border-radius: 12px;
393+
padding: 3px 10px;
394+
}

client/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<span id="cardsLeft">0</span>
2929
</div>
3030
</div>
31+
<div id="playerStats" style="display:none"></div>
3132
<div id="board">
3233
<div class="card"></div>
3334
<div class="card"></div>

client/js/app.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ $(function () {
2828
}, 300)
2929
renderBoardUpdate(null, data.board)
3030
renderStatsUpdate(data.stats)
31+
renderPlayerStats(data.allPlayerStats)
3132
if(!welcomeBack) {
3233
showTutorial()
3334
localStorage.setItem('welcomeBack_' + location.hostname, 'hell yeah')
@@ -70,6 +71,7 @@ $(function () {
7071
try {
7172
var data = JSON.parse(json)
7273
if(data.correct) {
74+
renderPlayerStats(data.allPlayerStats)
7375
var reason = 'good solution'
7476
//handle correct solution
7577
console.log(data)
@@ -135,6 +137,7 @@ $(function () {
135137
})
136138
renderBoardUpdate(data.oldCardsCids, data.newCards)
137139
$('.card.selected').removeClass('selected')
140+
renderPlayerStats(data.allPlayerStats)
138141
if(data.stats && data.stats.cardsLeft !== undefined) {
139142
$('#cardsLeft').html(data.stats.cardsLeft)
140143
}
@@ -203,6 +206,14 @@ $(function () {
203206
prompt('Share this link:', url)
204207
}
205208
})
209+
function renderPlayerStats(allStats) {
210+
if(!allStats || allStats.length <= 1) { $('#playerStats').hide(); return; }
211+
var html = allStats.map(function(p) {
212+
return '<span class="pstat">P' + p.playerNum + ': <strong>' + p.points + '</strong>pt (' + p.goodAttempts + '✓ ' + p.badAttempts + '✗)</span>';
213+
}).join('');
214+
$('#playerStats').html(html).show();
215+
}
216+
206217
var _turnToastEl = null
207218
var _turnToastInterval = null
208219

screenshot.png

6.5 KB
Loading

server/app.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ Session.prototype.getStatsFor = function(pid) {
184184
stats.cardsLeft = this.deck.cards.length;
185185
return stats
186186
}
187+
Session.prototype.getAllPlayerStats = function() {
188+
var cardsLeft = this.deck.cards.length;
189+
return Object.keys(this.players).map(function(pid, i) {
190+
var s = this.players[pid].stats;
191+
return { playerNum: i + 1, points: s.points, goodAttempts: s.goodAttempts, badAttempts: s.badAttempts, cardsLeft: cardsLeft };
192+
}, this);
193+
}
187194
Session.prototype.drawCards = function(cidsToDelete, count) {
188195
var that = this;
189196
count = count || 3;
@@ -340,6 +347,7 @@ io.on('connection', function(socket){
340347
else if(session.isSolution(solutionCids)) {
341348
var newCards = session.drawCards(solutionCids);
342349
stats = session.turnEnd('good solution');
350+
var allStats = session.getAllPlayerStats();
343351
//needs a check if this has worked!
344352

345353
// Auto-deal if no valid set remains on board
@@ -353,14 +361,16 @@ io.on('connection', function(socket){
353361
oldCardsCids: solutionCids,
354362
newCards: newCards,
355363
autoDeal: autoDeal,
356-
stats: stats
364+
stats: stats,
365+
allPlayerStats: allStats
357366
}));
358367

359368
socket.broadcast.emit('solution_found', JSON.stringify({
360369
oldCardsCids: solutionCids,
361370
newCards: newCards,
362371
autoDeal: autoDeal,
363-
stats: { cardsLeft: session.deck.cards.length }
372+
stats: { cardsLeft: session.deck.cards.length },
373+
allPlayerStats: allStats
364374
}));
365375
}
366376
else {

0 commit comments

Comments
 (0)