use new phosg JSON interface

This commit is contained in:
Martin Michelsen
2023-08-28 21:06:03 -07:00
parent a842880123
commit 466eb49c55
17 changed files with 404 additions and 487 deletions
+122 -143
View File
@@ -1083,151 +1083,45 @@ void PlayerConfig::encrypt(uint8_t basis) {
this->basis = basis;
}
HPType hp_type_for_name(const char* name) {
if (!strcmp(name, "DEFEAT_PLAYER")) {
return HPType::DEFEAT_PLAYER;
} else if (!strcmp(name, "DEFEAT_TEAM")) {
return HPType::DEFEAT_TEAM;
} else if (!strcmp(name, "COMMON_HP")) {
return HPType::COMMON_HP;
} else {
throw out_of_range("invalid HP type name");
}
}
const char* name_for_hp_type(HPType hp_type) {
switch (hp_type) {
case HPType::DEFEAT_PLAYER:
return "DEFEAT_PLAYER";
case HPType::DEFEAT_TEAM:
return "DEFEAT_TEAM";
case HPType::COMMON_HP:
return "COMMON_HP";
default:
throw out_of_range("invalid HP type");
}
}
DiceExchangeMode dice_exchange_mode_for_name(const char* name) {
if (!strcmp(name, "HIGH_ATK")) {
return DiceExchangeMode::HIGH_ATK;
} else if (!strcmp(name, "HIGH_DEF")) {
return DiceExchangeMode::HIGH_DEF;
} else if (!strcmp(name, "NONE")) {
return DiceExchangeMode::NONE;
} else {
throw out_of_range("invalid dice exchange mode name");
}
}
const char* name_for_dice_exchange_mode(DiceExchangeMode dice_exchange_mode) {
switch (dice_exchange_mode) {
case DiceExchangeMode::HIGH_ATK:
return "HIGH_ATK";
case DiceExchangeMode::HIGH_DEF:
return "HIGH_DEF";
case DiceExchangeMode::NONE:
return "NONE";
default:
throw out_of_range("invalid dice exchange mode");
}
}
AllowedCards allowed_cards_for_name(const char* name) {
if (!strcmp(name, "ALL")) {
return AllowedCards::ALL;
} else if (!strcmp(name, "N_ONLY")) {
return AllowedCards::N_ONLY;
} else if (!strcmp(name, "N_R_ONLY")) {
return AllowedCards::N_R_ONLY;
} else if (!strcmp(name, "N_R_S_ONLY")) {
return AllowedCards::N_R_S_ONLY;
} else {
throw out_of_range("invalid allowed cards name");
}
}
const char* name_for_allowed_cards(AllowedCards allowed_cards) {
switch (allowed_cards) {
case AllowedCards::ALL:
return "ALL";
case AllowedCards::N_ONLY:
return "N_ONLY";
case AllowedCards::N_R_ONLY:
return "N_R_ONLY";
case AllowedCards::N_R_S_ONLY:
return "N_R_S_ONLY";
default:
throw out_of_range("invalid allowed cards");
}
}
Rules::Rules() {
this->clear();
}
template <typename IntT>
void get_json_optional_int(const JSONObject::dict_type& dict, IntT& result, const char* key) {
try {
auto value_json = dict.at(key);
if (value_json->is_int()) {
result = value_json->as_int();
} else if (value_json->is_bool()) {
result = value_json->as_bool() ? 1 : 0;
} else {
throw runtime_error("int-valued field is not int or bool");
}
} catch (const out_of_range&) {
}
};
template <typename EnumT>
void get_json_optional_enum(const JSONObject::dict_type& dict, EnumT& result, const char* key, EnumT (*parse_str)(const char*)) {
try {
auto value_json = dict.at(key);
if (value_json->is_string()) {
result = parse_str(value_json->as_string().c_str());
} else if (value_json->is_int()) {
result = static_cast<EnumT>(value_json->as_int());
} else {
throw runtime_error("enum-valued field is not int or string");
}
} catch (const out_of_range&) {
}
};
Rules::Rules(shared_ptr<const JSONObject> json) {
Rules::Rules(const JSON& json) {
this->clear();
auto dict = json->as_dict();
get_json_optional_int(dict, this->overall_time_limit, "overall_time_limit");
get_json_optional_int(dict, this->phase_time_limit, "phase_time_limit");
get_json_optional_enum(dict, this->allowed_cards, "allowed_cards", allowed_cards_for_name);
get_json_optional_int(dict, this->min_dice, "min_dice");
get_json_optional_int(dict, this->max_dice, "max_dice");
get_json_optional_int(dict, this->disable_deck_shuffle, "disable_deck_shuffle");
get_json_optional_int(dict, this->disable_deck_loop, "disable_deck_loop");
get_json_optional_int(dict, this->char_hp, "char_hp");
get_json_optional_enum(dict, this->hp_type, "hp_type", hp_type_for_name);
get_json_optional_int(dict, this->no_assist_cards, "no_assist_cards");
get_json_optional_int(dict, this->disable_dialogue, "disable_dialogue");
get_json_optional_enum(dict, this->dice_exchange_mode, "dice_exchange_mode", dice_exchange_mode_for_name);
get_json_optional_int(dict, this->disable_dice_boost, "disable_dice_boost");
auto dict = json.as_dict();
this->overall_time_limit = json.get("overall_time_limit", this->overall_time_limit);
this->phase_time_limit = json.get("phase_time_limit", this->phase_time_limit);
this->allowed_cards = json.get_enum("allowed_cards", this->allowed_cards);
this->min_dice = json.get("min_dice", this->min_dice);
this->max_dice = json.get("max_dice", this->max_dice);
this->disable_deck_shuffle = json.get("disable_deck_shuffle", this->disable_deck_shuffle);
this->disable_deck_loop = json.get("disable_deck_loop", this->disable_deck_loop);
this->char_hp = json.get("char_hp", this->char_hp);
this->hp_type = json.get_enum("hp_type", this->hp_type);
this->no_assist_cards = json.get("no_assist_cards", this->no_assist_cards);
this->disable_dialogue = json.get("disable_dialogue", this->disable_dialogue);
this->dice_exchange_mode = json.get_enum("dice_exchange_mode", this->dice_exchange_mode);
this->disable_dice_boost = json.get("disable_dice_boost", this->disable_dice_boost);
}
shared_ptr<JSONObject> Rules::json() const {
unordered_map<string, shared_ptr<JSONObject>> dict;
dict.emplace("overall_time_limit", make_json_int(this->overall_time_limit));
dict.emplace("phase_time_limit", make_json_int(this->phase_time_limit));
dict.emplace("allowed_cards", make_json_str(name_for_allowed_cards(this->allowed_cards)));
dict.emplace("min_dice", make_json_int(this->min_dice));
dict.emplace("max_dice", make_json_int(this->max_dice));
dict.emplace("disable_deck_shuffle", make_json_bool(this->disable_deck_shuffle));
dict.emplace("disable_deck_loop", make_json_bool(this->disable_deck_loop));
dict.emplace("char_hp", make_json_int(this->char_hp));
dict.emplace("hp_type", make_json_str(name_for_hp_type(this->hp_type)));
dict.emplace("no_assist_cards", make_json_bool(this->no_assist_cards));
dict.emplace("disable_dialogue", make_json_bool(this->disable_dialogue));
dict.emplace("dice_exchange_mode", make_json_str(name_for_dice_exchange_mode(this->dice_exchange_mode)));
dict.emplace("disable_dice_boost", make_json_bool(this->disable_dice_boost));
return shared_ptr<JSONObject>(new JSONObject(std::move(dict)));
JSON Rules::json() const {
unordered_map<string, JSON> dict;
dict.emplace("overall_time_limit", this->overall_time_limit);
dict.emplace("phase_time_limit", this->phase_time_limit);
dict.emplace("allowed_cards", name_for_enum(this->allowed_cards));
dict.emplace("min_dice", this->min_dice);
dict.emplace("max_dice", this->max_dice);
dict.emplace("disable_deck_shuffle", this->disable_deck_shuffle);
dict.emplace("disable_deck_loop", this->disable_deck_loop);
dict.emplace("char_hp", this->char_hp);
dict.emplace("hp_type", name_for_enum(this->hp_type));
dict.emplace("no_assist_cards", this->no_assist_cards);
dict.emplace("disable_dialogue", this->disable_dialogue);
dict.emplace("dice_exchange_mode", name_for_enum(this->dice_exchange_mode));
dict.emplace("disable_dice_boost", this->disable_dice_boost);
return JSON(std::move(dict));
}
void Rules::set_defaults() {
@@ -2106,15 +2000,15 @@ set<uint32_t> MapIndex::all_numbers() const {
COMDeckIndex::COMDeckIndex(const string& filename) {
try {
auto json = JSONObject::parse(load_file(filename));
for (const auto& def_json : json->as_list()) {
auto json = JSON::parse(load_file(filename));
for (const auto& def_json : json.as_list()) {
auto& def = this->decks.emplace_back(new COMDeckDefinition());
def->index = this->decks.size() - 1;
def->player_name = def_json->at(0)->as_string();
def->deck_name = def_json->at(1)->as_string();
auto card_ids_json = def_json->at(2)->as_list();
def->player_name = def_json.at(0).as_string();
def->deck_name = def_json.at(1).as_string();
auto card_ids_json = def_json.at(2);
for (size_t z = 0; z < 0x1F; z++) {
def->card_ids[z] = card_ids_json.at(z)->as_int();
def->card_ids[z] = card_ids_json.at(z).as_int();
}
if (!this->decks_by_name.emplace(def->deck_name, def).second) {
throw runtime_error("duplicate COM deck name: " + def->deck_name);
@@ -2142,3 +2036,88 @@ shared_ptr<const COMDeckDefinition> COMDeckIndex::random_deck() const {
}
} // namespace Episode3
template <>
Episode3::HPType enum_for_name<Episode3::HPType>(const char* name) {
if (!strcmp(name, "DEFEAT_PLAYER")) {
return Episode3::HPType::DEFEAT_PLAYER;
} else if (!strcmp(name, "DEFEAT_TEAM")) {
return Episode3::HPType::DEFEAT_TEAM;
} else if (!strcmp(name, "COMMON_HP")) {
return Episode3::HPType::COMMON_HP;
} else {
throw out_of_range("invalid HP type name");
}
}
template <>
const char* name_for_enum<Episode3::HPType>(Episode3::HPType hp_type) {
switch (hp_type) {
case Episode3::HPType::DEFEAT_PLAYER:
return "DEFEAT_PLAYER";
case Episode3::HPType::DEFEAT_TEAM:
return "DEFEAT_TEAM";
case Episode3::HPType::COMMON_HP:
return "COMMON_HP";
default:
throw out_of_range("invalid HP type");
}
}
template <>
Episode3::DiceExchangeMode enum_for_name<Episode3::DiceExchangeMode>(const char* name) {
if (!strcmp(name, "HIGH_ATK")) {
return Episode3::DiceExchangeMode::HIGH_ATK;
} else if (!strcmp(name, "HIGH_DEF")) {
return Episode3::DiceExchangeMode::HIGH_DEF;
} else if (!strcmp(name, "NONE")) {
return Episode3::DiceExchangeMode::NONE;
} else {
throw out_of_range("invalid dice exchange mode name");
}
}
template <>
const char* name_for_enum<Episode3::DiceExchangeMode>(Episode3::DiceExchangeMode dice_exchange_mode) {
switch (dice_exchange_mode) {
case Episode3::DiceExchangeMode::HIGH_ATK:
return "HIGH_ATK";
case Episode3::DiceExchangeMode::HIGH_DEF:
return "HIGH_DEF";
case Episode3::DiceExchangeMode::NONE:
return "NONE";
default:
throw out_of_range("invalid dice exchange mode");
}
}
template <>
Episode3::AllowedCards enum_for_name<Episode3::AllowedCards>(const char* name) {
if (!strcmp(name, "ALL")) {
return Episode3::AllowedCards::ALL;
} else if (!strcmp(name, "N_ONLY")) {
return Episode3::AllowedCards::N_ONLY;
} else if (!strcmp(name, "N_R_ONLY")) {
return Episode3::AllowedCards::N_R_ONLY;
} else if (!strcmp(name, "N_R_S_ONLY")) {
return Episode3::AllowedCards::N_R_S_ONLY;
} else {
throw out_of_range("invalid allowed cards name");
}
}
template <>
const char* name_for_enum<Episode3::AllowedCards>(Episode3::AllowedCards allowed_cards) {
switch (allowed_cards) {
case Episode3::AllowedCards::ALL:
return "ALL";
case Episode3::AllowedCards::N_ONLY:
return "N_ONLY";
case Episode3::AllowedCards::N_R_ONLY:
return "N_R_ONLY";
case Episode3::AllowedCards::N_R_S_ONLY:
return "N_R_S_ONLY";
default:
throw out_of_range("invalid allowed cards");
}
}
+17 -11
View File
@@ -7,6 +7,7 @@
#include <phosg/Encoding.hh>
#include <phosg/JSON.hh>
#include <phosg/Tools.hh>
#include <phosg/Types.hh>
#include <set>
#include <string>
#include <unordered_map>
@@ -659,18 +660,12 @@ enum class HPType : uint8_t {
COMMON_HP = 2,
};
HPType hp_type_for_name(const char* name);
const char* name_for_hp_type(HPType hp_type);
enum class DiceExchangeMode : uint8_t {
HIGH_ATK = 0,
HIGH_DEF = 1,
NONE = 2,
};
DiceExchangeMode dice_exchange_mode_for_name(const char* name);
const char* name_for_dice_exchange_mode(DiceExchangeMode dice_exchange_mode);
enum class AllowedCards : uint8_t {
ALL = 0,
N_ONLY = 1,
@@ -678,9 +673,6 @@ enum class AllowedCards : uint8_t {
N_R_S_ONLY = 3,
};
AllowedCards allowed_cards_for_name(const char* name);
const char* name_for_allowed_cards(AllowedCards allowed_cards);
struct Rules {
// When this structure is used in a map/quest definition, FF in any of these
// fields means the user is allowed to override it. Any non-FF fields are
@@ -708,8 +700,8 @@ struct Rules {
// likely be more work than it's worth.
Rules();
explicit Rules(std::shared_ptr<const JSONObject> json);
std::shared_ptr<JSONObject> json() const;
explicit Rules(const JSON& json);
JSON json() const;
bool operator==(const Rules& other) const;
bool operator!=(const Rules& other) const;
void clear();
@@ -1115,3 +1107,17 @@ private:
};
} // namespace Episode3
// TODO: Figure out how to declare these inside the Episode3 namespace.
template <>
Episode3::HPType enum_for_name<Episode3::HPType>(const char* name);
template <>
const char* name_for_enum<Episode3::HPType>(Episode3::HPType hp_type);
template <>
Episode3::DiceExchangeMode enum_for_name<Episode3::DiceExchangeMode>(const char* name);
template <>
const char* name_for_enum<Episode3::DiceExchangeMode>(Episode3::DiceExchangeMode dice_exchange_mode);
template <>
Episode3::AllowedCards enum_for_name<Episode3::AllowedCards>(const char* name);
template <>
const char* name_for_enum<Episode3::AllowedCards>(Episode3::AllowedCards allowed_cards);
+44 -51
View File
@@ -313,7 +313,7 @@ Tournament::Tournament(
shared_ptr<const MapIndex> map_index,
shared_ptr<const COMDeckIndex> com_deck_index,
uint8_t number,
std::shared_ptr<const JSONObject> json)
const JSON& json)
: log(string_printf("[Tournament/%02hhX] ", number)),
map_index(map_index),
com_deck_index(com_deck_index),
@@ -326,35 +326,32 @@ void Tournament::init() {
bool is_registration_complete;
if (this->source_json) {
auto& dict = this->source_json->as_dict();
this->name = dict.at("name")->as_string();
this->map = this->map_index->definition_for_number(dict.at("map_number")->as_int());
this->rules = Rules(dict.at("rules"));
this->is_2v2 = dict.at("is_2v2")->as_bool();
is_registration_complete = dict.at("is_registration_complete")->as_bool();
this->name = this->source_json.at("name").as_string();
this->map = this->map_index->definition_for_number(this->source_json.at("map_number"));
this->rules = Rules(this->source_json.at("rules"));
this->is_2v2 = this->source_json.at("is_2v2");
is_registration_complete = this->source_json.at("is_registration_complete");
for (const auto& team_json : dict.at("teams")->as_list()) {
auto& team_dict = team_json->as_dict();
for (const auto& team_json : this->source_json.at("teams").as_list()) {
auto& team = this->teams.emplace_back(new Team(
this->shared_from_this(),
this->teams.size(),
team_dict.at("max_players")->as_int()));
team->name = team_dict.at("name")->as_string();
team->password = team_dict.at("password")->as_string();
team_index_to_rounds_cleared.emplace_back(team_dict.at("num_rounds_cleared")->as_int());
for (const auto& player_json : team_dict.at("player_specs")->as_list()) {
if (player_json->is_int()) {
uint32_t serial_number = player_json->as_int();
team->players.emplace_back(serial_number);
this->all_player_serial_numbers.emplace(serial_number);
team_json.at("max_players")));
team->name = team_json.at("name").as_string();
team->password = team_json.at("password").as_string();
team_index_to_rounds_cleared.emplace_back(team_json.at("num_rounds_cleared"));
for (const auto& player_json : team_json.at("player_specs").as_list()) {
if (player_json.is_int()) {
team->players.emplace_back(player_json);
this->all_player_serial_numbers.emplace(player_json);
} else {
team->players.emplace_back(this->com_deck_index->deck_for_name(player_json->as_string()));
team->players.emplace_back(this->com_deck_index->deck_for_name(player_json));
}
}
}
this->num_teams = this->teams.size();
this->source_json.reset();
this->source_json = nullptr;
} else {
// Create empty teams
@@ -459,34 +456,33 @@ void Tournament::init() {
}
}
std::shared_ptr<JSONObject> Tournament::json() const {
unordered_map<string, shared_ptr<JSONObject>> dict;
dict.emplace("name", make_json_str(this->name));
dict.emplace("map_number", make_json_int(this->map->map.map_number));
dict.emplace("rules", this->rules.json());
dict.emplace("is_2v2", make_json_bool(this->is_2v2));
dict.emplace("is_registration_complete", make_json_bool(this->current_state != State::REGISTRATION));
vector<shared_ptr<JSONObject>> teams_list;
JSON Tournament::json() const {
auto teams_list = JSON::list();
for (auto team : this->teams) {
unordered_map<string, shared_ptr<JSONObject>> team_dict;
team_dict.emplace("max_players", make_json_int(team->max_players));
vector<shared_ptr<JSONObject>> player_jsons_list;
auto players_list = JSON::list();
for (const auto& player : team->players) {
if (player.is_human()) {
player_jsons_list.emplace_back(make_json_int(player.serial_number));
players_list.emplace_back(player.serial_number);
} else {
player_jsons_list.emplace_back(make_json_str(player.com_deck->deck_name));
players_list.emplace_back(player.com_deck->deck_name);
}
}
team_dict.emplace("player_specs", make_json_list(std::move(player_jsons_list)));
team_dict.emplace("name", make_json_str(team->name));
team_dict.emplace("password", make_json_str(team->password));
team_dict.emplace("num_rounds_cleared", make_json_int(team->num_rounds_cleared));
teams_list.emplace_back(new JSONObject(std::move(team_dict)));
teams_list.emplace_back(JSON::dict({
{"max_players", team->max_players},
{"player_specs", std::move(players_list)},
{"name", team->name},
{"password", team->password},
{"num_rounds_cleared", team->num_rounds_cleared},
}));
}
dict.emplace("teams", make_json_list(std::move(teams_list)));
return shared_ptr<JSONObject>(new JSONObject(std::move(dict)));
return JSON::dict({
{"name", this->name},
{"map_number", this->map->map.map_number.load()},
{"rules", this->rules.json()},
{"is_2v2", this->is_2v2},
{"is_registration_complete", (this->current_state != State::REGISTRATION)},
{"teams", std::move(teams_list)},
});
}
uint8_t Tournament::get_number() const {
@@ -664,15 +660,13 @@ TournamentIndex::TournamentIndex(
return;
}
auto json = JSONObject::parse(load_file(this->state_filename));
auto& list = json->as_list();
if (list.size() != 0x20) {
auto json = JSON::parse(load_file(this->state_filename));
if (json.size() != 0x20) {
throw runtime_error("tournament JSON list length is incorrect");
}
for (size_t z = 0; z < 0x20; z++) {
if (!list.at(z)->is_null()) {
this->tournaments[z].reset(new Tournament(this->map_index, this->com_deck_index, z, list[z]));
if (!json.at(z).is_null()) {
this->tournaments[z].reset(new Tournament(this->map_index, this->com_deck_index, z, json.at(z)));
this->tournaments[z]->init();
}
}
@@ -683,16 +677,15 @@ void TournamentIndex::save() const {
return;
}
vector<shared_ptr<JSONObject>> list;
auto list = JSON::list();
for (size_t z = 0; z < 0x20; z++) {
if (this->tournaments[z]) {
list.emplace_back(this->tournaments[z]->json());
} else {
list.emplace_back(make_json_null());
list.emplace_back(nullptr);
}
}
auto json = make_json_list(std::move(list));
save_file(this->state_filename, json->serialize(JSONObject::SerializeOption::FORMAT));
save_file(this->state_filename, list.serialize(JSON::SerializeOption::FORMAT));
}
vector<shared_ptr<Tournament>> TournamentIndex::all_tournaments() const {
+3 -3
View File
@@ -104,11 +104,11 @@ public:
std::shared_ptr<const MapIndex> map_index,
std::shared_ptr<const COMDeckIndex> com_deck_index,
uint8_t number,
std::shared_ptr<const JSONObject> json);
const JSON& json);
~Tournament() = default;
void init();
std::shared_ptr<JSONObject> json() const;
JSON json() const;
uint8_t get_number() const;
const std::string& get_name() const;
@@ -134,7 +134,7 @@ private:
std::shared_ptr<const MapIndex> map_index;
std::shared_ptr<const COMDeckIndex> com_deck_index;
std::shared_ptr<const JSONObject> source_json;
JSON source_json;
uint8_t number;
std::string name;
std::shared_ptr<const MapIndex::MapEntry> map;