Compress room, fix ai starter

This commit is contained in:
Thibault Duplessis 2012-03-30 15:47:29 +02:00
parent 01cdd71206
commit ff498e7566
8 changed files with 36 additions and 25 deletions

View file

@ -9,7 +9,7 @@ mongo {
entry = lobby_entry
message = lobby_message
history = user_history
room = game_room
room = room
}
}
sync {

View file

@ -25,8 +25,11 @@ final class AppApi(
_ save(pov.game, g4)
} yield ()
def start(gameId: String, entryData: String): IO[Unit] =
starter.start(gameId, entryData) map (_ Unit)
def start(gameId: String, entryData: String): IO[Unit] = for {
g1 gameRepo game gameId
g2 starter.start(g1, entryData)
_ save(g1, g2)
} yield ()
def rematchAccept(
gameId: String,

View file

@ -12,9 +12,6 @@ final class Starter(
entryMemo: EntryMemo,
ai: Ai) extends IOTools {
def start(gameId: String, entryData: String): IO[DbGame] =
gameRepo game gameId flatMap { start(_, entryData) }
def start(game: DbGame, entryData: String): IO[DbGame] = for {
_ if (game.variant == Standard) io() else gameRepo saveInitialFen game
_ addEntry(game, entryData)

View file

@ -84,18 +84,20 @@ class GameRepo(collection: MongoCollection)
def saveInitialFen(dbGame: DbGame): IO[Unit] = io {
update(
DBObject("_id" -> dbGame.id),
$set ("initialFen" -> (Forsyth >> dbGame.toChess))
$set("initialFen" -> (Forsyth >> dbGame.toChess))
)
}
def ensureIndexes: IO[Unit] = io {
collection.ensureIndex(DBObject("status" -> 1))
collection.ensureIndex(DBObject("userIds" -> 1))
collection.ensureIndex(DBObject("winnerUserId" -> 1))
collection.ensureIndex(DBObject("turns" -> 1))
collection.ensureIndex(DBObject("updatedAt" -> -1))
collection.ensureIndex(DBObject("createdAt" -> -1))
collection.ensureIndex(DBObject("createdAt" -> -1, "userIds" -> 1))
collection.underlying |> { coll
coll.ensureIndex(DBObject("status" -> 1))
coll.ensureIndex(DBObject("userIds" -> 1), DBObject("sparse" -> true))
coll.ensureIndex(DBObject("winnerUserId" -> 1), DBObject("sparse" -> true))
coll.ensureIndex(DBObject("turns" -> 1))
coll.ensureIndex(DBObject("updatedAt" -> -1))
coll.ensureIndex(DBObject("createdAt" -> -1))
coll.ensureIndex(DBObject("createdAt" -> -1, "userIds" -> 1))
}
}
def dropIndexes: IO[Unit] = io {

View file

@ -19,7 +19,7 @@ class RoomRepo(collection: MongoCollection)
def addMessage(id: String, author: String, message: String): IO[Unit] = io {
collection.update(
DBObject("_id" -> id),
$push("messages" -> List(author, message)),
$push("messages" -> Room.encode(author, message)),
upsert = true,
multi = false
)

View file

@ -112,6 +112,7 @@ case class DbGame(
turns = game.turns,
positionHashes = history.positionHashes mkString,
castles = history.castleNotation,
lastMove = history.lastMove map { case (a, b) a + " " + b },
status =
if (situation.checkMate) Mate
else if (situation.staleMate) Stalemate

View file

@ -8,19 +8,28 @@ import collection.JavaConversions._
case class Room(
@Key("_id") id: String,
messages: List[BasicDBList]) {
messages: List[String]) {
def render: String = messages map (_.toList) map {
case author :: message :: Nil Room.render(author.toString, message.toString)
case _ ""
} mkString
def render: String = messages map ((Room.render _) compose Room.decode) mkString
}
object Room {
def render(author: String, message: String): String =
def encode(author: String, message: String): String = (author match {
case "white" "w"
case "black" "b"
case _ "s"
}) + message
def decode(encoded: String): (String, String) = (encoded take 1 match {
case "w" "white"
case "b" "black"
case _ "system"
}, encoded drop 1)
def render(msg: (String, String)): String =
"""<li class="%s%s">%s</li>""".format(
author,
if (author == "system") " trans_me" else "",
escapeXml(message))
msg._1,
if (msg._1 == "system") " trans_me" else "",
escapeXml(msg._2))
}

1
todo
View file

@ -1,3 +1,2 @@
messenger (use it to display end of game status string)
move times
blurs