@@ -12,13 +12,27 @@ using json = nlohmann::json;
1212
1313namespace webui {
1414
15+ /*
16+ Convention: pass id.index() for references (-1 for invalid value) and convert indexes back into values on client-side (0 for invalid value)
17+ */
18+
1519json format_color (sys::state& state, uint32_t c) {
1620 json j = json::object ();
1721
1822 j[" r" ] = sys::int_red_from_int (c);
1923 j[" g" ] = sys::int_green_from_int (c);
2024 j[" b" ] = sys::int_blue_from_int (c);
2125
26+ return j;
27+ }
28+
29+ json format_date (sys::state& state, sys::date date) {
30+ json j = json::object ();
31+ auto dt = date.to_ymd (state.start_date );
32+ j[" year" ] = dt.year ;
33+ j[" month" ] = dt.month ;
34+ j[" day" ] = dt.day ;
35+ j[" date" ] = std::to_string (dt.day ) + " ." + std::to_string (dt.month ) + " ." + std::to_string (dt.year );
2236
2337 return j;
2438}
@@ -60,7 +74,7 @@ json format_commodity(sys::state& state, dcon::commodity_id c) {
6074json format_commodity_link (sys::state& state, dcon::commodity_id c) {
6175 json j = json::object ();
6276
63- j[" id" ] = c.index ();;
77+ j[" id" ] = c.index ();
6478 j[" name" ] = text::produce_simple_string (state, state.world .commodity_get_name (c));
6579 j[" key" ] = state.to_string_view (state.world .commodity_get_name (c));
6680 j[" color" ] = format_color (state, state.world .commodity_get_color (c));
@@ -80,7 +94,7 @@ json format_nation(sys::state& state, dcon::nation_id n) {
8094 j[" color" ] = format_color (state, color);
8195
8296 auto capital = state.world .nation_get_capital (n);
83- j[" capital" ] = format_province_link (state, capital);
97+ j[" capital" ] = capital. id . index ( );
8498
8599 json jlist = json::array ();
86100 for (auto st : state.world .in_state_instance ) {
@@ -223,8 +237,10 @@ json format_province(sys::state& state, dcon::province_id pid) {
223237 j[" name" ] = province_name;
224238 j[" provid" ] = state.world .province_get_provid (prov);
225239
226- j[" owner" ] = format_nation_link (state, owner);
227- j[" state" ] = format_state_link (state, sid);
240+ if (owner) {
241+ j[" owner" ] = owner.index (); // format_nation_link(state, owner);
242+ }
243+ // j["state"] = format_state_instance_link(state, sid);
228244
229245 j[" x" ] = state.world .province_get_mid_point (prov).x ;
230246 j[" y" ] = state.world .province_get_mid_point (prov).y ;
@@ -270,7 +286,7 @@ json format_province(sys::state& state, dcon::province_id pid) {
270286 if (economy::rgo_max_employment (state, c, prov) > 100 .f ) {
271287 json t = json::object ();
272288
273- t[" commodity" ] = format_commodity_link (state, c );
289+ t[" commodity" ] = c. id . index ( );
274290
275291 t[" max_employment" ] = economy::rgo_max_employment (state, c, prov);
276292
@@ -286,7 +302,7 @@ json format_province(sys::state& state, dcon::province_id pid) {
286302 t[" employment" ][" no_education" ][" target" ] = int (target_employment);
287303 t[" employment" ][" no_education" ][" satisfaction" ] = satisfaction;
288304 t[" employment" ][" no_education" ][" actual" ] = int (target_employment * satisfaction);
289- t[" employment" ][" no_education" ][" wage" ] = state.world .province_get_labor_price (prov, economy::labor::no_education);
305+ t[" employment" ][" no_education" ][" wage" ] = double ( state.world .province_get_labor_price (prov, economy::labor::no_education) );
290306 t[" employment" ][" no_education" ][" max" ] = int (economy::rgo_max_employment (state, c, prov));
291307
292308 bool const is_mine = state.world .commodity_get_is_mine (c);
@@ -311,7 +327,9 @@ json format_province(sys::state& state, dcon::province_id pid) {
311327 json jlist = json::array ();
312328 for (auto floc : state.world .in_factory_location ) {
313329 if (floc.get_province () == prov) {
314- jlist.push_back (format_factory (state, floc.get_factory ()));
330+ // jlist.push_back(format_factory(state, floc.get_factory()));
331+ auto fid = floc.get_factory ().id .index ();
332+ jlist.push_back (fid);
315333 }
316334 }
317335 j[" factories" ] = jlist;
@@ -447,4 +465,135 @@ json format_wargoal(sys::state& state, sys::full_wg wid) {
447465 return j;
448466}
449467
468+ json format_ship (sys::state& state, dcon::ship_id id) {
469+ json j = json::object ();
470+
471+ j[" id" ] = id.index ();
472+
473+ // j["name"] = text::produce_simple_string(state, state.world.ship_get_name(id));
474+ j[" type" ] = state.world .ship_get_type (id).value ;
475+ j[" strength" ] = state.world .ship_get_strength (id);
476+ j[" org" ] = state.world .ship_get_org (id);
477+ j[" pending_split" ] = state.world .ship_get_pending_split (id);
478+ j[" experience" ] = state.world .ship_get_experience (id);
479+ return j;
480+
481+ }
482+ json format_army (sys::state& state, dcon::army_id id) {
483+ json j = json::object ();
484+
485+ j[" id" ] = id.index ();
486+ j[" name" ] = state.to_string_view (state.world .army_get_name (id));
487+
488+ j[" location" ] = (int32_t ) state.world .army_get_location_from_army_location (id).index ();
489+
490+ j[" black_flag" ] = state.world .army_get_black_flag (id);
491+ j[" is_retreating" ] = state.world .army_get_is_retreating (id);
492+ j[" is_rebel_hunter" ] = state.world .army_get_is_rebel_hunter (id);
493+ j[" moving_to_merge" ] = state.world .army_get_moving_to_merge (id);
494+ j[" special_order" ] = state.world .army_get_special_order (id);
495+ j[" dig_in" ] = state.world .army_get_dig_in (id);
496+ j[" path" ] = json::array ();
497+ for (auto p : state.world .army_get_path (id)) {
498+ j[" path" ].push_back ((int32_t )p.index ());
499+ }
500+ j[" arrival_time" ] = format_date (state, state.world .army_get_arrival_time (id));
501+ j[" unused_travel_days" ] = state.world .army_get_unused_travel_days (id);
502+ // j["ai_activity"] = state.world.army_get_ai_activity(id);
503+ // j["ai_province"] = state.world.army_get_ai_province(id);
504+ j[" is_ai_controlled" ] = state.world .army_get_is_ai_controlled (id);
505+
506+ j[" regiments" ] = json::array ();
507+ for (const auto am : state.world .army_get_army_membership (id)) {
508+ auto rid = am.get_regiment ();
509+ j[" regiments" ].push_back (rid.id .index ());
510+ }
511+
512+ return j;
513+
514+ }
515+ json format_navy (sys::state& state, dcon::navy_id id) {
516+ json j = json::object ();
517+
518+ j[" id" ] = id.index ();
519+ j[" name" ] = state.to_string_view (state.world .navy_get_name (id));
520+ j[" path" ] = json::array ();
521+
522+ j[" location" ] = (int32_t )state.world .navy_get_location_from_navy_location (id).index ();
523+
524+ for (auto p : state.world .navy_get_path (id)) {
525+ j[" path" ].push_back ((int32_t )p.index ());
526+ }
527+ j[" arrival_time" ] = format_date (state, state.world .navy_get_arrival_time (id));
528+ j[" unused_travel_days" ] = state.world .navy_get_unused_travel_days (id);
529+ j[" months_outside_naval_range" ] = state.world .navy_get_months_outside_naval_range (id);
530+ j[" is_retreating" ] = state.world .navy_get_is_retreating (id);
531+ j[" moving_to_merge" ] = state.world .navy_get_moving_to_merge (id);
532+ j[" ai_activity" ] = state.world .navy_get_ai_activity (id);
533+
534+ j[" ships" ] = json::array ();
535+ for (const auto am : state.world .navy_get_navy_membership (id)) {
536+ auto rid = am.get_ship ();
537+ j[" ships" ].push_back (rid.id .index ());
538+ }
539+ return j;
540+
541+ }
542+
543+ json format_regiment (sys::state& state, dcon::regiment_id id) {
544+ json j = json::object ();
545+
546+ j[" id" ] = id.index ();
547+ j[" name" ] = text::produce_simple_string (state, state.to_string_view (state.world .regiment_get_name (id)));
548+
549+ // Non-localised type name and id
550+ auto type = state.world .regiment_get_type (id);
551+ j[" type_name" ] = state.to_string_view (state.military_definitions .unit_base_definitions [type].name );
552+ j[" type_id" ] = type.index ();
553+
554+ j[" strength" ] = state.world .regiment_get_strength (id);
555+ j[" pending_combat_damage" ] = state.world .regiment_get_pending_combat_damage (id);
556+ j[" pending_attrition_damage" ] = state.world .regiment_get_pending_attrition_damage (id);
557+ j[" org" ] = state.world .regiment_get_org (id);
558+ j[" pending_split" ] = state.world .regiment_get_pending_split (id);
559+ j[" experience" ] = state.world .regiment_get_experience (id);
560+ return j;
561+ }
562+
563+ json format_unit_type (sys::state& state, dcon::unit_type_id id) {
564+ json j = json::object ();
565+
566+ auto type = state.military_definitions .unit_base_definitions [id];
567+
568+ j[" id" ] = id.index ();
569+ j[" name" ] = state.to_string_view (type.name );
570+
571+ j[" attack_or_gun_power" ] = type.attack_or_gun_power ;
572+ j[" build_cost" ] = format_commodity_set (state, type.build_cost );
573+ j[" build_time" ] = type.build_time ;
574+ j[" can_build_overseas" ] = type.can_build_overseas ;
575+ j[" capital" ] = type.capital ;
576+ j[" colonial_points" ] = type.colonial_points ;
577+ j[" default_organisation" ] = type.default_organisation ;
578+ j[" defence_or_hull" ] = type.defence_or_hull ;
579+ j[" discipline_or_evasion" ] = type.discipline_or_evasion ;
580+ j[" is_land" ] = type.is_land ;
581+ j[" maneuver" ] = type.maneuver ;
582+ j[" maximum_speed" ] = type.maximum_speed ;
583+ j[" min_port_level" ] = type.min_port_level ;
584+ j[" naval_icon" ] = type.naval_icon ;
585+ j[" primary_culture" ] = type.primary_culture ;
586+ j[" reconnaissance_or_fire_range" ] = type.reconnaissance_or_fire_range ;
587+ j[" siege_or_torpedo_attack" ] = type.siege_or_torpedo_attack ;
588+ j[" supply_consumption" ] = type.supply_consumption ;
589+ j[" supply_consumption_score" ] = type.supply_consumption_score ;
590+ j[" supply_cost" ] = format_commodity_set (state, type.supply_cost );
591+ j[" support" ] = type.support ;
592+
593+ return j;
594+
595+ }
596+
597+
598+
450599}
0 commit comments