Advertisement
Rochet2

Que

Jun 11th, 2013
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.54 KB | None | 0 0
  1. #include "ScriptPCH.h"
  2.  
  3. #define TOKENID   123 // obvious
  4.  
  5.  
  6. typedef std::set<uint64> playerList;
  7. playerList players;
  8. typedef UNORDERED_MAP<uint64, time_t> cooldownMap;
  9. cooldownMap cooldowns;
  10.  
  11. class queCommandScript : public CommandScript
  12. {
  13. public:
  14.     queCommandScript() : CommandScript("queCommandScript") { }
  15.  
  16.     ChatCommand* GetCommands() const
  17.     {
  18.         static ChatCommand QueSubCommandTable[] =
  19.         {
  20.             { "add",            SEC_GAMEMASTER,     true,   &HandleQueAddCommand,       "", NULL },
  21.             { "remove",         SEC_GAMEMASTER,     true,   &HandleQueRemCommand,       "", NULL },
  22.             { "list",           SEC_GAMEMASTER,     true,   &HandleQueListCommand,      "", NULL },
  23.             { "confirm",        SEC_PLAYER,         false,  &HandleQueConfirmCommand,   "", NULL },
  24.             { NULL,             0,                  false,  NULL,                       "", NULL }
  25.         };
  26.         static ChatCommand QueCommandTable[] =
  27.         {
  28.             { "queue",          SEC_PLAYER,         true,   NULL,                       "", QueSubCommandTable },
  29.             { NULL,             0,                  false,  NULL,                       "", NULL }
  30.         };
  31.         return QueCommandTable;
  32.     }
  33.  
  34.     static bool HandleQueAddCommand(ChatHandler* handler, const char* args)
  35.     {
  36.         if(!args)
  37.             return false;
  38.  
  39.         Player* target;
  40.         uint64 targetGuid;
  41.         std::string targetName;
  42.         if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
  43.             return false;
  44.  
  45.         AddPlayer(targetGuid);
  46.         return true;
  47.     }
  48.  
  49.     static bool HandleQueRemCommand(ChatHandler* handler, const char* args)
  50.     {
  51.         if(!args)
  52.             return false;
  53.  
  54.         Player* target;
  55.         uint64 targetGuid;
  56.         std::string targetName;
  57.         if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
  58.             return false;
  59.  
  60.         RemovePlayer(targetGuid);
  61.         return true;
  62.     }
  63.  
  64.     static bool HandleQueListCommand(ChatHandler* handler, const char* args)
  65.     {
  66.         handler->PSendSysMessage("Queued players:");
  67.         const playerList* players = GetPlayers();
  68.         if(players->empty())
  69.             return true;
  70.         playerList::const_iterator it = players->begin();
  71.         uint32 i = 0;
  72.         while(it != players->end() /*&& i < 50*/) // limit list?
  73.         {
  74.             // also prints offline players!
  75.             Player* player = sObjectAccessor->FindPlayer(*it);
  76.             if(player)
  77.                 handler->PSendSysMessage("%u. %s %s", ++i, player->GetName().c_str(), player->HasItemCount(TOKENID, 1) ? "is VIP" : "is not VIP");
  78.             ++it;
  79.         }
  80.         return true;
  81.     }
  82.  
  83.     static bool HandleQueConfirmCommand(ChatHandler* handler, const char* args)
  84.     {
  85.         if(!handler->GetSession())
  86.             return true;
  87.         Player* player = handler->GetSession()->GetPlayer();
  88.         if(!player)
  89.             return true;
  90.         const playerList* players = GetPlayers();
  91.         if(players->empty() || players->find(player->GetGUID()) == players->end())
  92.         {
  93.             handler->PSendSysMessage("You are not in the queue");
  94.             return true;
  95.         }
  96.  
  97.         bool isVIP = player->HasItemCount(TOKENID, 1);
  98.  
  99.         cooldownMap* cooldowns = GetCooldowns();
  100.         cooldownMap::const_iterator it = cooldowns->find(player->GetGUID());
  101.         if(it != cooldowns->end() && it->second >= time(NULL))
  102.         {
  103.             handler->PSendSysMessage("Cooldown remaining less than %u hours", uint32((it->second-time(NULL))/HOUR));
  104.             return true;
  105.         }
  106.         if(*players->begin() == player->GetGUID()) // first in que, confirm
  107.             ConfirmPlayer(player);
  108.         else if(isVIP)
  109.         {
  110.             player->DestroyItemCount(TOKENID, 1, true);
  111.             ConfirmPlayer(player);
  112.         }
  113.         else
  114.         {
  115.             uint32 pos = 0;
  116.             playerList::const_iterator it = players->begin();
  117.             while(++it != players->end() && (*it) != player->GetGUID())
  118.                 ++pos;
  119.             handler->PSendSysMessage("You are on place %u in the queue", ++pos);
  120.             return true;
  121.         }
  122.         (*cooldowns)[player->GetGUID()] = time(NULL) + (isVIP ? 8*HOUR : 24*HOUR); // 8/24 hour delay
  123.         return true;
  124.     }
  125.  
  126.     static cooldownMap* GetCooldowns()
  127.     {
  128.         return &cooldowns;
  129.     }
  130.     static playerList* GetPlayers()
  131.     {
  132.         return &players;
  133.     }
  134.     static bool AddPlayer(uint64 guid)
  135.     {
  136.         bool added = players.insert(guid).second;
  137.         if(added && players.size() <= 1)
  138.             SendUpdateMessage();
  139.         return added;
  140.     }
  141.     static void RemovePlayer(uint64 guid)
  142.     {
  143.         if (players.size() >= 2 && *players.begin() == guid)
  144.         {
  145.             players.erase(guid);
  146.             //cooldowns.erase(guid);//dont add incase player can rejoin que
  147.             SendUpdateMessage();
  148.         }
  149.         else
  150.         {
  151.             players.erase(guid);
  152.             //cooldowns.erase(guid);//dont add incase player can rejoin que
  153.         }
  154.     }
  155.     static void SendUpdateMessage()
  156.     {
  157.         if(players.empty())
  158.             return;
  159.         Player* top = sObjectAccessor->FindPlayer(*players.begin());
  160.         if(!top)
  161.         {
  162.             RemovePlayer(*players.begin());
  163.             return;
  164.         }
  165.         const char* name = top->GetName().c_str();
  166.         SessionMap const& smap = sWorld->GetAllSessions();
  167.         for (SessionMap::const_iterator iter = smap.begin(); iter != smap.end(); ++iter)
  168.             if (Player* player = iter->second->GetPlayer())
  169.                 if (player->GetSession() && player->IsGameMaster())
  170.                     ChatHandler(iter->second).PSendSysMessage("%s is on top of queue", name);
  171.     }
  172.  
  173.     static void ConfirmPlayer(Player* player)
  174.     {
  175.         // confirmation code here
  176.  
  177.         RemovePlayer(player->GetGUID());
  178.     }
  179. };
  180.  
  181. class queLogOut : PlayerScript
  182. {
  183. public:
  184.     queLogOut() : PlayerScript("queLogOut") { }
  185.  
  186.     /*
  187.     void OnLogout(Player* player)
  188.     {
  189.     RemovePlayer(player->GetGUID());
  190.     }
  191.     */
  192.     void OnLogin(Player* player)
  193.     {
  194.         queCommandScript::RemovePlayer(player->GetGUID());
  195.     }
  196. };
  197.  
  198. class queCreatureScript : public CreatureScript
  199. {
  200. public:
  201.     queCreatureScript() : CreatureScript("queCreatureScript") { }
  202.  
  203.     bool OnGossipHello(Player* player, Creature* creature)
  204.     {
  205.         if(players.find(player->GetGUID()) == players.end())
  206.             player->ADD_GOSSIP_ITEM(0, "Join queue", GOSSIP_SENDER_MAIN, 1);
  207.         else
  208.             player->ADD_GOSSIP_ITEM(0, "Leave queue", GOSSIP_SENDER_MAIN, 2);
  209.         player->ADD_GOSSIP_ITEM(7, "Nevermind..", GOSSIP_SENDER_MAIN, 0);
  210.         player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID());
  211.         return true;
  212.     }
  213.  
  214.     bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action)
  215.     {
  216.         player->PlayerTalkClass->ClearMenus();
  217.         if(sender != GOSSIP_SENDER_MAIN)
  218.             return false;
  219.  
  220.         switch(action)
  221.         {
  222.         case 1:
  223.             queCommandScript::AddPlayer(player->GetGUID());
  224.             break;
  225.         case 2:
  226.             queCommandScript::RemovePlayer(player->GetGUID());
  227.             break;
  228.         default:
  229.             player->CLOSE_GOSSIP_MENU();
  230.         }
  231.         return true;
  232.     }
  233. };
  234.  
  235. void AddSC_queSystem()
  236. {
  237.     new queCreatureScript();
  238.     new queCommandScript();
  239.     new queLogOut();
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement