X-Git-Url: https://git.cworth.org/git?p=lmno-server;a=blobdiff_plain;f=empathy.js;h=7b27ee40c5d196866b19036455c66e6b5ebe713e;hp=fdf62a18d41abb42e1a41741ee2625e313b9faf0;hb=3dae3833cb5247943e27649a9b16639c2187c8c5;hpb=471ad8aebe1ce9a933b68cfadaffe0f68a01c255 diff --git a/empathy.js b/empathy.js index fdf62a1..7b27ee4 100644 --- a/empathy.js +++ b/empathy.js @@ -50,17 +50,28 @@ class Empathy extends Game { reset() { - /* Before closing out the current round, we accumulate the score - * for each player into their runnning total. */ - for (let score of this.state.scores.scores) { - const player = this.players.find(p => p.name === score.player); - if (player.score) - player.score += score.score; - else - player.score = score.score; - - /* And broadcast that new score out. */ - this.broadcast_event('player-update', player.info_json()); + /* Before closing out the current round, we accumulate into each + * player's overall score the results from the current round. + * + * Note: Rather than applying the actual points from each round + * into the player's score, we instead accumulate up the number of + * players that they bested in each round. This ensures that each + * round receives an equal weight in the overall scoring. */ + let bested = this.state.scores.scores.reduce( + (total, score) => total + score.players.length, 0); + for (let i = 0; i < this.state.scores.scores.length; i++) { + const score = this.state.scores.scores[i]; + bested -= score.players.length; + for (let player_name of score.players) { + const player = this.players.find(p => p.name === player_name); + if (player.score) + player.score += bested; + else + player.score = bested; + + /* And broadcast that new score out. */ + this.broadcast_event('player-update', player.info_json()); + } } /* Now that we're done with the active prompt, we remove it from @@ -513,6 +524,18 @@ class Empathy extends Game { return b.score - a.score; }); + /* After sorting individual players by score, group players + * together who have the same score. */ + const reducer = (list, next) => { + if (list.length && list[list.length-1].score == next.score) + list[list.length-1].players.push(next.player); + else + list.push({players: [next.player], score: next.score}); + return list; + }; + + const grouped_scores = scores.reduce(reducer, []); + /* Put the word groups into a form the client can consume. */ const words_submitted = word_groups.map( @@ -531,7 +554,7 @@ class Empathy extends Game { /* Put this round's scores into the game state object so it will * be sent to any new clients that join. */ this.state.scores = { - scores: scores, + scores: grouped_scores, words: words_submitted };