1 //////////////////////////////////////////////////////////////////////////////// 2 // Copyright (C) 2004-2011 by The Allacrost Project 3 // Copyright (C) 2012-2018 by Bertram (Valyria Tear) 4 // All Rights Reserved 5 // 6 // This code is licensed under the GNU GPL version 2. It is free software 7 // and you may modify it and/or redistribute it under the terms of this license. 8 // See http://www.gnu.org/copyleft/gpl.html for details. 9 //////////////////////////////////////////////////////////////////////////////// 10 11 #include "shop_data_handler.h" 12 13 using namespace vt_utils; 14 15 namespace vt_global 16 { 17 ~ShopDataHandler()18ShopDataHandler::~ShopDataHandler() 19 { 20 Clear(); 21 } 22 Clear()23void ShopDataHandler::Clear() 24 { 25 _shop_data.clear(); 26 } 27 GetShopData(const std::string & shop_id)28const ShopData& ShopDataHandler::GetShopData(const std::string& shop_id) { 29 if (_shop_data.find(shop_id) == _shop_data.end()) 30 return _shop_data[std::string()]; // Return default empty shop data 31 return _shop_data.at(shop_id); 32 } 33 HasShopData(const std::string & shop_id) const34bool ShopDataHandler::HasShopData(const std::string& shop_id) const { 35 return (_shop_data.find(shop_id) != _shop_data.end()); 36 } 37 SetShopData(const std::string & shop_id,const ShopData & shop_data)38void ShopDataHandler::SetShopData(const std::string& shop_id, const ShopData& shop_data) 39 { 40 _shop_data[shop_id] = shop_data; 41 } 42 LoadShopData(vt_script::ReadScriptDescriptor & file)43void ShopDataHandler::LoadShopData(vt_script::ReadScriptDescriptor& file) 44 { 45 if(file.IsFileOpen() == false) { 46 PRINT_WARNING << "The file provided in the function argument was not open" << std::endl; 47 return; 48 } 49 50 if (!file.OpenTable("shop_data")) { 51 return; 52 } 53 54 std::vector<std::string> shop_ids; 55 file.ReadTableKeys(shop_ids); 56 57 for (size_t i = 0; i < shop_ids.size(); ++i) { 58 // Open the Shop Id table 59 if (!file.OpenTable(shop_ids[i])) 60 continue; 61 62 ShopData shop_data; 63 if (file.OpenTable("available_buy")) { 64 std::vector<std::string> item_ids; 65 file.ReadTableKeys(item_ids); 66 for (size_t j = 0; j < item_ids.size(); ++j) { 67 uint32_t item_count = file.ReadUInt(item_ids[j]); 68 shop_data._available_buy[std::stoi(item_ids[j].c_str())] = item_count; 69 } 70 file.CloseTable(); // available_buy 71 } 72 if (file.OpenTable("available_trade")) { 73 std::vector<std::string> item_ids; 74 file.ReadTableKeys(item_ids); 75 for (size_t j = 0; j < item_ids.size(); ++j) { 76 uint32_t item_count = file.ReadUInt(item_ids[j]); 77 shop_data._available_trade[std::stoi(item_ids[j].c_str())] = item_count; 78 } 79 file.CloseTable(); // available_trade 80 } 81 _shop_data[ shop_ids[i] ] = shop_data; 82 file.CloseTable(); // shop_id 83 } 84 file.CloseTable(); // shop_data 85 } 86 SaveShopData(vt_script::WriteScriptDescriptor & file)87void ShopDataHandler::SaveShopData(vt_script::WriteScriptDescriptor& file) 88 { 89 if(!file.IsFileOpen()) { 90 PRINT_WARNING << "The file was not open: " << file.GetFilename() << std::endl; 91 return; 92 } 93 94 file.WriteLine("shop_data = {"); 95 file.InsertNewLine(); 96 97 auto it = _shop_data.begin(); 98 auto it_end = _shop_data.end(); 99 for (; it != it_end; ++it) { 100 std::string shop_id = it->first; 101 const ShopData& shop_data = it->second; 102 103 file.WriteLine("\t[\"" + shop_id + "\"] = {"); 104 105 file.WriteLine("\t\tavailable_buy = {"); 106 auto it2 = shop_data._available_buy.begin(); 107 auto it2_end = shop_data._available_buy.end(); 108 for(; it2 != it2_end; ++it2) { 109 std::string item_id = NumberToString(it2->first); 110 std::string count = NumberToString(it2->second); 111 file.WriteLine("\t\t\t[\"" + item_id + "\"] = " + count + ","); 112 } 113 file.WriteLine("\t\t},"); 114 115 file.WriteLine("\t\tavailable_trade = {"); 116 auto it3 = shop_data._available_trade.begin(); 117 auto it3_end = shop_data._available_trade.end(); 118 for(; it3 != it3_end; ++it3) { 119 std::string item_id = NumberToString(it3->first); 120 std::string count = NumberToString(it3->second); 121 file.WriteLine("\t\t\t[\"" + item_id + "\"] = " + count + ","); 122 } 123 file.WriteLine("\t\t}"); 124 125 file.WriteLine("\t},"); 126 } 127 file.WriteLine("},"); // Close the shop_data table 128 file.InsertNewLine(); 129 } 130 131 } // namespace vt_global 132