Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.24 KB | None | 0 0
  1. local cLobbyManager = {};
  2.  
  3. function cLobbyManager:constructor()
  4.     -- CLASS VARS --
  5.     self.tblLobbys          = {};
  6.     self.tblModesData       = false;
  7.  
  8.     self.tblMinigames       = {
  9.         [1] = "cFiveTowers",
  10.         [2] = "cAimArena",
  11.     };
  12.  
  13.     self.tblPlayerLobbys    = {};
  14.  
  15.     self.tblLobbyMarker     = {
  16.         {-2111.07, -444.06, 37.73},
  17.     };
  18.  
  19.     -- BINDS --
  20.     self.bPlayerQuit        = bind(cLobbyManager.onPlayerQuit,self);
  21.     self.bRequestCreate     = bind(cLobbyManager.onRequestCreate, self);
  22.     self.bOnNewLobby        = bind(cLobbyManager.onRequestNewLobby, self);
  23.     self.bOnJoinLobby       = bind(cLobbyManager.onJoinLobby, self);
  24.     self.bOnSpectate        = bind(cLobbyManager.onSpeactate, self);
  25.     self.bOnQuitSpectate    = bind(cLobbyManager.onQuitSpectate, self);
  26.  
  27.     -- EVENTS --
  28.     addEventHandler("onPlayerQuit", root, self.bPlayerQuit);
  29.  
  30.     addEvent("LobbyManager:requestCreate", true);
  31.     addEventHandler("LobbyManager:requestCreate", root, self.bRequestCreate);
  32.  
  33.     addEvent("LobbyManager:create", true);
  34.     addEventHandler("LobbyManager:create", root, self.bOnNewLobby);
  35.  
  36.     addEvent("LobbyManager:join", true);
  37.     addEventHandler("LobbyManager:join", root, self.bOnJoinLobby);
  38.  
  39.     addEvent("LobbyManager:spectate", true);
  40.     addEventHandler("LobbyManager:spectate", root, self.bOnSpectate);
  41.  
  42.     addEvent("LobbyManager:quitSpectate", true);
  43.     addEventHandler("LobbyManager:quitSpectate", root, self.bOnQuitSpectate);
  44.  
  45.     -- INIT --
  46.     self:setup();
  47. end
  48.  
  49.  
  50. function cLobbyManager:setup()
  51.     for iID, strClass in pairs(self.tblMinigames) do
  52.         self.tblMinigames[iID] = nil;
  53.  
  54.         if (_G[strClass]) then
  55.             self.tblMinigames[iID] = strClass;
  56.         end
  57.     end
  58.  
  59.     for _, tblPos in pairs(self.tblLobbyMarker) do
  60.         local uMarker = createMarker(tblPos[1], tblPos[2], tblPos[3], "cylinder", 1.5, 237, 207, 75, 200);
  61.  
  62.         addEventHandler("onMarkerHit", uMarker, function(uPlayer, bDim)
  63.             if (bDim and uPlayer:isLoggedIn()) then
  64.                 local tblLobbys = {};
  65.                 for i, cLobby in pairs(self.tblLobbys) do
  66.                     table.insert(tblLobbys, {
  67.                         ID = i,
  68.                         Players = cLobby:getPlayerCount(),
  69.                         Status = cLobby:getStatus(),
  70.                         MaxPlayers = cLobby:getGame():getMaxPlayers(),
  71.                         Mode = cLobby:getGame():getName()
  72.                     });
  73.                 end
  74.  
  75.                 return uPlayer:triggerEvent("LobbyBrowser:enter", uPlayer, tblLobbys);
  76.             end
  77.         end);
  78.     end
  79. end
  80.  
  81. function cLobbyManager:canCreate(uPlayer)
  82.     return uPlayer:isAdmin(4);
  83. end
  84.  
  85. function cLobbyManager:getFreeDimension()
  86.     for i = 100, 9999, 1 do
  87.         local bUsed = false;
  88.         for _, cLobby in pairs(self.tblLobbys) do
  89.             if (cLobby:getDimension() == i) then
  90.                 bUsed = true;
  91.                 break;
  92.             end
  93.         end
  94.  
  95.         if (bUsed == false) then
  96.             return i;
  97.         end
  98.     end
  99.  
  100.     return math.random(100,9999);
  101. end
  102.  
  103. function cLobbyManager:onRequestNewLobby(iMode, tblSettings)
  104.     if (client and client:isLoggedIn()) then
  105.         if (self:canCreate(client)) then
  106.             local cLobby = self:onCreateLobby(client, iMode);
  107.             cLobby:setMinPlayers(tblSettings.MinPlayers);
  108.             cLobby:setMaxPlayers(tblSettings.MaxPlayers);
  109.             cLobby:setRoundMode(tblSettings.RoundMode);
  110.         end
  111.     end
  112. end
  113.  
  114. function cLobbyManager:onCreateLobby(uPlayer, iMode)
  115.     if (uPlayer:isLoggedIn() and self.tblMinigames[iMode])  then
  116.         local iLobby = #self.tblLobbys+1;
  117.         local iDimension = self:getFreeDimension();
  118.         self.tblLobbys[iLobby] = new(cGameLobby, iLobby, uPlayer, self.tblMinigames[iMode], iDimension);
  119.         self:onJoinLobby(iLobby, uPlayer);
  120.         return self.tblLobbys[iLobby];
  121.     end
  122.  
  123.     return false;
  124. end
  125.  
  126. function cLobbyManager:onPlayerQuit()
  127.     if (self:isInLobby(source)) then
  128.         return self:onLeaveLobby(source);
  129.     end
  130. end
  131.  
  132. function cLobbyManager:onLeaveLobby(uPlayer)
  133.     if (self:isInLobby(uPlayer)) then
  134.         local cLobby = self.tblPlayerLobbys[uPlayer];
  135.         cLobby:removePlayer(uPlayer);
  136.  
  137.         self.tblPlayerLobbys[uPlayer] = nil;
  138.  
  139.         if (cLobby:getPlayerCount() <= 0) then
  140.             cLobby:stop();
  141.         end
  142.     end
  143. end
  144.  
  145. function cLobbyManager:isInLobby(uPlayer)
  146.     if (self.tblPlayerLobbys[uPlayer]) then
  147.         return true;
  148.     end
  149.  
  150.     return false;
  151. end
  152.  
  153. function cLobbyManager:onJoinLobby(iLobby, uPlayer, bSpec)
  154.     local cLobby = self.tblLobbys[iLobby];
  155.     local cInLobby = self.tblPlayerLobbys[uPlayer];
  156.     if (cLobby and uPlayer:isLoggedIn() and (not cInLobby or cInLobby:getID() == iLobby)) then
  157.         if (cLobby:canJoin()) then
  158.             cLobby:addPlayer(uPlayer, bSpec);
  159.             self.tblPlayerLobbys[uPlayer] = cLobby;
  160.         else
  161.             uPlayer:showInfo("Beitritt ist nicht mehr möglich", 3000, "Fehler", "error");
  162.         end
  163.     end
  164. end
  165.  
  166. function cLobbyManager:onSpeactate(iLobby, uPlayer)
  167.     local cLobby = self.tblLobbys[iLobby];
  168.     if (cLobby and uPlayer:isLoggedIn()) then
  169.         if (cLobby:canSpectate()) then
  170.             cLobby:addSpectator(uPlayer);
  171.             self.tblPlayerLobbys[uPlayer] = cLobby;
  172.         else
  173.             uPlayer:showInfo("Zuschauen ist nicht möglich", 3000, "Fehler", "error");
  174.         end
  175.     end
  176. end
  177.  
  178. function cLobbyManager:onQuitSpectate(iLobby, uPlayer)
  179.     if (client and client:isLoggedIn()) then
  180.         if (self:isInLobby(client)) then
  181.             local cLobby = self.tblPlayerLobbys[client];
  182.             if (cLobby) then
  183.                 cLobby:removeSpectator(client);
  184.             end
  185.         end
  186.     end
  187. end
  188.  
  189. function cLobbyManager:onRequestCreate()
  190.     if ((client) and (client:isLoggedIn())) then
  191.         if (self:canCreate(client)) then
  192.             if not self.tblModesData then
  193.                 self.tblModesData = {};
  194.                 for iID, strClass in pairs(self.tblMinigames) do
  195.                     local cMode = new(_G[strClass]);
  196.                     table.insert(self.tblModesData, {
  197.                         ID = iID,
  198.                         Name = cMode:getName(),
  199.                         MinPlayer = cMode:getMinPlayers(),
  200.                         MaxPlayer = cMode:getMaxPlayers(),
  201.                     });
  202.  
  203.                     delete(cMode);
  204.                 end
  205.             end
  206.  
  207.             return client:triggerEvent("LobbyBrowser:startCreate", client, self.tblModesData);
  208.         else
  209.             client:showInfo("Du hast keine Rechte dazu", 3000, "Fehler", "error");
  210.         end
  211.     end
  212. end
  213.  
  214. addEventHandler("onResourceStart", resourceRoot, function()
  215.     cInstanceManager:new(cLobbyManager, "LobbyManager");
  216. end);
  217.  
  218. addCommandHandler("exitlobby", function(uPlayer)
  219.     cInstanceManager:get("LobbyManager"):onLeaveLobby(uPlayer);
  220. end);
  221.  
  222. -----------------------
  223. -- DEV COMMANDS
  224. -----------------------
  225.  
  226. addCommandHandler("tlobby", function(uPlayer)
  227.     local cM = cInstanceManager:get("LobbyManager");
  228.     local iLobby = #cM.tblLobbys+1;
  229.     local iDimension = cM:getFreeDimension();
  230.     cM.tblLobbys[iLobby] = new(cGameLobby,iLobby, uPlayer, cM.tblMinigames[1], iDimension);
  231. end);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement