Skip to content

Commit

Permalink
Messing with validation of messages. I have a bit more to do, but thi…
Browse files Browse the repository at this point in the history
…s is the first step
  • Loading branch information
phillc committed Jun 22, 2014
1 parent d9ef30f commit c8da57c
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 8 deletions.
12 changes: 11 additions & 1 deletion lib/agent.coffee
Expand Up @@ -72,11 +72,21 @@ module.exports = class Agent

forward: (message, data) ->
logger.verbose "<<<<<forwarding", message, " - ", data
@connectionState.handle "forward", message, data
if @validator
@validator.validate message, data, (err, d) =>
if !err
@connectionState.handle "forward", message, d
else
#TODO: Broadcast error
logger.error "EERRRRRORRORORRR", err.toString()
else
@connectionState.handle "forward", message, data

send: (message, data) ->
logger.verbose ">>>>>>>sending", message, " - ", data
@connectionState.handle "send", message, data

validateWith: (@validator) ->

# end: (message, data)

2 changes: 2 additions & 0 deletions lib/arena.coffee
Expand Up @@ -23,9 +23,11 @@ module.exports = class Arena extends EventEmitter

availablePositions = @builder.positions(agents.length)
game = @builder.createGame(availablePositions.slice(0))
messageValidator = @builder.validator()

for agent in und.shuffle(agents)
position = availablePositions.shift()
agent.validateWith(messageValidator)
do (game, position, agent) =>
logger.info "wiring up #{agent} to #{position}"

Expand Down
2 changes: 1 addition & 1 deletion lib/cardPile.coffee
Expand Up @@ -51,4 +51,4 @@ module.exports = class CardPile
@cards.length == 0

toJSON: ->
@cards
@cards.map (card) -> card.toJSON()
2 changes: 1 addition & 1 deletion lib/entrance.coffee
Expand Up @@ -9,7 +9,7 @@ module.exports = class Entrance
agent.send("entryGranted", message: "How ya doing?")
if arena = @arenas[data.game]
agent.in.once 'requestingGame', (data) =>
console.log "game requested", data
logger.info "game requested", data
arena.addAgent(agent)
else
agent.send("error", foo: "What?")
Expand Down
1 change: 1 addition & 0 deletions lib/fireworks/builder.coffee
Expand Up @@ -14,3 +14,4 @@ module.exports = class FireworksBuilder
createGame: (positions) ->
new FireworksGame(positions: positions)

validator: ->
4 changes: 0 additions & 4 deletions lib/hearts/actions.coffee
Expand Up @@ -4,10 +4,6 @@ Rank = require './rank'
Suit = require './suit'
logger = require '../logger'

# I'm happy with the way this logic is separated, but I am not happy with
# the fact that messages to the agents are in this async callback world,
# but messages from the agent to the game are going through the handleAction path

exports.PassCards = class PassCards
@build: (data) ->
new PassCards(data.cards.map (jsonCard) -> Card.fromJSON(jsonCard))
Expand Down
3 changes: 3 additions & 0 deletions lib/hearts/builder.coffee
@@ -1,4 +1,5 @@
HeartsGame = require './game'
Validator = require './validator'

module.exports = class HeartsBuilder
minAgents: 4
Expand All @@ -14,3 +15,5 @@ module.exports = class HeartsBuilder
createGame: ->
new HeartsGame({heartsMaxPoints: @gameOptions.heartsMaxPoints})

validator: ->
new Validator()
23 changes: 23 additions & 0 deletions lib/hearts/validator.coffee
@@ -0,0 +1,23 @@
Joi = require 'joi'

module.exports = class Validator
constructor: ->
@schemas =
readyForRound: Joi.object().keys({})
passCards:
Joi.object().keys
cards: Joi.array().length(3)
readyForTrick: Joi.object().keys({})
playCard:
Joi.object().keys
card: Joi.object()
finishedRound: Joi.object().keys({})
finishedGame: Joi.object().keys({})

validate: (message, data, callback) ->
schema = @schemas[message]
if schema
schema.validate data, (args...) ->
callback(args...)
else
callback "Unknown message, #{message}"
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -19,7 +19,8 @@
"connect-assets": "2.4.3",
"less": "1.3.x",
"machina": "0.3.4",
"socket.io": "1.0.3"
"socket.io": "1.0.3",
"joi": "4.6.0"
},
"devDependencies": {
"mocha": "1.12.0",
Expand Down
1 change: 1 addition & 0 deletions test/lib/arenaTest.coffee
Expand Up @@ -8,6 +8,7 @@ describe "Arena", ->
start: ->
on: ->
Game: {}
validator: -> null
@arena = new Arena(builder, [])

describe "#addAgent", ->
Expand Down
26 changes: 26 additions & 0 deletions test/lib/hearts/validatorTest.coffee
@@ -0,0 +1,26 @@
Validator = require "../../../lib/hearts/validator"
Pile = require "../../../lib/hearts/pile"
Builder = require "../../../lib/hearts/builder"

describe "Validator", ->
beforeEach ->
@validator = new Validator()

it "maps to the builder", ->
expect(new Builder().agentEvents).to.eql(Object.keys(@validator.schemas))

describe "#validate", ->
describe "passCards", ->
it "does not allow foo", (done) ->
@validator.validate "passCards", foo: 123, (err) ->
expect(err).to.not.be.empty
done()

it "allows cards", (done) ->
deck = Pile.createDeck()
cards = new Pile()
deck.moveCardsTo(3, cards)
@validator.validate "passCards", cards: cards.toJSON(), (err) ->
expect(err).to.be.null
done()

0 comments on commit c8da57c

Please sign in to comment.