Mercurial > irccd
annotate plugins/tictactoe/tictactoe.js @ 1142:c0365364971c release-4.0
misc: create release-4.0 branch
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 03 Feb 2022 13:13:50 +0100 |
parents | f06e9761cc90 |
children | d0e522ff5143 |
rev | line source |
---|---|
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
1 /* |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
2 * tictactoe.js -- tictactoe game for IRC |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
3 * |
1136
f06e9761cc90
misc: update copyright years
David Demelier <markand@malikania.fr>
parents:
1018
diff
changeset
|
4 * Copyright (c) 2013-2022 David Demelier <markand@malikania.fr> |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
5 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
6 * Permission to use, copy, modify, and/or distribute this software for any |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
7 * purpose with or without fee is hereby granted, provided that the above |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
8 * copyright notice and this permission notice appear in all copies. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
9 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
17 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
18 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
19 // Plugin information. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
20 info = { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
21 author: "David Demelier <markand@malikania.fr>", |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
22 license: "ISC", |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
23 summary: "A tictactoe game for IRC", |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
24 version: "@IRCCD_VERSION@" |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
25 }; |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
26 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
27 // Modules. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
28 var Plugin = Irccd.Plugin; |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
29 var Util = Irccd.Util; |
998
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
30 var Timer = Irccd.Timer; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
31 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
32 // Formats. |
847
a23b7b574ed2
irccd: rename [format] section to [templates], closes #1671
David Demelier <markand@malikania.fr>
parents:
824
diff
changeset
|
33 Plugin.templates = { |
998
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
34 "draw": "Nobody won.", |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
35 "invalid": "#{nickname}, please select a valid opponent.", |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
36 "running": "#{nickname}, the game is already running.", |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
37 "turn": "#{nickname}, it's your turn.", |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
38 "used": "#{nickname}, this square is already used.", |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
39 "win": "#{nickname}, congratulations, you won!", |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
40 "timeout": "Aborted due to #{nickname} inactivity." |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
41 }; |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
42 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
43 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
44 * Create a game. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
45 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
46 * This function creates a game without any checks. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
47 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
48 * @param server the server object |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
49 * @param channel the channel |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
50 * @param origin the source origin |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
51 * @param target the target nickname |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
52 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
53 function Game(server, channel, origin, target) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
54 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
55 this.server = server; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
56 this.origin = origin; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
57 this.target = target; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
58 this.channel = channel; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
59 this.players = [ Util.splituser(origin), target ]; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
60 this.player = Math.floor(Math.random() * 2); |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
61 this.grid = [ |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
62 [ '.', '.', '.' ], |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
63 [ '.', '.', '.' ], |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
64 [ '.', '.', '.' ] |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
65 ]; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
66 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
67 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
68 // Pending games requests checking for names listing. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
69 Game.requests = {}; |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
70 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
71 // List of games running. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
72 Game.map = {}; |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
73 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
74 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
75 * Create a unique id. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
76 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
77 * @param server the server object |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
78 * @param channel the channel |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
79 * @return the id |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
80 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
81 Game.id = function (server, channel) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
82 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
83 return channel + "@" + server.toString(); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
84 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
85 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
86 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
87 * Get a running game or undefined. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
88 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
89 * @param server the server object |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
90 * @param channel the channel |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
91 * @return the object or undefined if not running |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
92 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
93 Game.find = function (server, channel) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
94 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
95 return Game.map[Game.id(server, channel)]; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
96 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
97 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
98 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
99 * Populate a set of keywords. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
100 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
101 * @param server the server object |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
102 * @param origin the originator |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
103 * @param channel the channel |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
104 * @return an object of predefined keywords |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
105 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
106 Game.keywords = function (server, channel, origin) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
107 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
108 var kw = { |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
109 channel: channel, |
995
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
110 command: server.info().prefix + Plugin.info().name, |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
111 plugin: Plugin.info().name, |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
112 server: server.info().name |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
113 }; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
114 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
115 if (origin) { |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
116 kw.origin = origin; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
117 kw.nickname = Util.splituser(origin); |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
118 } |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
119 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
120 return kw; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
121 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
122 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
123 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
124 * Tells if a game is pending or running. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
125 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
126 * @param server the server object |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
127 * @param channel the channel |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
128 * @return true if any |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
129 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
130 Game.exists = function (server, channel) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
131 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
132 var id = Game.id(server, channel); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
133 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
134 return Game.requests[id] || Game.map[id]; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
135 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
136 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
137 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
138 * Delete a game from the registry. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
139 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
140 * @param server the server object |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
141 * @param channel the channel |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
142 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
143 Game.remove = function (server, channel) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
144 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
145 delete Game.map[Game.id(server, channel)]; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
146 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
147 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
148 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
149 * Erase games when some players leave channels. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
150 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
151 * @param server the server object |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
152 * @param origin the originator |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
153 * @param channel the channel |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
154 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
155 Game.clear = function (server, user, channel) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
156 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
157 var nickname = Util.splituser(user); |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
158 var game = Game.find(server, channel); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
159 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
160 if (game && (game.players[0] === nickname || game.players[1] === nickname)) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
161 Game.remove(server, channel); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
162 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
163 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
164 /** |
995
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
165 * Check if the target is valid. |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
166 * |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
167 * @param server the server object |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
168 * @param channel the channel string |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
169 * @param nickname the nickname who requested the game |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
170 * @param target the opponent |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
171 * @return true if target is valid |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
172 */ |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
173 Game.isValid = function (server, channel, nickname, target) |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
174 { |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
175 if (target === "" || target === nickname || target === server.info().nickname) |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
176 return false; |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
177 |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
178 var channels = server.info().channels; |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
179 var ch; |
998
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
180 |
995
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
181 for (var i = 0; i < channels.length; ++i) { |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
182 if (channels[i].name === channel) { |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
183 ch = channels[i]; |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
184 break; |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
185 } |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
186 } |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
187 |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
188 for (var i = 0; i < ch.users.length; ++i) |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
189 if (ch.users[i].nickname === target) |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
190 return true; |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
191 |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
192 return false; |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
193 } |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
194 |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
195 /** |
998
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
196 * Function called when a timeout occured. |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
197 * |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
198 * @param game the game to destroy |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
199 */ |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
200 Game.timeout = function (game) |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
201 { |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
202 var kw = Game.keywords(game.server, game.channel); |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
203 |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
204 kw.nickname = game.players[game.player]; |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
205 game.server.message(game.channel, Util.format(Plugin.templates.timeout, kw)); |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
206 Game.remove(game.server, game.channel); |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
207 } |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
208 |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
209 /** |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
210 * Show the game grid and the next player line. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
211 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
212 Game.prototype.show = function () |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
213 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
214 var kw = Game.keywords(this.server, this.channel); |
998
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
215 var self = this; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
216 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
217 // nickname is the current player. |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
218 kw.nickname = this.players[this.player]; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
219 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
220 this.server.message(this.channel, " a b c"); |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
221 this.server.message(this.channel, "1 " + this.grid[0].join(" ")); |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
222 this.server.message(this.channel, "2 " + this.grid[1].join(" ")); |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
223 this.server.message(this.channel, "3 " + this.grid[2].join(" ")); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
224 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
225 if (this.hasWinner()) |
847
a23b7b574ed2
irccd: rename [format] section to [templates], closes #1671
David Demelier <markand@malikania.fr>
parents:
824
diff
changeset
|
226 this.server.message(this.channel, Util.format(Plugin.templates.win, kw)); |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
227 else if (this.hasDraw()) |
847
a23b7b574ed2
irccd: rename [format] section to [templates], closes #1671
David Demelier <markand@malikania.fr>
parents:
824
diff
changeset
|
228 this.server.message(this.channel, Util.format(Plugin.templates.draw, kw)); |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
229 else |
847
a23b7b574ed2
irccd: rename [format] section to [templates], closes #1671
David Demelier <markand@malikania.fr>
parents:
824
diff
changeset
|
230 this.server.message(this.channel, Util.format(Plugin.templates.turn, kw)); |
998
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
231 |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
232 // Create a timer in case of inactivity (5 minutes). |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
233 this.timer = new Irccd.Timer(Irccd.Timer.Single, 300000, function () { |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
234 Game.timeout(self); |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
235 }); |
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
236 this.timer.start(); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
237 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
238 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
239 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
240 * Tells if it's the nickname's turn. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
241 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
242 * @param nickname the nickname to check |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
243 * @return true if nickname is allowed to play |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
244 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
245 Game.prototype.isTurn = function (nickname) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
246 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
247 return this.players[this.player] == nickname; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
248 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
249 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
250 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
251 * Place the column and row as the current player. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
252 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
253 * @param column the column (a, b or c) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
254 * @param row the row (1, 2 or 3) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
255 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
256 Game.prototype.place = function (column, row, origin) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
257 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
258 var columns = { a: 0, b: 1, c: 2 }; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
259 var rows = { 1: 0, 2: 1, 3: 2 }; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
260 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
261 column = columns[column]; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
262 row = rows[row]; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
263 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
264 var kw = Game.keywords(this.server, this.channel, origin); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
265 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
266 if (this.grid[row][column] !== '.') { |
847
a23b7b574ed2
irccd: rename [format] section to [templates], closes #1671
David Demelier <markand@malikania.fr>
parents:
824
diff
changeset
|
267 this.server.message(this.channel, Util.format(Plugin.templates.used, kw)); |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
268 return false; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
269 } |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
270 |
998
474a46e240ff
plugin tictactoe: now has a timeout of inactivity
David Demelier <markand@malikania.fr>
parents:
995
diff
changeset
|
271 this.timer.stop(); |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
272 this.grid[row][column] = this.player === 0 ? 'x' : 'o'; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
273 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
274 // Do not change if game is finished. |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
275 if (!this.hasWinner() && !this.hasDraw()) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
276 this.player = this.player === 0 ? 1 : 0; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
277 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
278 return true; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
279 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
280 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
281 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
282 * Check if there is a winner. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
283 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
284 * @return true if there is a winner |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
285 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
286 Game.prototype.hasWinner = function () |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
287 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
288 var lines = [ |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
289 [ [ 0, 0 ], [ 0, 1 ], [ 0, 2 ] ], |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
290 [ [ 1, 0 ], [ 1, 1 ], [ 1, 2 ] ], |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
291 [ [ 2, 0 ], [ 2, 1 ], [ 2, 2 ] ], |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
292 [ [ 0, 0 ], [ 1, 0 ], [ 2, 0 ] ], |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
293 [ [ 0, 1 ], [ 1, 1 ], [ 2, 1 ] ], |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
294 [ [ 0, 2 ], [ 1, 2 ], [ 2, 2 ] ], |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
295 [ [ 0, 0 ], [ 1, 1 ], [ 2, 2 ] ], |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
296 [ [ 0, 2 ], [ 1, 1 ], [ 2, 0 ] ] |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
297 ]; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
298 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
299 for (var i = 0; i < lines.length; ++i) { |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
300 var p1 = lines[i][0]; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
301 var p2 = lines[i][1]; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
302 var p3 = lines[i][2]; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
303 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
304 var result = this.grid[p1[0]][p1[1]] === this.grid[p2[0]][p2[1]] && |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
305 this.grid[p2[0]][p2[1]] === this.grid[p3[0]][p3[1]] && |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
306 this.grid[p3[0]][p3[1]] !== '.'; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
307 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
308 if (result) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
309 return true; |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
310 } |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
311 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
312 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
313 /** |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
314 * Check if there is draw game. |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
315 * |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
316 * @return true if game is draw |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
317 */ |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
318 Game.prototype.hasDraw = function () |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
319 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
320 for (var r = 0; r < 3; ++r) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
321 for (var c = 0; c < 3; ++c) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
322 if (this.grid[r][c] === '.') |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
323 return false; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
324 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
325 return true; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
326 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
327 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
328 function onCommand(server, origin, channel, message) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
329 { |
995
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
330 channel = channel.toLowerCase(); |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
331 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
332 var target = message.trim(); |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
333 var nickname = Util.splituser(origin); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
334 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
335 if (Game.exists(server, channel)) |
847
a23b7b574ed2
irccd: rename [format] section to [templates], closes #1671
David Demelier <markand@malikania.fr>
parents:
824
diff
changeset
|
336 server.message(channel, Util.format(Plugin.templates.running, Game.keywords(server, channel, origin))); |
995
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
337 else if (!Game.isValid(server, channel, nickname, target)) |
847
a23b7b574ed2
irccd: rename [format] section to [templates], closes #1671
David Demelier <markand@malikania.fr>
parents:
824
diff
changeset
|
338 server.message(channel, Util.format(Plugin.templates.invalid, Game.keywords(server, channel, origin))); |
995
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
339 else { |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
340 var game = new Game(server, channel, origin, target); |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
341 |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
342 Game.map[Game.id(server, channel)] = game; |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
343 game.show(); |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
344 } |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
345 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
346 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
347 function onMessage(server, origin, channel, message) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
348 { |
995
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
349 channel = channel.toLowerCase(); |
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
350 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
351 var nickname = Util.splituser(origin); |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
352 var game = Game.find(server, channel); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
353 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
354 if (!game || !game.isTurn(nickname)) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
355 return; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
356 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
357 var match = /^([abc]) ?([123])$/.exec(message.trim()); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
358 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
359 if (!match) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
360 return; |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
361 |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
362 if (game.place(match[1], match[2], origin)) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
363 game.show(); |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
364 if (game.hasWinner() || game.hasDraw()) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
365 Game.remove(server, channel); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
366 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
367 |
633
c07819d1d306
Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents:
632
diff
changeset
|
368 function onDisconnect(server) |
c07819d1d306
Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents:
632
diff
changeset
|
369 { |
773
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
370 for (var key in Game.map) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
371 if (key.endsWith(server.toString())) |
8c44bbcbbab9
Misc: style, cleanup and update
David Demelier <markand@malikania.fr>
parents:
722
diff
changeset
|
372 delete Game.map[key]; |
633
c07819d1d306
Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents:
632
diff
changeset
|
373 } |
c07819d1d306
Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents:
632
diff
changeset
|
374 |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
375 function onKick(server, origin, channel, target) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
376 { |
995
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
377 Game.clear(server, target, channel.toLowerCase()); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
378 } |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
379 |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
380 function onPart(server, origin, channel) |
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
381 { |
995
0d71bfa6c97a
tests: add plugin tests
David Demelier <markand@malikania.fr>
parents:
913
diff
changeset
|
382 Game.clear(server, origin, channel.toLowerCase()); |
632
e5d0f4289e04
Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
383 } |