Advertisement
xX-AAAAAAAAAA-Xx

ComputerCraft Pictionary Server

Sep 14th, 2024 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. -- Server program (run on one ComputerCraft computer)
  2.  
  3. local drawingData = {} -- Table to store drawing data
  4. local timerDuration = 60 -- Duration of the timer in seconds
  5. local isDrawing = false
  6. local clients = {} -- Table to store client IDs
  7.  
  8. -- Function to broadcast drawing data to all clients
  9. function broadcastDrawing()
  10. for _, id in ipairs(clients) do
  11. rednet.send(id, "DRAWING " .. textutils.serialize(drawingData))
  12. end
  13. end
  14.  
  15. -- Function to broadcast timer updates
  16. function broadcastTimer(timeLeft)
  17. for _, id in ipairs(clients) do
  18. rednet.send(id, "TIMER " .. timeLeft)
  19. end
  20. end
  21.  
  22. -- Function to handle incoming messages from clients
  23. function handleClientMessages()
  24. while true do
  25. local senderId, message = rednet.receive()
  26. if message == "JOIN" then
  27. table.insert(clients, senderId)
  28. elseif message:find("DRAW") then
  29. -- Update drawing data and broadcast
  30. drawingData = textutils.unserialize(message:sub(6))
  31. broadcastDrawing()
  32. elseif message:find("TIMER") then
  33. -- Update the timer display for all clients
  34. local timeLeft = message:sub(7)
  35. broadcastTimer(timeLeft)
  36. end
  37. end
  38. end
  39.  
  40. -- Main function to start the game
  41. function startGame()
  42. rednet.open("top")
  43. -- Set initial drawing data
  44. drawingData = {}
  45. isDrawing = true
  46.  
  47. -- Start timer
  48. local startTime = os.time()
  49. local endTime = startTime + timerDuration
  50.  
  51. while isDrawing do
  52. local currentTime = os.time()
  53. local timeLeft = math.max(0, endTime - currentTime)
  54.  
  55. -- Broadcast timer update
  56. broadcastTimer(timeLeft)
  57.  
  58. if timeLeft <= 0 then
  59. isDrawing = false
  60. broadcastTimer("Time's up!")
  61. break
  62. end
  63.  
  64. -- Sleep for 1 second before next update
  65. os.sleep(1)
  66. end
  67.  
  68. handleClientMessages()
  69. end
  70.  
  71. startGame()
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement