Skip to content

Instantly share code, notes, and snippets.

@rudyjahchan
Created February 28, 2014 18:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudyjahchan/9276808 to your computer and use it in GitHub Desktop.
Save rudyjahchan/9276808 to your computer and use it in GitHub Desktop.
Game = require '../models/game'
game = null
sendGameUpdate = (game)->
process.send
type: 'game'
game: game.toJSON()
sendGameStop = (game)->
process.send
type: 'stop'
name: game.name
createGame = (attributes)->
game = new Game(attributes)
game.addListener 'game', sendGameUpdate
game.addListener 'stopped', sendGameStop
game.start()
game
process.on 'message', (event)->
switch event.type
when 'init'
createGame(event.game)
when 'addCycle'
(game.width = event.game.width) if game.width > event.game.width
(game.height = event.game.height) if game.height > event.game.height
if (cycle = game.addCycle())?
process.send
type: 'cycleAdded'
id: event.id
cycleNumber: cycle.number
game: game.toJSON()
else
process.send
type: 'addCycleFailed'
id: event.id
name: event.game.name
when 'removeCycle'
game.removeCycle event.cycleNumber
when 'movement'
game.moveCycle event.cycleNumber, event.movement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment