MattiasBuelens

NetherPVP devices/master

Aug 12th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.80 KB | None | 0 0
  1. --[[
  2.  
  3.     Nether PVP Arena
  4.     Master device
  5.  
  6. --]]
  7.  
  8. game = game or {}
  9.  
  10. local GameState = {
  11.     Stopped = 0,
  12.     Starting = 1,
  13.     Started = 2
  14. }
  15.  
  16. local Master = common.newClass(mcnet.device.Server, {
  17.     -- Device
  18.     deviceType = "netherpvp:master",
  19.     -- Server
  20.     registerServantMessage = "netherpvp:registerCell",
  21.     registeredServantMessage = "netherpvp:registeredCell",
  22.     servantType = "netherpvp:cell",
  23.     -- Game
  24.     state = GameState.Stopped,
  25.     scores = nil,
  26.     winScore = 1,
  27.     startTimeout = 0,
  28.     -- Waiting
  29.     waitMessage = nil,
  30.     waitRemaining = nil,
  31.     waitCallback = nil,
  32.     waitContinue = nil
  33. })
  34. game.Master = Master
  35.  
  36. -- Constructor
  37. function Master:init()
  38.     mcnet.device.Server.init(self)
  39.  
  40.     -- Address
  41.     self.address = mcnet.address.new{
  42.         localPart = self.name,
  43.         domainPart = self.domain
  44.     }
  45.  
  46.     -- Reset
  47.     self:resetGame()
  48.  
  49.     self:addMessageHandler(game.msg.notifyWin, self.handleNotifyWin)
  50. end
  51.  
  52. function Master:checkServant(deviceType, deviceAddress)
  53.     return deviceType == self.servantType and deviceAddress.domainPart == self.domain
  54. end
  55.  
  56. function Master:getServantName(servantAddress)
  57.     return servantAddress.localPart
  58. end
  59.  
  60. function Master:stop()
  61.     -- Reset game
  62.     self:resetGame()
  63.     -- Stop device
  64.     mcnet.device.Server.stop(self)
  65. end
  66.  
  67. --[[
  68.  
  69.     Utilities
  70.  
  71. --]]
  72.  
  73. -- Broadcast message to all cells
  74. function Master:broadcastToCells(msg)
  75.     -- Sender address
  76.     msg.from = self.address
  77.  
  78.     -- Broadcast
  79.     for i,cellId in ipairs(self.servants:getAll()) do
  80.         self:send(cellId, msg)
  81.     end
  82. end
  83.  
  84. -- Wait for response from all cells
  85. function Master:waitForCells(messageType, callbackFunc, continueFunc)
  86.     assert(self.waitMessage == nil, "still waiting for "..tostring(self.waitMessage))
  87.     -- Start waiting
  88.     self.waitMessage = messageType
  89.     self.waitRemaining = common.combine(self.servants:getAll(), true)
  90.     self.waitCallback = callbackFunc
  91.     self.waitContinue = continueFunc
  92.     self:on("mcnet_message", self.handleWait, self)
  93. end
  94.  
  95. function Master:handleWait(senderId, msg)
  96.     if msg.type == self.waitMessage then
  97.         -- Remove from remaining
  98.         self.waitRemaining[senderId] = nil
  99.         -- Callback
  100.         if self.waitCallback ~= nil then
  101.             self.waitCallback(senderId, msg)
  102.         end
  103.         -- Check remaining
  104.         if next(self.waitRemaining) == nil then
  105.             local continue = self.waitContinue
  106.             -- Stop waiting
  107.             self:off("mcnet_message", self.handleWait, self)
  108.             self.waitMessage, self.waitRemaining = nil, nil
  109.             self.waitCallback, self.waitContinue = nil, nil
  110.             -- Callback
  111.             if continue ~= nil then continue() end
  112.         end
  113.     end
  114. end
  115.  
  116. --[[
  117.  
  118.     Colors
  119.  
  120. --]]
  121.  
  122. -- Get the current color of all cells and pass it to the callback
  123. function Master:getCellColors(callbackFunc)
  124.     -- Request color
  125.     self:broadcastToCells(mcnet.message.new{
  126.         type = game.msg.getColor
  127.     })
  128.     -- Wait for all colors
  129.     local cellColors = {}
  130.     self:waitForCells(game.msg.notifyColor,
  131.         function(cellId, msg)
  132.             cellColors[cellId] = tonumber(msg.data)
  133.         end,
  134.         function()
  135.             callbackFunc(cellColors)
  136.         end
  137.     )
  138. end
  139.  
  140. --[[
  141.  
  142.     Game
  143.  
  144. --]]
  145.  
  146. function Master:startGame()
  147.     self:trigger("startGame")
  148.     -- Reset game
  149.     self:resetGame()
  150.     -- TODO: Countdown?
  151.     -- Start new round
  152.     self:newRound()
  153. end
  154.  
  155. function Master:stopGame(winnerColor)
  156.     self:trigger("stopGame", winnerColor)
  157.     self:stopRound()
  158. end
  159.  
  160. function Master:resetGame()
  161.     self:trigger("resetGame")
  162.     -- Reset scores
  163.     self.scores = {}
  164.     -- Reset cells
  165.     self:stopRound()
  166. end
  167.  
  168. --[[
  169.  
  170.     Rounds
  171.  
  172. --]]
  173.  
  174. function Master:newRound()
  175.     -- Starting
  176.     self.state = GameState.Starting
  177.     self:trigger("newRound")
  178.  
  179.     self:getCellColors(function(cellColors)
  180.         local cells, colors = common.getKeys(cellColors), common.getValues(cellColors)
  181.         -- Randomly cycle the colors
  182.         local newColors = common.randomCycle(colors)
  183.         -- Initialize new round with new colors
  184.         for i,cellId in ipairs(cells) do
  185.             self:send(cellId, mcnet.message.new{
  186.                 type = game.msg.newRound,
  187.                 data = newColors[i]
  188.             })
  189.         end
  190.         -- Wait for all cells to get ready
  191.         self:waitForCells(game.msg.notifyReady, nil,
  192.             function()
  193.                 self:startRound()
  194.             end
  195.         )
  196.     end)
  197. end
  198.  
  199. function Master:startRound()
  200.     self:beforeStartRound()
  201.     -- Started
  202.     self.state = GameState.Started
  203.     self:trigger("startRound")
  204.     -- Drop lever
  205.     self:dropLever()
  206.     -- Broadcast
  207.     self:broadcastToCells(mcnet.message.new{
  208.         type = game.msg.startRound
  209.     })
  210. end
  211.  
  212. function Master:stopRound()
  213.     -- Stopped
  214.     self.state = GameState.Stopped
  215.     self:trigger("stopRound")
  216.     -- Broadcast
  217.     self:broadcastToCells(mcnet.message.new{
  218.         type = game.msg.stopRound
  219.     })
  220. end
  221.  
  222. function Master:beforeStartRound()
  223.     -- TODO: Countdown on big screen?
  224.     os.sleep(self.startTimeout)
  225. end
  226.  
  227. function Master:dropLever()
  228.     -- TODO
  229. end
  230.  
  231. --[[
  232.  
  233.     Round winning
  234.  
  235. --]]
  236.  
  237. function Master:winRound(winnerColor)
  238.     -- Stop round
  239.     self:stopRound()
  240.  
  241.     -- Add to score
  242.     local score = (self.scores[winnerColor] or 0) + 1
  243.     self.scores[winnerColor] = score
  244.     self:trigger("scoreChanged", winnerColor, score)
  245.  
  246.     -- New round or game over
  247.     if score == self.winScore then
  248.         self:stopGame()
  249.     else
  250.         self:newRound()
  251.     end
  252. end
  253.  
  254. function Master:handleNotifyWin(senderId, msg)
  255.     if self.state == GameState.Started then
  256.         self:winRound(tonumber(msg.data))
  257.     end
  258. end
Advertisement
Add Comment
Please, Sign In to add comment