2 -- Copyright (C) 2011 Gunnar Beutner
4 -- This program is free software; you can redistribute it and/or
5 -- modify it under the terms of the GNU General Public License
6 -- as published by the Free Software Foundation; either version 2
7 -- of the License, or (at your option) any later version.
9 -- This program is distributed in the hope that it will be useful,
10 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
11 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 -- GNU General Public License for more details.
14 -- You should have received a copy of the GNU General Public License
15 -- along with this program; if not, write to the Free Software
16 -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 -- make idle notifications independent from game delay
23 -- scientists vote on kills
25 local BOTNICK = "labspace"
26 local BOTACCOUNT = "labspace"
27 local BOTACCOUNTID = 5022574
28 local BOTCHANNELS = { "#labspace" }
32 local DB = "labspace.db"
34 local KILLMESSAGES = {
35 "was brutally murdered.",
36 "was vaporized by the scientist's death ray.",
37 "slipped into a coma after drinking their poison-laced morning coffee.",
38 "was crushed to death by a 5-ton boulder.",
39 "couldn't escape from the scientist's killbot army."
43 local ls_gamestate = {}
46 local ls_lastalivecheck = 0
47 local ls_sched = Scheduler()
59 ls_bot = irc_localregisteruserid(BOTNICK, "lab", "space", "Labspace", BOTACCOUNT, BOTACCOUNTID, "+iXr", handler)
63 function ls_join_channels()
64 for _, channel in pairs(BOTCHANNELS) do
65 ls_add_channel(channel)
68 for _, channel in pairs(ls_db.channels) do
69 if not ls_is_game_channel(channel) then
70 ls_add_channel(channel)
75 function ls_split_message(message)
76 message, _ = message:gsub("^ +", "")
77 message, _ = message:gsub(" +", " ")
78 message, _ = message:gsub(" +$", "")
82 for token in string.gmatch(message, "%S+") do
83 table.insert(tokens, token)
89 function handler(target, revent, ...)
90 if revent == "irc_onchanmsg" then
91 local numeric, channel, message = ...
93 if not ls_is_game_channel(channel) then
97 ls_keepalive(channel, numeric)
99 local tokens = ls_split_message(message)
100 local command = tokens[1]
103 if command == "!add" then
104 ls_cmd_add(channel, numeric)
105 elseif command == "!remove" then
106 ls_cmd_remove(channel, numeric)
107 elseif command == "!wait" then
108 ls_cmd_wait(channel, numeric)
109 elseif command == "!start" then
110 ls_cmd_start(channel, numeric)
111 elseif command == "!status" then
112 ls_cmd_status(channel, numeric)
113 elseif command == "!hl" then
114 ls_cmd_hl(channel, numeric)
115 elseif command == "!enable" then
116 ls_cmd_enable(channel, numeric)
117 elseif command == "!disable" then
118 ls_cmd_disable(channel, numeric)
121 ls_flush_modes(channel)
123 elseif revent == "irc_onmsg" or revent == "irc_onnotice" then
124 local numeric, message = ...
126 local tokens = ls_split_message(message)
128 local command = tokens[1]
129 local argument = tokens[2]
132 if command == "kill" then
133 ls_cmd_kill(numeric, argument)
134 elseif command == "investigate" then
135 ls_cmd_investigate(numeric, argument)
136 elseif command == "vote" then
137 ls_cmd_vote(numeric, argument)
138 elseif command == "guard" then
139 ls_cmd_guard(numeric, argument)
140 elseif command == "smite" and onstaff(numeric) then
141 ls_cmd_smite(numeric, argument)
142 elseif command == "addchan" and ontlz(numeric) then
143 ls_cmd_addchan(numeric, argument)
144 elseif command == "delchan" and ontlz(numeric) then
145 ls_cmd_delchan(numeric, argument)
148 elseif revent == "irc_onkilled" then
151 elseif revent == "irc_onkillreconnect" then
161 function irc_onpart(channel, numeric, message)
162 if not ls_is_game_channel(channel) then
166 if ls_get_role(channel, numeric) then
167 ls_remove_player(channel, numeric)
168 ls_advance_state(channel)
172 function irc_onkick(channel, kicked_numeric, kicker_numeric, message)
173 if not ls_is_game_channel(channel) then
177 if ls_bot == kicked_numeric then
178 ls_remove_channel(channel)
182 if ls_get_role(channel, kicked_numeric) then
183 ls_remove_player(channel, kicked_numeric)
184 ls_advance_state(channel)
187 irc_onkickall = irc_onkick
189 function irc_onquit(numeric)
190 for channel, _ in pairs(ls_gamestate) do
191 if ls_get_role(channel, numeric) then
192 ls_remove_player(channel, numeric)
193 ls_advance_state(channel)
199 for channel, _ in pairs(ls_gamestate) do
200 ls_advance_state(channel, true)
201 ls_flush_modes(channel)
204 if ls_lastalivecheck < os.time() - 30 then
205 ls_lastalivecheck = os.time()
207 for channel, _ in pairs(ls_gamestate) do
208 ls_check_alive(channel)
212 if ls_lastsave < os.time() - 60 then
213 ls_lastsave = os.time()
218 -- sends a debug message
219 function ls_debug(channel, message)
221 irc_localchanmsg(ls_bot, channel, "DEBUG: " .. message)
225 -- sends a notice to the specified target
226 function ls_notice(numeric, text)
227 irc_localnotice(ls_bot, numeric, text)
230 -- sends a message to the specified target
231 function ls_chanmsg(channel, text)
232 irc_localchanmsg(ls_bot, channel, text)
235 -- formats the specified role identifier for output in a message
236 function ls_format_role(role)
237 if role == "scientist" then
238 return "Mad Scientist"
239 elseif role == "investigator" then
240 return "Investigator"
241 elseif role == "citizen" then
243 elseif role == "lobby" then
246 return "Unknown Role"
250 -- formats the specified player name for output in a message (optionally
251 -- revealing that player's role in the game)
252 function ls_format_player(channel, numeric, reveal)
253 local nick = irc_getnickbynumeric(numeric)
254 local result = "\002" .. nick.nick .. "\002"
257 result = result .. " (" .. ls_format_role(ls_get_role(channel, numeric)) .. ")"
263 -- formats a list of player names for output in a message (optionally
264 -- revealing their roles in the game)
265 function ls_format_players(channel, numerics, reveal, no_and)
269 for _, numeric in pairs(numerics) do
271 if not no_and and i == table.getn(numerics) - 1 then
272 result = result .. " and "
274 result = result .. ", "
278 result = result .. ls_format_player(channel, numeric, reveal)
285 -- returns the current state of the game
286 function ls_get_state(channel)
287 return ls_gamestate[channel]["state"]
290 -- gets the timeout for the current state
291 function ls_get_timeout(channel)
292 return ls_gamestate[channel]["timeout"]
295 -- gets the delay for the current state
296 function ls_get_delay(channel)
297 return ls_gamestate[channel]["delay"]
300 -- gets the ts when !hl was last used
301 function ls_get_lasthl(channel)
302 return ls_gamestate[channel]["lasthl"]
305 -- gets whether the bot is enabled
306 function ls_get_enabled(channel)
307 return ls_gamestate[channel]["enabled"]
310 -- returns true if the game state delay was exceeded, false otherwise
311 function ls_delay_exceeded(channel)
312 return ls_get_delay(channel) < os.time()
315 function ls_get_waitcount(channel)
316 return ls_gamestate[channel]["waitcount"]
319 -- sets the game state
320 function ls_set_state(channel, state)
321 ls_gamestate[channel]["state"] = state
323 ls_set_timeout(channel, -1)
324 ls_set_delay(channel, 30)
327 -- sets the game state timeout (in seconds)
328 function ls_set_timeout(channel, timeout)
329 if timeout == -1 then
330 ls_gamestate[channel]["timeout"] = -1
332 ls_gamestate[channel]["timeout"] = os.time() + timeout
336 -- sets the game state delay (in seconds)
337 function ls_set_delay(channel, delay)
338 ls_gamestate[channel]["delay"] = os.time() + delay
339 ls_debug(channel, "changed gamestate delay to " .. delay)
342 -- sets the !hl timestamp
343 function ls_set_lasthl(channel, ts)
344 ls_gamestate[channel]["lasthl"] = ts
347 -- sets whether the bot is enabled
348 function ls_set_enabled(channel, enabled)
349 ls_gamestate[channel]["enabled"] = enabled
352 function ls_set_waitcount(channel, count)
353 ls_gamestate[channel]["waitcount"] = count
356 -- returns true if the game state timeout was exceeded, false otherwise
357 function ls_timeout_exceeded(channel)
358 local timeout = ls_get_timeout(channel)
360 return timeout ~= -1 and timeout < os.time()
363 -- returns true if there's a game in progress, false otherwise
364 function ls_game_in_progress(channel)
365 return ls_get_state(channel) ~= "lobby"
368 -- returns the name of the channel the specified nick is playing on
369 -- if the nick isn't playing any games nil is returned instead
370 function ls_chan_for_numeric(numeric)
371 for channel, _ in pairs(ls_gamestate) do
372 if ls_get_role(channel, numeric) then
380 function ls_cmd_add(channel, numeric)
381 ls_add_player(channel, numeric)
384 function ls_cmd_remove(channel, numeric)
385 ls_remove_player(channel, numeric)
388 function ls_cmd_wait(channel, numeric)
389 if ls_game_in_progress(channel) then
390 ls_notice(numeric, "Sorry, there's no lobby at the moment.")
394 if table.getn(ls_get_players(channel)) >= MINPLAYERS then
395 local count = ls_get_waitcount(channel)
398 ls_notice(numeric, "Sorry, the timeout can only be extended twice per game.")
402 ls_set_waitcount(channel, count + 1)
405 if not ls_get_role(channel, numeric) then
406 ls_notice(numeric, "Sorry, you need to be in the lobby to use this command.")
410 ls_set_timeout(channel, 120)
411 ls_set_delay(channel, 45)
413 ls_chanmsg(channel, "Lobby timeout was reset.")
416 function ls_cmd_start(channel, numeric)
417 if ls_game_in_progress(channel) then
418 ls_notice(numeric, "Sorry, there's no lobby at the moment.")
422 if not ls_get_role(channel, numeric) then
423 ls_notice(numeric, "Sorry, you need to be in the lobby to use this command.")
427 ls_advance_state(channel)
429 ls_flush_modes(channel)
432 function ls_cmd_status(channel, numeric)
433 if not ls_get_role(channel, numeric) then
434 ls_notice(numeric, "Sorry, you need to be in the lobby to use this command.")
438 ls_chanmsg(channel, "Players: " .. ls_format_players(channel, ls_get_players(channel)))
440 if ls_game_in_progress(channel) then
441 ls_chanmsg(channel, "Roles: " ..
442 table.getn(ls_get_players(channel, "scientist")) .. "x " .. ls_format_role("scientist") .. ", " ..
443 table.getn(ls_get_players(channel, "investigator")) .. "x " .. ls_format_role("investigator") .. ", " ..
444 table.getn(ls_get_players(channel, "citizen")) .. "x " .. ls_format_role("citizen"))
448 function ls_cmd_hl(channel, numeric)
449 if ls_game_in_progress(channel) then
450 ls_notice(numeric, "Sorry, there's no lobby at the moment.")
454 if not ls_get_role(channel, numeric) then
455 ls_notice(numeric, "Sorry, you need to be in the lobby to use this command.")
459 if ls_get_lasthl(channel) > os.time() - 300 then
460 ls_notice(numeric, "Sorry, you can only use that command once every 5 minute.")
464 if string.lower(channel) == "#labspace" then
465 ls_notice(numeric, "Sorry, you can't use this command here.")
468 ls_set_lasthl(channel, os.time())
472 for nick in channelusers_iter(channel, { nickpusher.numeric }) do
473 local numeric = nick[1]
475 if not ls_get_role(channel, numeric) then
476 table.insert(numerics, numeric)
479 if table.getn(numerics) > 10 then
480 ls_chanmsg(channel, "HL: " .. ls_format_players(channel, numerics, false, true))
485 if table.getn(numerics) > 0 then
486 ls_chanmsg(channel, "HL: " .. ls_format_players(channel, numerics, false, true))
490 function ls_cmd_enable(channel, numeric)
491 local chanuser = irc_getuserchanmodes(numeric, channel)
493 if not chanuser or not chanuser.opped then
494 ls_notice(channel, "You need to be opped to use this command.")
498 ls_set_enabled(channel, true)
499 ls_notice(numeric, "Game has been enabled.")
502 function ls_cmd_disable(channel, numeric)
503 local chanuser = irc_getuserchanmodes(numeric, channel)
505 if not chanuser or not chanuser.opped then
506 ls_notice(channel, "You need to be opped to use this command.")
510 if ls_game_in_progress(channel) then
511 ls_chanmsg(channel, ls_format_player(channel, numeric) .. " disabled the game.")
514 ls_stop_game(channel)
515 ls_flush_modes(channel)
517 ls_set_enabled(channel, false)
518 ls_notice(numeric, "Game has been disabled.")
521 function ls_cmd_kill(numeric, victim)
523 ls_notice(numeric, "Syntax: kill <nick>")
527 local channel = ls_chan_for_numeric(numeric)
530 ls_notice(numeric, "You haven't joined any game lobby.")
534 ls_keepalive(channel, numeric)
536 if ls_get_role(channel, numeric) ~= "scientist" then
537 ls_notice(numeric, "You need to be a scientist to use this command.")
541 if ls_get_state(channel) ~= "kill" then
542 ls_notice(numeric, "Sorry, you can't use this command right now.")
546 if not ls_get_active(channel, numeric) then
547 ls_notice(numeric, "Sorry, it's not your turn to choose a victim.")
551 local victimnick = irc_getnickbynick(victim)
553 if not victimnick then
554 ls_notice(numeric, "Sorry, I don't know who that is.")
558 local victimnumeric = victimnick.numeric
560 if not ls_get_role(channel, victimnumeric) then
561 ls_notice(numeric, "Sorry, " .. ls_format_player(channel, victimnumeric) .. " isn't playing the game.")
565 if math.random(100) > 85 then
566 ls_chanmsg(channel, "The scientists' attack was not successful tonight. Nobody died.")
567 elseif ls_get_guarded(channel, victimnumeric) then
568 for _, player in pairs(ls_get_players(channel)) do
569 ls_set_trait(channel, player, "force", false)
572 ls_set_guarded(channel, victimnumeric, false)
574 ls_chanmsg(channel, "The attack on " .. ls_format_player(channel, victimnumeric) .. " was deflected by a force field. The force field generator has now run out of power.")
575 elseif ls_get_trait(channel, victimnumeric, "infested") then
576 ls_devoice_player(channel, numeric)
577 ls_devoice_player(channel, victimnumeric)
579 ls_remove_player(channel, numeric, true)
580 ls_remove_player(channel, victimnumeric, true)
582 ls_chanmsg(channel, "An alien bursts out of " .. ls_format_player(channel, victimnumeric, true) .. "'s chest just as " .. ls_format_player(channel, numeric, true) .. " was about to murder them, killing them both.")
584 ls_devoice_player(channel, victimnumeric)
586 if numeric == victimnumeric then
587 ls_chanmsg(channel, ls_format_player(channel, victimnumeric, true) .. " committed suicide.")
589 if ls_get_role(channel, victimnumeric) == "scientist" then
590 ls_chanmsg(channel, ls_format_player(channel, victimnumeric, true) .. " was brutally murdered. Oops.")
592 local killmessage = KILLMESSAGES[math.random(table.getn(KILLMESSAGES))]
594 ls_chanmsg(channel, ls_format_player(channel, victimnumeric, true) .. " " .. killmessage)
598 ls_remove_player(channel, victimnumeric, true)
601 ls_set_state(channel, "investigate")
602 ls_advance_state(channel)
604 ls_flush_modes(channel)
607 function ls_cmd_investigate(numeric, victim)
609 ls_notice(numeric, "Syntax: investigate <nick>")
613 local channel = ls_chan_for_numeric(numeric)
616 ls_notice(numeric, "You haven't joined any game lobby.")
620 if ls_get_role(channel, numeric) ~= "investigator" then
621 ls_notice(numeric, "You need to be an investigator to use this command.")
625 ls_keepalive(channel, numeric)
627 if ls_get_state(channel) ~= "investigate" then
628 ls_notice(numeric, "Sorry, you can't use this command right now.")
632 local victimnick = irc_getnickbynick(victim)
634 if not victimnick then
635 ls_notice(numeric, "Sorry, I don't know who that is.")
639 local victimnumeric = victimnick.numeric
641 if not ls_get_role(channel, victimnumeric) then
642 ls_notice(numeric, "Sorry, " .. ls_format_player(channel, victimnumeric) .. " isn't playing the game.")
646 local investigators = ls_get_players(channel, "investigator")
648 for _, investigator in pairs(investigators) do
649 if investigator ~= numeric then
650 ls_notice(investigator, "Another investigator picked a target.")
654 if math.random(100) > 85 then
655 ls_chanmsg(channel, ls_format_player(channel, numeric) .. "'s fine detective work reveals " .. ls_format_player(channel, victimnumeric) .. "'s role: " .. ls_format_role(ls_get_role(channel, victimnumeric)))
658 if numeric == victimnumeric then
659 ls_notice(numeric, "You're the investigator. Excellent detective work!")
661 ls_notice(numeric, ls_format_player(channel, victimnumeric) .. "'s role is: " .. ls_format_role(ls_get_role(channel, victimnumeric)))
664 ls_set_state(channel, "vote")
665 ls_advance_state(channel)
667 ls_flush_modes(channel)
670 function ls_cmd_vote(numeric, victim)
672 ls_notice(numeric, "Syntax: vote <nick>")
676 local channel = ls_chan_for_numeric(numeric)
679 ls_notice(numeric, "You haven't joined any game lobby.")
683 if ls_get_state(channel) ~= "vote" then
684 ls_notice(numeric, "Sorry, you can't use this command right now.")
688 ls_keepalive(channel, numeric)
690 local victimnick = irc_getnickbynick(victim)
692 if not victimnick then
693 ls_notice(numeric, "Sorry, I don't know who that is.")
697 local victimnumeric = victimnick.numeric
699 if not ls_get_role(channel, victimnumeric) then
700 ls_notice(numeric, "Sorry, " .. ls_format_player(channel, victimnumeric) .. " isn't playing the game.")
704 if ls_get_vote(channel, numeric) == victimnumeric then
705 ls_notice(numeric, "You already voted for " .. ls_format_player(channel, victimnumeric) .. ".")
709 ls_set_vote(channel, numeric, victimnumeric)
710 ls_notice(numeric, "Done.")
712 ls_advance_state(channel)
714 ls_flush_modes(channel)
717 function ls_cmd_guard(numeric, victim)
719 ls_notice(numeric, "Syntax: vote <nick>")
723 local channel = ls_chan_for_numeric(numeric)
726 ls_notice(numeric, "You haven't joined any game lobby.")
730 if not ls_get_trait(channel, numeric, "force") then
731 ls_notice(numeric, "Sorry, you need the force field generator to use this command.")
735 ls_keepalive(channel, numeric)
737 local victimnick = irc_getnickbynick(victim)
739 if not victimnick then
740 ls_notice(numeric, "Sorry, I don't know who that is.")
744 local victimnumeric = victimnick.numeric
746 if not ls_get_role(channel, victimnumeric) then
747 ls_notice(numeric, "Sorry, " .. ls_format_player(channel, victimnumeric) .. " isn't playing the game.")
753 if victimnumeric == numeric then
756 target = ls_format_player(channel, victimnumeric)
759 for _, player in pairs(ls_get_players(channel)) do
760 ls_set_guarded(channel, player, (player == victimnumeric))
763 ls_notice(numeric, "You are now protecting " .. target .. ".")
766 function ls_cmd_smite(numeric, victim)
768 ls_notice(numeric, "Syntax: smite <nick>")
772 local victimnick = irc_getnickbynick(victim)
774 if not victimnick then
775 ls_notice(numeric, "Sorry, I don't know who that is.")
779 local victimnumeric = victimnick.numeric
780 local channel = ls_chan_for_numeric(victimnumeric)
783 ls_notice(numeric, "Sorry, " .. victimnick.nick .. " isn't playing the game.")
787 ls_chanmsg(channel, ls_format_player(channel, victimnumeric, true) .. " was struck by lightning.")
788 ls_remove_player(channel, victimnumeric, true)
790 ls_advance_state(channel)
792 ls_flush_modes(channel)
795 function ls_cmd_addchan(numeric, channel)
797 ls_notice(numeric, "Syntax: addchan <#channel>")
801 if not irc_getchaninfo(channel) then
802 ls_notice(numeric, "The specified channel does not exist.")
806 if ls_is_game_channel(channel) then
807 ls_notice(numeric, "The bot is already on that channel.")
811 ls_add_channel(channel)
813 ls_notice(numeric, "Done.")
816 function ls_cmd_delchan(numeric, channel)
818 ls_notice(numeric, "Syntax: delchan <#channel>")
822 if not ls_is_game_channel(channel) then
823 ls_notice(numeric, "The bot is not on that channel.")
827 ls_remove_channel(channel, true)
829 ls_notice(numeric, "Done.")
832 function ls_keepalive(channel, numeric)
833 if ls_get_role(channel, numeric) then
834 ls_set_seen(channel, numeric, os.time())
837 -- extend lobby timeout if we don't have enough players yet
838 if ls_get_state(channel) == "lobby" and table.getn(ls_get_players(channel)) < MINPLAYERS then
839 ls_set_delay(channel, 90)
840 ls_set_timeout(channel, 150)
844 function ls_timer_announce_players(channel)
845 ls_gamestate[channel]["announce_timer"] = nil
847 local new_players = {}
849 for _, numeric in pairs(ls_get_players(channel)) do
850 if not ls_get_announced(channel, numeric) then
851 table.insert(new_players, numeric)
852 ls_set_announced(channel, numeric, true)
853 ls_voice_player(channel, numeric)
857 ls_flush_modes(channel)
859 if table.getn(new_players) > 0 then
860 local count = table.getn(ls_get_players(channel))
869 ls_chanmsg(channel, ls_format_players(channel, new_players) .. " joined the game (" .. count .. " " .. subject .. " in the lobby).")
873 function ls_add_channel(channel)
874 ls_gamestate[channel] = { players = {}, state = "lobby", timeout = -1, delay = os.time() + 30, waitcount = 0, lasthl = 0, enabled = true }
875 irc_localjoin(ls_bot, channel)
876 irc_localsimplechanmode(ls_bot, channel, "-m")
879 function ls_remove_channel(channel, part)
880 if ls_gamestate[channel]["announce_timer"] then
881 ls_sched:remove(ls_gamestate[channel]["announce_timer"])
884 ls_gamestate[channel] = nil
887 irc_localpart(ls_bot, channel)
892 ls_db = loadtable(basepath() .. "db/" .. DB)
895 ls_db = ls_dbdefaults()
902 for channel, _ in pairs(ls_gamestate) do
903 table.insert(channels, channel)
906 ls_db.channels = channels
908 savetable(basepath() .. "db/" .. DB, ls_db)
911 function ls_dbdefaults()
913 db.channels = BOTCHANNELS
918 function ls_add_player(channel, numeric, forced)
919 local role = ls_get_role(channel, numeric)
922 ls_chanmsg(channel, "\001ACTION slaps " .. ls_format_player(channel, numeric) .. "\001")
927 if not ls_get_enabled(channel) then
928 ls_notice(numeric, "Sorry, the game is currently disabled.")
932 if ls_game_in_progress(channel) then
933 ls_notice(numeric, "Sorry, you can't join the game right now.")
937 local chanuser = irc_getuserchanmodes(numeric, channel)
940 ls_notice(numeric, "Sorry, you must be on the channel to use this command.")
944 if chanuser.opped then
945 ls_notice(numeric, "You must not be opped to use this command.")
949 if table.getn(ls_get_players(channel)) >= MAXPLAYERS then
950 ls_notice(numeric, "Sorry, the game's lobby is full.")
954 if ls_chan_for_numeric(numeric) then
955 ls_notice(numeric, "Sorry, you can't play on multiple channels at once.")
960 ls_set_role(channel, numeric, "lobby")
961 ls_set_seen(channel, numeric, os.time())
964 ls_set_announced(channel, numeric, false)
966 if ls_gamestate[channel]["announce_timer"] then
967 ls_sched:remove(ls_gamestate[channel]["announce_timer"])
969 ls_gamestate[channel]["announce_timer"] = ls_sched:add(5, ls_timer_announce_players, channel)
971 ls_notice(numeric, "You were added to the lobby.")
973 ls_set_announced(channel, numeric, true)
974 ls_voice_player(channel, numeric)
977 ls_set_delay(channel, 30)
978 ls_set_timeout(channel, 90)
981 function ls_voice_player(channel, numeric)
982 if not ls_gamestate[channel]["modes"] then
983 ls_gamestate[channel]["modes"] = {}
986 table.insert(ls_gamestate[channel]["modes"], true)
987 table.insert(ls_gamestate[channel]["modes"], "v")
988 table.insert(ls_gamestate[channel]["modes"], numeric)
991 function ls_devoice_player(channel, numeric)
992 if not ls_gamestate[channel]["modes"] then
993 ls_gamestate[channel]["modes"] = {}
996 table.insert(ls_gamestate[channel]["modes"], false)
997 table.insert(ls_gamestate[channel]["modes"], "v")
998 table.insert(ls_gamestate[channel]["modes"], numeric)
1001 function ls_flush_modes(channel)
1002 if ls_gamestate[channel]["modes"] then
1003 irc_localovmode(ls_bot, channel, ls_gamestate[channel]["modes"])
1004 ls_gamestate[channel]["modes"] = nil
1008 function ls_remove_player(channel, numeric, forced)
1009 local role = ls_get_role(channel, numeric)
1015 local announced = ls_get_announced(channel, numeric)
1017 local force_field = ls_get_trait(channel, numeric, "force")
1019 ls_set_role(channel, numeric, nil)
1021 ls_devoice_player(channel, numeric)
1023 for _, player in pairs(ls_get_players(channel)) do
1024 if ls_get_vote(channel, player) == numeric then
1025 ls_set_vote(channel, player, nil)
1029 ls_set_guarded(channel, player, false)
1035 if ls_game_in_progress(channel) then
1036 ls_chanmsg(channel, ls_format_player(channel, numeric) .. " committed suicide. Goodbye, cruel world.")
1038 ls_chanmsg(channel, ls_format_player(channel, numeric) .. " left the game (" .. table.getn(ls_get_players(channel)) .. " players in the lobby).")
1042 ls_notice(numeric, "You were removed from the lobby.")
1044 ls_set_delay(channel, 30)
1045 ls_set_timeout(channel, 90)
1049 function ls_get_players(channel, role)
1052 for player, _ in pairs(ls_gamestate[channel]["players"]) do
1053 if not role or ls_get_role(channel, player) == role then
1054 table.insert(players, player)
1061 function ls_is_game_channel(channel)
1062 return ls_gamestate[channel]
1065 function ls_get_role(channel, numeric)
1066 if not ls_gamestate[channel]["players"][numeric] then
1070 return ls_gamestate[channel]["players"][numeric]["role"]
1073 function ls_set_role(channel, numeric, role)
1074 if not ls_gamestate[channel]["players"][numeric] or role == "lobby" then
1075 ls_gamestate[channel]["players"][numeric] = {
1084 ls_gamestate[channel]["players"][numeric]["role"] = role
1086 ls_gamestate[channel]["players"][numeric] = nil
1089 if role and role ~= "lobby" then
1090 ls_notice(numeric, "Your role for this round is '" .. ls_format_role(role) .. "'.")
1094 function ls_get_trait(channel, numeric, trait)
1095 return ls_gamestate[channel]["players"][numeric]["traits"][trait]
1098 function ls_set_trait(channel, numeric, trait, enabled)
1099 ls_gamestate[channel]["players"][numeric]["traits"][trait] = enabled
1102 function ls_get_guarded(channel, numeric, guarded)
1103 return ls_gamestate[channel]["players"][numeric]["guarded"]
1106 function ls_set_guarded(channel, numeric, guarded)
1107 ls_gamestate[channel]["players"][numeric]["guarded"] = guarded
1110 function ls_get_seen(channel, numeric)
1111 return ls_gamestate[channel]["players"][numeric]["seen"]
1114 function ls_set_seen(channel, numeric, seen)
1115 ls_gamestate[channel]["players"][numeric]["seen"] = seen
1118 function ls_get_vote(channel, numeric)
1119 if not ls_gamestate[channel]["players"][numeric] then
1123 return ls_gamestate[channel]["players"][numeric]["vote"]
1126 function ls_set_vote(channel, numeric, votenumeric)
1127 if ls_get_vote(channel, numeric) == votenumeric then
1133 for _, player in pairs(ls_get_players(channel)) do
1134 if ls_get_vote(channel, player) == votenumeric then
1139 -- increase count for this new vote
1142 if numeric ~= votenumeric then
1143 if ls_get_vote(channel, numeric) then
1144 ls_chanmsg(channel, ls_format_player(channel, numeric) .. " changed their vote to " .. ls_format_player(channel, votenumeric) .. " (" .. count .. " votes).")
1146 ls_chanmsg(channel, ls_format_player(channel, numeric) .. " voted for " .. ls_format_player(channel, votenumeric) .. " (" .. count .. " votes).")
1149 ls_chanmsg(channel, ls_format_player(channel, numeric) .. " voted for himself. Oops! (" .. count .. " votes)")
1153 if ls_gamestate[channel]["players"][numeric] then
1154 ls_gamestate[channel]["players"][numeric]["vote"] = votenumeric
1158 function ls_get_active(channel, numeric)
1159 return ls_gamestate[channel]["players"][numeric]["active"]
1162 function ls_set_active(channel, numeric, active)
1163 ls_gamestate[channel]["players"][numeric]["active"] = active
1166 function ls_get_announced(channel, numeric)
1167 return ls_gamestate[channel]["players"][numeric]["announced"]
1170 function ls_set_announced(channel, numeric, announced)
1171 ls_gamestate[channel]["players"][numeric]["announced"] = announced
1174 function ls_pick_player(players)
1175 return players[math.random(table.getn(players))]
1178 function ls_number_scientists(numPlayers)
1179 return math.ceil((numPlayers - 2) / 5.0)
1182 function ls_number_investigators(numPlayers)
1183 return math.ceil((numPlayers - 5) / 6.0)
1186 function ls_start_game(channel)
1187 local players = ls_get_players(channel)
1189 irc_localsimplechanmode(ls_bot, channel, "+m")
1191 for nick in channelusers_iter(channel, { nickpusher.numeric }) do
1192 local numeric = nick[1]
1194 if ls_get_role(channel, numeric) then
1195 ls_voice_player(channel, numeric)
1196 ls_keepalive(channel, numeric)
1198 ls_devoice_player(channel, numeric)
1202 ls_chanmsg(channel, "Starting the game...")
1204 for _, player in pairs(players) do
1205 ls_set_role(channel, player, "lobby")
1208 local players_count = table.getn(players)
1209 local scientists_count = 0
1210 local scientists_needed = ls_number_scientists(players_count)
1213 while scientists_count < scientists_needed do
1214 local scientist_index = math.random(table.getn(players))
1215 ls_set_role(channel, table.remove(players, scientist_index), "scientist")
1216 scientists_count = scientists_count + 1
1219 -- notify scientists about each other
1220 for _, scientist in pairs(ls_get_players(channel, "scientist")) do
1221 for _, scientist_notify in pairs(ls_get_players(channel, "scientist")) do
1222 if scientist ~= scientist_notify then
1223 ls_notice(scientist_notify, ls_format_player(channel, scientist) .. " is also a scientist.")
1228 local investigators_count = 0
1229 local investigators_needed = ls_number_investigators(players_count)
1231 -- pick investigators
1232 while investigators_count < investigators_needed do
1233 local investigator_index = math.random(table.getn(players))
1234 ls_set_role(channel, table.remove(players, investigator_index), "investigator")
1235 investigators_count = investigators_count + 1
1238 -- rest of the players are citizens
1239 for _, player in pairs(players) do
1240 ls_set_role(channel, player, "citizen")
1243 -- give someone the force field generator
1244 local force_owner = players[math.random(table.getn(players))]
1245 ls_set_trait(channel, force_owner, "force", true)
1246 ls_set_guarded(channel, force_owner, true)
1247 ls_notice(force_owner, "You've found the \002force field generator\002. Use /notice " .. BOTNICK .. " guard <nick> to protect someone.")
1248 ls_notice(force_owner, "You are currently protecting yourself.")
1250 -- make someone infested if there are at least 6 citizens
1251 if table.getn(players) > 6 then
1252 local infested_player = players[math.random(table.getn(players))]
1253 ls_set_trait(channel, infested_player, "infested", true)
1254 ls_notice(infested_player, "You're infested with an \002alien parasite\002.")
1255 ls_chanmsg(channel, "It's " .. ls_format_player(channel, infested_player) .. ".")
1258 ls_chanmsg(channel, "Roles have been assigned: " ..
1259 table.getn(ls_get_players(channel, "scientist")) .. "x " .. ls_format_role("scientist") .. ", " ..
1260 table.getn(ls_get_players(channel, "investigator")) .. "x " .. ls_format_role("investigator") .. ", " ..
1261 table.getn(ls_get_players(channel, "citizen")) .. "x " .. ls_format_role("citizen") .. " - Good luck!")
1263 ls_set_state(channel, "kill")
1264 ls_advance_state(channel)
1267 function ls_stop_game(channel)
1268 ls_set_state(channel, "lobby")
1269 ls_set_waitcount(channel, 0)
1271 for _, player in pairs(ls_get_players(channel)) do
1272 ls_remove_player(channel, player, true)
1275 irc_localsimplechanmode(ls_bot, channel, "-m")
1278 -- makes sure people are not afk
1279 function ls_check_alive(channel)
1280 if not ls_game_in_progress(channel) then
1284 local dead_players = {}
1285 local idle_players = {}
1287 for _, player in pairs(ls_get_players(channel)) do
1288 local seen = ls_get_seen(channel, player)
1291 if seen < os.time() - 120 then
1292 table.insert(dead_players, player)
1293 elseif seen < os.time() - 60 then
1294 table.insert(idle_players, player)
1299 if table.getn(dead_players) > 0 then
1302 if table.getn(dead_players) ~= 1 then
1308 ls_chanmsg(channel, ls_format_players(channel, dead_players) .. " " .. verb .. " to be dead (AFK).")
1310 for _, player in pairs(dead_players) do
1311 ls_remove_player(channel, player, true)
1315 if table.getn(idle_players) > 0 then
1316 ls_chanmsg(channel, "Hi " .. ls_format_players(channel, idle_players) .. ", please say something if you're still alive.")
1320 function ls_advance_state(channel, delayed)
1321 if delayed and not ls_delay_exceeded(channel) then
1325 ls_debug(channel, "ls_advance_state")
1327 ls_set_delay(channel, 30)
1329 local players = ls_get_players(channel)
1330 local scientists = ls_get_players(channel, "scientist")
1331 local investigators = ls_get_players(channel, "investigator")
1333 -- game start condition
1334 if not ls_game_in_progress(channel) then
1335 if table.getn(players) < MINPLAYERS then
1336 if table.getn(players) > 0 then
1337 if ls_timeout_exceeded(channel) then
1338 ls_chanmsg(channel, "Lobby was closed because there aren't enough players.")
1339 ls_stop_game(channel)
1341 ls_chanmsg(channel, "Game will start when there are at least " .. MINPLAYERS .. " players.")
1345 ls_start_game(channel)
1351 -- winning condition when everyone is dead
1352 if table.getn(players) == 0 then
1353 ls_chanmsg(channel, "Everyone is dead.")
1354 ls_stop_game(channel)
1358 -- winning condition for scientists
1359 if table.getn(scientists) >= table.getn(players) - table.getn(scientists) then
1360 ls_chanmsg(channel, "There are equal to or more scientists than citizens. Science wins again: " .. ls_format_players(channel, scientists, true))
1361 ls_stop_game(channel)
1365 -- winning condition for citizen
1366 if table.getn(scientists) == 0 then
1367 ls_chanmsg(channel, "All scientists have been eliminated. The citizens win this round: " .. ls_format_players(channel, players, true))
1368 ls_stop_game(channel)
1372 -- make sure there's progress towards the game's end
1373 local state = ls_get_state(channel)
1374 local timeout = ls_get_timeout(channel)
1376 if state == "kill" then
1377 if timeout == -1 then
1378 local active_scientist = scientists[math.random(table.getn(scientists))]
1380 for _, scientist in pairs(scientists) do
1381 if scientist == active_scientist then
1382 ls_set_active(channel, scientist, true)
1383 ls_notice(scientist, "It's your turn to select a citizen to kill. Use /notice " .. BOTNICK .. " kill <nick> to kill someone.")
1385 ls_set_active(channel, scientist, false)
1386 ls_notice(scientist, ls_format_player(channel, active_scientist) .. " is choosing a victim.")
1390 if table.getn(scientists) > 1 then
1391 ls_chanmsg(channel, "The citizens are asleep while the mad scientists are choosing a target.")
1393 ls_chanmsg(channel, "The citizens are asleep while the mad scientist is choosing a target.")
1396 ls_set_timeout(channel, 120)
1397 elseif ls_timeout_exceeded(channel) then
1398 ls_chanmsg(channel, "The scientists failed to set their alarm clocks. Nobody dies tonight.")
1399 ls_set_state(channel, "investigate")
1400 ls_advance_state(channel)
1402 ls_chanmsg(channel, "The scientists still need to pick someone to kill.")
1406 if state == "investigate" then
1407 -- the investigators are already dead
1408 if table.getn(investigators) == 0 then
1409 ls_set_state(channel, "vote")
1410 ls_advance_state(channel)
1414 if timeout == -1 then
1415 local active_investigator = investigators[math.random(table.getn(investigators))]
1417 for _, investigator in pairs(investigators) do
1418 if investigator == active_investigator then
1419 ls_set_active(channel, investigator, true)
1420 ls_notice(investigator, "You need to choose someone to investigate: /notice " .. BOTNICK .. " investigate <nick>")
1422 ls_set_active(channel, investigator, false)
1423 ls_notice(investigator, "Another investigator is choosing a target.")
1427 if table.getn(investigators) > 1 then
1428 ls_chanmsg(channel, "It's now up to the investigators to find the mad scientists.")
1430 ls_chanmsg(channel, "It's now up to the investigator to find the mad scientists.")
1433 ls_set_timeout(channel, 120)
1434 elseif ls_timeout_exceeded(channel) then
1435 ls_chanmsg(channel, "Looks like the investigator is still firmly asleep.")
1436 ls_set_state(channel, "vote")
1437 ls_advance_state(channel)
1439 ls_chanmsg(channel, "The investigator still needs to do their job.");
1443 if state == "vote" then
1444 local missing_votes = {}
1446 for _, player in pairs(players) do
1447 if not ls_get_vote(channel, player) then
1448 table.insert(missing_votes, player)
1452 if timeout == -1 then
1453 for _, player in pairs(players) do
1454 ls_set_vote(channel, player, nil)
1457 ls_chanmsg(channel, "It's now up to the citizens to vote who to lynch (via /notice " .. BOTNICK .. " vote <nick>).")
1458 ls_set_timeout(channel, 120)
1459 elseif ls_timeout_exceeded(channel) or table.getn(missing_votes) == 0 then
1463 for _, player in pairs(players) do
1464 local vote = ls_get_vote(channel, player)
1467 if not votes[vote] then
1469 table.insert(votees, vote)
1472 votes[vote] = votes[vote] + 1
1476 local function votecomp(v1, v2)
1477 if votes[v1] > votes[v2] then
1482 table.sort(votees, votecomp)
1484 local message_suffix, candidates
1486 if table.getn(votees) > 0 then
1489 for _, votee in pairs(votees) do
1490 if message ~= "" then
1491 message = message .. ", "
1494 message = message .. votes[votee] .. "x " .. ls_format_player(channel, votee)
1497 ls_chanmsg(channel, "Votes: " .. message)
1499 local most_votes = votes[votees[1]]
1502 for _, votee in pairs(votees) do
1503 if votes[votee] == most_votes then
1504 table.insert(candidates, votee)
1508 message_suffix = "was lynched by the angry mob."
1510 candidates = players
1511 message_suffix = "was hit by a stray high-energy laser beam."
1514 local victim_index = math.random(table.getn(candidates))
1515 local victim = candidates[victim_index]
1517 ls_devoice_player(channel, victim)
1519 ls_chanmsg(channel, ls_format_player(channel, victim, true) .. " " .. message_suffix)
1520 ls_remove_player(channel, victim, true)
1522 ls_set_state(channel, "kill")
1523 ls_advance_state(channel)
1525 ls_chanmsg(channel, "Some of the citizens still need to vote: " .. ls_format_players(channel, missing_votes))