-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsocket.coffee
More file actions
executable file
·80 lines (69 loc) · 1.39 KB
/
Copy pathsocket.coffee
File metadata and controls
executable file
·80 lines (69 loc) · 1.39 KB
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
http = require 'http'
io = require 'socket.io'
redis = require 'redis'
redisClient = redis.createClient()
redisClient.on 'error', (err)->
console.log 'Error' + err
server = http.createServer()
server.listen 8080
socket = io.listen server
pacman =
type: 'location'
x: 450
y: 150
sprite: 'pacman'
blinky =
x: 10
y: 60
pinky =
x: 10
y: 30
ghosts = ['clyde','inky','blinky','pinky']
counter = 0
socket.on 'connection', (client)->
getSprite = (name)->
redisClient.get name, (error, data)->
return if not data
console.log error
console.log JSON.parse data
data = JSON.parse data
client.send
type:'location'
sprite: name
x: data.x
y: data.y
ghost = ghosts.pop()
console.log ghost
client.send
type: 'ghost'
name: ghost
client.broadcast
type: 'newghost'
client.on 'message', (message)->
console.log message
switch message.type
when 'location'
redisClient.set message.ghost, JSON.stringify
x: message.x
y: message.y
client.broadcast
type: 'location'
sprite: message.ghost
x: message.x
y: message.y
when 'win'
client.broadcast
type: 'win'
ghost: ghost
client.send
type: 'win'
ghost: ghost
client.on 'disconnect', ->
console.log 'disconnected'
ghosts.push ghost
console.log 'resetting' + ghost
client.broadcast
type: 'location'
sprite: ghost
x: -100
y: -100