vimtronner
Version:
A multi-player Vim trainer.
63 lines (49 loc) • 1.74 kB
text/coffeescript
http = require 'http'
socketio = require 'socket.io'
Moniker = require 'moniker'
Game = require '../models/game'
ClientSocket = require './client_socket'
createGame = (attributes, server)->
game = new Game(attributes)
game.addListener 'game', server.onGameChange
game.addListener 'stopped', server.onGameStopped
game.start()
game
class Server
constructor: (attributes={})->
= attributes.createGame ? createGame
= {}
getGame: (attributes) ->
attributes.name ?= Moniker.choose()
[attributes.name] = ([attributes.name] ? createGame(attributes, @))
gameList: ->
(game.toJSON() for name, game of )
onGameChange: (game)=>
.sockets.in(game.name).emit 'game', game.toJSON()
onGameStopped: (game)=>
socket.disconnect() for socket in .sockets.clients(game.name)
delete [game.name]
listen: (=8766, options..., cb=(->))=>
= setInterval , 180000
collectedOptions = { log: false }
for option in options
for key, value of option
collectedOptions[key] = value
= http.createServer()
= socketio.listen(, collectedOptions)
.sockets.on 'connection',
.listen , cb
checkForDeadGames: =>
games = (game for name, game of )
game.stop() for game in games when game.outdated
onConnection: (socket)=>
new ClientSocket(socket, @)
onRequest: (request, response)=>
response.writeHead 303, {
'Location': 'http://carbonfive.github.io/vimtronner'
}
response.end()
close: (cb=(->))->
clearInterval
?.close cb
module.exports = Server