Advertisement
Guest User

Untitled

a guest
Apr 4th, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.68 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. //////////////////////////////////////////////////////////////////////
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU General Public License
  6. // as published by the Free Software Foundation; either version 2
  7. // of the License, or (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, write to the Free Software Foundation,
  16. // Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17. //////////////////////////////////////////////////////////////////////
  18.  
  19. #include "otpch.h"
  20.  
  21. #include "iomarket.h"
  22. #include "iologindata.h"
  23. #include "configmanager.h"
  24. #include "item.h"
  25.  
  26. extern ConfigManager g_config;
  27.  
  28. MarketOfferList IOMarket::getActiveOffers(MarketAction_t action, uint16_t itemId)
  29. {
  30.     Database* db = Database::getInstance();
  31.     DBQuery query;
  32.     query << "SELECT `id`, `player_id`, `amount`, `price`, `created`, `anonymous` FROM `market_offers` WHERE `sale` = "
  33.         << action << " AND `itemtype` = " << itemId << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  34.  
  35.     DBResult* result;
  36.     if(!(result = db->storeQuery(query.str())))
  37.         return MarketOfferList();
  38.  
  39.     MarketOfferList offerList;
  40.     do
  41.     {
  42.         MarketOffer offer;
  43.         offer.amount = result->getDataInt("amount");
  44.         offer.price = result->getDataInt("price");
  45.         offer.timestamp = result->getDataInt("created") + g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION);
  46.         offer.counter = result->getDataInt("id") & 0xFFFF;
  47.  
  48.         if(!result->getDataInt("anonymous"))
  49.         {
  50.             IOLoginData::getInstance()->getNameByGuid(result->getDataInt("player_id"), offer.playerName);
  51.             if(offer.playerName.empty())
  52.                 offer.playerName = "Anonymous";
  53.         }
  54.         else
  55.             offer.playerName = "Anonymous";
  56.  
  57.         offerList.push_back(offer);
  58.     }
  59.     while(result->next());
  60.     result->free();
  61.     return offerList;
  62. }
  63.  
  64. MarketOfferList IOMarket::getOwnOffers(MarketAction_t action, uint32_t playerId)
  65. {
  66.     Database* db = Database::getInstance();
  67.     DBQuery query;
  68.     query << "SELECT `id`, `amount`, `price`, `created`, `anonymous`, `itemtype` FROM `market_offers` WHERE `player_id` = "
  69.         << playerId << " AND `sale` = " << action << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  70.  
  71.     DBResult* result;
  72.     if(!(result = db->storeQuery(query.str())))
  73.         return MarketOfferList();
  74.  
  75.     MarketOfferList offerList;
  76.     do
  77.     {
  78.         MarketOffer offer;
  79.         offer.amount = result->getDataInt("amount");
  80.         offer.price = result->getDataInt("price");
  81.         offer.timestamp = result->getDataInt("created") + g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION);
  82.         offer.counter = result->getDataInt("id") & 0xFFFF;
  83.         offer.itemId = result->getDataInt("itemtype");
  84.  
  85.         offerList.push_back(offer);
  86.     }
  87.     while(result->next());
  88.     result->free();
  89.     return offerList;
  90. }
  91.  
  92. HistoryMarketOfferList IOMarket::getOwnHistory(MarketAction_t action, uint32_t playerId)
  93. {
  94.     Database* db = Database::getInstance();
  95.     DBQuery query;
  96.     query << "SELECT `id`, `itemtype`, `amount`, `price`, `expires_at`, `state` FROM `market_history` WHERE `player_id` = "
  97.         << playerId << " AND `sale` = " << action << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  98.  
  99.     DBResult* result;
  100.     if(!(result = db->storeQuery(query.str())))
  101.         return HistoryMarketOfferList();
  102.  
  103.     HistoryMarketOfferList offerList;
  104.     do
  105.     {
  106.         HistoryMarketOffer offer;
  107.         offer.itemId = result->getDataInt("itemtype");
  108.         offer.amount = result->getDataInt("amount");
  109.         offer.price = result->getDataInt("price");
  110.         offer.timestamp = result->getDataInt("expires_at");
  111.  
  112.         MarketOfferState_t offerState = (MarketOfferState_t)result->getDataInt("state");
  113.         if(offerState == OFFERSTATE_ACCEPTEDEX)
  114.             offerState = OFFERSTATE_ACCEPTED;
  115.  
  116.         offer.state = offerState;
  117.         offerList.push_back(offer);
  118.     }
  119.     while(result->next());
  120.     result->free();
  121.     return offerList;
  122. }
  123.  
  124. ExpiredMarketOfferList IOMarket::getExpiredOffers(MarketAction_t action)
  125. {
  126.     Database* db = Database::getInstance();
  127.     DBQuery query;
  128.     query << "SELECT `id`, `amount`, `price`, `itemtype`, `player_id` FROM `market_offers` WHERE `sale` = " << action << " AND `created` <= "
  129.         << (time(NULL) - g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION)) << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  130.  
  131.     DBResult* result;
  132.     if(!(result = db->storeQuery(query.str())))
  133.         return ExpiredMarketOfferList();
  134.  
  135.     ExpiredMarketOfferList offerList;
  136.     do
  137.     {
  138.         ExpiredMarketOffer offer;
  139.         offer.id = result->getDataInt("id");
  140.         offer.amount = result->getDataInt("amount");
  141.         offer.price = result->getDataInt("price");
  142.         offer.itemId = result->getDataInt("itemtype");
  143.         offer.playerId = result->getDataInt("player_id");
  144.  
  145.         offerList.push_back(offer);
  146.     }
  147.     while(result->next());
  148.     result->free();
  149.     return offerList;
  150. }
  151.  
  152. int32_t IOMarket::getPlayerOfferCount(uint32_t playerId)
  153. {
  154.     Database* db = Database::getInstance();
  155.     DBQuery query;
  156.     query << "SELECT COUNT(*) AS `count` FROM `market_offers` WHERE `player_id` = " << playerId << " AND `world_id` = "
  157.         << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  158.  
  159.     DBResult* result;
  160.     if(!(result = db->storeQuery(query.str())))
  161.         return -1;
  162.  
  163.     int32_t tmp = result->getDataInt("count");
  164.     result->free();
  165.     return tmp;
  166. }
  167.  
  168. MarketOfferEx IOMarket::getOfferById(uint32_t id)
  169. {
  170.     Database* db = Database::getInstance();
  171.     DBQuery query;
  172.     query << "SELECT `id`, `sale`, `itemtype`, `amount`, `created`, `price`, `player_id`, `anonymous` FROM `market_offers` WHERE `id` = " << id
  173.         << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  174.  
  175.     DBResult* result;
  176.     if(!(result = db->storeQuery(query.str())))
  177.         return MarketOfferEx();
  178.  
  179.     MarketOfferEx offer;
  180.     offer.type = (MarketAction_t)result->getDataInt("sale");
  181.     offer.amount = result->getDataInt("amount");
  182.     offer.counter = result->getDataInt("id") & 0xFFFF;
  183.     offer.timestamp = result->getDataInt("created");
  184.     offer.price = result->getDataInt("price");
  185.     offer.itemId = result->getDataInt("itemtype");
  186.  
  187.     int32_t playerId = result->getDataInt("player_id");
  188.     offer.playerId = playerId;
  189.     if(!result->getDataInt("anonymous"))
  190.     {
  191.         IOLoginData::getInstance()->getNameByGuid(playerId, offer.playerName);
  192.         if(offer.playerName.empty())
  193.             offer.playerName = "Anonymous";
  194.     }
  195.     else
  196.         offer.playerName = "Anonymous";
  197.  
  198.     result->free();
  199.     return offer;
  200. }
  201.  
  202. uint32_t IOMarket::getOfferIdByCounter(uint32_t timestamp, uint16_t counter)
  203. {
  204.     Database* db = Database::getInstance();
  205.     DBQuery query;
  206.     query << "SELECT `id` FROM `market_offers` WHERE `created` = " << (timestamp - g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION))
  207.         << " AND (`id` & 65535) = " << counter << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " LIMIT 1;";
  208.  
  209.     DBResult* result;
  210.     if(!(result = db->storeQuery(query.str())))
  211.         return 0;
  212.  
  213.     uint32_t tmp = result->getDataInt("id");
  214.     result->free();
  215.     return tmp;
  216. }
  217.  
  218. void IOMarket::createOffer(uint32_t playerId, MarketAction_t action, uint32_t itemId, uint16_t amount, uint32_t price, bool anonymous)
  219. {
  220.     DBQuery query;
  221.     query << "SELECT @atrybuty:=attributes FROM player_depotitems WHERE player_id = " << playerId << " AND itemtype = " << itemId
  222.         << ";INSERT INTO `market_offers` (`player_id`, `world_id`, `sale`, `itemtype`, `amount`, `price`, `created`, `anonymous`) VALUES (" << playerId
  223.         << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << action << ", " << itemId << ", " << amount << ", " << price
  224.         << ",  @czas , " << anonymous << ");UPDATE market_offers SET attributes = @atrybuty WHERE player_id = " << playerId
  225.         << " AND itemtype = " << itemId << ";";
  226.     Database::getInstance()->query(query.str());
  227. }
  228.  
  229. void IOMarket::acceptOffer(uint32_t offerId, uint16_t amount)
  230. {
  231.     DBQuery query;
  232.     query << "UPDATE `market_offers` SET `amount` = `amount` - " << amount << " WHERE `id` = " << offerId << " AND `world_id` = "
  233.         << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  234.     Database::getInstance()->query(query.str());
  235. }
  236.  
  237. void IOMarket::appendHistory(uint32_t playerId, MarketAction_t type, uint16_t itemId, uint16_t amount, uint32_t price, time_t timestamp, MarketOfferState_t state)
  238. {
  239.     DBQuery query;
  240.     query << "INSERT INTO `market_history` (`player_id`, `world_id`, `sale`, `itemtype`, `amount`, `price`, `expires_at`, `inserted`, `state`) VALUES "
  241.         << "(" << playerId << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << type << ", " << itemId << ", " << amount << ", " << price << ", " << timestamp << ", " << time(NULL) << ", " << state << ");";
  242.     Database::getInstance()->query(query.str());
  243. }
  244.  
  245. void IOMarket::moveOfferToHistory(uint32_t offerId, MarketOfferState_t state)
  246. {
  247.     Database* db = Database::getInstance();
  248.     DBQuery query;
  249.     query << "SELECT `player_id`, `sale`, `itemtype`, `amount`, `price`, `created` FROM `market_offers` WHERE `id` = " << offerId << " AND `world_id` = "
  250.         << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  251.  
  252.     DBResult* result;
  253.     if(!(result = db->storeQuery(query.str())))
  254.         return;
  255.  
  256.     query.str("");
  257.     query << "DELETE FROM `market_offers` WHERE `id` = " << offerId << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  258.     if(!db->query(query.str()))
  259.     {
  260.         result->free();
  261.         return;
  262.     }
  263.  
  264.     appendHistory(result->getDataInt("player_id"), (MarketAction_t)result->getDataInt("sale"), result->getDataInt("itemtype"), result->getDataInt("amount"),
  265.         result->getDataInt("price"), result->getDataInt("created") + g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION), state);
  266.     result->free();
  267. }
  268.  
  269. void IOMarket::clearOldHistory()
  270. {
  271.     DBQuery query;
  272.     query << "DELETE FROM `market_history` WHERE `inserted` <= " << (time(NULL) - g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION)) << " AND `world_id` = "
  273.         << g_config.getNumber(ConfigManager::WORLD_ID) << ";";
  274.     Database::getInstance()->query(query.str());
  275. }
  276.  
  277. void IOMarket::updateStatistics()
  278. {
  279.     Database* db = Database::getInstance();
  280.     DBQuery query;
  281.     query << "SELECT `sale`, `itemtype`, COUNT(`price`) AS `num`, MIN(`price`) AS `min`, MAX(`price`) AS `max`, SUM(`price`) AS `sum` FROM `market_history` WHERE `state` = "
  282.         << OFFERSTATE_ACCEPTED << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " GROUP BY `itemtype`, `sale`;";
  283.  
  284.     DBResult* result;
  285.     if(!(result = db->storeQuery(query.str())))
  286.         return;
  287.  
  288.     do
  289.     {
  290.         MarketStatistics* statistics;
  291.         if(result->getDataInt("sale") == MARKETACTION_BUY)
  292.             statistics = &purchaseStatistics[result->getDataInt("itemtype")];
  293.         else
  294.             statistics = &saleStatistics[result->getDataInt("itemtype")];
  295.  
  296.         statistics->numTransactions = result->getDataInt("num");
  297.         statistics->lowestPrice = result->getDataInt("min");
  298.         statistics->totalPrice = result->getDataLong("sum");
  299.         statistics->highestPrice = result->getDataInt("max");
  300.     }
  301.     while(result->next());
  302.     result->free();
  303. }
  304.  
  305. MarketStatistics* IOMarket::getPurchaseStatistics(uint16_t itemId)
  306. {
  307.     std::map<uint16_t, MarketStatistics>::iterator it = purchaseStatistics.find(itemId);
  308.     if(it == purchaseStatistics.end())
  309.         return NULL;
  310.  
  311.     return &it->second;
  312. }
  313.  
  314. MarketStatistics* IOMarket::getSaleStatistics(uint16_t itemId)
  315. {
  316.     std::map<uint16_t, MarketStatistics>::iterator it = saleStatistics.find(itemId);
  317.     if(it == saleStatistics.end())
  318.         return NULL;
  319.  
  320.     return &it->second;
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement