Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 65 additions & 10 deletions src/network/webapi/controllers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,22 @@ inline void init(sys::state& state) noexcept {
if(state.host_settings.alice_expose_webui != 1 || state.network_mode == sys::network_mode_type::client) {
return;
}
/* Conventions:
/{collection}/(\d+) for one entry retrieval,
/{collection}/all to retrieve all entries of type
*/

svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
res.set_content("Homepage", "text/plain");
});

svr.Get("/date", [&](const httplib::Request& req, httplib::Response& res) {
auto dt = state.current_date.to_ymd(state.start_date);
json j = json::object();
j["year"] = dt.year;
j["month"] = dt.month;
j["day"] = dt.day;
j["date"] = std::to_string(dt.day) + "." + std::to_string(dt.month) + "." + std::to_string(dt.year);

json j = format_date(state, state.current_date);
res.set_content(j.dump(), "text/plain");
});


svr.Get("/nations", [&](const httplib::Request& req, httplib::Response& res) {
svr.Get("/nation/all", [&](const httplib::Request& req, httplib::Response& res) {
json jlist = json::array();

for(auto nation : state.world.in_nation) {
Expand Down Expand Up @@ -86,7 +84,7 @@ inline void init(sys::state& state) noexcept {
res.set_content(j.dump(), "text/plain");
});

svr.Get("/commodities", [&](const httplib::Request& req, httplib::Response& res) {
svr.Get("/commodity/all", [&](const httplib::Request& req, httplib::Response& res) {
json jlist = json::array();

for(auto commodity : state.world.in_commodity) {
Expand Down Expand Up @@ -178,7 +176,7 @@ inline void init(sys::state& state) noexcept {
res.set_content(j.dump(), "text/plain");
});

svr.Get("/provinces", [&](const httplib::Request& req, httplib::Response& res) {
svr.Get("/province/all", [&](const httplib::Request& req, httplib::Response& res) {
json jlist = json::array();

for(auto prov : state.world.in_province) {
Expand Down Expand Up @@ -304,6 +302,63 @@ inline void init(sys::state& state) noexcept {
res.set_content(j.dump(), "text/plain");
});

svr.Get("/unittype/all", [&](const httplib::Request& req, httplib::Response& res) {
json jlist = json::array();

for(uint32_t i = 2; i < state.military_definitions.unit_base_definitions.size(); ++i) {
dcon::unit_type_id uid = dcon::unit_type_id{ dcon::unit_type_id::value_base_t(i) };
auto j = json::object();

jlist.push_back(format_unit_type(state, uid));
}

res.set_content(jlist.dump(), "text/plain");
});

svr.Get(R"(/army/all)", [&](const httplib::Request& req, httplib::Response& res) {

auto j = json::array();

for(auto a : state.world.in_army) {
j.push_back(format_army(state, a));
}

res.set_content(j.dump(), "text/plain");
});

svr.Get(R"(/navy/all)", [&](const httplib::Request& req, httplib::Response& res) {

auto j = json::array();

for(auto n : state.world.in_navy) {
j.push_back(format_navy(state, n));
}

res.set_content(j.dump(), "text/plain");
});

svr.Get(R"(/regiment/(\d+))", [&](const httplib::Request& req, httplib::Response& res) {
auto match = req.matches[1];
auto num = std::atoi(match.str().c_str());

dcon::regiment_id p{ dcon::regiment_id::value_base_t(num) };

auto j = format_regiment(state, p);

res.set_content(j.dump(), "text/plain");
});

svr.Get(R"(/ship/(\d+))", [&](const httplib::Request& req, httplib::Response& res) {
auto match = req.matches[1];
auto num = std::atoi(match.str().c_str());

dcon::ship_id p{ dcon::ship_id::value_base_t(num) };

auto j = format_ship(state, p);

res.set_content(j.dump(), "text/plain");
});

svr.listen("0.0.0.0", 1234);
}

Expand Down
163 changes: 156 additions & 7 deletions src/network/webapi/jsonlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,27 @@ using json = nlohmann::json;

namespace webui {

/*
Convention: pass id.index() for references (-1 for invalid value) and convert indexes back into values on client-side (0 for invalid value)
*/

json format_color(sys::state& state, uint32_t c) {
json j = json::object();

j["r"] = sys::int_red_from_int(c);
j["g"] = sys::int_green_from_int(c);
j["b"] = sys::int_blue_from_int(c);

return j;
}

json format_date(sys::state& state, sys::date date) {
json j = json::object();
auto dt = date.to_ymd(state.start_date);
j["year"] = dt.year;
j["month"] = dt.month;
j["day"] = dt.day;
j["date"] = std::to_string(dt.day) + "." + std::to_string(dt.month) + "." + std::to_string(dt.year);

return j;
}
Expand Down Expand Up @@ -60,7 +74,7 @@ json format_commodity(sys::state& state, dcon::commodity_id c) {
json format_commodity_link(sys::state& state, dcon::commodity_id c) {
json j = json::object();

j["id"] = c.index();;
j["id"] = c.index();
j["name"] = text::produce_simple_string(state, state.world.commodity_get_name(c));
j["key"] = state.to_string_view(state.world.commodity_get_name(c));
j["color"] = format_color(state, state.world.commodity_get_color(c));
Expand All @@ -80,7 +94,7 @@ json format_nation(sys::state& state, dcon::nation_id n) {
j["color"] = format_color(state, color);

auto capital = state.world.nation_get_capital(n);
j["capital"] = format_province_link(state, capital);
j["capital"] = capital.id.index();

json jlist = json::array();
for(auto st : state.world.in_state_instance) {
Expand Down Expand Up @@ -223,8 +237,10 @@ json format_province(sys::state& state, dcon::province_id pid) {
j["name"] = province_name;
j["provid"] = state.world.province_get_provid(prov);

j["owner"] = format_nation_link(state, owner);
j["state"] = format_state_link(state, sid);
if(owner) {
j["owner"] = owner.index(); // format_nation_link(state, owner);
}
// j["state"] = format_state_instance_link(state, sid);

j["x"] = state.world.province_get_mid_point(prov).x;
j["y"] = state.world.province_get_mid_point(prov).y;
Expand Down Expand Up @@ -270,7 +286,7 @@ json format_province(sys::state& state, dcon::province_id pid) {
if(economy::rgo_max_employment(state, c, prov) > 100.f) {
json t = json::object();

t["commodity"] = format_commodity_link(state, c);
t["commodity"] = c.id.index();

t["max_employment"] = economy::rgo_max_employment(state, c, prov);

Expand All @@ -286,7 +302,7 @@ json format_province(sys::state& state, dcon::province_id pid) {
t["employment"]["no_education"]["target"] = int(target_employment);
t["employment"]["no_education"]["satisfaction"] = satisfaction;
t["employment"]["no_education"]["actual"] = int(target_employment * satisfaction);
t["employment"]["no_education"]["wage"] = state.world.province_get_labor_price(prov, economy::labor::no_education);
t["employment"]["no_education"]["wage"] = double(state.world.province_get_labor_price(prov, economy::labor::no_education));
t["employment"]["no_education"]["max"] = int(economy::rgo_max_employment(state, c, prov));

bool const is_mine = state.world.commodity_get_is_mine(c);
Expand All @@ -311,7 +327,9 @@ json format_province(sys::state& state, dcon::province_id pid) {
json jlist = json::array();
for(auto floc : state.world.in_factory_location) {
if(floc.get_province() == prov) {
jlist.push_back(format_factory(state, floc.get_factory()));
// jlist.push_back(format_factory(state, floc.get_factory()));
auto fid = floc.get_factory().id.index();
jlist.push_back(fid);
}
}
j["factories"] = jlist;
Expand Down Expand Up @@ -447,4 +465,135 @@ json format_wargoal(sys::state& state, sys::full_wg wid) {
return j;
}

json format_ship(sys::state& state, dcon::ship_id id) {
json j = json::object();

j["id"] = id.index();

// j["name"] = text::produce_simple_string(state, state.world.ship_get_name(id));
j["type"] = state.world.ship_get_type(id).value;
j["strength"] = state.world.ship_get_strength(id);
j["org"] = state.world.ship_get_org(id);
j["pending_split"] = state.world.ship_get_pending_split(id);
j["experience"] = state.world.ship_get_experience(id);
return j;

}
json format_army(sys::state& state, dcon::army_id id) {
json j = json::object();

j["id"] = id.index();
j["name"] = state.to_string_view(state.world.army_get_name(id));

j["location"] = (int32_t) state.world.army_get_location_from_army_location(id).index();

j["black_flag"] = state.world.army_get_black_flag(id);
j["is_retreating"] = state.world.army_get_is_retreating(id);
j["is_rebel_hunter"] = state.world.army_get_is_rebel_hunter(id);
j["moving_to_merge"] = state.world.army_get_moving_to_merge(id);
j["special_order"] = state.world.army_get_special_order(id);
j["dig_in"] = state.world.army_get_dig_in(id);
j["path"] = json::array();
for(auto p : state.world.army_get_path(id)) {
j["path"].push_back((int32_t)p.index());
}
j["arrival_time"] = format_date(state, state.world.army_get_arrival_time(id));
j["unused_travel_days"] = state.world.army_get_unused_travel_days(id);
// j["ai_activity"] = state.world.army_get_ai_activity(id);
// j["ai_province"] = state.world.army_get_ai_province(id);
j["is_ai_controlled"] = state.world.army_get_is_ai_controlled(id);

j["regiments"] = json::array();
for(const auto am : state.world.army_get_army_membership(id)) {
auto rid = am.get_regiment();
j["regiments"].push_back(rid.id.index());
}

return j;

}
json format_navy(sys::state& state, dcon::navy_id id) {
json j = json::object();

j["id"] = id.index();
j["name"] = state.to_string_view(state.world.navy_get_name(id));
j["path"] = json::array();

j["location"] = (int32_t)state.world.navy_get_location_from_navy_location(id).index();

for(auto p : state.world.navy_get_path(id)) {
j["path"].push_back((int32_t)p.index());
}
j["arrival_time"] = format_date(state, state.world.navy_get_arrival_time(id));
j["unused_travel_days"] = state.world.navy_get_unused_travel_days(id);
j["months_outside_naval_range"] = state.world.navy_get_months_outside_naval_range(id);
j["is_retreating"] = state.world.navy_get_is_retreating(id);
j["moving_to_merge"] = state.world.navy_get_moving_to_merge(id);
j["ai_activity"] = state.world.navy_get_ai_activity(id);

j["ships"] = json::array();
for(const auto am : state.world.navy_get_navy_membership(id)) {
auto rid = am.get_ship();
j["ships"].push_back(rid.id.index());
}
return j;

}

json format_regiment(sys::state& state, dcon::regiment_id id) {
json j = json::object();

j["id"] = id.index();
j["name"] = text::produce_simple_string(state, state.to_string_view(state.world.regiment_get_name(id)));

// Non-localised type name and id
auto type = state.world.regiment_get_type(id);
j["type_name"] = state.to_string_view(state.military_definitions.unit_base_definitions[type].name);
j["type_id"] = type.index();

j["strength"] = state.world.regiment_get_strength(id);
j["pending_combat_damage"] = state.world.regiment_get_pending_combat_damage(id);
j["pending_attrition_damage"] = state.world.regiment_get_pending_attrition_damage(id);
j["org"] = state.world.regiment_get_org(id);
j["pending_split"] = state.world.regiment_get_pending_split(id);
j["experience"] = state.world.regiment_get_experience(id);
return j;
}

json format_unit_type(sys::state& state, dcon::unit_type_id id) {
json j = json::object();

auto type = state.military_definitions.unit_base_definitions[id];

j["id"] = id.index();
j["name"] = state.to_string_view(type.name);

j["attack_or_gun_power"] = type.attack_or_gun_power;
j["build_cost"] = format_commodity_set(state, type.build_cost);
j["build_time"] = type.build_time;
j["can_build_overseas"] = type.can_build_overseas;
j["capital"] = type.capital;
j["colonial_points"] = type.colonial_points;
j["default_organisation"] = type.default_organisation;
j["defence_or_hull"] = type.defence_or_hull;
j["discipline_or_evasion"] = type.discipline_or_evasion;
j["is_land"] = type.is_land;
j["maneuver"] = type.maneuver;
j["maximum_speed"] = type.maximum_speed;
j["min_port_level"] = type.min_port_level;
j["naval_icon"] = type.naval_icon;
j["primary_culture"] = type.primary_culture;
j["reconnaissance_or_fire_range"] = type.reconnaissance_or_fire_range;
j["siege_or_torpedo_attack"] = type.siege_or_torpedo_attack;
j["supply_consumption"] = type.supply_consumption;
j["supply_consumption_score"] = type.supply_consumption_score;
j["supply_cost"] = format_commodity_set(state, type.supply_cost);
j["support"] = type.support;

return j;

}



}
1 change: 1 addition & 0 deletions src/network/webapi/jsonlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using json = nlohmann::json;
namespace webui {

json format_color(sys::state& state, uint32_t c);
json format_date(sys::state& state, sys::date date);

json format_commodity(sys::state& state, dcon::commodity_id c);
json format_commodity_link(sys::state& state, dcon::commodity_id c);
Expand Down
Loading