Skip to content
This repository was archived by the owner on Jul 18, 2023. It is now read-only.

Commit c591ed4

Browse files
author
makemeunsee
committed
show winner and scores at the end of the game
1 parent 57d7742 commit c591ed4

File tree

10 files changed

+81
-35
lines changed

10 files changed

+81
-35
lines changed

app/assets/javascripts/robotagram_game.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,16 @@ var messageReceived = function(event){
679679
currentGame.gamePhase = PHASEID_SHOW_SOLUTION
680680
console.log("entered phase " + currentGame.gamePhase);
681681
notifyGameTimeUp();
682+
var $endOfGameWinner = $("#endOfGameWinner");
683+
var winner = d.args[0];
684+
if(winner === "") {
685+
winner = $_("game.nowinner");
686+
}
687+
$endOfGameWinner.append(" " + winner);
688+
var $endOfGameScores = $("#endOfGameScores");
689+
for (var i=1;i<d.args.length;i++) {
690+
$endOfGameScores.append("<tr><td>" + d.args[i] + "</td></tr>");
691+
}
682692
} else {
683693
activateNextGameLink();
684694
notifyGameTimeUp();

app/controllers/Gaming.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ object Gaming extends CookieLang {
114114
// list of players and scores (persisted + players in the room)
115115
private def playersAndScores(roomName:String, gameId:String) : Seq[(String, Option[Int])] = {
116116
val wsRoom = WsManager.room(roomName).get
117-
var scores: mutable.HashMap[String, Option[Int]] = new mutable.HashMap[String, Option[Int]]()
117+
val scores: mutable.HashMap[String, Option[Int]] = new mutable.HashMap[String, Option[Int]]()
118118

119119
DbScore.findByGame(gameId).foreach{s=>
120120
scores += ((s.playerName, Some(s.score)))

app/models/DbScore.scala

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@ import java.util.Date
66
import play.api.db.DB
77
import anorm.SqlParser._
88
import scala.Some
9+
import scala.collection.mutable.HashMap
10+
import scala.collection.Map
911

1012
case class DbScore(id: Pk[Long], score: Int, dateSubmitted:Date, playerName:String)
11-
case class DbRoomScores(score: Int, gameId: String, playerName:String)
13+
case class DbRoomScores(score: Int, gameId: String, playerName:String, date: Date)
1214

1315
object DbRoomScores{
1416

15-
val simple = {
17+
private val simple = {
1618
get[Int]("scores.score") ~
1719
get[String]("scores.game_id") ~
18-
get[String]("users.name") map {
19-
case score~gameId~playerName => DbRoomScores(score, gameId, playerName)
20+
get[String]("users.name") ~
21+
get[Date]("scores.submitted_on") map {
22+
case score~gameId~playerName~date => DbRoomScores(score, gameId, playerName, date)
2023
}
2124
}
2225

23-
def scoresInRoom(roomId: Long): Seq[DbRoomScores] = {
26+
private def scoresInRoom(roomId: Long): Seq[DbRoomScores] = {
2427
DB.withConnection { implicit connection =>
2528
SQL("""
26-
select scores.score, scores.game_id, users.name
29+
select scores.score, scores.game_id, users.name, scores.submitted_on
2730
from games
2831
inner join scores
2932
on scores.game_id = games.id
@@ -36,6 +39,21 @@ object DbRoomScores{
3639
).as(DbRoomScores.simple *)
3740
}
3841
}
42+
43+
def winnersOfRoomId(roomId: Long): Map[String, (String, Int, Date)] = scoresInRoom(roomId).foldLeft(Map[String, (String, Int, Date)]()) {
44+
// fold to find the author of shortest and first solution of each game
45+
(map: Map[String, (String, Int, Date)], gameScore: DbRoomScores) => {
46+
val gameId = gameScore.gameId
47+
map.get(gameId) match {
48+
case None => map + (gameId -> (gameScore.playerName, gameScore.score, gameScore.date))
49+
case Some((playerName, score, date)) => if (score < gameScore.score || (score == gameScore.score && date.before(gameScore.date)) ) {
50+
map
51+
} else {
52+
map + (gameId -> (gameScore.playerName, gameScore.score, gameScore.date))
53+
}
54+
}
55+
}
56+
}
3957

4058
}
4159

app/models/Game.scala

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import java.util.{Date,Random}
1111
import play.api.Logger
1212
import controllers.Gaming
1313
import controllers.MessageID.TIME_UP
14+
import play.api.i18n.Messages
1415

1516

1617
class Game(val id:String, val roomId: Long, val board:Board, val goal: Goal, val startDate:Date, val endDate:Date, val robots:Map[Color, Robot], val gamePhase: Phase){
@@ -33,30 +34,22 @@ class Game(val id:String, val roomId: Long, val board:Board, val goal: Goal, val
3334
// switch to next game phase unless already at the end of the cycle
3435
val endedGame = toPhase(SHOW_SOLUTION)
3536
endedGame.timer.start()
36-
//TODO: update room scores, show them during game too
37-
println(DbRoomScores.scoresInRoom(roomId).foldLeft(new HashMap[String, (String, Int)]) {
38-
// first fold: first the winner of each game
39-
(map: HashMap[String, (String, Int)], gameScore: DbRoomScores) => {
40-
val gameId = gameScore.gameId
41-
map.get(gameId) match {
42-
case None => map + (gameId -> (gameScore.playerName, gameScore.score))
43-
case Some((playerName, score)) => if (score < gameScore.score) {
44-
map
45-
} else {
46-
map + (gameId -> (gameScore.playerName, gameScore.score))
47-
}
48-
}
49-
}
50-
}.foldLeft(new HashMap[String, Int]) {
51-
// second fold: count number of victories for each winner
52-
(map: HashMap[String, Int], victory: (String, (String, Int))) => map.get(victory._2._1) match {
37+
val winners = DbRoomScores.winnersOfRoomId(roomId)
38+
val winner = winners.get(id) match {
39+
case None => ""
40+
case Some((username, _, _)) => username
41+
}
42+
val scores = winners.foldLeft(new HashMap[String, Int]) {
43+
// fold to count number of victories for each winner
44+
(map: HashMap[String, Int], victory: (String, (String, Int, Date))) => map.get(victory._2._1) match {
5345
case None => map + (victory._2._1 -> 1)
5446
case Some(count) => map + (victory._2._1 -> (count+1))
5547
}
56-
})
48+
}.map(pair => pair._1 + ": " + pair._2).toList
49+
Gaming.notifyRoom(DbRoom.findById(roomId).get.name, TIME_UP, winner :: scores)
50+
} else {
51+
Gaming.notifyRoom(DbRoom.findById(roomId).get.name, TIME_UP, Seq())
5752
}
58-
// announce game dead to the rooms
59-
Gaming.notifyRoom(DbRoom.findById(roomId).get.name, TIME_UP, Seq())
6053
}
6154

6255
// game init

app/views/gaming/game.scala.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,17 @@ <h3>@Messages("game.solutionFound.title")</h3>
9595
<h3>@Messages("game.endOfGame.title")</h3>
9696
</div>
9797
<div class="modal-body" align="center">
98+
<p id="endOfGameWinner">@Messages("game.winner")</p>
99+
<p>@Messages("game.scores")</p>
100+
<table id="endOfGameScores">
101+
</table>
102+
<br>
98103
<p>@Messages("game.endOfGame.details")</p>
99104
</div>
100105
<div id="endOfGameModalFooter" class="modal-footer">
101106
<a href="@routes.Home.index" class="btn">@Messages("game.endOfGame.giveup")</a>
102107
@* "current" is the url for the current game in this room *@
103-
<p id="waitingForGame">@Messages("game.endOfGame.waiting")</p>
108+
<p id="waitingForGame" class="btn">@Messages("game.endOfGame.waiting")</p>
104109
</div>
105110
</div>
106111
</div>

app/views/shared/scores.scala.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
@(scores: Seq[(String, Option[Int])])
1+
@(scores: Seq[(String, Option[Int])])(implicit flash: Flash, request: RequestHeader, lang: Lang) @* implicits are needed ! *@
22

33
@import models._
44

55
<table class="table table-condensed">
66
<thead>
77
<tr>
8-
<th>Player</th>
9-
<th>Best score</th>
8+
<th>@Messages("score.player")</th>
9+
<th>@Messages("score.best")</th>
1010
</tr>
1111
</thead>
1212
<tbody id="leaderBoard">

conf/messages

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,20 @@ game.solutionFound.announce=Announce solution
105105
game.solutionFound.improve=Improve solution
106106
game.solutionFound.retry=Retry from zero
107107

108+
game.winner=The winner of this round is...
109+
game.scores=Score (rounds won):
110+
108111
game.endOfGame.title=Time is over !
109-
game.endOfGame.details=Another game is starting now ...
112+
game.endOfGame.details=Another game is going to start soon...
110113
game.endOfGame.giveup=Give up
111114
game.endOfGame.waiting=Waiting for the next game to start...
112115

116+
# Score
117+
# =====
118+
score.player=Player
119+
score.best=Best score
120+
121+
113122
# Already in the same room in another window
114123
alreadyIn.title=Already in this room
115124
alreadyIn.whatdo=Disconnect the other connection?

conf/messages.fr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,20 @@ game.solutionFound.announce=Annoncer ma solution
102102
game.solutionFound.improve=Améliorer ma solution
103103
game.solutionFound.retry=Recommencer depuis le début
104104

105+
game.winner=Le gagnant de la manche est...
106+
game.scores=Score (manches gagnées) :
107+
105108
game.endOfGame.title=Le temps est écoulé !
106-
game.endOfGame.details=Une autre partie commence...
109+
game.endOfGame.details=Une autre partie va bientôt commencer...
107110
game.endOfGame.giveup=Abandonner
108111
game.endOfGame.waiting=En attente de démarrage de la partie suivante...
109112

113+
# Score
114+
# =====
115+
score.player=Joueur
116+
score.best=Meilleur score
117+
118+
110119
# Already in the same room in another window
111120
alreadyIn.title=Déjà dans cette salle
112121
alreadyIn.whatdo=Couper l''autre connexion ?

public/i18n/Messages.client.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ game.leave.gameIsActive.confirm=A game is active... are you sure you want to lea
77
game.kicked=You''ve been kicked!
88
game.noselection=A robot must be selected
99
game.nextgame=Join the next game !
10-
game.solutionfound=A solution was announced! No time to lose!
10+
game.solutionfound=A solution was announced! No time to lose!
11+
game.nowinner=no one!

public/i18n/Messages.client_fr.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ game.leave.gameIsActive.confirm=Une partie est en cours...Êtes-vous sûr de vou
77
game.kicked=Vous avez été kické !
88
game.noselection=Il faut sélectionner un robot
99
game.nextgame=Rejoindre la partie suivante !
10-
game.solutionfound=Une solution a été trouvée ! Plus de temps à perdre !
10+
game.solutionfound=Une solution a été trouvée ! Plus de temps à perdre !
11+
game.nowinner=personne !

0 commit comments

Comments
 (0)