comparison plugins/roulette/roulette.js @ 207:6635b9187d71

Irccd: switch to 4 spaces indent, #518
author David Demelier <markand@malikania.fr>
date Tue, 21 Jun 2016 20:52:17 +0200
parents 52affa4ade71
children c6fbb6e0e06d
comparison
equal deleted inserted replaced
206:11808e98218f 207:6635b9187d71
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 // Plugin information. 19 // Plugin information.
20 info = { 20 info = {
21 author: "David Demelier <markand@malikania.fr>", 21 author: "David Demelier <markand@malikania.fr>",
22 license: "ISC", 22 license: "ISC",
23 summary: "A russian roulette for IRC", 23 summary: "A russian roulette for IRC",
24 version: "@IRCCD_VERSION@" 24 version: "@IRCCD_VERSION@"
25 }; 25 };
26 26
27 // Modules. 27 // Modules.
28 var Logger = Irccd.Logger; 28 var Logger = Irccd.Logger;
29 var Plugin = Irccd.Plugin; 29 var Plugin = Irccd.Plugin;
32 32
33 /** 33 /**
34 * Formats for writing. 34 * Formats for writing.
35 */ 35 */
36 Plugin.format = { 36 Plugin.format = {
37 "lucky": "#{nickname}, you're lucky this time", 37 "lucky": "#{nickname}, you're lucky this time",
38 "shot": "HEADSHOT" 38 "shot": "HEADSHOT"
39 }; 39 };
40 40
41 function Gun(server, channel) 41 function Gun(server, channel)
42 { 42 {
43 this.server = server; 43 this.server = server;
44 this.channel = channel; 44 this.channel = channel;
45 this.index = 0; 45 this.index = 0;
46 this.bullet = Math.floor(Math.random() * 6); 46 this.bullet = Math.floor(Math.random() * 6);
47 } 47 }
48 48
49 /** 49 /**
50 * Map of games. 50 * Map of games.
51 */ 51 */
58 * @param channel the channel name 58 * @param channel the channel name
59 * @return the hangman instance or undefined if no one exists 59 * @return the hangman instance or undefined if no one exists
60 */ 60 */
61 Gun.find = function (server, channel) 61 Gun.find = function (server, channel)
62 { 62 {
63 return Gun.map[server.toString() + '@' + channel]; 63 return Gun.map[server.toString() + '@' + channel];
64 } 64 }
65 65
66 /** 66 /**
67 * Create a new game, store it in the map and return it. 67 * Create a new game, store it in the map and return it.
68 * 68 *
70 * @param channel the channel name 70 * @param channel the channel name
71 * @return the hangman object 71 * @return the hangman object
72 */ 72 */
73 Gun.create = function (server, channel) 73 Gun.create = function (server, channel)
74 { 74 {
75 return Gun.map[server.toString() + "@" + channel] = new Gun(server, channel); 75 return Gun.map[server.toString() + "@" + channel] = new Gun(server, channel);
76 } 76 }
77 77
78 /** 78 /**
79 * Remove the specified game from the map. 79 * Remove the specified game from the map.
80 * 80 *
81 * @param game the game to remove 81 * @param game the game to remove
82 */ 82 */
83 Gun.remove = function (game) 83 Gun.remove = function (game)
84 { 84 {
85 delete Gun.map[game.server + "@" + game.channel]; 85 delete Gun.map[game.server + "@" + game.channel];
86 } 86 }
87 87
88 /** 88 /**
89 * Load all formats. 89 * Load all formats.
90 */ 90 */
91 Gun.loadFormats = function () 91 Gun.loadFormats = function ()
92 { 92 {
93 // --- DEPRECATED ------------------------------------------ 93 // --- DEPRECATED ------------------------------------------
94 // 94 //
95 // This code will be removed. 95 // This code will be removed.
96 // 96 //
97 // Since: 2.1.0 97 // Since: 2.1.0
98 // Until: 3.0.0 98 // Until: 3.0.0
99 // Reason: new [format] section replaces it. 99 // Reason: new [format] section replaces it.
100 // 100 //
101 // ---------------------------------------------------------- 101 // ----------------------------------------------------------
102 for (var key in Plugin.format) { 102 for (var key in Plugin.format) {
103 var optname = "format-" + key; 103 var optname = "format-" + key;
104 104
105 if (typeof (Plugin.config[optname]) !== "string") 105 if (typeof (Plugin.config[optname]) !== "string")
106 continue; 106 continue;
107 107
108 if (Plugin.config[optname].length === 0) 108 if (Plugin.config[optname].length === 0)
109 Logger.warning("skipping empty '" + optname + "' format"); 109 Logger.warning("skipping empty '" + optname + "' format");
110 else 110 else
111 Plugin.format[key] = Plugin.config[optname]; 111 Plugin.format[key] = Plugin.config[optname];
112 } 112 }
113 } 113 }
114 114
115 Gun.prototype.shot = function () 115 Gun.prototype.shot = function ()
116 { 116 {
117 return this.index++ === this.bullet; 117 return this.index++ === this.bullet;
118 } 118 }
119 119
120 function onLoad() 120 function onLoad()
121 { 121 {
122 Gun.loadFormats(); 122 Gun.loadFormats();
123 } 123 }
124 124
125 onReload = onLoad; 125 onReload = onLoad;
126 126
127 function onCommand(server, origin, channel) 127 function onCommand(server, origin, channel)
128 { 128 {
129 var kw = { 129 var kw = {
130 channel: channel, 130 channel: channel,
131 command: server.info().commandChar + Plugin.info().name, 131 command: server.info().commandChar + Plugin.info().name,
132 nickname: Util.splituser(origin), 132 nickname: Util.splituser(origin),
133 origin: origin, 133 origin: origin,
134 server: server.toString(), 134 server: server.toString(),
135 plugin: Plugin.info().name 135 plugin: Plugin.info().name
136 }; 136 };
137 137
138 var game = Gun.find(server, channel); 138 var game = Gun.find(server, channel);
139 139
140 if (!game) 140 if (!game)
141 game = Gun.create(server, channel); 141 game = Gun.create(server, channel);
142 142
143 if (game.shot()) { 143 if (game.shot()) {
144 server.kick(Util.splituser(origin), channel, Util.format(Plugin.format["shot"], kw)); 144 server.kick(Util.splituser(origin), channel, Util.format(Plugin.format["shot"], kw));
145 Gun.remove(game); 145 Gun.remove(game);
146 } else { 146 } else {
147 kw.count = (6 - game.index).toString(); 147 kw.count = (6 - game.index).toString();
148 server.message(channel, Util.format(Plugin.format["lucky"], kw)); 148 server.message(channel, Util.format(Plugin.format["lucky"], kw));
149 } 149 }
150 } 150 }