Last active
March 13, 2019 21:56
-
-
Save mq1n/f31e4f1130819c1404b1348c8b28613d to your computer and use it in GitHub Desktop.
mob_drop_info.json game part
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool ITEM_MANAGER::ReadMonsterDropItemGroupNew(const char * c_pszFileName) | |
{ | |
sys_log(0, "monster_drop_item json parser started!"); | |
auto wfilename = StringToWstring(c_pszFileName); | |
// File content | |
std::wifstream in(wfilename.c_str(), std::ios_base::binary); | |
in.exceptions(std::ios_base::badbit | std::ios_base::failbit | std::ios_base::eofbit); | |
auto content = std::wstring(std::istreambuf_iterator<wchar_t>(in), std::istreambuf_iterator<wchar_t>()); | |
in.close(); | |
if (content.size() == 0) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : Null file content"); | |
return false; | |
} | |
auto lastnode = std::wstring(L""); | |
try | |
{ | |
// Root json tree | |
boost::property_tree::wptree root; | |
std::wistringstream wiss(content); | |
boost::property_tree::read_json(wiss, root); | |
for (auto context = root.begin(); context != root.end(); ++context) | |
{ | |
// Group array | |
auto key = context->first; | |
auto value = context->second.get_value<std::wstring>(); | |
lastnode = key; | |
auto grouptree = root.get_child(key); | |
if (grouptree.empty()) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : Syntax error %s : no group node, node %ls", c_pszFileName, key.c_str()); | |
return false; | |
} | |
auto type = grouptree.get<std::wstring>(L"Type"); | |
if (type.empty()) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : Syntax error %s : no type (kill|drop), node %ls", c_pszFileName, key.c_str()); | |
return false; | |
} | |
auto mob = grouptree.get<std::wstring>(L"Mob"); | |
if (mob.empty()) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : Syntax error %s : no mob vnum, node %ls", c_pszFileName, key.c_str()); | |
return false; | |
} | |
auto iMobVnum = std::stoi(mob); | |
auto iKillDrop = 1; | |
if (type == L"kill") | |
{ | |
auto killdrop = grouptree.get<std::wstring>(L"Kill_drop"); | |
if (killdrop.empty()) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : Syntax error %s : no kill drop count, node %ls", c_pszFileName, key.c_str()); | |
return false; | |
} | |
iKillDrop = std::stoi(killdrop); | |
} | |
auto iLevelLimit = 0; | |
if (type == L"limit") | |
{ | |
auto levellimit = grouptree.get<std::wstring>(L"Level_limit"); | |
if (levellimit.empty()) | |
{ | |
sys_err("ReadmonsterDropItemGroup : Syntax error %s : no level_limit, node %ls", c_pszFileName, key.c_str()); | |
return false; | |
} | |
} | |
sys_log(0,"MOB_ITEM_GROUP %ls [%ls] %d %d %d", key.c_str(), type.c_str(), iMobVnum, iKillDrop, iLevelLimit); | |
if (iKillDrop == 0) | |
{ | |
continue; | |
} | |
auto groupmap = std::map < std::wstring, std::map <std::string, std::string> > (); | |
for (auto group = grouptree.begin(); group != grouptree.end(); ++group) | |
{ | |
// Mob array | |
std::wstring dropkey = group->first; | |
if (group->second.get_value<std::wstring>().size() == 0) | |
{ | |
// Drop array | |
auto droptree = grouptree.get_child(dropkey); | |
auto dropmap = std::map <std::string, std::string>(); | |
for (auto dropctx = droptree.begin(); dropctx != droptree.end(); ++dropctx) | |
{ | |
auto dropctxkey = WstringToString(dropctx->first); | |
auto dropctxvalue = WstringToString(dropctx->second.get_value<std::wstring>()); | |
if (dropctxkey != "item" && dropctxkey != "count" && dropctxkey != "rate" && dropctxkey != "rare") | |
{ | |
sys_err("ReadmonsterDropItemGroupNew : Syntax error %s : unknown drop context, node %s", c_pszFileName, dropctxkey.c_str()); | |
return false; | |
} | |
dropmap[dropctxkey] = dropctxvalue; | |
} | |
groupmap[dropkey] = dropmap; | |
} | |
} | |
for (const auto& group : groupmap) | |
{ | |
auto dropmap = group.second; | |
if (type == L"kill") | |
{ | |
auto pkGroup = std::make_shared<CMobItemGroup>(iMobVnum, iKillDrop, WstringToString(key)); | |
if (!pkGroup.get()) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : null group ptr"); | |
return false; | |
} | |
DWORD dwVnum = 0; | |
if (!GetVnumByOriginalName(dropmap["item"].c_str(), dwVnum)) | |
{ | |
str_to_number(dwVnum, dropmap["item"].c_str()); | |
if (!ITEM_MANAGER::instance().GetTable(dwVnum)) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : there is no item %s : node %ls : vnum %d", dropmap["item"].c_str(), key.c_str(), dwVnum); | |
return false; | |
} | |
} | |
auto iCount = std::atoi(dropmap["count"].c_str()); | |
if (iCount < 1) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : there is no count for item %s : node %ls : vnum %d, count %d", dropmap["item"].c_str(), key.c_str(), dwVnum, iCount); | |
return false; | |
} | |
auto iPartPct = std::atoi(dropmap["rate"].c_str()); | |
if (iPartPct == 0) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : there is no drop percent for item %s : node %ls : pct %d", dropmap["item"].c_str(), key.c_str(), iPartPct); | |
return false; | |
} | |
auto iRarePct = 0; | |
auto itrare = dropmap.find("rare"); | |
if (itrare != dropmap.end()) | |
iRarePct = std::atoi(itrare->second.c_str()); | |
iRarePct = MINMAX(0, iRarePct, 100); | |
sys_log(0, " %ls count %d rare %d", key.c_str(), iCount, iRarePct); | |
pkGroup->AddItem(dwVnum, iCount, iPartPct, iRarePct); | |
m_map_pkMobItemGroup.insert({ iMobVnum, pkGroup.get() }); | |
} | |
else if (type == L"drop") | |
{ | |
bool bNew = true; | |
CDropItemGroup* pkGroup = nullptr; | |
auto it = m_map_pkDropItemGroup.find(iMobVnum); | |
if (it == m_map_pkDropItemGroup.end()) | |
{ | |
pkGroup = M2_NEW CDropItemGroup(0, iMobVnum, WstringToString(key)); | |
} | |
else | |
{ | |
bNew = false; | |
pkGroup = it->second; | |
} | |
if (!pkGroup) | |
{ | |
sys_err("ReadDropItemGroupNew : null group ptr"); | |
return false; | |
} | |
DWORD dwVnum = 0; | |
if (!GetVnumByOriginalName(dropmap["item"].c_str(), dwVnum)) | |
{ | |
str_to_number(dwVnum, dropmap["item"].c_str()); | |
if (!ITEM_MANAGER::instance().GetTable(dwVnum)) | |
{ | |
sys_err("ReadDropItemGroupNew : there is no item %s : node %ls", dropmap["item"].c_str(), key.c_str()); | |
M2_DELETE(pkGroup); | |
return false; | |
} | |
} | |
auto iCount = std::atoi(dropmap["count"].c_str()); | |
if (iCount < 1) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : there is no count for item %s : node %ls", dropmap["item"].c_str(), key.c_str()); | |
M2_DELETE(pkGroup); | |
return false; | |
} | |
auto fPercent = std::atof(dropmap["rate"].c_str()); | |
DWORD dwPct = (DWORD)(10000.0f * fPercent); | |
sys_log(0, " name %s pct %d count %d", dropmap["item"].c_str(), dwPct, iCount); | |
pkGroup->AddItem(dwVnum, dwPct, iCount); | |
if (bNew) | |
m_map_pkDropItemGroup.insert({ iMobVnum, pkGroup }); | |
} | |
else if (type == L"limit") | |
{ | |
auto pkLevelItemGroup = std::make_shared<CLevelItemGroup>(iLevelLimit); | |
if (!pkLevelItemGroup.get()) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : null group ptr"); | |
return false; | |
} | |
DWORD dwVnum = 0; | |
if (!GetVnumByOriginalName(dropmap["item"].c_str(), dwVnum)) | |
{ | |
str_to_number(dwVnum, dropmap["item"].c_str()); | |
if (!ITEM_MANAGER::instance().GetTable(dwVnum)) | |
{ | |
sys_err("ReadDropItemGroupNew : there is no item %s : node %ls", dropmap["item"].c_str(), key.c_str()); | |
return false; | |
} | |
} | |
auto iCount = std::atoi(dropmap["count"].c_str()); | |
if (iCount < 1) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : there is no count for item %s : node %ls", dropmap["item"].c_str(), key.c_str()); | |
return false; | |
} | |
auto fPercent = std::atof(dropmap["rate"].c_str()); | |
DWORD dwPct = (DWORD)(10000.0f * fPercent); | |
sys_log(0, " name %s pct %d count %d", dropmap["item"].c_str(), dwPct, iCount); | |
pkLevelItemGroup->AddItem(dwVnum, dwPct, iCount); | |
m_map_pkLevelItemGroup.insert({ iMobVnum, pkLevelItemGroup.get() }); | |
} | |
else if (type == L"thiefgloves") | |
{ | |
auto pkGroup = std::make_shared<CBuyerThiefGlovesItemGroup>(0, iMobVnum, WstringToString(key)); | |
if (!pkGroup.get()) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : null group ptr"); | |
return false; | |
} | |
DWORD dwVnum = 0; | |
if (!GetVnumByOriginalName(dropmap["item"].c_str(), dwVnum)) | |
{ | |
str_to_number(dwVnum, dropmap["item"].c_str()); | |
if (!ITEM_MANAGER::instance().GetTable(dwVnum)) | |
{ | |
sys_err("ReadDropItemGroupNew : there is no item %s : node %ls", dropmap["item"].c_str(), key.c_str()); | |
return false; | |
} | |
} | |
auto iCount = std::atoi(dropmap["count"].c_str()); | |
if (iCount < 1) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew : there is no count for item %s : node %ls", dropmap["item"].c_str(), key.c_str()); | |
return false; | |
} | |
auto fPercent = std::atof(dropmap["rate"].c_str()); | |
DWORD dwPct = (DWORD)(10000.0f * fPercent); | |
sys_log(0, " name %s pct %d count %d", dropmap["item"].c_str(), dwPct, iCount); | |
pkGroup->AddItem(dwVnum, dwPct, iCount); | |
m_map_pkGloveItemGroup.insert({ iMobVnum, pkGroup.get() }); | |
} | |
else | |
{ | |
sys_err("ReadMonsterDropItemGroup : Syntax error %s : invalid type %s (kill|drop), node %ls", c_pszFileName, type.c_str(), key.c_str()); | |
return false; | |
} | |
} | |
} | |
} | |
catch (std::exception & e) | |
{ | |
sys_err("ReadMonsterDropItemGroupNew > Exception handled! Error: %s Last node: %ls\n", e.what(), lastnode.c_str()); | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment