Skip to content

Commit

Permalink
Don't log every message in server and fix client.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed Apr 17, 2017
1 parent 93666d6 commit a3b2471
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 6 additions & 1 deletion snake/message.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json, algorithm, future
import json, algorithm, future, strutils

# TODO: It's annoying that we need to import these for FoodKind and Point...
import gamelight/[vec, geometry], food
Expand Down Expand Up @@ -31,6 +31,11 @@ type
count*: int
top*: Player

proc `$`*(player: Player): string =
return "Player(nick: $1, score: $2, alive: $3, paused: $4)" % [
player.nickname, $player.score, $player.alive, $player.paused
]

proc parseMessage*(data: string): Message =
let json = parseJson(data)

Expand Down
2 changes: 1 addition & 1 deletion snake/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ proc updateClients(server: Server) {.async.} =
proc updateTopScore(server: Server, player: Player, hostname: string,
replay: Replay) =
if server.top.score < player.score:
info("New high score: $1 $2" % [$player, hostname])
server.top = player
server.top.alive = true

Expand Down Expand Up @@ -174,7 +175,6 @@ proc processClient(server: Server, client: Client) {.async.} =

let frame = frameFut.read()
if frame.opcode == Opcode.Text:
info("Received data from " & $client)
let processFut = processMessage(server, client, frame.data)
if processFut.failed:
error("Client ($1) attempted to send bad JSON? " % $client,
Expand Down
10 changes: 7 additions & 3 deletions tests/client.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import asyncdispatch, os, random

import gamelight/vec
import websocket

import ../snake/message
import ../snake/[message, replay, food]

proc createConnection(ip: string, i: int) {.async.} =
let ws = await newAsyncWebsocket(ip, Port(25473), "", ssl = false,
Expand All @@ -19,9 +20,12 @@ proc createConnection(ip: string, i: int) {.async.} =
await ws.sock.sendPing(true)

proc sendMessages() {.async.} =
await ws.sock.sendText(toJson(createHelloMessage("bot" & $i)), true)
var replay = newReplay()
await ws.sock.sendText(toJson(createHelloMessage("bot" & $i, nil)), true)
for i in 0 .. 90:
let msg = toJson(createScoreUpdateMessage(i, alive=true, paused=false))
let msg = toJson(createReplayEventMessage(
replay.recordFoodEaten((0.0, 0.0), Nibble)
))
await sleepAsync(random(800 .. 4000))
await ws.sock.sendText(msg, true)

Expand Down

0 comments on commit a3b2471

Please sign in to comment.