Skip to content

Commit

Permalink
Declare and show a winner at the end of each game.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1mmy authored and dgreensp committed Apr 10, 2012
1 parent 3c638d8 commit bba2469
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
8 changes: 8 additions & 0 deletions examples/wordplay/client/wordplay.css
Expand Up @@ -146,6 +146,14 @@ button:active {
font-style: italic;
}

#scores .winner {
background-color: yellow;
}

#scores .winner_text {
float: right;
}

#scores .word {
float: left;
font-size: 1.25em;
Expand Down
5 changes: 4 additions & 1 deletion examples/wordplay/client/wordplay.html
Expand Up @@ -99,13 +99,16 @@ <h1>{{count}} other players are in the lobby:</h1>

<template name="player">
<div class="player">
<div class="header">
<div class="header {{winner}}">
{{#if name}}
{{name}}
{{else}}
<span class="unnamed">no name</span>
{{/if}}
<span class="score">{{total_score}}</span>
{{#if winner}}
<span class="winner_text">Winner!</span>
{{/if}}
</div>
{{> words}}
</div>
Expand Down
7 changes: 7 additions & 0 deletions examples/wordplay/client/wordplay.js
Expand Up @@ -178,6 +178,13 @@ Template.scores.players = function () {
return game() && game().players;
};

Template.player.winner = function () {
var g = game();
if (!g.winners || !_.include(g.winners, this._id))
return '';
return 'winner';
};

Template.player.total_score = function () {
var words = Words.find({game_id: game() && game()._id,
player_id: this._id});
Expand Down
20 changes: 19 additions & 1 deletion examples/wordplay/server/game.js
Expand Up @@ -20,8 +20,26 @@ Meteor.methods({
var interval = Meteor.setInterval(function () {
clock -= 1;
Games.update(game_id, {$set: {clock: clock}});
if (clock === 0)

// end of game
if (clock === 0) {
// stop the clock
Meteor.clearInterval(interval);
// declare zero or more winners
var scores = {};
Words.find({game_id: game_id}).forEach(function (word) {
if (!scores[word.player_id])
scores[word.player_id] = 0;
scores[word.player_id] += word.score;
});
var high_score = _.max(scores);
var winners = [];
_.each(scores, function (score, player_id) {
if (score == high_score)
winners.push(player_id);
});
Games.update(game_id, {$set: {winners: winners}});
}
}, 1000);

return game_id;
Expand Down

0 comments on commit bba2469

Please sign in to comment.