· 4 years ago · Nov 19, 2020, 10:18 AM
1
2#ifdef __ALLOW_BOTS__
3void AccountAPI::joinBots(zpoker::IWaitlistStorage::Ptr& waitlistStorage, zpoker::BalanceController::Ptr& balanceController,
4 const zpoker::player_pool_id_t& ppId, const zpoker::id_t& buyInConfigId, const zpoker::Funds& funds,
5 const std::size_t& botsCount, const zpoker::WaitlistEntryStatus& waitlistEntryStatus,
6 std::vector<zpoker::JoinResultEntry::CPtr>& joinResults)
7{
8 auto db{ getPrimaryConnection() };
9
10 for (std::size_t i{ 0 }; i < botsCount; ++i)
11 {
12 const auto newAiId{ ++m_AiId };
13 const auto nickname{ "bot_bot" + std::to_string(newAiId) };
14 const auto password{ "1qazXSW@" + std::to_string(newAiId) };
15 const auto email{ "bot" + std::to_string(newAiId) + "@gmail.com" };
16
17 if (!isEmailInUse(db, email))
18 {
19 nlohmann::json signUpBody{ { "nickname", nickname },
20 { "password", password },
21 { "first_name", "bot" },
22 { "second_name", "bot" },
23 { "email", email },
24 { "phone_number", "+380989035" + std::to_string(newAiId) },
25 { "gender", "M" },
26 { "country_id", std::uint64_t{ 1 } },
27 { "dob", "1991-11-13" },
28 { "zip", "49000" },
29 { "city", "Dnipro" },
30 { "address1", "Robocha 19/31" },
31 { "address2", "Robocha 19/31" },
32 { "registration_ip", "127.0.0.1" },
33 { "is_bot", true } };
34
35 signUp(balanceController, signUpBody);
36 }
37
38 const auto loginResult{ login(nickname, password) };
39
40 if (!loginResult.first)
41 {
42 LOG(ERROR) << "AccountAPI::joinBots -> failed to login a bot";
43 continue;
44 }
45
46 const auto botId{ loginResult.second.value("player_id", zpoker::getInvalidId()) };
47
48 const auto waitlistEntryId{ waitlistStorage->createNewWaitlistEntry(
49 botId, ppId, buyInConfigId, waitlistEntryStatus, true) };
50
51 const auto updateBalanceResult{ updateBalance(
52 balanceController,
53 botId,
54 { zpoker::UpdateBalanceReason::Reserve, funds, waitlistEntryId, ppId, buyInConfigId, {}, {}, {} }) };
55
56 if (zpoker::UpdateBalanceResultCodeTools::isCriticalFailCode(updateBalanceResult.first))
57 {
58 continue;
59 }
60
61 // join the best suitable table or create new one and join
62 const auto result{ waitlistStorage->joinExistingOrCreateNewTable(botId, ppId, buyInConfigId, waitlistEntryId) };
63
64 if (result)
65 {
66 for (const auto& data : *result)
67 {
68 auto it{ std::find_if(
69 std::begin(joinResults), std::end(joinResults), [&data](const zpoker::JoinResultEntry::CPtr& joinResult) {
70 return joinResult->getTableId() == data->getTableId();
71 }) };
72
73 if (it == std::end(joinResults))
74 {
75 joinResults.push_back(data);
76 }
77 else
78 {
79 *it = data;
80 }
81 }
82 }
83 }
84}
85#endif