Guest User

Untitled

a guest
Dec 31st, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.35 KB | None | 0 0
  1. #include <Store.h>
  2.  
  3. #include <Points.h>
  4.  
  5. #include "AtlasShop.h"
  6. #include "DBHelper.h"
  7. #include "ShopLog.h"
  8.  
  9. namespace AtlasShop::Store
  10. {
  11. /**
  12. * \brief Buy an item from shop
  13. */
  14. bool BuyItem(AShooterPlayerController* player_controller, const nlohmann::basic_json<>& item_entry, uint64 steam_id,
  15. int amount)
  16. {
  17. bool success = false;
  18.  
  19. if (amount <= 0)
  20. {
  21. amount = 1;
  22. }
  23.  
  24. const unsigned price = item_entry["Price"];
  25. const int final_price = price * amount;
  26. if (final_price <= 0)
  27. {
  28. return false;
  29. }
  30.  
  31. const int points = Points::GetPoints(steam_id);
  32.  
  33. if (points >= final_price && Points::SpendPoints(final_price, steam_id))
  34. {
  35. auto items_map = item_entry["Items"];
  36. for (const auto& item : items_map)
  37. {
  38. const float quality = item["Quality"];
  39. const bool force_blueprint = item["ForceBlueprint"];
  40. const int default_amount = item["Amount"];
  41. std::string blueprint = item["Blueprint"];
  42.  
  43. FString fblueprint(blueprint.c_str());
  44.  
  45. for (int i = 0; i < amount; ++i)
  46. {
  47. TArray<UPrimalItem*> out_items;
  48. player_controller->GiveItem(&out_items, &fblueprint, default_amount, quality, force_blueprint,
  49. false, 0);
  50. }
  51. }
  52.  
  53. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  54. *GetText("BoughtItem"));
  55.  
  56. success = true;
  57. }
  58. else
  59. {
  60. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  61. *GetText("NoPoints"));
  62. }
  63.  
  64. return success;
  65. }
  66.  
  67. /**
  68. * \brief Buy an unlockengram from shop
  69. */
  70. bool UnlockEngram(AShooterPlayerController* player_controller, const nlohmann::basic_json<>& item_entry,
  71. uint64 steam_id)
  72. {
  73. bool success = false;
  74. const int price = item_entry["Price"];
  75.  
  76. const int points = Points::GetPoints(steam_id);
  77.  
  78. if (points >= price && Points::SpendPoints(price, steam_id))
  79. {
  80. auto items_map = item_entry["Items"];
  81. for (const auto& item : items_map)
  82. {
  83. const std::string blueprint = item["Blueprint"];
  84. FString fblueprint(blueprint);
  85.  
  86. auto* cheat_manager = static_cast<UShooterCheatManager*>(player_controller->CheatManagerField());
  87. cheat_manager->UnlockEngram(&fblueprint);
  88. }
  89.  
  90.  
  91. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  92. *GetText("BoughtItem"));
  93.  
  94. success = true;
  95. }
  96. else
  97. {
  98. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  99. *GetText("NoPoints"));
  100. }
  101.  
  102. return success;
  103. }
  104.  
  105. /**
  106. * \brief Buy an Command from shop
  107. */
  108. bool BuyCommand(AShooterPlayerController* player_controller, const nlohmann::basic_json<>& item_entry,
  109. uint64 steam_id)
  110. {
  111. bool success = false;
  112. const int price = item_entry["Price"];
  113.  
  114. const int points = Points::GetPoints(steam_id);
  115.  
  116. if (points >= price && Points::SpendPoints(price, steam_id))
  117. {
  118. auto items_map = item_entry["Items"];
  119. for (const auto& item : items_map)
  120. {
  121. const std::string command = item["Command"];
  122.  
  123. FString fcommand(command);
  124. FString result;
  125. player_controller->ConsoleCommand(&result, &fcommand, true);
  126. }
  127.  
  128. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  129. *GetText("BoughtItem"));
  130.  
  131. success = true;
  132. }
  133. else
  134. {
  135. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  136. *GetText("NoPoints"));
  137. }
  138.  
  139. return success;
  140. }
  141.  
  142.  
  143. /**
  144. * \brief Buy a dino from shop
  145. */
  146. bool BuyDino(AShooterPlayerController* player_controller, const nlohmann::basic_json<>& item_entry, uint64 steam_id)
  147. {
  148. bool success = false;
  149.  
  150. const int price = item_entry["Price"];
  151. const int level = item_entry["Level"];
  152. const bool neutered = item_entry.value("Neutered", false);
  153. std::string blueprint = item_entry["Blueprint"];
  154.  
  155. const int points = Points::GetPoints(steam_id);
  156.  
  157. if (points >= price && Points::SpendPoints(price, steam_id))
  158. {
  159. const FString fblueprint(blueprint.c_str());
  160.  
  161. ArkApi::GetApiUtils().SpawnDino(player_controller, fblueprint, nullptr, level, true, neutered);
  162.  
  163. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  164. *GetText("BoughtDino"));
  165.  
  166. success = true;
  167. }
  168. else
  169. {
  170. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  171. *GetText("NoPoints"));
  172. }
  173.  
  174. return success;
  175. }
  176.  
  177. /**
  178. * \brief Buy a beacon from shop
  179. */
  180. bool BuyBeacon(AShooterPlayerController* player_controller, const nlohmann::basic_json<>& item_entry,
  181. uint64 steam_id)
  182. {
  183. bool success = false;
  184.  
  185. const int price = item_entry["Price"];
  186. std::string class_name = item_entry["ClassName"];
  187.  
  188. const int points = Points::GetPoints(steam_id);
  189.  
  190. if (points >= price && Points::SpendPoints(price, steam_id))
  191. {
  192. FString fclass_name(class_name.c_str());
  193.  
  194. auto* cheatManager = static_cast<UShooterCheatManager*>(player_controller->CheatManagerField());
  195. cheatManager->Summon(&fclass_name);
  196.  
  197. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  198. *GetText("BoughtBeacon"));
  199.  
  200. success = true;
  201. }
  202. else
  203. {
  204. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  205. *GetText("NoPoints"));
  206. }
  207.  
  208. return success;
  209. }
  210.  
  211. /**
  212. * \brief Buy experience from shop
  213. */
  214. bool BuyExperience(AShooterPlayerController* player_controller, const nlohmann::basic_json<>& item_entry,
  215. uint64 steam_id)
  216. {
  217. bool success = false;
  218.  
  219. const int price = item_entry["Price"];
  220. const float amount = item_entry["Amount"];
  221. const bool give_to_dino = item_entry["GiveToDino"];
  222.  
  223. if (!give_to_dino && ArkApi::IApiUtils::IsRidingDino(player_controller))
  224. {
  225. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  226. *GetText("RidingDino"));
  227. return false;
  228. }
  229.  
  230. const int points = Points::GetPoints(steam_id);
  231.  
  232. if (points >= price && Points::SpendPoints(price, steam_id))
  233. {
  234. player_controller->AddExperience(amount, false, true);
  235.  
  236. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  237. *GetText("BoughtExp"));
  238.  
  239. success = true;
  240. }
  241. else
  242. {
  243. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  244. *GetText("NoPoints"));
  245. }
  246.  
  247. return success;
  248. }
  249.  
  250. bool Buy(AShooterPlayerController* player_controller, const FString& item_id, int amount)
  251. {
  252. if (ArkApi::IApiUtils::IsPlayerDead(player_controller))
  253. {
  254. return false;
  255. }
  256.  
  257. if (!IsStoreEnabled(player_controller))
  258. {
  259. return false;
  260. }
  261.  
  262. if (amount <= 0)
  263. {
  264. amount = 1;
  265. }
  266.  
  267. bool success = false;
  268.  
  269. const uint64 steam_id = ArkApi::IApiUtils::GetSteamIdFromController(player_controller);
  270.  
  271. if (DBHelper::IsPlayerExists(steam_id))
  272. {
  273. auto items_list = config["ShopItems"];
  274.  
  275. auto item_entry_iter = items_list.find(item_id.ToString());
  276. if (item_entry_iter == items_list.end())
  277. {
  278. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  279. *GetText("WrongId"));
  280. return false;
  281. }
  282.  
  283. auto item_entry = item_entry_iter.value();
  284.  
  285. const std::string type = item_entry["Type"];
  286.  
  287. const int min_level = item_entry.value("MinLevel", 1);
  288. const int max_level = item_entry.value("MaxLevel", 999);
  289.  
  290. auto* primal_character = static_cast<APrimalCharacter*>(player_controller->CharacterField());
  291. UPrimalCharacterStatusComponent* char_component = primal_character->MyCharacterStatusComponentField();
  292.  
  293. const int level = char_component->BaseCharacterLevelField() + char_component->ExtraCharacterLevelField();
  294. if (level < min_level || level > max_level)
  295. {
  296. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  297. *GetText("BadLevel"), min_level, max_level);
  298. return false;
  299. }
  300.  
  301. if (type == "item")
  302. {
  303. success = BuyItem(player_controller, item_entry, steam_id, amount);
  304. }
  305. else if (type == "dino")
  306. {
  307. success = BuyDino(player_controller, item_entry, steam_id);
  308. }
  309. else if (type == "beacon")
  310. {
  311. success = BuyBeacon(player_controller, item_entry, steam_id);
  312. }
  313. else if (type == "experience")
  314. {
  315. success = BuyExperience(player_controller, item_entry, steam_id);
  316. }
  317. else if (type == "unlockengram")
  318. {
  319. success = UnlockEngram(player_controller, item_entry, steam_id);
  320. }
  321. else if (type == "command")
  322. {
  323. success = BuyCommand(player_controller, item_entry, steam_id);
  324. }
  325.  
  326. if (success)
  327. {
  328. const std::wstring log = fmt::format(TEXT("{}({}) bought item \"{}\". Amount - {}"),
  329. *ArkApi::IApiUtils::GetSteamName(player_controller), steam_id,
  330. *item_id, amount);
  331.  
  332. ShopLog::GetLog()->info(ArkApi::Tools::Utf8Encode(log));
  333. }
  334. }
  335.  
  336. return success;
  337. }
  338.  
  339. // Chat callbacks
  340.  
  341. void ChatBuy(AShooterPlayerController* player_controller, FString* message, EChatSendMode::Type /*unused*/)
  342. {
  343. TArray<FString> parsed;
  344. message->ParseIntoArray(parsed, L" ", true);
  345.  
  346. if (parsed.IsValidIndex(1))
  347. {
  348. int amount = 0;
  349.  
  350. if (parsed.IsValidIndex(2))
  351. {
  352. try
  353. {
  354. amount = std::stoi(*parsed[2]);
  355. }
  356. catch (const std::exception& exception)
  357. {
  358. Log::GetLog()->warn("({} {}) Parsing error {}", __FILE__, __FUNCTION__, exception.what());
  359. return;
  360. }
  361. }
  362.  
  363. Buy(player_controller, parsed[1], amount);
  364. }
  365. else
  366. {
  367. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  368. *GetText("BuyUsage"));
  369. }
  370. }
  371.  
  372. void ShowItems(AShooterPlayerController* player_controller, FString* message, EChatSendMode::Type /*unused*/)
  373. {
  374. TArray<FString> parsed;
  375. message->ParseIntoArray(parsed, L" ", true);
  376.  
  377. int page = 0;
  378.  
  379. if (parsed.IsValidIndex(1))
  380. {
  381. try
  382. {
  383. page = std::stoi(*parsed[1]) - 1;
  384. }
  385. catch (const std::exception&)
  386. {
  387. return;
  388. }
  389. }
  390. else
  391. {
  392. ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"),
  393. *GetText("ShopUsage"));
  394. }
  395.  
  396. if (page < 0)
  397. {
  398. return;
  399. }
  400.  
  401. auto items_list = config["ShopItems"];
  402.  
  403. const int items_per_page = config["General"].value("ItemsPerPage", 20);
  404. const float display_time = config["General"].value("ShopDisplayTime", 15.0f);
  405. const float text_size = config["General"].value("ShopTextSize", 1.3f);
  406.  
  407. const unsigned start_index = page * items_per_page;
  408. if (start_index >= items_list.size())
  409. {
  410. return;
  411. }
  412.  
  413. auto start = items_list.begin();
  414. advance(start, start_index);
  415.  
  416. FString store_str = "";
  417.  
  418. for (auto iter = start; iter != items_list.end(); ++iter)
  419. {
  420. const size_t i = distance(items_list.begin(), iter);
  421. if (i == start_index + items_per_page)
  422. {
  423. break;
  424. }
  425.  
  426. auto item = iter.value();
  427.  
  428. const int price = item["Price"];
  429. const std::string type = item["Type"];
  430. const std::wstring description = ArkApi::Tools::Utf8Decode(item.value("Description", "No description"));
  431.  
  432. if (type == "dino")
  433. {
  434. const int level = item["Level"];
  435.  
  436. store_str += FString::Format(*GetText("StoreListDino"), i + 1, description, level,
  437. ArkApi::Tools::Utf8Decode(iter.key()), price);
  438. }
  439. else
  440. {
  441. store_str += FString::Format(*GetText("StoreListItem"), i + 1, description,
  442. ArkApi::Tools::Utf8Decode(iter.key()),
  443. price);
  444. }
  445. }
  446.  
  447. store_str = FString::Format(*GetText("StoreListFormat"), *store_str);
  448.  
  449. ArkApi::GetApiUtils().SendNotification(player_controller, FColorList::Green, text_size, display_time, nullptr,
  450. *store_str);
  451.  
  452. FString shopmessage = GetText("ShopMessage");
  453. if (shopmessage != ArkApi::Tools::Utf8Decode("No message").c_str())
  454. {
  455. shopmessage = FString::Format(*shopmessage, page + 1,
  456. items_list.size() % items_per_page == 0
  457. ? items_list.size() / items_per_page
  458. : items_list.size() / items_per_page + 1);
  459. ArkApi::GetApiUtils().SendNotification(player_controller, FColorList::Green, text_size, display_time,
  460. nullptr,
  461. *shopmessage);
  462. }
  463. }
  464.  
  465. bool IsStoreEnabled(AShooterPlayerController* player_controller)
  466. {
  467. return AtlasShop::IsStoreEnabled(player_controller);
  468. }
  469.  
  470. void ToogleStore(bool Enabled, const FString& Reason)
  471. {
  472. AtlasShop::ToogleStore(Enabled, Reason);
  473. }
  474.  
  475. void Init()
  476. {
  477. auto& commands = ArkApi::GetCommands();
  478.  
  479. commands.AddChatCommand(GetText("BuyCmd"), &ChatBuy);
  480. commands.AddChatCommand(GetText("ShopCmd"), &ShowItems);
  481. }
  482. } // namespace Store // namespace AtlasShop
Advertisement
Add Comment
Please, Sign In to add comment