Advertisement
RodrickLord

[Chatbox] Minigame: Bomb

Jul 4th, 2015
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.78 KB | None | 0 0
  1. -- [Chatbox] Minigame: Bomb
  2.  
  3. local chatbox = peripheral.find "chatbox"
  4.  
  5. local defaultTries = 5
  6. local chosenColor
  7. local started = false
  8. local leaderPath = "bomb.leaders"
  9.  
  10. local function getLeaders()
  11.   if fs.exists(leaderPath) then
  12.     local f = fs.open(leaderPath, "r")
  13.     local leaders = textutils.unserialize(f.readAll())
  14.     f.close()
  15.     return leaders
  16.   else
  17.     return {}
  18.   end
  19. end
  20.  
  21. local function findTable( tbl, player )
  22.   for i = 1,#tbl do
  23.     if tbl[i].player == player then
  24.       return i
  25.     end
  26.   end
  27.   return false
  28. end
  29.  
  30. local function addLeader( player )
  31.   local leaders = getLeaders()
  32.   local playerOrder = findTable( leaders, player )
  33.   if playerOrder then
  34.     leaders[playerOrder].score = leaders[playerOrder].score+1
  35.   else
  36.     leaders[#leaders+1] = {player = player, score = 1}
  37.   end
  38.   local f = fs.open(leaderPath, "w")
  39.   f.write(textutils.serialize(leaders))
  40.   f.close()
  41. end
  42.  
  43. local function say( msg )
  44.   local oldLabel = chatbox.getLabel()
  45.   chatbox.setLabel "Bomb"
  46.   chatbox.say( msg )
  47.   chatbox.setLabel( oldLabel )
  48. end
  49.  
  50. local function tell( player, msg )
  51.   local oldLabel = chatbox.getLabel()
  52.   chatbox.setLabel "Bomb"
  53.   chatbox.tell( player, msg)
  54.   chatbox.setLabel( oldLabel )
  55. end
  56.  
  57. local function strTbl( str )
  58.   local r = {}
  59.   for word in str:gmatch "([^%s]+)" do
  60.     r[#r+1] = word
  61.   end
  62.   return r
  63. end
  64.  
  65. local function randomColor()
  66.   local color = 2^math.random( 0,15 )
  67.   for k,v in pairs( colors ) do
  68.     if color == v then
  69.       return k
  70.     end
  71.   end
  72. end
  73.  
  74. local function checkColor( color )
  75.   if color:lower() == "grey" then
  76.     color = "gray"
  77.   elseif color:lower() == "lightgrey" then
  78.     color = "lightgray"
  79.   end
  80.   for k,v in pairs( colors ) do
  81.     if color:lower() == k:lower() and (k ~= "subtract" and k ~= "combine" and k ~= "test") then
  82.       return color:lower()
  83.     end
  84.   end
  85.   return false
  86. end
  87.  
  88. local function doBomb()
  89.   sleep(1)
  90.   local tries = defaultTries
  91.   while tries > 0 do
  92.     local _,_,player,cmd = os.pullEvent "chatbox_command"
  93.     cmd = strTbl( cmd )
  94.     if cmd[1] == "bomb" then
  95.       local color = checkColor( cmd[2] )
  96.       if color then
  97.         if color == chosenColor:lower() then
  98.           say( player.." DISARMED the Bomb with "..color.."! Congrats" )
  99.           addLeader(player)
  100.           return
  101.         else
  102.           tries = tries-1
  103.           say( player.." failed to disarm the bomb with "..color )
  104.           say( tries.." tries left!"  )
  105.         end
  106.       end
  107.     end
  108.   end
  109.   say( "The Bomb EXPLODED! Color was "..chosenColor )
  110. end
  111.  
  112. local function startBomb()
  113.   while true do
  114.     started = false
  115.     term.clear()
  116.     term.setCursorPos(1,1)
  117.     print "Waiting for start...\nUse \"##bomb arm\" to arm the Bomb\n"
  118.     local _,_,player,cmd = os.pullEvent "chatbox_command"
  119.     if cmd == "bomb arm" then
  120.       say( player.." armed the Bomb! "..defaultTries.." tries remaining" )
  121.       print( player.." armed the Bomb " )
  122.       say "Use \"##bomb <color>\" to (try) to disarm it!"
  123.       chosenColor = randomColor()
  124.       print "Press [SPACE] to see which color got selected"
  125.      
  126.       sleep(0.5)
  127.       started = true
  128.       doBomb()
  129.       chosenColor = false
  130.       say "Do \"##bomb arm\" to arm another Bomb!"
  131.     end
  132.   end
  133. end
  134.  
  135. local function compare(a,b)
  136.   return a.score > b.score
  137. end
  138.  
  139. local function help()
  140.   while true do
  141.     local _,_,player,cmd = os.pullEvent "chatbox_command"
  142.     cmd = strTbl(cmd)
  143.     if cmd[1] == "bomb" then
  144.       if cmd[2] == "arm" and started then
  145.         tell( player, "Bomb already armed! Do \"##bomb <color>\"" )
  146.      
  147.       elseif cmd[2] == "info" then
  148.         tell( player, "Bomb Minigame created by ItsRodrick. Ask him for the program")
  149.         tell( player, "Cool, you found a bit hidden feature" )
  150.      
  151.       elseif cmd[2] == "leaderboard" then
  152.         tell(player, "Leaderboard:")
  153.         local leaders = getLeaders()
  154.         table.sort( leaders, compare )
  155.         local lenght = #leaders<10 and #leaders or 10
  156.         for i = 1,lenght do
  157.           tell(player, "["..i.."] "..leaders[i].player..": "..leaders[i].score )
  158.         end
  159.       elseif not checkColor(cmd[2]) and cmd[2]~="arm" then
  160.         tell( player, "\"##bomb arm\" - Arms a new Bomb" )
  161.         tell( player, "\"##bomb <color>\" - Tries to disarm the Bomb. Use the colors you use in CC, not case-sensitive")
  162.         tell( player, "\"##bomb leaderboard\" - Shows the Leaderboard of who disarmed the bomb")
  163.         tell( player, "\"##bomb info\" - Shows credits and extra info" )
  164.       end
  165.     end
  166.   end
  167. end
  168.  
  169. local function sayColor()
  170.   while true do
  171.     local _, key = os.pullEvent "key"
  172.     if key == keys.space and chosenColor then
  173.       print( "Selected color: "..chosenColor )
  174.     end
  175.   end
  176. end
  177.  
  178. parallel.waitForAny( startBomb, help, sayColor )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement