-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
101 lines (76 loc) · 2.61 KB
/
app.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
###
Game hack server
###
url = require 'url'
path = require 'path'
fs = require 'fs'
#mapHelper = require './mapHelper.functions'
PlayerList = require './PlayerList'
# MIME Types
mimeTypes =
html: "text/html"
jpeg: "image/jpeg"
jpg: "image/jpeg"
png: "image/png"
js: "text/javascript"
css: "text/css"
mp3: "audio/mpeg"
app = require('http').createServer (req, res) ->
uri = url.parse(req.url).pathname
query = url.parse(req.url, true).query
if uri is '/'
uri = '/index.html'
# no special case, so use the filesystem
filename = path.join process.cwd()+"/../client/", uri
path.exists filename, (exists) ->
unless exists
console.log "not exists: " + filename
res.writeHead 404, {'Content-Type': 'text/plain'}
res.write '404 Not Found\n'
res.end('not found')
return
mimeType = mimeTypes[path.extname(filename).split(".")[1]]
res.writeHead 200, {'Content-Type':mimeType}
fileStream = fs.createReadStream filename
fileStream.pipe res
# Port stuff, start the webserver
args = process.argv.splice 2
if args.length < 1
port = 8080
else
port = args[0]
port = Number port
if isNaN port
port = 8080
portSocket = port + 1
console.log 'Listening on port', port, 'and', portSocket
app.listen port
# Game time starts here:
gameTimeZero = new Date().getTime()/1000
# List of players
playerList = new PlayerList()
# Socket Server
## start socket io
io = require('socket.io').listen portSocket
io.sockets.on 'connection', (socket) =>
socket.setMaxListeners 0
playerId = null
# send gametime
socket.emit 'startGame', { y: playerList.getStartY(), gametime: (new Date().getTime()/1000) - gameTimeZero}
socket.on 'playerEvent', (data) =>
playerId = data.id
playerList.getAddEventForPlayer data.id, data.event
eventBroadcastEvent = (id, event) =>
if id isnt playerId
socket.emit 'playerEventBroadcast', { id: id, event:event}
playerList.on 'playerEventBroadcast', eventBroadcastEvent
socket.on 'playerDead', (data) =>
playerList.deletePlayer data.id
removePlayerBroadcastEvent = (id) =>
socket.emit 'removePlayerBroadcast', id
playerList.on 'removePlayerBroadcast', removePlayerBroadcastEvent
socket.on 'disconnect', () =>
playerList.removeListener 'playerEventBroadcast', eventBroadcastEvent
playerList.removeListener 'removePlayerBroadcast', removePlayerBroadcastEvent
socket.on 'sendY', (y) =>
playerList.setY y