Created
March 8, 2024 13:23
-
-
Save allquantor/7598eaad1997f3908e93fb2f94bdf834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Constants | |
GAME_TIME = 1000 * 60 * 15 -- 15 minutes game time in milliseconds | |
TOKEN_ADDR = "Sa0iBLPNyJQrwpTTG-tWLQU-1QeUAJA73DdxGGiKoJc" -- Testnet cred token contract | |
TOKEN_MINIMUM = 10000 -- 10 $CRED | |
-- Game state | |
GameState = { | |
Treasury = 0, -- The balance of testnet cred in the round | |
Timeout = 0, -- The time when the game ends | |
LastSender = nil, -- State of the last sender | |
} | |
-- Resets the game to its initial state | |
function resetGameState(timestamp) | |
GameState.Treasury = 0 | |
GameState.Timeout = timestamp + GAME_TIME | |
GameState.LastSender = nil | |
print('New game started! Timeout: ' .. (GameState.Timeout) .. '.') | |
end | |
-- Handles the start of the game | |
function handleStart(m) | |
if m.From == ao.id then | |
resetGameState(m.Timestamp) | |
end | |
end | |
-- Processes a buy action | |
function handleBuy(m) | |
if m.Timestamp >= GameState.Timeout then | |
if GameState.LastSender then | |
print(GameState.LastSender .. " won the game! Dispatching " .. GameState.Treasury .. " tokenz...") | |
ao.send({ | |
Target = TOKEN_ADDR, | |
Action = "Transfer", | |
Recipient = GameState.LastSender, | |
Quantity = tostring(GameState.Treasury) | |
}) | |
end | |
resetGameState(m.Timestamp) | |
end | |
GameState.LastSender = m.Sender | |
GameState.Treasury = GameState.Treasury + tonumber(m.Quantity) | |
GameState.Timeout = m.Timestamp + GAME_TIME | |
local TimeRemaining = (GameState.Timeout - m.Timestamp) // 1000 | |
print('Game continues! Current balance: ' .. GameState.Treasury .. '. Leader: ' .. GameState.LastSender .. '. Remaining time (secs): ' .. TimeRemaining) | |
end | |
-- Checks if the game should end | |
function checkGameEnd(m) | |
if m.Timestamp >= GameState.Timeout and GameState.LastSender then | |
print(GameState.LastSender .. " won the game! Dispatching " .. GameState.Treasury .. " tokenz...") | |
ao.send({ | |
Target = TOKEN_ADDR, | |
Action = "Transfer", | |
Recipient = GameState.LastSender, | |
Quantity = tostring(GameState.Treasury) | |
}) | |
resetGameState(m.Timestamp) | |
else | |
local TimeRemaining = (GameState.Timeout - m.Timestamp) // 1000 | |
print('Game continues! Current balance: ' .. GameState.Treasury .. '. Leader: ' .. GameState.LastSender .. '. Remaining time (secs): ' .. TimeRemaining) | |
end | |
end | |
-- Sends game info to the requester | |
function sendGameInfo(m) | |
ao.send({ | |
Target = m.From, | |
Data = | |
"WELCOME TO THE ARENA.\n" .. | |
"Send me $CRED and I will reset the timer for 15 mins.\n" .. | |
"Last person to send me tokens when timer gets to zero gets all.\n" | |
}) | |
end | |
-- Sends game info to the requester | |
function sendGameState(m) | |
local TimeRemaining = (GameState.Timeout - m.Timestamp) // 1000 | |
local Msg = 'Game continues! Current balance: ' .. GameState.Treasury .. '. Leader: ' .. GameState.LastSender .. '. Remaining time (secs): ' .. TimeRemaining | |
ao.send({ Target = m.From, Data = Msg}) | |
end | |
Handlers.add("Start", function(m) return m.From == ao.id and m.Action == "Start" end, handleStart) | |
Handlers.add("Buy", | |
function(m) return m.From == TOKEN_ADDR and m.Action == "Credit-Notice" and tonumber(m.Quantity) >= TOKEN_MINIMUM end, | |
handleBuy) | |
Handlers.add("what-is-it", Handlers.utils.hasMatchingTag("Action", "Info"), sendGameInfo) | |
Handlers.add("game-state", Handlers.utils.hasMatchingTag("Action", "Tick"), sendGameState) | |
Handlers.add("checkGameEnd", function(m) return "continue" end, checkGameEnd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment