Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.10 KB | None | 0 0
  1. #include "ScriptPCH.h"
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. std::string getString(std::string string, uint32 number);
  7. std::string getDPString(std::string string, uint32 number, uint8 quality);
  8. uint32 getDPFromID(uint32 item_id);
  9.  
  10. bool canRun = true;
  11.  
  12. class dp_items
  13. {
  14. public:
  15. dp_items() {}
  16. dp_items(uint32 item_id, uint8 item_type, std::string item_name, uint8 quality, uint32 dp_cost)
  17. {
  18. this->item_id = item_id;
  19. this->dp_cost = dp_cost;
  20. this->quality = quality;
  21. this->item_type = item_type;
  22. this->item_name = item_name;
  23. }
  24. inline uint32 getItemID() { return item_id; }
  25. inline uint32 getDPCost() { return dp_cost; }
  26. inline uint8 getQuality() { return quality; }
  27. inline uint8 getItemType() { return item_type; }
  28. inline std::string getName() { return item_name; }
  29.  
  30. private:
  31. uint32 item_id, dp_cost;
  32. uint8 quality, item_type;
  33. std::string item_name;
  34. };
  35.  
  36. std::vector<dp_items> items;
  37.  
  38. class load_dp : public WorldScript
  39. {
  40. public:
  41. load_dp() : WorldScript("load_dp") {}
  42.  
  43. void OnStartup() {
  44. QueryResult loadQuery = WorldDatabase.Query(loadStmnt.c_str());
  45. Field *fields = loadQuery->Fetch();
  46. if (fields) {
  47. do
  48. {
  49. dp_items item = dp_items(fields[0].GetUInt32(), fields[1].GetUInt8(), fields[2].GetString(), fields[3].GetUInt8(), fields[4].GetUInt32());
  50. items.push_back(item);
  51. } while (loadQuery->NextRow());
  52. TC_LOG_INFO("server.loading", "DP Items Loaded Successfully.");
  53. }
  54. else
  55. {
  56. canRun = false;
  57. }
  58. QueryResult testQuery = LoginDatabase.Query("SELECT * from account LIMIT 1;");
  59. if (testQuery->GetFieldCount() < 25)
  60. {
  61. TC_LOG_INFO("server.loading", "ERROR: Missing dp_balance column.");
  62. canRun = false;
  63. }
  64. else
  65. {
  66. TC_LOG_INFO("server.loading", "DP System Loaded Successfully.");
  67. }
  68. }
  69.  
  70. private:
  71. std::string loadStmnt = "SELECT * from dp_items;";
  72.  
  73. };
  74.  
  75. class gossip_dp : public CreatureScript {
  76. public:
  77. gossip_dp() : CreatureScript("gossip_dp") { }
  78.  
  79. uint32 getDP(Player* player)
  80. {
  81. QueryResult dQuery = LoginDatabase.Query(getString(dpQuery, player->GetSession()->GetAccountId()).c_str());
  82. Field *field = dQuery->Fetch();
  83. return field[0].GetUInt32();
  84. }
  85.  
  86. bool OnGossipHello(Player* player, Creature* creature)
  87. {
  88. if (canRun)
  89. {
  90. if (player->IsInCombat())
  91. {
  92. player->GetSession()->SendAreaTriggerMessage("You are in combat");
  93. return false;
  94. }
  95.  
  96. AddGossipItemFor(player, GOSSIP_ICON_MONEY_BAG, getString(currentDP, getDP(player)).c_str(), GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 1);
  97. AddGossipItemFor(player, GOSSIP_ICON_VENDOR, "Preview Donation Items", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 7);
  98. AddGossipItemFor(player, GOSSIP_ICON_BATTLE, "Donation Weapons", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 2);
  99. AddGossipItemFor(player, GOSSIP_ICON_VENDOR, "Donation Armor", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 3);
  100. AddGossipItemFor(player, GOSSIP_ICON_VENDOR, "Donation Items(misc)", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 4);
  101. AddGossipItemFor(player, GOSSIP_ICON_TRAINER, "Donation Spells", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 5);
  102. AddGossipItemFor(player, GOSSIP_ICON_CHAT, "Nevermind", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 6);
  103. SendGossipMenuFor(player, 1, creature->GetGUID());
  104. return true;
  105. }
  106. else
  107. {
  108. return false;
  109. }
  110.  
  111. }
  112.  
  113. bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 actions)
  114. {
  115. player->PlayerTalkClass->ClearMenus();
  116. if (sender == GOSSIP_SENDER_MAIN) {
  117. switch (actions)
  118. {
  119. case GOSSIP_ACTION_INFO_DEF + 1:
  120. creature->Whisper(getString(currentDP, getDP(player)), LANG_UNIVERSAL, player, false);
  121. player->PlayerTalkClass->SendCloseGossip();
  122. break;
  123. case GOSSIP_ACTION_INFO_DEF + 2:
  124. for (unsigned int i = 0; i < items.size(); i++){
  125. if (items.at(i).getItemType() == 1)
  126. AddGossipItemFor(player, GOSSIP_ICON_BATTLE, getDPString(items.at(i).getName(), items.at(i).getDPCost(), items.at(i).getQuality()).c_str(), GOSSIP_SENDER_MAIN+1, items.at(i).getItemID());
  127. }
  128. AddGossipItemFor(player, GOSSIP_ICON_CHAT, "Back", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 8);
  129. SendGossipMenuFor(player, 1, creature->GetGUID());
  130. \
  131. break;
  132. case GOSSIP_ACTION_INFO_DEF + 3:
  133. for (unsigned int i = 0; i < items.size(); i++) {
  134. if (items.at(i).getItemType() == 2)
  135. AddGossipItemFor(player, GOSSIP_ICON_TABARD, getDPString(items.at(i).getName(), items.at(i).getDPCost(), items.at(i).getQuality()).c_str(), GOSSIP_SENDER_MAIN+1, items.at(i).getItemID());
  136. }
  137. AddGossipItemFor(player, GOSSIP_ICON_CHAT, "Back", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 8);
  138. SendGossipMenuFor(player, 1, creature->GetGUID());
  139. break;
  140. case GOSSIP_ACTION_INFO_DEF + 4:
  141. for (unsigned int i = 0; i < items.size(); i++){
  142. if (items.at(i).getItemType() == 3)
  143. AddGossipItemFor(player, GOSSIP_ICON_VENDOR, getDPString(items.at(i).getName(), items.at(i).getDPCost(), items.at(i).getQuality()).c_str(), GOSSIP_SENDER_MAIN+1, items.at(i).getItemID());
  144. }
  145. AddGossipItemFor(player, GOSSIP_ICON_CHAT, "Back", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 8);
  146. SendGossipMenuFor(player, 1, creature->GetGUID());
  147. break;
  148. case GOSSIP_ACTION_INFO_DEF + 5:
  149. for (unsigned int i = 0; i < items.size(); i++){
  150. if (items.at(i).getItemType() == 4)
  151. AddGossipItemFor(player, GOSSIP_ICON_TRAINER, getDPString(items.at(i).getName(), items.at(i).getDPCost(), items.at(i).getQuality()).c_str(), GOSSIP_SENDER_MAIN+2, items.at(i).getItemID());
  152. }
  153. AddGossipItemFor(player, GOSSIP_ICON_CHAT, "Back", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 8);
  154. SendGossipMenuFor(player, 1, creature->GetGUID());
  155. break;
  156. case GOSSIP_ACTION_INFO_DEF + 6:
  157. player->PlayerTalkClass->SendCloseGossip();
  158. break;
  159. case GOSSIP_ACTION_INFO_DEF + 7:
  160. player->PlayerTalkClass->SendCloseGossip();
  161. SendListInventory(player, creature);
  162. break;
  163. case GOSSIP_ACTION_INFO_DEF + 8:
  164. player->PlayerTalkClass->ClearMenus();
  165. OnGossipHello(player, creature);
  166. break;
  167. }
  168. }
  169. else if (sender == GOSSIP_SENDER_MAIN + 1)
  170. {
  171. uint32 dpCost = getDPFromID(actions);
  172. uint32 dpBAL = getDP(player);
  173. if (dpCost <= dpBAL)
  174. {
  175. if (player->AddItem(actions, 1)) {
  176. LoginDatabase.PExecute(accountQuery.c_str(), (dpBAL - dpCost), player->GetSession()->GetAccountId());
  177. player->GetSession()->SendNotification("SUCCESS - Your item has been added, thank you for your purchase.");
  178. }
  179. else
  180. {
  181. player->GetSession()->SendNotification("ERROR - An error occurred, item is unique or your bags are full. Contact staff with any questions.");
  182. }
  183. }
  184. else
  185. {
  186. creature->Whisper(notEnoughDP, LANG_UNIVERSAL, player, false);
  187. player->GetSession()->SendNotification("ERROR - Not enough DP for transaction.");
  188. }
  189. player->PlayerTalkClass->SendCloseGossip();
  190. }
  191. else if (sender == GOSSIP_SENDER_MAIN + 2)
  192. {
  193. uint32 dpCost = getDPFromID(actions);
  194. uint32 dpBAL = getDP(player);
  195. if (dpCost <= dpBAL)
  196. {
  197. if (!player->HasSpell(actions)){
  198. player->LearnSpell(actions, false, false);
  199. LoginDatabase.PExecute(accountQuery.c_str(), (dpBAL - dpCost), player->GetSession()->GetAccountId());
  200. player->GetSession()->SendNotification("SUCCESS - You have been taught the selected spell.");
  201.  
  202. }
  203. else
  204. {
  205. player->GetSession()->SendNotification("ERROR - You already know this spell.");
  206. }
  207. }
  208. else
  209. {
  210. creature->Whisper(notEnoughDP, LANG_UNIVERSAL, player, false);
  211. player->GetSession()->SendNotification("ERROR - Not enough DP for transaction.");
  212. }
  213. player->PlayerTalkClass->SendCloseGossip();
  214. }
  215.  
  216. return true;
  217. }
  218.  
  219. // Do NOT modify anything in SendListInventory, may break previewing or allow players to buy items
  220. void SendListInventory(Player *player, Creature *creature)
  221. {
  222. // remove fake death
  223. if (player->HasUnitState(UNIT_STATE_DIED))
  224. player->RemoveAurasByType(SPELL_AURA_FEIGN_DEATH);
  225. if (creature->HasUnitState(UNIT_STATE_MOVING))
  226. creature->StopMoving();
  227.  
  228. VendorItemData *vItems = new VendorItemData();
  229. for (uint32 i = 0; i < items.size(); i++) // Allows us to create a vendor window on the fly.
  230. {
  231. if (items.at(i).getItemType() != 4) // If the item is not a spell, lets put it in the vendor.
  232. vItems->AddItem(items.at(i).getItemID(), 0, 0, 59475);
  233. }
  234. if (!vItems)
  235. {
  236. WorldPacket data(SMSG_LIST_INVENTORY, 8 + 1 + 1);
  237. data << uint64(creature->GetGUID());
  238. data << uint8(0); // count == 0, next will be error code
  239. data << uint8(0); // "Vendor has no inventory"
  240. player->GetSession()->SendPacket(&data);
  241. return;
  242. }
  243. uint8 itemCount = vItems->GetItemCount();
  244. uint8 count = 0;
  245. WorldPacket data(SMSG_LIST_INVENTORY, 8 + 1 + itemCount * 8 * 4);
  246. data << uint64(creature->GetGUID());
  247. size_t countPos = data.wpos();
  248. data << uint8(count);
  249.  
  250. float discountMod = player->GetReputationPriceDiscount(creature);
  251. for (uint8 slot = 0; slot < itemCount; ++slot)
  252. {
  253. if (VendorItem const* item = vItems->GetItem(slot))
  254. {
  255. if (ItemTemplate const* itemTemplate = sObjectMgr->GetItemTemplate(item->item))
  256. {
  257. if (!(itemTemplate->AllowableClass & player->getClassMask()) && itemTemplate->Bonding == BIND_WHEN_PICKED_UP && !player->IsGameMaster())
  258. continue;
  259. // Only display items in vendor lists for the team the
  260. // player is on. If GM on, display all items.
  261. if (!player->IsGameMaster() && ((itemTemplate->Flags2 & ITEM_FLAG2_FACTION_HORDE && player->GetTeam() == ALLIANCE) || (itemTemplate->Flags2 == ITEM_FLAG2_FACTION_ALLIANCE && player->GetTeam() == HORDE)))
  262. continue;
  263.  
  264.  
  265. // reputation discount
  266. int32 price = item->IsGoldRequired(itemTemplate) ? uint32(floor(itemTemplate->BuyPrice * discountMod)) : 0;
  267.  
  268. data << uint32(slot + 1); // client expects counting to start at 1
  269. data << uint32(item->item);
  270. data << uint32(itemTemplate->DisplayInfoID);
  271. data << int32(0);
  272. data << uint32(price);
  273. data << uint32(itemTemplate->MaxDurability);
  274. data << uint32(itemTemplate->BuyCount);
  275. data << uint32(item->ExtendedCost);
  276.  
  277. if (++count >= MAX_VENDOR_ITEMS)
  278. break;
  279. }
  280. }
  281. }
  282.  
  283. if (count == 0)
  284. {
  285. data << uint8(0);
  286. player->GetSession()->SendPacket(&data);
  287. return;
  288. }
  289.  
  290. data.put<uint8>(countPos, count);
  291. player->GetSession()->SendPacket(&data);
  292.  
  293. }
  294. private:
  295. //Do not edit the Queries/constants below, they're set up 100%.
  296. std::string accountQuery = "UPDATE account SET dp_balance = %u WHERE id = %u;";
  297. std::string currentDP = "Current DP: ";
  298. std::string notEnoughDP = "You do not have enough DP, you can acquire more DP by donating on our website.";
  299. std::string dpQuery = "SELECT dp_balance FROM account WHERE id=";
  300. };
  301.  
  302. //Do NOT modify any of the below functions unless you are sure you know what you're doing.
  303. uint32 getDPFromID(uint32 item_id)
  304. {
  305. //Not the most efficient loop. Let me know if you are using a large amount of items/spells
  306. //and I can implement a more efficient search.
  307. uint32 dpCost = 99999;
  308. for (unsigned int i = 0; i < items.size(); i++)
  309. if (item_id == items.at(i).getItemID())
  310. dpCost = items.at(i).getDPCost();
  311. return dpCost;
  312. }
  313. std::string getString(std::string string, uint32 number)
  314. {
  315. std::ostringstream oss;
  316. oss << string << number;
  317. return oss.str();
  318. }
  319. std::string getDPString(std::string string, uint32 number, uint8 quality)
  320. {
  321.  
  322. //Colors currently disabled.
  323. /*std::string color;
  324. switch (quality)
  325. {
  326. case 0:
  327. color = "|cff9d9d9d";
  328. break;
  329. case 1:
  330. color = "|cffffffff";
  331. break;
  332. case 2:
  333. color = "|cff47b247";
  334. break;
  335. case 3:
  336. color = "|cff00003d";
  337. break;
  338. case 5:
  339. color = "|cff331f00";
  340. break;
  341. default:
  342. color = "|cff3d003d";
  343. break;
  344. }*/
  345. std::ostringstream oss;
  346. //oss << color << string << " - (" << number << "DP)"; //With Color
  347. oss << string << " - (" << number << "DP)"; //Without color
  348. return oss.str();
  349. }
  350.  
  351.  
  352. void AddSC_DPSystem()
  353. {
  354. new load_dp();
  355. new gossip_dp();
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement