annotate plugins/tictactoe/tictactoe.js @ 634:06f5486bfdcb

Plugin tictactoe: fix and simplify cell syntax, closes #768 @15m
author David Demelier <markand@malikania.fr>
date Wed, 14 Mar 2018 13:44:22 +0100
parents c07819d1d306
children 3e816cebed2c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 *
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
4 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
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 = {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 author: "David Demelier <markand@malikania.fr>",
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
22 license: "ISC",
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
23 summary: "A tictactoe game for IRC",
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
24 version: "@IRCCD_VERSION@"
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;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
30
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
31 // Formats.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
32 Plugin.format = {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
33 "draw": "nobody won",
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
34 "invalid": "#{nickname}, please select a valid opponent",
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 "running": "#{nickname}, the game is already running",
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 "turn": "#{nickname}, it's your turn",
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
37 "used": "#{nickname}, this square is already used",
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
38 "win": "#{nickname}, congratulations, you won!"
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 };
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
40
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 * Create a game.
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 * 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
45 *
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 * @param server the server object
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 * @param channel the channel
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 * @param origin the source origin
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
49 * @param target the target nickname
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
51 function Game(server, channel, origin, target)
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 this.server = server;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 this.origin = origin;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
55 this.target = target;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
56 this.channel = channel;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
57 this.players = [ Util.splituser(origin), target ];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
58 this.player = Math.floor(Math.random() * 2);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
59 this.grid = [
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
60 [ '.', '.', '.' ],
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
61 [ '.', '.', '.' ],
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 [ '.', '.', '.' ]
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
63 ];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
64 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
65
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
66 // Pending games requests checking for names listing.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 Game.requests = {};
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
68
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
69 // List of games running.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
70 Game.map = {};
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
71
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
72 /**
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
73 * Create a unique id.
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 * @param server the server object
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
76 * @param channel the channel
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
77 * @return the id
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
78 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 Game.id = function (server, channel)
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 return channel + "@" + server.toString();
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
83
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 * Get a running game or undefined.
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 * @param server the server object
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
88 * @param channel the channel
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 * @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
90 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
91 Game.find = function (server, channel)
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 return Game.map[Game.id(server, channel)];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
94 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
95
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 * Request a game after the name list gets received.
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 * @param server the server object
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 * @param channel the channel
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
101 * @param origin the originator
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
102 * @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
103 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
104 Game.postpone = function (server, channel, origin, target)
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 /*
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
107 * Get list of users on the channel to avoid playing against a non existing
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
108 * target.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
109 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
110 Game.requests[Game.id(server, channel)] = new Game(server, channel, origin, target);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
111 server.names(channel);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
112 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
113
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
114 /**
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
115 * Populate a set of keywords.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
116 *
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
117 * @param server the server object
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
118 * @param origin the originator
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
119 * @param channel the channel
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
120 * @return an object of predefined keywords
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 Game.keywords = function (server, channel, origin)
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 var kw = {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
125 channel: channel,
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
126 command: server.info().commandChar + Plugin.info().name,
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
127 plugin: Plugin.info().name,
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
128 server: server.info().name
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
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
131 if (origin) {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
132 kw.origin = origin;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
133 kw.nickname = Util.splituser(origin);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
134 }
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 return kw;
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
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 * 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
141 *
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
142 * @param server the server object
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
143 * @param channel the channel
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
144 * @return true if any
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
145 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
146 Game.exists = function (server, channel)
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 var id = Game.id(server, channel);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
149
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
150 return Game.requests[id] || Game.map[id];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
151 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
152
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
153 /**
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
154 * Delete a game from the registry.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
155 *
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
156 * @param server the server object
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
157 * @param channel the channel
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
158 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
159 Game.remove = function (server, channel)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
160 {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
161 delete Game.map[Game.id(server, channel)];
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 /**
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
165 * Erase games when some players leave channels.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
166 *
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
167 * @param server the server object
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
168 * @param origin the originator
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
169 * @param channel the channel
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
170 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
171 Game.clear = function (server, user, channel)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
172 {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
173 var nickname = Util.splituser(user);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
174 var game = Game.find(server, channel);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
175
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
176 if (game && (game.players[0] === nickname || game.players[1] === nickname))
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
177 Game.remove(server, channel);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
178 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
179
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
180 /**
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
181 * 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
182 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
183 Game.prototype.show = function ()
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
184 {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
185 var kw = Game.keywords(this.server, this.channel);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
186
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
187 // nickname is the current player.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
188 kw.nickname = this.players[this.player];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
189
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
190 this.server.message(this.channel, " a b c");
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
191 this.server.message(this.channel, "1 " + this.grid[0].join(" "));
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
192 this.server.message(this.channel, "2 " + this.grid[1].join(" "));
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
193 this.server.message(this.channel, "3 " + this.grid[2].join(" "));
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
194
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
195 if (this.hasWinner())
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
196 this.server.message(this.channel, Util.format(Plugin.format.win, kw));
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
197 else if (this.hasDraw())
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
198 this.server.message(this.channel, Util.format(Plugin.format.draw, kw));
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
199 else
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
200 this.server.message(this.channel, Util.format(Plugin.format.turn, kw));
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
201 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
202
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
203 /**
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
204 * 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
205 *
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
206 * @param nickname the nickname to check
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
207 * @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
208 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
209 Game.prototype.isTurn = function (nickname)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
210 {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
211 return this.players[this.player] == nickname;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
212 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
213
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
214 /**
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
215 * 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
216 *
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
217 * @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
218 * @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
219 */
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
220 Game.prototype.place = function (column, row, origin)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
221 {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
222 var columns = { a: 0, b: 1, c: 2 };
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
223 var rows = { 1: 0, 2: 1, 3: 2 };
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
224
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
225 column = columns[column];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
226 row = rows[row];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
227
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
228 var kw = Game.keywords(this.server, this.channel, origin);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
229
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
230 if (this.grid[row][column] !== '.') {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
231 this.server.message(this.channel, Util.format(Plugin.format.used, kw));
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
232 return false;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
233 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
234
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
235 this.grid[row][column] = this.player === 0 ? 'x' : 'o';
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
236
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
237 // Do not change if game is finished.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
238 if (!this.hasWinner() && !this.hasDraw())
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
239 this.player = this.player === 0 ? 1 : 0;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
240
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
241 return true;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
242 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
243
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 * Check if there is a winner.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
246 *
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
247 * @return true if there is a winner
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 Game.prototype.hasWinner = function ()
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 var lines = [
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
252 [ [ 0, 0 ], [ 0, 1 ], [ 0, 2 ] ],
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
253 [ [ 1, 0 ], [ 1, 1 ], [ 1, 2 ] ],
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
254 [ [ 2, 0 ], [ 2, 1 ], [ 2, 2 ] ],
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
255 [ [ 0, 0 ], [ 1, 0 ], [ 2, 0 ] ],
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
256 [ [ 0, 1 ], [ 1, 1 ], [ 2, 1 ] ],
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
257 [ [ 0, 2 ], [ 1, 2 ], [ 2, 2 ] ],
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
258 [ [ 0, 0 ], [ 1, 1 ], [ 2, 2 ] ],
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
259 [ [ 0, 2 ], [ 1, 1 ], [ 2, 0 ] ]
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
260 ];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
261
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
262 for (var i = 0; i < lines.length; ++i) {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
263 var p1 = lines[i][0];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
264 var p2 = lines[i][1];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
265 var p3 = lines[i][2];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
266
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
267 var result = this.grid[p1[0]][p1[1]] === this.grid[p2[0]][p2[1]] &&
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
268 this.grid[p2[0]][p2[1]] === this.grid[p3[0]][p3[1]] &&
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
269 this.grid[p3[0]][p3[1]] !== '.';
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
270
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
271 if (result)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
272 return true;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
273 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
274 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
275
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
276 /**
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
277 * Check if there is draw game.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
278 *
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
279 * @return true if game is draw
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 Game.prototype.hasDraw = function ()
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
282 {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
283 for (var r = 0; r < 3; ++r)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
284 for (var c = 0; c < 3; ++c)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
285 if (this.grid[r][c] === '.')
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
286 return false;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
287
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
288 return true;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
289 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
290
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
291 function onNames(server, channel, list)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
292 {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
293 var id = Game.id(server, channel);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
294 var game = Game.requests[id];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
295
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
296 // Names can come from any other plugin/event.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
297 if (!game)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
298 return;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
299
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
300 // Not a valid target? destroy the game.
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
301 if (list.indexOf(game.target) < 0)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
302 server.message(channel, Util.format(Plugin.format.invalid,
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
303 Game.keywords(server, channel, game.origin)));
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
304 else {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
305 Game.map[id] = game;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
306 game.show();
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
307 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
308
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
309 delete Game.requests[id];
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
310 }
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 function onCommand(server, origin, channel, message)
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 var target = message.trim();
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
315 var nickname = Util.splituser(origin);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
316
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
317 if (Game.exists(server, channel))
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
318 server.message(channel, Util.format(Plugin.format.running, Game.keywords(server, channel, origin)));
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
319 else if (target === "" || target === nickname || target === server.info().nickname)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
320 server.message(channel, Util.format(Plugin.format.invalid, Game.keywords(server, channel, origin)));
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
321 else
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
322 Game.postpone(server, channel, origin, message);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
323 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
324
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
325 function onMessage(server, origin, channel, message)
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 var nickname = Util.splituser(origin);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
328 var game = Game.find(server, channel);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
329
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
330 if (!game || !game.isTurn(nickname))
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
331 return;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
332
634
06f5486bfdcb Plugin tictactoe: fix and simplify cell syntax, closes #768 @15m
David Demelier <markand@malikania.fr>
parents: 633
diff changeset
333 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
334
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
335 if (!match)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
336 return;
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
337
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
338 if (game.place(match[1], match[2], origin))
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
339 game.show();
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
340 if (game.hasWinner() || game.hasDraw())
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
341 Game.remove(server, channel);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
342 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
343
633
c07819d1d306 Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents: 632
diff changeset
344 function onDisconnect(server)
c07819d1d306 Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents: 632
diff changeset
345 {
c07819d1d306 Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents: 632
diff changeset
346 for (var key in Game.map)
c07819d1d306 Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents: 632
diff changeset
347 if (key.endsWith(server.toString()))
c07819d1d306 Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents: 632
diff changeset
348 delete Game.map[key];
c07819d1d306 Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents: 632
diff changeset
349 }
c07819d1d306 Irccd: add onDisconnect event, closes #767 @1h
David Demelier <markand@malikania.fr>
parents: 632
diff changeset
350
632
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
351 function onKick(server, origin, channel, target)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
352 {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
353 Game.clear(server, target, channel);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
354 }
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
355
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
356 function onPart(server, origin, channel)
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
357 {
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
358 Game.clear(server, origin, channel);
e5d0f4289e04 Plugin tictactoe: brand new plugin, closes #393 @6h
David Demelier <markand@malikania.fr>
parents:
diff changeset
359 }