comparison plugins/roulette/roulette.js @ 0:1158cffe5a5e

Initial import
author David Demelier <markand@malikania.fr>
date Mon, 08 Feb 2016 16:43:14 +0100
parents
children ce3e96deb9e4
comparison
equal deleted inserted replaced
-1:000000000000 0:1158cffe5a5e
1 /*
2 * roulette.js -- russian roulette game
3 *
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 var Logger = Irccd.Logger;
20 var Plugin = Irccd.Plugin;
21 var Server = Irccd.Server;
22 var Util = Irccd.Util;
23
24 /* Plugin information */
25 info = {
26 author: "David Demelier <markand@malikania.fr>",
27 license: "ISC",
28 summary: "A russian roulette for IRC",
29 version: "@IRCCD_VERSION@"
30 };
31
32 function Gun(server, channel)
33 {
34 this.server = server;
35 this.channel = channel;
36 this.index = 0;
37 this.bullet = Math.floor(Math.random() * 6);
38 }
39
40 /**
41 * Map of games.
42 */
43 Gun.map = {};
44
45 /**
46 * Formats for writing.
47 */
48 Gun.formats = {
49 "lucky": "#{nickname}, you're lucky this time",
50 "shot": "HEADSHOT"
51 };
52
53 /**
54 * Search for an existing game.
55 *
56 * @param server the server object
57 * @param channel the channel name
58 * @return the hangman instance or undefined if no one exists
59 */
60 Gun.find = function (server, channel)
61 {
62 return Gun.map[server.toString() + '@' + channel];
63 }
64
65 /**
66 * Create a new game, store it in the map and return it.
67 *
68 * @param server the server object
69 * @param channel the channel name
70 * @return the hangman object
71 */
72 Gun.create = function (server, channel)
73 {
74 return Gun.map[server.toString() + "@" + channel] = new Gun(server, channel);
75 }
76
77 /**
78 * Remove the specified game from the map.
79 *
80 * @param game the game to remove
81 */
82 Gun.remove = function (game)
83 {
84 delete Gun.map[game.server + "@" + game.channel];
85 }
86
87 /**
88 * Load all formats.
89 */
90 Gun.loadFormats = function ()
91 {
92 for (var key in Gun.formats) {
93 var optname = "format-" + key;
94
95 if (typeof (Plugin.config[optname]) !== "string")
96 continue;
97
98 if (Plugin.config[optname].length === 0)
99 Logger.warning("skipping empty '" + optname + "' format");
100 else
101 Gun.formats[key] = Plugin.config[optname];
102 }
103 }
104
105 Gun.prototype.shot = function ()
106 {
107 return this.index++ === this.bullet;
108 }
109
110 function onLoad()
111 {
112 Gun.loadFormats();
113 }
114
115 onReload = onLoad;
116
117 function onCommand(server, origin, channel)
118 {
119 var kw = {
120 channel: channel,
121 command: server.info().commandChar + Plugin.info().name,
122 nickname: Util.splituser(origin),
123 origin: origin,
124 server: server,
125 plugin: Plugin.info().name
126 };
127
128 var game = Gun.find(server, channel);
129
130 if (!game)
131 game = Gun.create(server, channel);
132
133 if (game.shot()) {
134 server.kick(Util.splituser(origin), channel, Util.format(Gun.formats["shot"], kw));
135 Gun.remove(game);
136 } else {
137 kw.count = game.index;
138 server.message(channel, Util.format(Gun.formats["lucky"], kw));
139 }
140 }