Advertisement
thespeedy

shop

Oct 13th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.98 KB | None | 0 0
  1. bool ConvertToShopItemTable(IN CGroupNode* pNode, OUT TShopTableEx& shopTable)
  2. {
  3.     if (!pNode->GetValue("vnum", 0, shopTable.dwVnum))
  4.     {
  5.         sys_err("Group %s does not have vnum.", pNode->GetNodeName().c_str());
  6.         return false;
  7.     }
  8.  
  9.     if (!pNode->GetValue("name", 0, shopTable.name))
  10.     {
  11.         sys_err("Group %s does not have name.", pNode->GetNodeName().c_str());
  12.         return false;
  13.     }
  14.    
  15.     if (shopTable.name.length() >= SHOP_TAB_NAME_MAX)
  16.     {
  17.         sys_err("Shop name length must be less than %d. Error in Group %s, name %s", SHOP_TAB_NAME_MAX, pNode->GetNodeName().c_str(), shopTable.name.c_str());
  18.         return false;
  19.     }
  20.  
  21.     std::string stCoinType;
  22.     if (!pNode->GetValue("cointype", 0, stCoinType))
  23.     {
  24.         stCoinType = "Gold";
  25.     }
  26.    
  27.     if (boost::iequals(stCoinType, "Gold"))
  28.     {
  29.         shopTable.coinType = SHOP_COIN_TYPE_GOLD;
  30.     }
  31.     else if (boost::iequals(stCoinType, "SecondaryCoin"))
  32.     {
  33.         shopTable.coinType = SHOP_COIN_TYPE_SECONDARY_COIN;
  34.     }
  35.     else
  36.     {
  37.         sys_err("Group %s has undefine cointype(%s).", pNode->GetNodeName().c_str(), stCoinType.c_str());
  38.         return false;
  39.     }
  40.  
  41.     CGroupNode* pItemGroup = pNode->GetChildNode("items");
  42.     if (!pItemGroup)
  43.     {
  44.         sys_err("Group %s does not have 'group items'.", pNode->GetNodeName().c_str());
  45.         return false;
  46.     }
  47.  
  48.     int itemGroupSize = pItemGroup->GetRowCount();
  49.     std::vector <TShopItemTable> shopItems(itemGroupSize);
  50.     if (itemGroupSize >= SHOP_HOST_ITEM_MAX_NUM)
  51.     {
  52.         sys_err("count(%d) of rows of group items of group %s must be smaller than %d", itemGroupSize, pNode->GetNodeName().c_str(), SHOP_HOST_ITEM_MAX_NUM);
  53.         return false;
  54.     }
  55.  
  56.     for (int i = 0; i < itemGroupSize; i++)
  57.     {
  58.         if (!pItemGroup->GetValue(i, "vnum", shopItems[i].vnum))
  59.         {
  60.             sys_err("row(%d) of group items of group %s does not have vnum column", i, pNode->GetNodeName().c_str());
  61.             return false;
  62.         }
  63.        
  64.         if (!pItemGroup->GetValue(i, "count", shopItems[i].count))
  65.         {
  66.             sys_err("row(%d) of group items of group %s does not have count column", i, pNode->GetNodeName().c_str());
  67.             return false;
  68.         }
  69.         if (!pItemGroup->GetValue(i, "price", shopItems[i].price))
  70.         {
  71.             sys_err("row(%d) of group items of group %s does not have price column", i, pNode->GetNodeName().c_str());
  72.             return false;
  73.         }
  74.     }
  75.     std::string stSort;
  76.     if (!pNode->GetValue("sort", 0, stSort))
  77.     {
  78.         stSort = "None";
  79.     }
  80.  
  81.     if (boost::iequals(stSort, "Asc"))
  82.     {
  83.         std::sort(shopItems.begin(), shopItems.end(), CompareShopItemName);
  84.     }
  85.     else if(boost::iequals(stSort, "Desc"))
  86.     {
  87.         std::sort(shopItems.rbegin(), shopItems.rend(), CompareShopItemName);
  88.     }
  89.  
  90.     CGrid grid = CGrid(5, 9);
  91.     int iPos;
  92.  
  93.     memset(&shopTable.items[0], 0, sizeof(shopTable.items));
  94.  
  95.     for (int i = 0; i < shopItems.size(); i++)
  96.     {
  97.         TItemTable * item_table = ITEM_MANAGER::instance().GetTable(shopItems[i].vnum);
  98.         if (!item_table)
  99.         {
  100.             sys_err("vnum(%d) of group items of group %s does not exist", shopItems[i].vnum, pNode->GetNodeName().c_str());
  101.             return false;
  102.         }
  103.  
  104.         iPos = grid.FindBlank(1, item_table->bSize);
  105.  
  106.         grid.Put(iPos, 1, item_table->bSize);
  107.         shopTable.items[iPos] = shopItems[i];
  108.     }
  109.  
  110.     shopTable.byItemCount = shopItems.size();
  111.     return true;
  112. }
  113.  
  114. bool CShopManager::ReadShopTableEx(const char* stFileName)
  115. {
  116.     // file 유무 체크.
  117.     // 없는 경우는 에러로 처리하지 않는다.
  118.     FILE* fp = fopen(stFileName, "rb");
  119.     if (NULL == fp)
  120.         return true;
  121.     fclose(fp);
  122.  
  123.     CGroupTextParseTreeLoader loader;
  124.     if (!loader.Load(stFileName))
  125.     {
  126.         sys_err("%s Load fail.", stFileName);
  127.         return false;
  128.     }
  129.  
  130.     CGroupNode* pShopNPCGroup = loader.GetGroup("shopnpc");
  131.     if (NULL == pShopNPCGroup)
  132.     {
  133.         sys_err("Group ShopNPC is not exist.");
  134.         return false;
  135.     }
  136.  
  137.     typedef std::multimap <DWORD, TShopTableEx> TMapNPCshop;
  138.     TMapNPCshop map_npcShop;
  139.     for (int i = 0; i < pShopNPCGroup->GetRowCount(); i++)
  140.     {
  141.         DWORD npcVnum;
  142.         std::string shopName;
  143.         if (!pShopNPCGroup->GetValue(i, "npc", npcVnum) || !pShopNPCGroup->GetValue(i, "group", shopName))
  144.         {
  145.             sys_err("Invalid row(%d). Group ShopNPC rows must have 'npc', 'group' columns", i);
  146.             return false;
  147.         }
  148.         std::transform(shopName.begin(), shopName.end(), shopName.begin(), (int(*)(int))std::tolower);
  149.         CGroupNode* pShopGroup = loader.GetGroup(shopName.c_str());
  150.         if (!pShopGroup)
  151.         {
  152.             sys_err("Group %s is not exist.", shopName.c_str());
  153.             return false;
  154.         }
  155.         TShopTableEx table;
  156.         if (!ConvertToShopItemTable(pShopGroup, table))
  157.         {
  158.             sys_err("Cannot read Group %s.", shopName.c_str());
  159.             return false;
  160.         }
  161.         if (m_map_pkShopByNPCVnum.find(npcVnum) != m_map_pkShopByNPCVnum.end())
  162.         {
  163.             sys_err("%d cannot have both original shop and extended shop", npcVnum);
  164.             return false;
  165.         }
  166.        
  167.         map_npcShop.insert(TMapNPCshop::value_type(npcVnum, table));   
  168.     }
  169.  
  170.     for (TMapNPCshop::iterator it = map_npcShop.begin(); it != map_npcShop.end(); ++it)
  171.     {
  172.         DWORD npcVnum = it->first;
  173.         TShopTableEx& table = it->second;
  174.         if (m_map_pkShop.find(table.dwVnum) != m_map_pkShop.end())
  175.         {
  176.             sys_err("Shop vnum(%d) already exists", table.dwVnum);
  177.             return false;
  178.         }
  179.         TShopMap::iterator shop_it = m_map_pkShopByNPCVnum.find(npcVnum);
  180.        
  181.         LPSHOPEX pkShopEx = NULL;
  182.         if (m_map_pkShopByNPCVnum.end() == shop_it)
  183.         {
  184.             pkShopEx = M2_NEW CShopEx;
  185.             pkShopEx->Create(0, npcVnum);
  186.             m_map_pkShopByNPCVnum.insert(TShopMap::value_type(npcVnum, pkShopEx));
  187.         }
  188.         else
  189.         {
  190.             pkShopEx = dynamic_cast <CShopEx*> (shop_it->second);
  191.             if (NULL == pkShopEx)
  192.             {
  193.                 sys_err("WTF!!! It can't be happend. NPC(%d) Shop is not extended version.", shop_it->first);
  194.                 return false;
  195.             }
  196.         }
  197.  
  198.         if (pkShopEx->GetTabCount() >= SHOP_TAB_COUNT_MAX)
  199.         {
  200.             sys_err("ShopEx cannot have tab more than %d", SHOP_TAB_COUNT_MAX);
  201.             return false;
  202.         }
  203.  
  204.         if (pkShopEx->GetVnum() != 0 && m_map_pkShop.find(pkShopEx->GetVnum()) != m_map_pkShop.end())
  205.         {
  206.             sys_err("Shop vnum(%d) already exist.", pkShopEx->GetVnum());
  207.             return false;
  208.         }
  209.         m_map_pkShop.insert(TShopMap::value_type (pkShopEx->GetVnum(), pkShopEx));
  210.         pkShopEx->AddShopTable(table);
  211.     }
  212.  
  213.     return true;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement