144 lines
3.9 KiB
C++
144 lines
3.9 KiB
C++
#include "precompile.h"
|
|
|
|
#include "custom_battle.h"
|
|
#include "custom_team.h"
|
|
#include "custom_member.h"
|
|
#include "netdata.h"
|
|
|
|
void CustomBattle::Init()
|
|
{
|
|
|
|
}
|
|
|
|
void CustomBattle::UnInit()
|
|
{
|
|
|
|
}
|
|
|
|
void CustomBattle::ParseResult(a8::XObject& obj)
|
|
{
|
|
if (!obj.HasKey("errcode")) {
|
|
parse_ok_ = false;
|
|
return;
|
|
}
|
|
room_id_ = obj.Get("room_id").GetString();
|
|
room_uuid_ = obj.Get("room_uuid").GetString();
|
|
zone_id_ = obj.Get("zone_id");
|
|
node_id_ = obj.Get("node_id");
|
|
start_time_ = obj.Get("start_time");
|
|
sign_ = obj.Get("sign").GetString();
|
|
|
|
auto team_list = obj.At("team_list");
|
|
if (!team_list || !team_list->IsArray()) {
|
|
parse_ok_ = false;
|
|
return;
|
|
}
|
|
for (int i = 0;i < team_list->Size(); ++i) {
|
|
auto team_obj = team_list->At(i);
|
|
if (!team_obj || !team_obj->IsObject()) {
|
|
parse_ok_ = false;
|
|
return;
|
|
}
|
|
std::string team_uuid = team_obj->Get("team_uuid").GetString();
|
|
int is_view = team_obj->Get("is_view").GetInt();
|
|
auto member_list = team_obj->At("members");
|
|
if (!member_list ||
|
|
!member_list->IsArray()) {
|
|
parse_ok_ = false;
|
|
return;
|
|
}
|
|
auto team = GetTeamByTeamUuid(team_uuid);
|
|
if (!team) {
|
|
team = std::make_shared<CustomTeam>();
|
|
team->team_uuid_ = team_uuid;
|
|
team->is_view_ = is_view ? true : false;
|
|
uuid_hash_[team->team_uuid_] = team;
|
|
}
|
|
for (int ii = 0; ii < member_list->Size(); ++ii) {
|
|
auto member_obj = member_list->At(ii);
|
|
if (!member_obj || !member_obj->IsObject()) {
|
|
parse_ok_ = false;
|
|
return;
|
|
}
|
|
auto member = std::make_shared<CustomMember>();
|
|
member->team_ = team.get();
|
|
member->account_id_ = member_obj->Get("account_id").GetString();
|
|
member->session_id_ = member_obj->Get("session_id").GetString();
|
|
member->battle_context_ = std::make_shared<BattleDataContext>();
|
|
member->battle_context_->ParseResult(*member_obj);
|
|
if (!member->battle_context_->parse_ok) {
|
|
parse_ok_ = false;
|
|
return;
|
|
}
|
|
member_id_hash_[member->account_id_] = member;
|
|
team->member_hash_[member->account_id_] = member;
|
|
account_hash_[member->account_id_] = team;
|
|
}
|
|
}
|
|
|
|
raw_data_ = std::make_shared<a8::XObject>();
|
|
obj.DeepCopy(*raw_data_);
|
|
parse_ok_ = true;
|
|
}
|
|
|
|
bool CustomBattle::CanAdd(const std::string& account_id, const std::string& session_id)
|
|
{
|
|
auto member = GetMemberByAccountId(account_id);
|
|
if (!member) {
|
|
return false;
|
|
}
|
|
if (member->IsJoined()) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::shared_ptr<CustomMember> CustomBattle::GetMemberByAccountId(const std::string& account_id)
|
|
{
|
|
auto itr = member_id_hash_.find(account_id);
|
|
return itr != member_id_hash_.end() ? itr->second : nullptr;
|
|
}
|
|
|
|
std::shared_ptr<CustomTeam> CustomBattle::GetTeamByAccountId(const std::string& account_id)
|
|
{
|
|
auto itr = account_hash_.find(account_id);
|
|
return itr != account_hash_.end() ? itr->second : nullptr;
|
|
}
|
|
|
|
std::shared_ptr<CustomTeam> CustomBattle::GetTeamByTeamUuid(const std::string& team_uuid)
|
|
{
|
|
auto itr = uuid_hash_.find(team_uuid);
|
|
return itr != uuid_hash_.end() ? itr->second : nullptr;
|
|
}
|
|
|
|
bool CustomBattle::AllIsJoined()
|
|
{
|
|
bool ok = true;
|
|
for (auto& pair : member_id_hash_) {
|
|
if (!pair.second->IsJoined()) {
|
|
ok = false;
|
|
break;
|
|
}
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
int CustomBattle::GetMemberNum()
|
|
{
|
|
return member_id_hash_.size();
|
|
}
|
|
|
|
int CustomBattle::GetTeamNum()
|
|
{
|
|
return uuid_hash_.size();
|
|
}
|
|
|
|
void CustomBattle::TraverseMemberList(std::function<bool (CustomMember*)> func)
|
|
{
|
|
for (auto& pair : member_id_hash_) {
|
|
if (!func(pair.second.get())) {
|
|
break;
|
|
}
|
|
}
|
|
}
|