Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Server program (run on one ComputerCraft computer)
- local drawingData = {} -- Table to store drawing data
- local timerDuration = 60 -- Duration of the timer in seconds
- local isDrawing = false
- local clients = {} -- Table to store client IDs
- -- Function to broadcast drawing data to all clients
- function broadcastDrawing()
- for _, id in ipairs(clients) do
- rednet.send(id, "DRAWING " .. textutils.serialize(drawingData))
- end
- end
- -- Function to broadcast timer updates
- function broadcastTimer(timeLeft)
- for _, id in ipairs(clients) do
- rednet.send(id, "TIMER " .. timeLeft)
- end
- end
- -- Function to handle incoming messages from clients
- function handleClientMessages()
- while true do
- local senderId, message = rednet.receive()
- if message == "JOIN" then
- table.insert(clients, senderId)
- elseif message:find("DRAW") then
- -- Update drawing data and broadcast
- drawingData = textutils.unserialize(message:sub(6))
- broadcastDrawing()
- elseif message:find("TIMER") then
- -- Update the timer display for all clients
- local timeLeft = message:sub(7)
- broadcastTimer(timeLeft)
- end
- end
- end
- -- Main function to start the game
- function startGame()
- rednet.open("top")
- -- Set initial drawing data
- drawingData = {}
- isDrawing = true
- -- Start timer
- local startTime = os.time()
- local endTime = startTime + timerDuration
- while isDrawing do
- local currentTime = os.time()
- local timeLeft = math.max(0, endTime - currentTime)
- -- Broadcast timer update
- broadcastTimer(timeLeft)
- if timeLeft <= 0 then
- isDrawing = false
- broadcastTimer("Time's up!")
- break
- end
- -- Sleep for 1 second before next update
- os.sleep(1)
- end
- handleClientMessages()
- end
- startGame()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement