Guest User

waitlist.cpp

a guest
Apr 14th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. ////////////////////////////////////////////////////////////////////////
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ////////////////////////////////////////////////////////////////////////
  17. #include "otpch.h"
  18. #include "waitlist.h"
  19.  
  20. #include "player.h"
  21.  
  22. #include "configmanager.h"
  23. #include "game.h"
  24.  
  25. extern ConfigManager g_config;
  26. extern Game g_game;
  27.  
  28. WaitList::iterator WaitingList::find(const Player* player, uint32_t& slot)
  29. {
  30. slot = 1;
  31. std::string name = asLowerCaseString(player->getName());
  32. for(WaitList::iterator it = waitList.begin(); it != waitList.end(); ++it)
  33. {
  34. if((*it)->ip == player->getIP() && asLowerCaseString((*it)->name) == name)
  35. return it;
  36.  
  37. ++slot;
  38. }
  39.  
  40. return waitList.end();
  41. }
  42.  
  43. int32_t WaitingList::getTime(int32_t slot)
  44. {
  45. if(slot < 5)
  46. return 5;
  47. else if(slot < 10)
  48. return 10;
  49. else if(slot < 20)
  50. return 20;
  51. else if(slot < 50)
  52. return 60;
  53.  
  54. return 120;
  55. }
  56.  
  57. bool WaitingList::login(const Player* player)
  58. {
  59. uint32_t online = g_game.getPlayersOnline(), max = g_config.getNumber(ConfigManager::MAX_PLAYERS);
  60. if(player->hasFlag(PlayerFlag_CanAlwaysLogin) || player->isAccountManager() || (waitList.empty()
  61. && online < max) || (g_config.getBool(ConfigManager::PREMIUM_SKIP_WAIT) && player->isPremium()))
  62. return true;
  63.  
  64. cleanup();
  65. uint32_t slot = 0;
  66.  
  67. WaitList::iterator it = find(player, slot);
  68. if(it != waitList.end())
  69. {
  70. if((online + slot) > max)
  71. {
  72. //let them wait a bit longer
  73. (*it)->timeout = OTSYS_TIME() + getTimeout(slot) * 1000;
  74. return false;
  75. }
  76.  
  77. //should be able to login now
  78. delete *it;
  79. waitList.erase(it);
  80. return true;
  81. }
  82.  
  83. Wait* wait = new Wait();
  84. if(player->isPremium())
  85. {
  86. slot = 1;
  87. WaitList::iterator it = waitList.end();
  88. for(WaitList::iterator wit = waitList.begin(); wit != it; ++wit)
  89. {
  90. if(!(*wit)->premium)
  91. {
  92. it = wit;
  93. break;
  94. }
  95.  
  96. ++slot;
  97. }
  98.  
  99. waitList.insert(it, wait);
  100. }
  101. else
  102. {
  103. waitList.push_back(wait);
  104. slot = waitList.size();
  105. }
  106.  
  107. wait->name = player->getName();
  108. wait->ip = player->getIP();
  109. wait->premium = player->isPremium();
  110.  
  111. wait->timeout = OTSYS_TIME() + getTimeout(slot) * 1000;
  112. return false;
  113. }
  114.  
  115. int32_t WaitingList::getSlot(const Player* player)
  116. {
  117. uint32_t slot = 0;
  118. WaitList::iterator it = find(player, slot);
  119. if(it != waitList.end())
  120. return slot;
  121.  
  122. return -1;
  123. }
  124.  
  125. void WaitingList::cleanup()
  126. {
  127. for(WaitList::iterator it = waitList.begin(); it != waitList.end();)
  128. {
  129. if(((*it)->timeout - OTSYS_TIME()) <= 0)
  130. {
  131. delete *it;
  132. it = waitList.erase(it);
  133. }
  134. else
  135. ++it;
  136. }
  137. }
Add Comment
Please, Sign In to add comment