Advertisement
Guest User

PythonShop.cpp

a guest
May 28th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.64 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "PythonShop.h"
  3.  
  4. #include "PythonNetworkStream.h"
  5.  
  6. //BOOL CPythonShop::GetSlotItemID(DWORD dwSlotPos, DWORD* pdwItemID)
  7. //{
  8. //  if (!CheckSlotIndex(dwSlotPos))
  9. //      return FALSE;
  10. //  const TShopItemData * itemData;
  11. //  if (!GetItemData(dwSlotPos, &itemData))
  12. //      return FALSE;
  13. //  *pdwItemID=itemData->vnum;
  14. //  return TRUE;
  15. //}
  16. void CPythonShop::SetTabCoinType(BYTE tabIdx, BYTE coinType)
  17. {
  18.     if (tabIdx >= m_bTabCount)
  19.     {  
  20.         TraceError("Out of Index. tabIdx(%d) must be less than %d.", tabIdx, SHOP_TAB_COUNT_MAX);
  21.         return;
  22.     }
  23.     m_aShoptabs[tabIdx].coinType = coinType;
  24. }
  25.  
  26. BYTE CPythonShop::GetTabCoinType(BYTE tabIdx)
  27. {
  28.     if (tabIdx >= m_bTabCount)
  29.     {
  30.         TraceError("Out of Index. tabIdx(%d) must be less than %d.", tabIdx, SHOP_TAB_COUNT_MAX);
  31.         return 0xff;
  32.     }
  33.     return m_aShoptabs[tabIdx].coinType;
  34. }
  35.  
  36. void CPythonShop::SetTabName(BYTE tabIdx, const char* name)
  37. {
  38.     if (tabIdx >= m_bTabCount)
  39.     {  
  40.         TraceError("Out of Index. tabIdx(%d) must be less than %d.", tabIdx, SHOP_TAB_COUNT_MAX);
  41.         return;
  42.     }
  43.     m_aShoptabs[tabIdx].name = name;
  44. }
  45.  
  46. const char* CPythonShop::GetTabName(BYTE tabIdx)
  47. {
  48.     if (tabIdx >= m_bTabCount)
  49.     {
  50.         TraceError("Out of Index. tabIdx(%d) must be less than %d.", tabIdx, SHOP_TAB_COUNT_MAX);
  51.         return NULL;
  52.     }
  53.  
  54.     return m_aShoptabs[tabIdx].name.c_str();
  55. }
  56.  
  57. void CPythonShop::SetItemData(DWORD dwIndex, const TShopItemData & c_rShopItemData)
  58. {
  59.     BYTE tabIdx = dwIndex / SHOP_HOST_ITEM_MAX_NUM;
  60.     DWORD dwSlotPos = dwIndex % SHOP_HOST_ITEM_MAX_NUM;
  61.    
  62.     SetItemData(tabIdx, dwSlotPos, c_rShopItemData);
  63. }
  64.  
  65. BOOL CPythonShop::GetItemData(DWORD dwIndex, const TShopItemData ** c_ppItemData)
  66. {
  67.     BYTE tabIdx = dwIndex / SHOP_HOST_ITEM_MAX_NUM;
  68.     DWORD dwSlotPos = dwIndex % SHOP_HOST_ITEM_MAX_NUM;
  69.  
  70.     return GetItemData(tabIdx, dwSlotPos, c_ppItemData);
  71. }
  72.  
  73. void CPythonShop::SetItemData(BYTE tabIdx, DWORD dwSlotPos, const TShopItemData & c_rShopItemData)
  74. {
  75.     if (tabIdx >= SHOP_TAB_COUNT_MAX || dwSlotPos >= SHOP_HOST_ITEM_MAX_NUM)
  76.     {
  77.         TraceError("Out of Index. tabIdx(%d) must be less than %d. dwSlotPos(%d) must be less than %d", tabIdx, SHOP_TAB_COUNT_MAX, dwSlotPos, SHOP_HOST_ITEM_MAX_NUM);
  78.         return;
  79.     }
  80.  
  81.     m_aShoptabs[tabIdx].items[dwSlotPos] = c_rShopItemData;
  82. }
  83.  
  84. BOOL CPythonShop::GetItemData(BYTE tabIdx, DWORD dwSlotPos, const TShopItemData ** c_ppItemData)
  85. {
  86.     if (tabIdx >= SHOP_TAB_COUNT_MAX || dwSlotPos >= SHOP_HOST_ITEM_MAX_NUM)
  87.     {
  88.         TraceError("Out of Index. tabIdx(%d) must be less than %d. dwSlotPos(%d) must be less than %d", tabIdx, SHOP_TAB_COUNT_MAX, dwSlotPos, SHOP_HOST_ITEM_MAX_NUM);
  89.         return FALSE;
  90.     }
  91.  
  92.     *c_ppItemData = &m_aShoptabs[tabIdx].items[dwSlotPos];
  93.  
  94.     return TRUE;
  95. }
  96. //
  97. //BOOL CPythonShop::CheckSlotIndex(DWORD dwSlotPos)
  98. //{
  99. //  if (dwSlotPos >= SHOP_HOST_ITEM_MAX_NUM * SHOP_TAB_COUNT_MAX)
  100. //      return FALSE;
  101. //
  102. //  return TRUE;
  103. //}
  104.  
  105. void CPythonShop::ClearPrivateShopStock()
  106. {
  107.     m_PrivateShopItemStock.clear();
  108. }
  109. void CPythonShop::AddPrivateShopItemStock(TItemPos ItemPos, BYTE dwDisplayPos, DWORD dwPrice)
  110. {
  111.     DelPrivateShopItemStock(ItemPos);
  112.  
  113.     TShopItemTable SellingItem;
  114.     SellingItem.vnum = 0;
  115.     SellingItem.count = 0;
  116.     SellingItem.pos = ItemPos;
  117.     SellingItem.price = dwPrice;
  118.     SellingItem.display_pos = dwDisplayPos;
  119.     m_PrivateShopItemStock.insert(make_pair(ItemPos, SellingItem));
  120. }
  121. void CPythonShop::DelPrivateShopItemStock(TItemPos ItemPos)
  122. {
  123.     if (m_PrivateShopItemStock.end() == m_PrivateShopItemStock.find(ItemPos))
  124.         return;
  125.  
  126.     m_PrivateShopItemStock.erase(ItemPos);
  127. }
  128. int CPythonShop::GetPrivateShopItemPrice(TItemPos ItemPos)
  129. {
  130.     TPrivateShopItemStock::iterator itor = m_PrivateShopItemStock.find(ItemPos);
  131.  
  132.     if (m_PrivateShopItemStock.end() == itor)
  133.         return 0;
  134.  
  135.     TShopItemTable & rShopItemTable = itor->second;
  136.     return rShopItemTable.price;
  137. }
  138. struct ItemStockSortFunc
  139. {
  140.     bool operator() (TShopItemTable & rkLeft, TShopItemTable & rkRight)
  141.     {
  142.         return rkLeft.display_pos < rkRight.display_pos;
  143.     }
  144. };
  145. #ifdef ENABLE_OFFLINE_SHOP
  146. void CPythonShop::BuildPrivateShop(const char * c_szName, DWORD days)
  147. #else
  148. void CPythonShop::BuildPrivateShop(const char * c_szName)
  149. #endif
  150. {
  151.     std::vector<TShopItemTable> ItemStock;
  152.     ItemStock.reserve(m_PrivateShopItemStock.size());
  153.  
  154.     TPrivateShopItemStock::iterator itor = m_PrivateShopItemStock.begin();
  155.     for (; itor != m_PrivateShopItemStock.end(); ++itor)
  156.     {
  157.         ItemStock.push_back(itor->second);
  158.     }
  159.  
  160.     std::sort(ItemStock.begin(), ItemStock.end(), ItemStockSortFunc());
  161. #ifdef ENABLE_OFFLINE_SHOP
  162.     CPythonNetworkStream::Instance().SendBuildPrivateShopPacket(c_szName, ItemStock, days);
  163. #else
  164.     CPythonNetworkStream::Instance().SendBuildPrivateShopPacket(c_szName, ItemStock);
  165. #endif
  166. }
  167.  
  168. void CPythonShop::Open(BOOL isPrivateShop, BOOL isMainPrivateShop)
  169. {
  170.     m_isShoping = TRUE;
  171.     m_isPrivateShop = isPrivateShop;
  172.     m_isMainPlayerPrivateShop = isMainPrivateShop;
  173. }
  174.  
  175. void CPythonShop::Close()
  176. {
  177.     m_isShoping = FALSE;
  178.     m_isPrivateShop = FALSE;
  179.     m_isMainPlayerPrivateShop = FALSE;
  180. }
  181.  
  182. BOOL CPythonShop::IsOpen()
  183. {
  184.     return m_isShoping;
  185. }
  186.  
  187. BOOL CPythonShop::IsPrivateShop()
  188. {
  189.     return m_isPrivateShop;
  190. }
  191.  
  192. BOOL CPythonShop::IsMainPlayerPrivateShop()
  193. {
  194.     return m_isMainPlayerPrivateShop;
  195. }
  196.  
  197. void CPythonShop::Clear()
  198. {
  199.     m_isShoping = FALSE;
  200.     m_isPrivateShop = FALSE;
  201.     m_isMainPlayerPrivateShop = FALSE;
  202.     ClearPrivateShopStock();
  203.     m_bTabCount = 1;
  204.  
  205.     for (int i = 0; i < SHOP_TAB_COUNT_MAX; i++)
  206.         memset (m_aShoptabs[i].items, 0, sizeof(TShopItemData) * SHOP_HOST_ITEM_MAX_NUM);
  207. }
  208.  
  209. CPythonShop::CPythonShop(void)
  210. {
  211.     Clear();
  212. }
  213.  
  214. CPythonShop::~CPythonShop(void)
  215. {
  216. }
  217.  
  218. PyObject * shopOpen(PyObject * poSelf, PyObject * poArgs)
  219. {
  220.     int isPrivateShop = false;
  221.     PyTuple_GetInteger(poArgs, 0, &isPrivateShop);
  222.     int isMainPrivateShop = false;
  223.     PyTuple_GetInteger(poArgs, 1, &isMainPrivateShop);
  224.  
  225.     CPythonShop& rkShop=CPythonShop::Instance();
  226.     rkShop.Open(isPrivateShop, isMainPrivateShop);
  227.     return Py_BuildNone();
  228. }
  229.  
  230. PyObject * shopClose(PyObject * poSelf, PyObject * poArgs)
  231. {
  232.     CPythonShop& rkShop=CPythonShop::Instance();
  233.     rkShop.Close();
  234.     return Py_BuildNone();
  235. }
  236.  
  237. PyObject * shopIsOpen(PyObject * poSelf, PyObject * poArgs)
  238. {
  239.     CPythonShop& rkShop=CPythonShop::Instance();
  240.     return Py_BuildValue("i", rkShop.IsOpen());
  241. }
  242.  
  243. PyObject * shopIsPrviateShop(PyObject * poSelf, PyObject * poArgs)
  244. {
  245.     CPythonShop& rkShop=CPythonShop::Instance();
  246.     return Py_BuildValue("i", rkShop.IsPrivateShop());
  247. }
  248.  
  249. PyObject * shopIsMainPlayerPrivateShop(PyObject * poSelf, PyObject * poArgs)
  250. {
  251.     CPythonShop& rkShop=CPythonShop::Instance();
  252.     return Py_BuildValue("i", rkShop.IsMainPlayerPrivateShop());
  253. }
  254.  
  255. PyObject * shopGetItemID(PyObject * poSelf, PyObject * poArgs)
  256. {
  257.     int nPos;
  258.     if (!PyTuple_GetInteger(poArgs, 0, &nPos))
  259.         return Py_BuildException();
  260.  
  261.     const TShopItemData * c_pItemData;
  262.     if (CPythonShop::Instance().GetItemData(nPos, &c_pItemData))
  263.         return Py_BuildValue("i", c_pItemData->vnum);
  264.  
  265.     return Py_BuildValue("i", 0);
  266. }
  267.  
  268. PyObject * shopGetItemCount(PyObject * poSelf, PyObject * poArgs)
  269. {
  270.     int iIndex;
  271.     if (!PyTuple_GetInteger(poArgs, 0, &iIndex))
  272.         return Py_BuildException();
  273.  
  274.     const TShopItemData * c_pItemData;
  275.     if (CPythonShop::Instance().GetItemData(iIndex, &c_pItemData))
  276.         return Py_BuildValue("i", c_pItemData->count);
  277.  
  278.     return Py_BuildValue("i", 0);
  279. }
  280.  
  281. PyObject * shopGetItemPrice(PyObject * poSelf, PyObject * poArgs)
  282. {
  283.     int iIndex;
  284.     if (!PyTuple_GetInteger(poArgs, 0, &iIndex))
  285.         return Py_BuildException();
  286.  
  287.     const TShopItemData * c_pItemData;
  288.     if (CPythonShop::Instance().GetItemData(iIndex, &c_pItemData))
  289.         return Py_BuildValue("i", c_pItemData->price);
  290.  
  291.     return Py_BuildValue("i", 0);
  292. }
  293.  
  294. PyObject * shopGetItemMetinSocket(PyObject * poSelf, PyObject * poArgs)
  295. {
  296.     int iIndex;
  297.     if (!PyTuple_GetInteger(poArgs, 0, &iIndex))
  298.         return Py_BuildException();
  299.     int iMetinSocketIndex;
  300.     if (!PyTuple_GetInteger(poArgs, 1, &iMetinSocketIndex))
  301.         return Py_BuildException();
  302.  
  303.     const TShopItemData * c_pItemData;
  304.     if (CPythonShop::Instance().GetItemData(iIndex, &c_pItemData))
  305.         return Py_BuildValue("i", c_pItemData->alSockets[iMetinSocketIndex]);
  306.  
  307.     return Py_BuildValue("i", 0);
  308. }
  309.  
  310. PyObject * shopGetItemAttribute(PyObject * poSelf, PyObject * poArgs)
  311. {
  312.     int iIndex;
  313.     if (!PyTuple_GetInteger(poArgs, 0, &iIndex))
  314.         return Py_BuildException();
  315.     int iAttrSlotIndex;
  316.     if (!PyTuple_GetInteger(poArgs, 1, &iAttrSlotIndex))
  317.         return Py_BuildException();
  318.  
  319.     if (iAttrSlotIndex >= 0 && iAttrSlotIndex < ITEM_ATTRIBUTE_SLOT_MAX_NUM)
  320.     {
  321.         const TShopItemData * c_pItemData;
  322.         if (CPythonShop::Instance().GetItemData(iIndex, &c_pItemData))
  323.             return Py_BuildValue("ii", c_pItemData->aAttr[iAttrSlotIndex].bType, c_pItemData->aAttr[iAttrSlotIndex].sValue);
  324.     }
  325.  
  326.     return Py_BuildValue("ii", 0, 0);
  327. }
  328.  
  329. PyObject * shopClearPrivateShopStock(PyObject * poSelf, PyObject * poArgs)
  330. {
  331.     CPythonShop::Instance().ClearPrivateShopStock();
  332.     return Py_BuildNone();
  333. }
  334. #ifdef ENABLE_FULL_YANG
  335. void CPythonShop::AddPrivateShopItemStock(TItemPos ItemPos, BYTE dwDisplayPos, long long dwPrice)
  336. #else
  337. void CPythonShop::AddPrivateShopItemStock(TItemPos ItemPos, BYTE dwDisplayPos, DWORD dwPrice)
  338. #endif
  339. {
  340.     DelPrivateShopItemStock(ItemPos);
  341.  
  342.     TShopItemTable SellingItem;
  343.     SellingItem.vnum = 0;
  344.     SellingItem.count = 0;
  345.     SellingItem.pos = ItemPos;
  346.     SellingItem.price = dwPrice;
  347.     SellingItem.display_pos = dwDisplayPos;
  348.     m_PrivateShopItemStock.insert(make_pair(ItemPos, SellingItem));
  349. }
  350. PyObject * shopDelPrivateShopItemStock(PyObject * poSelf, PyObject * poArgs)
  351. {
  352.     BYTE bItemWindowType;
  353.     if (!PyTuple_GetInteger(poArgs, 0, &bItemWindowType))
  354.         return Py_BuildException();
  355.     WORD wItemSlotIndex;
  356.     if (!PyTuple_GetInteger(poArgs, 1, &wItemSlotIndex))
  357.         return Py_BuildException();
  358.  
  359.     CPythonShop::Instance().DelPrivateShopItemStock(TItemPos(bItemWindowType, wItemSlotIndex));
  360.     return Py_BuildNone();
  361. }
  362. #ifdef ENABLE_FULL_YANG
  363. long long CPythonShop::GetPrivateShopItemPrice(TItemPos ItemPos)
  364. #else
  365. int CPythonShop::GetPrivateShopItemPrice(TItemPos ItemPos)
  366. #endif
  367. {
  368.     TPrivateShopItemStock::iterator itor = m_PrivateShopItemStock.find(ItemPos);
  369.  
  370.     if (m_PrivateShopItemStock.end() == itor)
  371.         return 0;
  372.  
  373.     TShopItemTable & rShopItemTable = itor->second;
  374.     return rShopItemTable.price;
  375. }
  376. PyObject * shopBuildPrivateShop(PyObject * poSelf, PyObject * poArgs)
  377. {
  378.     char * szName;
  379.     if (!PyTuple_GetString(poArgs, 0, &szName))
  380.         return Py_BuildException();
  381. #ifdef ENABLE_OFFLINE_SHOP
  382.     int days;
  383.     if (!PyTuple_GetInteger(poArgs, 1, &days))
  384.         return Py_BuildException();
  385.     CPythonShop::Instance().BuildPrivateShop(szName,days);
  386. #else
  387.     CPythonShop::Instance().BuildPrivateShop(szName);
  388. #endif
  389.     return Py_BuildNone();
  390. }
  391.  
  392. PyObject * shopGetItemPrice(PyObject * poSelf, PyObject * poArgs)
  393. {
  394.     int iIndex;
  395.     if (!PyTuple_GetInteger(poArgs, 0, &iIndex))
  396.         return Py_BuildException();
  397.  
  398.     const TShopItemData * c_pItemData;
  399.     if (CPythonShop::Instance().GetItemData(iIndex, &c_pItemData))
  400. #ifdef ENABLE_FULL_YANG
  401.         return Py_BuildValue("L", c_pItemData->price);
  402. #else
  403.         return Py_BuildValue("i", c_pItemData->price);
  404. #endif
  405.  
  406.     return Py_BuildValue("i", 0);
  407. }
  408. PyObject * shopGetPrivateShopItemPrice(PyObject * poSelf, PyObject * poArgs)
  409. {
  410.     BYTE bItemWindowType;
  411.     if (!PyTuple_GetInteger(poArgs, 0, &bItemWindowType))
  412.         return Py_BuildException();
  413.     WORD wItemSlotIndex;
  414.     if (!PyTuple_GetInteger(poArgs, 1, &wItemSlotIndex))
  415.         return Py_BuildException();
  416. #ifdef ENABLE_FULL_YANG
  417.     return Py_BuildValue("L", CPythonShop::Instance().GetPrivateShopItemPrice(TItemPos(bItemWindowType, wItemSlotIndex)));
  418. #else
  419.     return Py_BuildValue("i", CPythonShop::Instance().GetPrivateShopItemPrice(TItemPos(bItemWindowType, wItemSlotIndex)));
  420. #endif
  421.  
  422.     return Py_BuildValue("i", 0);
  423. }
  424. PyObject * shopAddPrivateShopItemStock(PyObject * poSelf, PyObject * poArgs)
  425. {
  426.     BYTE bItemWindowType;
  427.     if (!PyTuple_GetInteger(poArgs, 0, &bItemWindowType))
  428.         return Py_BuildException();
  429.     WORD wItemSlotIndex;
  430.     if (!PyTuple_GetInteger(poArgs, 1, &wItemSlotIndex))
  431.         return Py_BuildException();
  432.     int iDisplaySlotIndex;
  433.     if (!PyTuple_GetInteger(poArgs, 2, &iDisplaySlotIndex))
  434.         return Py_BuildException();
  435.  
  436. #ifdef ENABLE_FULL_YANG
  437.     PyObject* val;
  438.     if (!PyTuple_GetObject(poArgs, 3, &val))
  439.         return Py_BuildException();
  440.     long long iPrice = PyLong_AsLongLong(val);
  441. #else
  442.     int iPrice;
  443.     if (!PyTuple_GetInteger(poArgs, 3, &iPrice))
  444.         return Py_BuildException();
  445. #endif
  446.  
  447.     CPythonShop::Instance().AddPrivateShopItemStock(TItemPos(bItemWindowType, wItemSlotIndex), iDisplaySlotIndex, iPrice);
  448.  
  449.     return Py_BuildNone();
  450. }
  451.  
  452. PyObject * shopGetTabCount(PyObject * poSelf, PyObject * poArgs)
  453. {
  454.     return Py_BuildValue("i", CPythonShop::instance().GetTabCount());
  455. }
  456.  
  457. PyObject * shopGetTabName(PyObject * poSelf, PyObject * poArgs)
  458. {
  459.     BYTE bTabIdx;
  460.     if (!PyTuple_GetInteger(poArgs, 0, &bTabIdx))
  461.         return Py_BuildException();
  462.  
  463.     return Py_BuildValue("s", CPythonShop::instance().GetTabName(bTabIdx));
  464. }
  465.  
  466. PyObject * shopGetTabCoinType(PyObject * poSelf, PyObject * poArgs)
  467. {
  468.     BYTE bTabIdx;
  469.     if (!PyTuple_GetInteger(poArgs, 0, &bTabIdx))
  470.         return Py_BuildException();
  471.  
  472.     return Py_BuildValue("i", CPythonShop::instance().GetTabCoinType(bTabIdx));
  473. }
  474.  
  475. void initshop()
  476. {
  477.     static PyMethodDef s_methods[] =
  478.     {
  479.         // Shop
  480.         { "Open",                       shopOpen,                       METH_VARARGS },
  481.         { "Close",                      shopClose,                      METH_VARARGS },
  482.         { "IsOpen",                     shopIsOpen,                     METH_VARARGS },
  483.         { "IsPrivateShop",              shopIsPrviateShop,              METH_VARARGS },
  484.         { "IsMainPlayerPrivateShop",    shopIsMainPlayerPrivateShop,    METH_VARARGS },
  485.         { "GetItemID",                  shopGetItemID,                  METH_VARARGS },
  486.         { "GetItemCount",               shopGetItemCount,               METH_VARARGS },
  487.         { "GetItemPrice",               shopGetItemPrice,               METH_VARARGS },
  488.         { "GetItemMetinSocket",         shopGetItemMetinSocket,         METH_VARARGS },
  489.         { "GetItemAttribute",           shopGetItemAttribute,           METH_VARARGS },
  490.         { "GetTabCount",                shopGetTabCount,                METH_VARARGS },
  491.         { "GetTabName",                 shopGetTabName,                 METH_VARARGS },
  492.         { "GetTabCoinType",             shopGetTabCoinType,             METH_VARARGS },
  493.  
  494.         // Private Shop
  495.         { "ClearPrivateShopStock",      shopClearPrivateShopStock,      METH_VARARGS },
  496.         { "AddPrivateShopItemStock",    shopAddPrivateShopItemStock,    METH_VARARGS },
  497.         { "DelPrivateShopItemStock",    shopDelPrivateShopItemStock,    METH_VARARGS },
  498.         { "GetPrivateShopItemPrice",    shopGetPrivateShopItemPrice,    METH_VARARGS },
  499.         { "BuildPrivateShop",           shopBuildPrivateShop,           METH_VARARGS },
  500.         { NULL,                         NULL,                           NULL },
  501.     };
  502.     PyObject * poModule = Py_InitModule("shop", s_methods);
  503.  
  504.     PyModule_AddIntConstant(poModule, "SHOP_SLOT_COUNT", SHOP_HOST_ITEM_MAX_NUM);
  505.     PyModule_AddIntConstant(poModule, "SHOP_COIN_TYPE_GOLD", SHOP_COIN_TYPE_GOLD);
  506.     PyModule_AddIntConstant(poModule, "SHOP_COIN_TYPE_SECONDARY_COIN", SHOP_COIN_TYPE_SECONDARY_COIN);
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement