-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.coffee
184 lines (144 loc) · 4.12 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Module dependencies.
express = require('express')
routes = require('./routes')
user = require('./routes/user')
http = require('http')
path = require('path')
spell = require('lazy')
_ = require('underscore')
lazy = require("lazy")
fs = require("fs")
app = express()
app.configure( ->
app.set('port', process.env.PORT || 3000)
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(express.favicon())
app.use(express.logger('dev'))
app.use(express.bodyParser())
app.use(express.methodOverride())
app.use(app.router)
app.use(express.static(path.join(__dirname, 'public')))
)
app.configure('development', ->
app.use(express.errorHandler())
)
app.get('/', routes.index)
app.get('/users', user.list)
server = http.createServer(app)
io = require('socket.io').listen(server)
server.listen(app.get('port'), ->
console.log("Express server listening on port " + app.get('port'))
)
status = "All is well."
class Clients
constructor: ->
@players = {}
clear: ->
console.log "clearing"
for id, words of @players
console.log id, words
@players[id] = []
addWord: (clientId, word) ->
if @players[clientId]?
@players[clientId].push word
else
@players[clientId] = []
@players[clientId].push word
console.log "aading word #{word}"
console.log @players[clientId]
playerUsedWord: (clientId, word) ->
return false unless @players[clientId]
console.log clientId, word
_.find(@players[clientId], (w) ->
w is word
)
playerWords: (clientId) ->
@players[clientId].reverse()
class Scoreboard
constructor: ->
@scores = {}
addScore: (clientId, score) ->
if @scores[clientId]?
@scores[clientId] += score * 2
else
@scores[clientId] = score * 2
exists: (clientId) ->
@scores[clientId]?
class Bag
constructor: ->
@grabLetters()
@getDictWords(@setDictWords)
grabLetters: ->
bag = "AAAABBCCDDDEEEEFFGGHHIIIJKKLLLLMMMMNNNNOOOOPPPPPPQRRRRRSSSSTTTTUUUUVWWWXYYYZ"
@letters = _.map([0..15], ->
bag[Math.floor(Math.random() * bag.length)]
)
getDictWords: (callback) ->
@dict = []
new lazy(fs.createReadStream('public/words'))
.lines
.map( (line) =>
line.toString().toUpperCase()[0..-1]
).join(callback)
setDictWords: (dict) =>
@dict = dict
console.log "@dict size #{@dict.length}"
wordIsInBag: (word) ->
@comp = _.clone @letters
for l in word
index = _.indexOf(@comp, l)
return false if index is -1
@comp.splice index, 1
true
isValidWord: (word) ->
return false if word.length < 3
_.find(@dict, (w) ->
word is w
)
TheBag = new Bag
scoreboard = new Scoreboard
clients = new Clients
io.sockets.on('connection', (socket) ->
unless scoreboard.exists(socket.id)
socket.emit('letters', { letters: TheBag.letters })
socket.emit('youare', socket.id)
socket.emit('scoreboard', scoreboard.scores)
# note the use of io.sockets to emit but socket.on to listen
socket.on('word', (data) ->
console.log 'submitting', data
# is this in TheBag?
unless TheBag.wordIsInBag data
console.log "word not in bag"
socket.emit('wrong', { status: 'not in bag' })
return
isValid = TheBag.isValidWord data
console.log "isValid #{isValid}"
unless isValid
console.log "word not valid"
socket.emit('wrong', { status: 'not valid' })
return
if clients.playerUsedWord(socket.id, data)
console.log "word already used"
socket.emit('wrong', { status: 'word already used' })
return
clients.addWord(socket.id, data)
socket.emit('right', { status: 'correct', words: clients.playerWords(socket.id) })
# update scores
scoreboard.addScore socket.id, data.length
console.log scoreboard
io.sockets.emit('scoreboard', scoreboard.scores)
)
)
countdown = 31
setInterval( =>
countdown -= 1
#send timer message
console.log "sending tock #{countdown}"
io.sockets.emit('tock', countdown)
if countdown is 0
TheBag = new Bag
clients.clear()
io.sockets.emit('letters', { letters: TheBag.letters })
countdown = 31
, 1000)