Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- [Chatbox] Minigame: Bomb
- local chatbox = peripheral.find "chatbox"
- local defaultTries = 5
- local chosenColor
- local started = false
- local leaderPath = "bomb.leaders"
- local function getLeaders()
- if fs.exists(leaderPath) then
- local f = fs.open(leaderPath, "r")
- local leaders = textutils.unserialize(f.readAll())
- f.close()
- return leaders
- else
- return {}
- end
- end
- local function findTable( tbl, player )
- for i = 1,#tbl do
- if tbl[i].player == player then
- return i
- end
- end
- return false
- end
- local function addLeader( player )
- local leaders = getLeaders()
- local playerOrder = findTable( leaders, player )
- if playerOrder then
- leaders[playerOrder].score = leaders[playerOrder].score+1
- else
- leaders[#leaders+1] = {player = player, score = 1}
- end
- local f = fs.open(leaderPath, "w")
- f.write(textutils.serialize(leaders))
- f.close()
- end
- local function say( msg )
- local oldLabel = chatbox.getLabel()
- chatbox.setLabel "Bomb"
- chatbox.say( msg )
- chatbox.setLabel( oldLabel )
- end
- local function tell( player, msg )
- local oldLabel = chatbox.getLabel()
- chatbox.setLabel "Bomb"
- chatbox.tell( player, msg)
- chatbox.setLabel( oldLabel )
- end
- local function strTbl( str )
- local r = {}
- for word in str:gmatch "([^%s]+)" do
- r[#r+1] = word
- end
- return r
- end
- local function randomColor()
- local color = 2^math.random( 0,15 )
- for k,v in pairs( colors ) do
- if color == v then
- return k
- end
- end
- end
- local function checkColor( color )
- if color:lower() == "grey" then
- color = "gray"
- elseif color:lower() == "lightgrey" then
- color = "lightgray"
- end
- for k,v in pairs( colors ) do
- if color:lower() == k:lower() and (k ~= "subtract" and k ~= "combine" and k ~= "test") then
- return color:lower()
- end
- end
- return false
- end
- local function doBomb()
- sleep(1)
- local tries = defaultTries
- while tries > 0 do
- local _,_,player,cmd = os.pullEvent "chatbox_command"
- cmd = strTbl( cmd )
- if cmd[1] == "bomb" then
- local color = checkColor( cmd[2] )
- if color then
- if color == chosenColor:lower() then
- say( player.." DISARMED the Bomb with "..color.."! Congrats" )
- addLeader(player)
- return
- else
- tries = tries-1
- say( player.." failed to disarm the bomb with "..color )
- say( tries.." tries left!" )
- end
- end
- end
- end
- say( "The Bomb EXPLODED! Color was "..chosenColor )
- end
- local function startBomb()
- while true do
- started = false
- term.clear()
- term.setCursorPos(1,1)
- print "Waiting for start...\nUse \"##bomb arm\" to arm the Bomb\n"
- local _,_,player,cmd = os.pullEvent "chatbox_command"
- if cmd == "bomb arm" then
- say( player.." armed the Bomb! "..defaultTries.." tries remaining" )
- print( player.." armed the Bomb " )
- say "Use \"##bomb <color>\" to (try) to disarm it!"
- chosenColor = randomColor()
- print "Press [SPACE] to see which color got selected"
- sleep(0.5)
- started = true
- doBomb()
- chosenColor = false
- say "Do \"##bomb arm\" to arm another Bomb!"
- end
- end
- end
- local function compare(a,b)
- return a.score > b.score
- end
- local function help()
- while true do
- local _,_,player,cmd = os.pullEvent "chatbox_command"
- cmd = strTbl(cmd)
- if cmd[1] == "bomb" then
- if cmd[2] == "arm" and started then
- tell( player, "Bomb already armed! Do \"##bomb <color>\"" )
- elseif cmd[2] == "info" then
- tell( player, "Bomb Minigame created by ItsRodrick. Ask him for the program")
- tell( player, "Cool, you found a bit hidden feature" )
- elseif cmd[2] == "leaderboard" then
- tell(player, "Leaderboard:")
- local leaders = getLeaders()
- table.sort( leaders, compare )
- local lenght = #leaders<10 and #leaders or 10
- for i = 1,lenght do
- tell(player, "["..i.."] "..leaders[i].player..": "..leaders[i].score )
- end
- elseif not checkColor(cmd[2]) and cmd[2]~="arm" then
- tell( player, "\"##bomb arm\" - Arms a new Bomb" )
- tell( player, "\"##bomb <color>\" - Tries to disarm the Bomb. Use the colors you use in CC, not case-sensitive")
- tell( player, "\"##bomb leaderboard\" - Shows the Leaderboard of who disarmed the bomb")
- tell( player, "\"##bomb info\" - Shows credits and extra info" )
- end
- end
- end
- end
- local function sayColor()
- while true do
- local _, key = os.pullEvent "key"
- if key == keys.space and chosenColor then
- print( "Selected color: "..chosenColor )
- end
- end
- end
- parallel.waitForAny( startBomb, help, sayColor )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement