Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. #include <API/ARK/Ark.h>
  4. #include <API/UE/Math/ColorList.h>
  5. #include <Logger/Logger.h>
  6.  
  7. #include "json.hpp"
  8.  
  9.  
  10.  
  11. #pragma comment(lib, "ArkApi.lib")
  12.  
  13.  
  14. nlohmann::json config;
  15.  
  16. FString GetText(const std::string& str)
  17. {
  18.     return FString(ArkApi::Tools::Utf8Decode(config["Messages"].value(str, "No message")).c_str());
  19. }
  20.  
  21. void ReadConfig()
  22. {
  23.     const std::string config_path = ArkApi::Tools::GetCurrentDir() + "/ArkApi/Plugins/CustomShop/config.json";
  24.     std::ifstream file{ config_path };
  25.     if (!file.is_open())
  26.         throw std::runtime_error("Can't open config.json");
  27.  
  28.     file >> config;
  29.  
  30.     file.close();
  31. }
  32.  
  33.  
  34.  
  35.  
  36. bool SellItem(AShooterPlayerController* player_controller, const nlohmann::basic_json<>& item_entry, uint64 steam_id, int amount)
  37. {
  38.     bool success = false;
  39.  
  40.  
  41.     auto items_map = item_entry["ReqItems"];
  42.     for (const auto& item : items_map)
  43.     {
  44.         const int default_amount = item["Amount"];
  45.         const std::string blueprint = item["Blueprint"];
  46.  
  47.         const int final_amount = default_amount * amount;
  48.         if (final_amount <= 0)
  49.             return false;
  50.  
  51.         FString fblueprint(blueprint);
  52.  
  53.                    
  54.         UPrimalInventoryComponent* inventory = player_controller->GetPlayerCharacter()->MyInventoryComponentField();
  55.         if (inventory == nullptr)
  56.         {
  57.             return false;
  58.         }
  59.  
  60.         int item_count = 0;
  61.  
  62.         // Count items
  63.  
  64.         TArray<UPrimalItem*> items_for_removal;
  65.  
  66.         TArray<UPrimalItem*> items = inventory->InventoryItemsField();
  67.         for (UPrimalItem* item : items)
  68.         {
  69.             if (item->ClassField() != nullptr)
  70.             {
  71.                 const FString item_bp = ArkApi::IApiUtils::GetItemBlueprint(item);
  72.  
  73.                 if (item_bp == fblueprint)
  74.                 {
  75.                     items_for_removal.Add(item);
  76.  
  77.                     item_count += item->GetItemQuantity();
  78.                     if (item_count >= final_amount)
  79.                     {
  80.                         break;
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.  
  86.         if (item_count >= final_amount)
  87.         {
  88.             item_count = 0;
  89.  
  90.             // Remove items
  91.             for (UPrimalItem* item : items_for_removal)
  92.             {
  93.                 item_count += item->GetItemQuantity();
  94.  
  95.                 if (item_count > final_amount)
  96.                 {
  97.                     item->SetQuantity(item_count - final_amount, true);
  98.                     inventory->NotifyClientsItemStatus(item, false, false, true, false, false, nullptr, nullptr, false, false, true);
  99.                 }
  100.                 else
  101.                 {
  102.                     inventory->RemoveItem(&item->ItemIDField(), false, false, true, true);
  103.                 }
  104.             }
  105.  
  106.             ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"), *GetText("SoldItems"));
  107.  
  108.             success = true;
  109.         }
  110.         else
  111.         {
  112.             ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"), *GetText("NotEnoughItems"), item_count, final_amount);
  113.         }
  114.         return success;
  115.  
  116.  
  117.     }
  118.  
  119.  
  120.        
  121. //const FString blueprint = FString(item_entry.value("Blueprint", "").c_str());
  122. //  const int needed_amount = item_entry.value("Amount", 1) * amount;
  123.  
  124.    
  125. }
  126.  
  127. bool Sell(AShooterPlayerController* player_controller, const FString& item_id, int amount)
  128. {
  129.     if (ArkApi::IApiUtils::IsPlayerDead(player_controller))
  130.         return false;
  131.  
  132.     if (amount <= 0)
  133.         amount = 1;
  134.  
  135.     bool success = false;
  136.  
  137.     const uint64 steam_id = ArkApi::IApiUtils::GetSteamIdFromController(player_controller);
  138.  
  139.    
  140.         auto items_list = config.value("BuyItems", nlohmann::json::object());
  141.  
  142.         auto item_entry_iter = items_list.find(item_id.ToString());
  143.         if (item_entry_iter == items_list.end())
  144.         {
  145.             ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"), *GetText("WrongId"));
  146.             return false;
  147.         }
  148.  
  149.         auto item_entry = item_entry_iter.value();
  150.  
  151.         success = SellItem(player_controller, item_entry, steam_id, amount);
  152.  
  153.         if (success)
  154.         {
  155.             const std::wstring log = fmt::format(TEXT("{}({}) sold item \"{}\". Amount - {}")*ArkApi::IApiUtils::GetSteamName(player_controller), steam_id, *item_id,    amount);
  156.  
  157.         //  ShopLog::GetLog()->info(ArkApi::Tools::Utf8Encode(log));
  158.         }
  159.    
  160.  
  161.     return success;
  162. }
  163.  
  164. void ChatSell(AShooterPlayerController* player_controller, FString* message, EChatSendMode::Type)
  165. {
  166.     TArray<FString> parsed;
  167.     message->ParseIntoArray(parsed, L" ", true);
  168.  
  169.     if (parsed.IsValidIndex(1))
  170.     {
  171.        
  172.  
  173.         Sell(player_controller, parsed[1], 1);
  174.     }
  175.     else
  176.     {
  177.         ArkApi::GetApiUtils().SendChatMessage(player_controller, GetText("Sender"), *GetText("SellUsage"));
  178.     }
  179. }
  180.  
  181.  
  182.  
  183. void Load()
  184. {
  185.     Log::Get().Init("CustomShop");
  186.  
  187.     try
  188.     {
  189.         ReadConfig();
  190.     }
  191.     catch (const std::exception& error)
  192.     {
  193.         Log::GetLog()->error(error.what());
  194.         throw;
  195.     }
  196.  
  197.     ArkApi::GetCommands().AddChatCommand(GetText("SellCmd"), &ChatSell);
  198. }
  199.  
  200. void Unload()
  201. {
  202.  
  203. }
  204.  
  205.  
  206. BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  207. {
  208.     switch (ul_reason_for_call)
  209.     {
  210.     case DLL_PROCESS_ATTACH:
  211.         Load();
  212.         break;
  213.     case DLL_PROCESS_DETACH:
  214.         Unload();
  215.         break;
  216.     }
  217.     return TRUE;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement