Bind native balance planning and restore its saved runtime value (by Codex)
This commit is contained in:
1 parent
256d6d5211
commit
fd5c2b5330
24 files changed
+717
-28
No files matched your search
@@ -42,6 +42,7 @@
|
||||
#include "import/save.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
#include <system_error>
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
@@ -7825,12 +7826,22 @@ void ModernSession::restore_native_session_context(const cli::Json &root) {
|
||||
if (!row.object() || !row.object()->contains("address") || !row.object()->contains("value"))
|
||||
throw FormatError("Invalid runtime byte");
|
||||
const auto at = uint32_t(integer(row.object()->at("address"), 0, UINT32_MAX));
|
||||
if (!club_modal_runtime_.contains(at) ||
|
||||
!runtime.emplace(at, uint8_t(integer(row.object()->at("value"), 0, 255))).second)
|
||||
const auto value = uint8_t(integer(row.object()->at("value"), 0, 255));
|
||||
const bool planned_ticket = at >= ::bmh::layout::PlanTicketPrice &&
|
||||
at < ::bmh::layout::PlanTicketPrice + 4 && !g_.memory.has(at);
|
||||
if ((!club_modal_runtime_.contains(at) && !planned_ticket) ||
|
||||
!runtime.emplace(at, value).second)
|
||||
throw FormatError("Unknown or duplicated runtime byte");
|
||||
if (planned_ticket && value > 25)
|
||||
throw FormatError("Invalid planned ticket price");
|
||||
}
|
||||
for (const auto &[address, value] : club_modal_runtime_) {
|
||||
(void)value;
|
||||
const bool planned_ticket = address >= ::bmh::layout::PlanTicketPrice &&
|
||||
address < ::bmh::layout::PlanTicketPrice + 4 && !g_.memory.has(address);
|
||||
if (!planned_ticket && !runtime.contains(address))
|
||||
throw FormatError("Incomplete native runtime bytes");
|
||||
}
|
||||
if (runtime.size() != club_modal_runtime_.size())
|
||||
throw FormatError("Incomplete native runtime bytes");
|
||||
std::vector<ui::DeskMatchCallargs> callargs;
|
||||
for (const auto &row : array(field("desk_callargs"), 4)) {
|
||||
const auto &fields = array(row, 2);
|
||||
@@ -8764,6 +8775,8 @@ std::string ui_text(const cli::Json& j, const char* key) {
|
||||
return v && v->string() ? *v->string() : std::string{};
|
||||
}
|
||||
|
||||
#include "modern/app/ui_balance.inc"
|
||||
|
||||
// The original mailbox of a manager, each message with an id that stays the same while the
|
||||
// mailbox shifts its entries (the entry index does not): "message:<day>-<text hash>", "-<n>"
|
||||
// for the n-th equal message of a day.
|
||||
@@ -9000,12 +9013,20 @@ cli::Json ModernSession::ui_read(const cli::Json& request, uint64_t revision) co
|
||||
return answer;
|
||||
}
|
||||
if (manager < 0 || size_t(manager) >= g_.manager_teams.size() ||
|
||||
(kind != "achievements" && kind != "statistics" && kind != "opening" && kind != "lineup" && kind != "tactics" && kind != "training" && kind != "bank" && kind != "contracts" &&
|
||||
(kind != "balance" && kind != "achievements" && kind != "statistics" && kind != "opening" && kind != "lineup" && kind != "tactics" && kind != "training" && kind != "bank" && kind != "contracts" &&
|
||||
kind != "advertising" && kind != "stadium" && kind != "transfer" && kind != "postmatch" && kind != "credit" &&
|
||||
!(kind == "friendly" && g_.manager_teams[size_t(manager)] == no_team())))
|
||||
return modern::ui_read(g_, context, request);
|
||||
if (const std::string wanted = ui_text(request, "managerId"); !wanted.empty() && wanted != modern::ui_manager_id(g_, manager))
|
||||
return modern::ui_error(request_id, "manager_not_active", "Dieser Manager sitzt nicht am Schreibtisch.", revision, false);
|
||||
if (kind == "balance") {
|
||||
try {
|
||||
return modern::ui_snapshot(g_, context, request_id,
|
||||
ui_balance_plan(g_, exe_, club_modal_runtime_, manager, request).data);
|
||||
} catch (const std::exception &e) {
|
||||
return modern::ui_error(request_id, "balance_unavailable", e.what(), revision, false);
|
||||
}
|
||||
}
|
||||
if(kind=="achievements") {
|
||||
const int team=g_.manager_teams[size_t(manager)];
|
||||
if(team<0||team>=league_clubs()||manager>3)return modern::ui_error(request_id,"no_club","Erfolge sind am Vereinsschreibtisch verfügbar.",revision,false);
|
||||
@@ -10585,6 +10606,33 @@ cli::Json ModernSession::ui_command(const cli::Json& request, uint64_t& revision
|
||||
if (!dispatch_ticket_price(r, revision, error)) { restore(); return refuse("ticket_refused", error); }
|
||||
if (revision != previous) revision = previous + 1;
|
||||
answer = modern::ui_command_result(request_id, previous, revision, {"stadium"}, "Eintrittspreis gesetzt.");
|
||||
} else if (kind == "balance.commit") {
|
||||
if (balance_active() || transfer_offer_editor_ || contract_quote_ || stadium_.block_expansion)
|
||||
return refuse("decision_pending", "Bitte die offene Entscheidung zuerst abschließen.");
|
||||
if (ui_text(*payload, "view") != "future")
|
||||
return refuse("invalid_payload", "Eine Zukunftsplanung wird benötigt.");
|
||||
const auto *edits = ui_field(*payload, "edits");
|
||||
if (!edits || !edits->array() || edits->array()->empty())
|
||||
return refuse("invalid_payload", "Keine Planänderungen vorhanden.");
|
||||
try {
|
||||
auto plan = ui_balance_plan(g_, exe_, club_modal_runtime_, manager, *payload);
|
||||
const auto *accept = ui_field(*payload, "acceptWarning");
|
||||
if (plan.warning && (!accept || !std::holds_alternative<bool>(accept->value) ||
|
||||
!std::get<bool>(accept->value)))
|
||||
return refuse("confirmation_required", "Die Bilanzwarnung muss bestätigt werden.");
|
||||
const auto address = ::bmh::layout::PlanTicketPrice + uint32_t(manager);
|
||||
g_.manager_records = std::move(plan.records);
|
||||
if (g_.memory.has(address))
|
||||
g_.memory.set8(address, plan.ticket);
|
||||
else
|
||||
club_modal_runtime_[address] = plan.ticket;
|
||||
revision = previous + 1;
|
||||
answer = modern::ui_command_result(request_id, previous, revision,
|
||||
{"balance", "desktop"}, "Bilanzplanung übernommen.");
|
||||
} catch (const std::exception &e) {
|
||||
restore();
|
||||
return refuse("balance_refused", e.what());
|
||||
}
|
||||
} else if (kind == "bank.commit") {
|
||||
// payload {action: deposit|withdraw, amount: Money} or {action: buy|sell, assetId: stock:<n>|fund:<n>, count}
|
||||
const std::string action = ui_text(*payload, "action");
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// Transport adapter for the existing, pure balance-plan draft operations.
|
||||
struct UiBalancePlan {
|
||||
BalancePlanDraft draft;
|
||||
std::vector<uint8_t> records;
|
||||
uint8_t ticket = 0;
|
||||
bool warning = false;
|
||||
cli::Json data;
|
||||
};
|
||||
UiBalancePlan ui_balance_plan(const ModernState &state, const LeImage &exe,
|
||||
const std::map<uint32_t, uint8_t> &runtime,
|
||||
int manager, const cli::Json &query) {
|
||||
using J = cli::Json;
|
||||
using O = J::Object;
|
||||
using A = J::Array;
|
||||
if (manager < 0 || manager >= 4 ||
|
||||
size_t(manager) >= state.manager_teams.size() ||
|
||||
state.manager_teams[size_t(manager)] < 0 ||
|
||||
state.manager_teams[size_t(manager)] >= modern::league_clubs())
|
||||
throw FormatError("balance_requires_club");
|
||||
if (!state.memory.has(::bmh::layout::GameMode) ||
|
||||
state.memory.u8(::bmh::layout::GameMode) != 3)
|
||||
throw FormatError("balance_requires_mode_three");
|
||||
const auto view = ui_text(query, "view");
|
||||
const int wanted = view.empty() || view == "current" ? 0
|
||||
: view == "real" ? 1
|
||||
: view == "future" ? 2
|
||||
: -1;
|
||||
if (wanted < 0)
|
||||
throw FormatError("invalid_balance_view");
|
||||
ModernState work = state;
|
||||
if (!work.memory.has(::bmh::layout::PlanTicketPrice))
|
||||
work.memory.add_block(::bmh::layout::PlanTicketPrice, 4);
|
||||
for (int i = 0; i < 4; ++i)
|
||||
work.memory.set8(
|
||||
::bmh::layout::PlanTicketPrice + uint32_t(i),
|
||||
source_runtime_byte(state, runtime, exe,
|
||||
::bmh::layout::PlanTicketPrice + uint32_t(i)));
|
||||
auto draft = modern::open_balance_plan(work, manager);
|
||||
for (int i = 0; int(draft.view) != wanted && i < 3; ++i)
|
||||
modern::cycle_balance_view(work, draft);
|
||||
A applied;
|
||||
const auto *edits = ui_field(query, "edits");
|
||||
if (edits) {
|
||||
if (!edits->array() || edits->array()->size() > 18)
|
||||
throw FormatError("invalid_balance_edits");
|
||||
std::set<int> seen;
|
||||
for (const auto &edit : *edits->array()) {
|
||||
const auto id = ui_text(edit, "id");
|
||||
int row = -1;
|
||||
if (std::sscanf(id.c_str(), "balance:%d", &row) != 1 ||
|
||||
id != "balance:" + std::to_string(row) || row < 0 || row >= 18 ||
|
||||
!seen.insert(row).second)
|
||||
throw FormatError("invalid_balance_row");
|
||||
const auto *value = ui_field(edit, "amount");
|
||||
const auto *units = value ? ui_field(*value, "minorUnits") : nullptr;
|
||||
if (!value || ui_text(*value, "currency") != "EUR" || !units ||
|
||||
!units->integer() || *units->integer() != 2)
|
||||
throw FormatError("invalid_balance_money");
|
||||
const auto raw = ui_text(*value, "minor");
|
||||
size_t end = 0;
|
||||
int64_t cents = -1;
|
||||
try {
|
||||
cents = std::stoll(raw, &end);
|
||||
} catch (const std::exception &) {
|
||||
throw FormatError("invalid_balance_money");
|
||||
}
|
||||
if (end != raw.size() || raw != std::to_string(cents) || cents < 0 ||
|
||||
cents % 100 || cents / 100 > INT32_MAX)
|
||||
throw FormatError("invalid_balance_money");
|
||||
if (modern::edit_balance_plan(work, draft, row, int32_t(cents / 100)) !=
|
||||
BalanceEditStatus::changed)
|
||||
throw FormatError("balance_edit_refused");
|
||||
applied.push_back(
|
||||
O{{"id", id}, {"amount", modern::ui_money(cents / 100)}});
|
||||
}
|
||||
}
|
||||
if (!std::isfinite(draft.total) || draft.total < INT32_MIN ||
|
||||
draft.total > INT32_MAX)
|
||||
throw FormatError("balance_total_overflow");
|
||||
const auto label = [&](uint32_t address) {
|
||||
if (!state.texts || address < ::bmh::layout::TextPointerTable ||
|
||||
(address - ::bmh::layout::TextPointerTable) % 4)
|
||||
throw FormatError("balance_label_unavailable");
|
||||
return ui::original_to_utf8(
|
||||
(*state.texts)[(address - ::bmh::layout::TextPointerTable) / 4]);
|
||||
};
|
||||
A items, summaries;
|
||||
int64_t income = 0, costs = 0;
|
||||
for (int row = 0; row < 18; ++row) {
|
||||
const auto amount = draft.rows[size_t(row)];
|
||||
const auto maximum = modern::balance_row_maximum(draft, row);
|
||||
if (row < 7)
|
||||
income += amount;
|
||||
else
|
||||
costs += amount;
|
||||
items.push_back(
|
||||
O{{"id", "balance:" + std::to_string(row)},
|
||||
{"label", label(::bmh::layout::BalancePlan_DrawRows_TextPointer5 +
|
||||
uint32_t(row) * 4)},
|
||||
{"section", row < 7 ? "income" : "costs"},
|
||||
{"amount", modern::ui_money(amount)},
|
||||
{"editable", wanted != 0 && maximum >= 0},
|
||||
{"minimum", modern::ui_money(0)},
|
||||
{"maximum", maximum >= 0 ? modern::ui_money(maximum) : J(nullptr)}});
|
||||
}
|
||||
for (int row = 18; row < 24; ++row)
|
||||
summaries.push_back(
|
||||
O{{"id", "balance:" + std::to_string(row)},
|
||||
{"label", label(::bmh::layout::BalancePlan_DrawRows_TextPointer4 +
|
||||
uint32_t(row - 18) * 4)},
|
||||
{"unit", row == 21 || row == 23 ? "money" : "count"},
|
||||
{"value", row == 21 || row == 23
|
||||
? modern::ui_money(draft.rows[size_t(row)])
|
||||
: J(int64_t(draft.rows[size_t(row)]))}});
|
||||
const bool warning = modern::close_balance_plan(work, draft);
|
||||
UiBalancePlan result;
|
||||
result.draft = draft;
|
||||
result.records = std::move(work.manager_records);
|
||||
result.ticket =
|
||||
work.memory.u8(::bmh::layout::PlanTicketPrice + uint32_t(manager));
|
||||
result.warning = warning;
|
||||
result.data = O{
|
||||
{"kind", "balance"},
|
||||
{"teamId",
|
||||
modern::ui_team_id(state, state.manager_teams[size_t(manager)])},
|
||||
{"view", wanted == 0 ? "current"
|
||||
: wanted == 1 ? "real"
|
||||
: "future"},
|
||||
{"edits", std::move(applied)},
|
||||
{"items", std::move(items)},
|
||||
{"summaries", std::move(summaries)},
|
||||
{"months", draft.months_left},
|
||||
{"income", modern::ui_money(income)},
|
||||
{"costs", modern::ui_money(costs)},
|
||||
{"depreciation", modern::ui_money(draft.depreciation)},
|
||||
{"depreciationLabel",
|
||||
label(::bmh::layout::BalancePlan_DrawRows_TextPointer6)},
|
||||
{"net", modern::ui_money(int64_t(std::llround(draft.total)))},
|
||||
{"warning", warning},
|
||||
{"warningText",
|
||||
warning ? label(::bmh::layout::BalancePlan_BalanceFromGame_TextPointer2)
|
||||
: std::string()}};
|
||||
return result;
|
||||
}
|
||||
Reference in new issue
Block a user