MattiasBuelens

NetherPVP master

Aug 12th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.06 KB | None | 0 0
  1. --[[
  2.  
  3.     Nether PVP Arena
  4.     Master program
  5.  
  6. --]]
  7.  
  8. -- Load dependencies
  9. dofile("/common/all")
  10. program.load("../mcnet/message", "../mcnet/names", "../mcnet/device")
  11. program.load("../mcnet/devices/server")
  12. program.load("msg", "devices/master")
  13.  
  14. -- Create master
  15. local configPath = "/master.conf"
  16. local defaultConfig = {
  17.     name = "master",
  18.     domain = "netherpvp",
  19.     detectTimeout = 1
  20. }
  21. local config = common.loadStore(configPath, defaultConfig)
  22. local master = game.Master:new{
  23.     -- MCNet
  24.     name = config.name,
  25.     domain = config.domain,
  26.     -- Game
  27.     winScore = 3,
  28.     startTimeout = 3
  29. }
  30.  
  31. print("  Address: "..mcnet.address.format(master.address))
  32. print("  Computer ID: "..tostring(os.computerID()))
  33. print()
  34.  
  35. -- Register handlers
  36. master:on("registeredServant", function(cellId, cellName)
  37.     print("Registered ", cellName, " at ", cellId)
  38. end)
  39.  
  40. master:on("unregisteredServant", function(cellId, cellName)
  41.     print("Unregistered ", cellName, " at ", cellId)
  42. end)
  43.  
  44. master:on("startGame", function()
  45.     print("New game started")
  46. end)
  47.  
  48. master:on("startRound", function()
  49.     print("New round started")
  50. end)
  51.  
  52. master:on("scoreChanged", function(color, score)
  53.     print("Score change for ", color..": ", score)
  54. end)
  55.  
  56. master:on("stopGame", function()
  57.     print("Game stopped")
  58. end)
  59.  
  60. master:on("char", function(char)
  61.     if char == "q" then
  62.         -- Confirm quit
  63.         write("Confirm quit? [y/n] > ")
  64.         local event, char = os.pullEvent("char")
  65.         -- Check response
  66.         local response = (string.lower(char) == "y" and "y") or "n"
  67.         print(response)
  68.         if (response == "y") then
  69.             master:stop()
  70.         end
  71.     end
  72. end)
  73.  
  74. master:on("error", function(errorType, errorMessage)
  75.     print("[ERROR] "..errorMessage)
  76. end)
  77.  
  78. -- Main
  79. master:start(config.detectTimeout)
  80. print("Waiting for start signal...")
  81. print("  Press Q to quit")
  82. print()
  83.  
  84. -- TODO Remote control
  85. master:startGame()
  86.  
  87. while master.isRunning do
  88.     master:trigger(os.pullEvent())
  89. end
  90.  
  91. print()
  92. print("Stopped")
  93. print()
Advertisement
Add Comment
Please, Sign In to add comment