-
Notifications
You must be signed in to change notification settings - Fork 0
/
games.coffee
255 lines (183 loc) · 5.34 KB
/
games.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
_ = require 'lodash'
Q = require 'q'
async = require 'async-q'
Chance = require 'chance'
irc = require 'irc-colors'
PokemonDb = require './pokemondb'
asArray = (collection) ->
_.chain collection
.map (v,k) -> [k, v]
.zip()
.value()
Chance::partialString = (string) ->
percent = @floating min: 0.2, max: 0.45
len = string.length * percent // 1
restLen = string.length - len
string[...len] + (new Array restLen+1).join '_'
fixPokemonName = (name) ->
return name if name.toLowerCase() in ['ho-oh', 'mr-mime']
name.replace /\-.+/g, ''
class exports.Game
constructor: (@module, @origin, @originStr, @params) ->
start: (a...) ->
@onStart? a...
stop: (a...) ->
@onStop? a...
@module.gameStopped @
say: (msg) ->
@module.reply @origin, msg
class exports.GuessPokemonGame extends exports.Game
pokemonCount: 719
questionTimeout: 12 * 1000
nextQuestionDelay: 3 * 1000
hintCount: 3
maxRounds: Infinity
hints:
hard: asArray
'type': 8
'stat': 3
'move': 12
'species': 5
final: asArray
'partial name': 1
getDifficulty: (currentHint) ->
return 'final' if currentHint is @hintCount
'hard'
constructor: ->
super
@chance = new Chance
@stopped = no
@currentRound = 0
for key, value of @params
switch key
when 'hints'
@hintCount = Number value
when 'rounds'
if value in ['infinite', 'infinity', 'unlimited']
@maxRounds = Infinity
else @maxRounds = Number value
when 'hint-interval'
@questionTimeout = (Number value) * 1000
when 'next-question-delay'
@nextQuestionDelay = (Number value) * 1000
onStart: ->
@startQuestion()
onStop: ->
@stopped = yes
@stopCurrentQuestion()
assertNotStopped: (pkmn) ->
if @stopped or (pkmn? and pkmn isnt @currentPkmn)
e = new Error 'Stopped.'
e.stopped = yes
throw e
startQuestion: ->
Q.fcall =>
@assertNotStopped()
@currentRound++
@say irc.green 'Getting next Pokemon...'
id = @chance.pick [0...@pokemonCount]
PokemonDb.pokemon {id}
.then (@currentPkmn) =>
@assertNotStopped()
@currentPkmn.name = fixPokemonName @currentPkmn.name
descObj = @chance.pick currentPkmn.descriptions
PokemonDb.get descObj.resource_uri
.then (@desc) =>
@assertNotStopped()
console.log 'The game may now commence.'
@say "
#{irc.bold "Who's that Pokemon?!"}
#{@redactPokemonName @currentPkmn, @desc.description}
"
@hintsGiven = 0
@hintLimits =
'type': 1
'stat': 2
'move': 3
'species': 1
'partial name': 1
.then =>
pkmn = @currentPkmn
Q.fcall =>
async.until (=> @hintsGiven >= @hintCount), =>
Q.delay @questionTimeout
.then =>
@assertNotStopped pkmn
@dropHint @getDifficulty ++@hintsGiven
.delay @questionTimeout
.then =>
@assertNotStopped pkmn
@correctAnswer null
.fail (err) =>
if not err.stopped?
console.error err.stack
@say "Looks like there was an error! #{irc.red.bold err.toString()}"
@stop()
dropHint: (difficulty = 'hard') ->
loop
hintType = @chance.weighted @hints[difficulty]...
continue if @hintLimits[hintType]? and @hintLimits[hintType]-- <= 0
switch hintType
when 'type'
[type1, type2] = @currentPkmn.types
type = if type2? then "#{type1.name}-#{type2.name}" else type1.name
@say "It is of type #{irc.bold type}."
when 'stat'
stats =
hp: 'HP'
attack: 'Attack'
defense: 'Defense'
sp_def: 'Sp. Defense'
sp_atk: 'Sp. Attack'
speed: 'Speed'
stat = @chance.pick _.keys stats
@say "Its base #{irc.bold stats[stat]} is #{irc.bold @currentPkmn[stat]}."
when 'move'
continue if @currentPkmn.moves.length <= 0
move = @chance.pick @currentPkmn.moves
continue if move.learn_type in ['machine']
learnedFrom = switch move.learn_type
when 'tutor' then "from a #{irc.bold 'Tutor'}"
when 'level up' then "at #{irc.bold 'level ' + move.level}"
when 'egg move' then "as an #{irc.bold 'Egg Move'}"
@say "It can learn #{irc.bold move.name} #{learnedFrom}."
when 'species'
continue if not @currentPkmn.species? or @currentPkmn.species is ''
@say "It is known as the #{irc.bold @currentPkmn.species}."
when 'partial name'
name = @chance.partialString @currentPkmn.name
@say "Its name is #{irc.bold name}."
console.log "Dropped hint of type '#{hintType}' (difficulty: #{difficulty})"
break
stopCurrentQuestion: ->
@currentPkmn = null
correctAnswer: (answeredBy) ->
if answeredBy?
console.log "Answered by #{answeredBy.user}"
@say "
#{irc.bold answeredBy.user} got the right answer!
It was #{irc.bold @currentPkmn.name}!
"
else
console.log 'No one answered.'
@say "#{irc.red.bold "Time's up!"} It was #{irc.bold @currentPkmn.name}!"
@stopCurrentQuestion()
if @currentRound >= @maxRounds
@say 'Game has been stopped.'
console.log 'Halted game.'
@stop()
Q.delay @nextQuestionDelay
.then => @startQuestion()
redactPokemonName: (pkmn, text) ->
text.replace (new RegExp pkmn.name, 'ig'), '[REDACTED]'
onMessage: (origin, message) ->
return if not @currentPkmn?
normalize = (str) ->
str
.replace /[^\w]/g, ''
.toLowerCase()
if (normalize message) is (normalize @currentPkmn.name)
@correctAnswer origin
exports.gameTypes =
'whos-that-pokemon': exports.GuessPokemonGame
'guess-pokemon': exports.GuessPokemonGame