Skip to content

Commit

Permalink
Initial wormhole stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
alicealys committed Sep 30, 2023
1 parent 9bd3a23 commit 3f1b891
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 11 deletions.
133 changes: 130 additions & 3 deletions src/server/database/models/wormholes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace database::wormholes
}

void add_wormhole(const std::uint64_t player_id, const std::uint64_t to_player_id,
const wormhole_flag flag, const std::uint32_t retaliate_point)
const wormhole_flag flag, const bool is_open, const std::uint32_t retaliate_point)
{
database::access([&](database_t& db)
{
Expand All @@ -53,14 +53,141 @@ namespace database::wormholes
wormholes_table.to_player_id = to_player_id,
wormholes_table.flag = static_cast<std::uint32_t>(flag),
wormholes_table.retaliate_score = retaliate_point,
wormholes_table.is_open = is_open,
wormholes_table.create_date = std::chrono::system_clock::now())
);
});
}

std::vector<wormhole> find_active_wormholes(const std::uint64_t player_id)
std::vector<wormhole> find_active_wormholes(const std::uint64_t player_id, const std::uint64_t other_player_id)
{
return {};
return database::access<std::vector<wormhole>>([&](database_t& db)
{
auto results = db->operator()(
sqlpp::select(sqlpp::all_of(wormholes_table))
.from(wormholes_table)
.where(wormholes_table.is_open == true && wormholes_table.retaliate_score > 0 &&
((wormholes_table.player_id == player_id && wormholes_table.to_player_id == other_player_id) ||
(wormholes_table.player_id == other_player_id && wormholes_table.to_player_id == player_id)))
);

std::vector<wormhole> list;

const auto now = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch());

for (auto& row : results)
{
wormhole w(row);

if (now - w.get_create_date() > 24h * 30)
{
continue;
}

list.emplace_back(w);
}

return list;
});
}

std::unordered_map<std::uint64_t, wormhole_status> find_active_wormholes(const std::uint64_t player_id)
{
return database::access<std::unordered_map<std::uint64_t, wormhole_status>>([&](database_t& db)
{
auto results = db->operator()(
sqlpp::select(sqlpp::all_of(wormholes_table))
.from(wormholes_table)
.where(wormholes_table.is_open == true && wormholes_table.retaliate_score > 0 &&
wormholes_table.player_id == player_id || wormholes_table.to_player_id == player_id)
);

std::unordered_map<std::uint64_t, wormhole_status> list;

const auto now = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch());

for (auto& row : results)
{
wormhole w(row);

const auto to_player_id = player_id == w.get_to_player_id()
? w.get_player_id() : w.get_to_player_id();

auto& status = list[to_player_id];

status.player_id = player_id;
status.to_player_id = to_player_id;

if (now - w.get_create_date() > wormhole_duration)
{
continue;
}

if (w.get_to_player_id() == to_player_id)
{
status.score += w.get_retaliate_score();
}
else
{
status.first = false;
status.score -= w.get_retaliate_score();
}

if (w.get_create_date() > status.expire)
{
status.expire = w.get_create_date();
}
}

for (auto i = list.begin(), end = list.end(); i != end; )
{
if (i->second.score > 0)
{
i->second.open = true;
i->second.expire += wormhole_duration;
++i;
}
else
{
i = list.erase(i);
}
}

return list;
});
}

wormhole_status get_wormhole_status(const std::uint64_t from_player_id, const std::uint64_t to_player_id)
{
const auto wormholes = find_active_wormholes(from_player_id, to_player_id);

wormhole_status status{};
status.first = true;

for (auto& wormhole : wormholes)
{
if (wormhole.get_to_player_id() == to_player_id)
{
status.score += wormhole.get_retaliate_score();
}
else
{
status.first = false;
status.score -= wormhole.get_retaliate_score();
}

if (wormhole.get_create_date() > status.expire)
{
status.expire = wormhole.get_create_date();
}
}

status.expire += wormhole_duration;
status.open = status.score > 0;

return status;
}

class table final : public table_interface
Expand Down
22 changes: 18 additions & 4 deletions src/server/database/models/wormholes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace database::wormholes
DEFINE_FIELD(is_open, sqlpp::boolean);
DEFINE_FIELD(create_date, sqlpp::time_point);
DEFINE_TABLE(wormholes, id_field_t, player_id_field_t, to_player_id_field_t,
retaliate_score_field_t, flag_field_t, create_date_field_t);
retaliate_score_field_t, flag_field_t, is_open_field_t, create_date_field_t);

enum wormhole_flag
{
Expand All @@ -26,6 +26,8 @@ namespace database::wormholes
wormhole_flag_count
};

constexpr auto wormhole_duration = 24h * 31;

class wormhole
{
public:
Expand All @@ -36,7 +38,7 @@ namespace database::wormholes
this->player_id_ = row.player_id;
this->to_player_id_ = row.to_player_id;
this->retaliate_score_ = static_cast<std::uint32_t>(row.retaliate_score);
this->flag_ = static_cast<wormhole_flag>(row.flag);
this->flag_ = static_cast<wormhole_flag>(static_cast<std::uint32_t>(row.flag));
this->is_open_ = row.is_open;
this->create_date_ = row.create_date.value().time_since_epoch();
}
Expand Down Expand Up @@ -87,10 +89,22 @@ namespace database::wormholes

};

struct wormhole_status
{
std::uint64_t player_id;
std::uint64_t to_player_id;
std::uint32_t score;
bool open;
bool first;
std::chrono::microseconds expire;
};

wormhole_flag get_flag_id(const std::string& flag);

void add_wormhole(const std::uint64_t player_id, const std::uint64_t to_player_id,
const wormhole_flag flag, const std::uint32_t retaliate_point);
const wormhole_flag flag, const bool is_open, const std::uint32_t retaliate_point);

std::unordered_map<std::uint64_t, wormhole_status> find_active_wormholes(const std::uint64_t player_id);

std::vector<wormhole> find_active_wormholes(const std::uint64_t player_id);
wormhole_status get_wormhole_status(const std::uint64_t from_player_id, const std::uint64_t to_player_id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "database/models/fobs.hpp"
#include "database/models/player_records.hpp"
#include "database/models/players.hpp"
#include "database/models/wormholes.hpp"

#include <utils/nt.hpp>

Expand Down Expand Up @@ -168,7 +169,9 @@ namespace tpp
result["session"]["xnkid"] = {};
result["session"]["xuid"] = 0;

if (owner_record->is_shield_active())
const auto wormhole = database::wormholes::get_wormhole_status(player->get_id(), fob->get_player_id());

if ((!wormhole.open || !wormhole.first) && owner_record->is_shield_active())
{
result["result"] = "ERR_SNEAK_RESTRICTION";
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "database/models/player_records.hpp"
#include "database/models/players.hpp"
#include "database/models/sneak_results.hpp"
#include "database/models/wormholes.hpp"

#include <utils/nt.hpp>

Expand Down Expand Up @@ -53,19 +54,68 @@ namespace tpp
return targets;
}

target_list_t get_enemy_list(CALLBACK_ARGS)
{
auto list = database::wormholes::find_active_wormholes(player.get_id());
target_list_t targets;

const auto now = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch());

for (auto& [to_player_id, wormhole] : list)
{
target_data_t target{};

auto left_hour = 0;
if (wormhole.expire > now)
{
const auto diff = wormhole.expire - now;
left_hour = std::chrono::duration_cast<std::chrono::hours>(diff).count();
}

target.extra_data["owner_detail_record"]["enemy"] = 1;
target.extra_data["owner_fob_record"]["left_hour"] = left_hour;

target.player_id = wormhole.to_player_id;

targets.emplace_back(target);
}

return targets;
}

target_list_t get_injury_list(CALLBACK_ARGS)
{
auto list = database::sneak_results::get_sneak_results(player.get_id(), std::min(limit, 10u));
target_list_t targets;

const auto now = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch());

for (auto& row : list)
{
target_data_t target{};

auto& sneak_data = row.get_data();

const auto wormhole = database::wormholes::get_wormhole_status(player.get_id(), row.get_player_id());

if (wormhole.open)
{
auto left_hour = 0;
if (wormhole.expire > now)
{
const auto diff = wormhole.expire - now;
left_hour = std::chrono::duration_cast<std::chrono::hours>(diff).count();
}

target.extra_data["owner_detail_record"]["enemy"] = 1;
target.extra_data["owner_fob_record"]["left_hour"] = left_hour;
}

target.extra_data["owner_fob_record"]["injury_staff_count"] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
target.extra_data["is_win"] = sneak_data["sneak_result"] == "WIN";
target.extra_data["is_win"] = static_cast<int>(row.is_win());
target.extra_data["cluster"] = row.get_platform();

for (auto i = 0; i < sneak_data["injure_soldier_id"].size(); i++)
{
Expand Down Expand Up @@ -205,6 +255,7 @@ namespace tpp
{
{"PICKUP", get_pickup_list},
{"PICKUP_HIGH", get_pickup_high_list},
{"ENEMY", get_enemy_list},
{"INJURY", get_injury_list},
{"CHALLENGE", get_challenge_list},
{"DEPLOYED", get_deployed_list},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ namespace tpp
}

const auto to_player_id = to_player_id_j.get<std::uint64_t>();
const auto retaliate_score = to_player_id_j.get<std::uint32_t>();
const auto retaliate_score = retaliate_score_j.get<std::uint32_t>();
const auto flag = flag_j.get<std::string>();
const auto flag_id = database::wormholes::get_flag_id(flag);
const auto is_open = data["is_open"] == 1;

if (flag_id == database::wormholes::wormhole_flag_invalid)
{
result["result"] = "ERR_INVALIDARG";
return result;
}

database::wormholes::add_wormhole(player->get_id(), to_player_id, flag_id, retaliate_score);
database::wormholes::add_wormhole(player->get_id(), to_player_id, flag_id, is_open, retaliate_score);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "cmd_set_security_challenge.hpp"

#include "database/models/players.hpp"
#include "database/models/player_records.hpp"

#include <utils/nt.hpp>

Expand All @@ -24,6 +25,8 @@ namespace tpp
result["result"] = "ERR_DATABASE";
}

database::player_records::clear_shield_date(player->get_id());

return result;
}
}

0 comments on commit 3f1b891

Please sign in to comment.