-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
68 lines (45 loc) · 1.48 KB
/
index.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
module.exports = (Module) ->
games = require './games'
convertParamsToObj = (params) ->
pattern = /(\S+)\s*=\s*(\S+)/g
o = {}
while (match = pattern.exec params)?
o[match[1]] = match[2]
o
class PokegamesModule extends Module
shortName: 'Pokegames'
helpText:
default: 'A module with Pokemon games for Kurea'
originString: (origin) -> "#{origin.bot.getName()};#{origin.channel}"
constructor: (moduleManager) ->
super
@games = {}
startGame = (origin, route) =>
return if not origin.channel?
{mode} = route.params
[params] = route.splats
originStr = @originString origin
if @games[originStr]?
@reply origin, 'There is already a game going on in here!'
return
GameClazz = games.gameTypes[mode]
if not GameClazz?
@reply origin, "Unknown game mode: '#{mode}'"
return
@games[originStr] = game = new GameClazz @, origin, originStr, convertParamsToObj params
game.start()
@addRoute 'pokegames start :mode *', startGame
@addRoute 'pokegames start :mode', startGame
@addRoute 'pokegames stop', (origin, route) =>
return if not origin.channel?
originStr = @originString origin
@games[originStr].stop()
@on 'message', (bot, user, channel, message) =>
game = @games[@originString {bot, user, channel}]
game?.onMessage? {bot, user, channel}, message
destroy: ->
for originStr, game of @games
game.stop()
gameStopped: (game) ->
delete @games[game.originStr]
PokegamesModule