From 99cfb30eeaafac7b11ea8562202e1fece5d3c363 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 5 Jan 2023 19:35:33 +0100 Subject: Naively implemented JSON universe Repository --- universe/src/JsonRepository.cpp | 80 ++++++++++++++++++++++++++++++++++++++--- universe/src/JsonRepository.h | 6 +++- 2 files changed, 80 insertions(+), 6 deletions(-) (limited to 'universe/src') diff --git a/universe/src/JsonRepository.cpp b/universe/src/JsonRepository.cpp index ddbbfcf..a4abfe5 100644 --- a/universe/src/JsonRepository.cpp +++ b/universe/src/JsonRepository.cpp @@ -1,47 +1,117 @@ #include "JsonRepository.h" +#include #include +#include +#include #include +#include + #include #include #include +using json = nlohmann::json; + + namespace kurator { namespace universe { -JsonRepository::JsonRepository(const char*) +void +from_json(const json& item, ShipType& ship) { + item.at("name").get_to(ship.name); + item.at("base_health_points").get_to(ship.base_health_points); + item.at("max_speed").get_to(ship.max_speed); +} + + +void +from_json(const json& item, TurretType& turret) +{ + item.at("name").get_to(turret.name); + if (item.contains("base_damage")) + item.at("base_damage").get_to(turret.base_damage); + if (item.contains("rounds")) + item.at("rounds").get_to(turret.rounds); + if (item.contains("rate_of_fire")) + item.at("rate_of_fire").get_to(turret.rate_of_fire); + if (item.contains("reload")) + item.at("reload").get_to(turret.reload); + if (item.contains("optimal_range")) + item.at("optimal_range").get_to(turret.optimal_range); + if (item.contains("falloff_modifier")) + item.at("falloff_modifier").get_to(turret.falloff_modifier); + if (item.contains("falloff_intensity")) + item.at("falloff_intensity").get_to(turret.falloff_intensity); +} + + +template +std::map +parse(const std::string& path) +try { + std::fstream file{path, file.binary | file.in}; // std::filesystem? + if (!file.is_open()) + throw "could not load universe part"; // FIXME: proper errors and handling above + std::map map; + const auto root = json::parse(file); + for (const auto& item : root) { + auto def = item.get(); + map[def.name] = def; + } + return map; +} +catch (const json::parse_error&) { + throw; +} + + +JsonRepository::JsonRepository(const std::string& path) +{ + ships = parse(path + "/ship_types.json"); + turrets = parse(path + "/turret_types.json"); } ShipType JsonRepository::ship_type(const std::string& id) const -{ +try { + return ships.at(id); +} +catch (const std::out_of_range&) { throw NotFound(id); } TurretType JsonRepository::turret_type(const std::string& id) const -{ +try { + return turrets.at(id); +} +catch (const std::out_of_range&) { throw NotFound(id); } void -JsonRepository::for_ship_types(std::function) const +JsonRepository::for_ship_types(std::function func) const { + for (const auto& [_, type] : ships) + func(type); } void -JsonRepository::for_turret_types(std::function) const +JsonRepository::for_turret_types(std::function func) const { + for (const auto& [_, type] : turrets) + func(type); } diff --git a/universe/src/JsonRepository.h b/universe/src/JsonRepository.h index dc6e7f1..ea6bf46 100644 --- a/universe/src/JsonRepository.h +++ b/universe/src/JsonRepository.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -17,11 +18,14 @@ namespace universe class JsonRepository : public Repository { public: - explicit JsonRepository(const char* path); + explicit JsonRepository(const std::string& path); ShipType ship_type(const std::string& id) const override; TurretType turret_type(const std::string& id) const override; void for_ship_types(std::function func) const; void for_turret_types(std::function func) const; +private: + std::map ships; + std::map turrets; }; -- cgit v1.1