-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.coffee
executable file
·76 lines (62 loc) · 2.44 KB
/
server.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Lounge GCP Server Node
#
# Copyright (c) 2013 by it's authors.
# Some rights reserved. See LICENSE..
IO = 10
module.exports =
start: (callback) ->
# express web application configuration
@server = (express = require 'express')()
@server.set 'views', "views"
@server.set 'view engine', 'jade'
@server.use express.bodyParser()
@server.use express.static 'public'
@server.set 'view options', { pretty: true }
# html routes
@server.get '/', (req, res) -> res.render 'points'
# GCP messaging protocol socket.io server implementation
IO = require('socket.io').listen @server.listen 7777, callback
IO.set 'log level', 1
History = []
IO.sockets.on 'connection', (player) ->
player.on 'login', (msg) ->
if name = msg.match(/I am (.+)$/)?[1]
@set 'name', name # player logged in
@emit 'welcome', "Logged in as "+name
else
@emit 'sorry', "Try again later"
@disconnect 'unauthorized'
player.on 'chat', (text) ->
@get 'name', (err, name) -> if name
msg = { text: text, sender: name }
IO.sockets.emit 'chat', msg
History.push { what: 'chat', msg: msg }
player.on 'host', (match) ->
@get 'name', (err, name) => if name
match.id = RandomGUID()
match.host = name
@join match.id
IO.sockets.emit 'host', match
History.push { what: 'host', msg: match }
player.on 'join', (match) ->
@get 'name', (err, name) => if name
@join match.id
match.player = name
IO.sockets.emit 'join', match
History.push { what: 'join', msg: match }
player.on 'msg', (msg) ->
@get 'name', (err, name) => if name
msg.sender = name
msg.next = SelectNext(msg.match, name)
IO.sockets.in(msg.match).emit 'msg', msg
History.push { what: 'msg', msg: msg }
player.on 'history', (msg) ->
@emit m.what, m.msg for m in History
player.on 'logout', (msg) ->
console.log "logout"
RandomGUID = () -> ((1+Math.random())*0x1000000|0).toString(16).substring(1)
SelectNext = (match, activePlayer) -> (n for n in (s.store.data.name for s in IO.sockets.clients(match)) when n != activePlayer)[0]
stop: (callback) -> IO.server.close(); callback()
gcp: ->
require('socket.io-client').connect "http://localhost:7777",
{ 'force new connection': true }