Modern UI contract: mailbox messages and news.update

The news read carries the desk manager's mailbox as the contract's
messages with ids that survive the original's compaction. news.update
marks a message read or unread, moves it to the trash and back (saved
marks; the message stays in the original mailbox) and archives it as the
original's keep.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-10-29 09:16:57 +01:00
1 parent c361dccd34
commit 179cd970ad
6 files changed
+139 -2

No files matched your search

+84
View File
@@ -8763,6 +8763,26 @@ std::string ui_text(const cli::Json& j, const char* key) {
return v && v->string() ? *v->string() : std::string{};
}
// 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.
std::vector<std::pair<std::string, ui::MailboxMessage>> ui_mailbox(const ModernState& g, int manager) {
std::vector<std::pair<std::string, ui::MailboxMessage>> out;
ui::MailboxState box;
try { box = ui::mailbox_state_from_game(g, manager); } catch (const std::exception&) { return out; }
std::map<std::string, int> seen;
for (auto& m : box.messages) {
uint32_t hash = 2166136261u; // FNV-1a
for (unsigned char c : m.text) hash = (hash ^ c) * 16777619u;
char key[40];
std::snprintf(key, sizeof key, "message:%u-%08x", unsigned(m.date_counter), hash);
std::string id = key;
if (const int n = seen[id]++; n > 0) id += "-" + std::to_string(n + 1);
out.emplace_back(std::move(id), std::move(m));
}
return out;
}
// The runtime team a contract team id names (a world id or "team:<n>"), or -1.
int ui_team_index(const ModernState& g, const std::string& id) {
if (id.empty()) return -1;
@@ -8950,6 +8970,34 @@ cli::Json ModernSession::ui_read(const cli::Json& request, uint64_t revision) co
context.career_ended = career_complete();
const std::string kind = ui_text(request, "kind"), request_id = ui_text(request, "requestId");
const int manager = context.manager;
if (kind == "news" && manager >= 0 && size_t(manager) < g_.manager_teams.size()) {
// the press events (content read) and the desk manager's mailbox: the original's messages
// with the interface's read / folder marks (news.update; archive is the original's "keep")
cli::Json answer = modern::ui_read(g_, context, request);
auto* data = std::get_if<O>(&answer.value);
if (!data || !data->count("data")) return answer;
auto& d = std::get<O>((*data)["data"].value);
const std::string owner = modern::ui_manager_id(g_, manager);
const auto marks = g_.news_marks.find(owner);
A messages;
for (const auto& [id, m] : ui_mailbox(g_, manager)) {
const uint8_t flags = marks == g_.news_marks.end() || !marks->second.count(id) ? 0 : marks->second.at(id);
const AlmanacDate day = almanac_date(m.date_counter, g_.display_start_year);
char date[16];
std::snprintf(date, sizeof date, "%04d-%02d-%02d", day.year, day.month, day.day);
const std::string first_line = m.text.substr(0, std::min(m.text.find('\n'), size_t(80)));
const auto text = [](const std::string& t) { return cli::Json(O{{"key", nullptr}, {"text", t}, {"locale", "de"}, {"args", O{}}}); };
messages.push_back(O{{"id", id}, {"ownerManagerId", owner}, {"date", date}, {"sender", text("Postfach")},
{"subject", text(first_line)}, {"paragraphs", A{text(m.text)}}, {"category", "mail"},
{"read", (flags & 1) != 0},
{"folder", (flags & 2) ? "trash" : m.kept_for_manager ? "archive" : "inbox"},
{"entityIds", A{}}, {"actionIds", A{}}});
}
d["messages"] = std::move(messages);
d["articles"] = A{}; // the interface renders and keeps its articles (PRESS.md)
d["dedupePolicyId"] = "rules:press-dedupe-v1";
return answer;
}
if (manager < 0 || size_t(manager) >= g_.manager_teams.size() ||
(kind != "statistics" && kind != "opening" && kind != "lineup" && kind != "tactics" && kind != "training" && kind != "bank" && kind != "contracts" &&
kind != "advertising" && kind != "stadium" && kind != "transfer" && kind != "postmatch" && kind != "credit" &&
@@ -10582,6 +10630,42 @@ cli::Json ModernSession::ui_command(const cli::Json& request, uint64_t& revision
}
revision = previous + 1;
answer = modern::ui_command_result(request_id, previous, revision, {"bank", "desktop"}, "Bankauftrag ausgeführt.");
} else if (kind == "news.update") {
// payload {messageId, action: read | unread | archive | trash | restore}: the desk manager's
// mailbox. read/unread and the trash are the interface's marks (saved, the message stays
// in the original mailbox, a restore brings it back); archive is the original's "keep"
// (a kept message is not dropped when the mailbox is full), restore releases it again.
const std::string id = ui_text(*payload, "messageId"), action = ui_text(*payload, "action");
if (action != "read" && action != "unread" && action != "archive" && action != "trash" && action != "restore")
return refuse("invalid_payload", "action ist read, unread, archive, trash oder restore.");
const auto box = ui_mailbox(g_, manager);
const auto it = std::find_if(box.begin(), box.end(), [&](const auto& m) { return m.first == id; });
if (it == box.end()) return refuse("unknown_message", "Diese Nachricht gibt es nicht (mehr).");
uint8_t& flags = g_.news_marks[modern::ui_manager_id(g_, manager)][id];
const uint8_t before = flags;
const bool kept = it->second.kept_for_manager;
bool keep_toggle = false;
if (action == "read") flags |= 1;
else if (action == "unread") flags &= uint8_t(~1);
else if (action == "trash") flags |= 2;
else if (action == "archive") flags &= uint8_t(~2), keep_toggle = !kept;
else flags &= uint8_t(~2), keep_toggle = kept; // restore: back to the inbox
if (keep_toggle) {
ui::MailboxActionRequest r;
r.kind = ui::MailboxActionKind::toggle_keep;
r.manager_slot = manager, r.revision = revision;
r.entry_index = it->second.entry_index, r.expected_entry = it->second.raw_entry;
uint64_t scratch = revision;
if (!dispatch_mailbox(r, scratch, error)) { restore(); return refuse("mailbox_refused", error); }
}
if (flags == 0) g_.news_marks[modern::ui_manager_id(g_, manager)].erase(id);
if (flags == before && !keep_toggle) {
answer = modern::ui_command_result(request_id, previous, previous, {}, "Unverändert.");
std::get<cli::Json::Object>(answer.value)["outcome"] = "no_change";
} else {
revision = previous + 1;
answer = modern::ui_command_result(request_id, previous, revision, {"news"}, "Postfach aktualisiert.");
}
} else if (kind == "lineup.commit") {
// payload {order: [player ids]}: the squad in this order (the first eleven start); the
// board opens as on the desk (the human line-up path), then rows are swapped into place
+1 -1
View File
@@ -154,7 +154,7 @@ A allowed_commands(const ModernState& g, const UiContext& c) {
"session.advance", "lineup.commit", "tactics.commit", "friendly.book", "manager.resign.commit",
"training.commit", "camp.book", "bank.commit", "toto.submit", "quote.request", "contract.offer",
"stadium.commit", "advertising.accept", "budget.commit", "ticket.commit", "transfer.submit",
"sponsor.answer", "environment.commit", "fixture.reschedule", "contract.cancel"};
"sponsor.answer", "environment.commit", "fixture.reschedule", "contract.cancel", "news.update"};
A out;
for (size_t i = 0; i < std::size(kAll); ++i)
if (!national || i < 5) out.push_back(kAll[i]);
+13
View File
@@ -174,6 +174,15 @@ void save_world_career(const std::filesystem::path& path, const World& world, co
{"traffic_scale", int64_t(state.traffic_scale)},
{"press", press_json(state.press)},
{"press_serial", int64_t(state.press_serial)},
{"news_marks", [&] {
O managers;
for (const auto& [manager, marks] : state.news_marks) {
O m;
for (const auto& [message, flags] : marks) m[message] = int(flags);
managers[manager] = std::move(m);
}
return J(std::move(managers));
}()},
{"press_month", int64_t(state.press_month)},
{"press_month_cash", [&] { A a; for (int64_t v : state.press_month_cash) a.push_back(v); return J(std::move(a)); }()},
{"career_end_reason", state.career_end_reason},
@@ -239,6 +248,10 @@ SavedWorldCareer load_world_career(const std::filesystem::path& path, const Worl
out.state.press = press_json(get(modern, "press"));
out.state.press_serial = uint32_t(num(get(modern, "press_serial")));
}
if (const auto it = o->find("news_marks"); it != o->end() && it->second.object())
for (const auto& [manager, marks] : *it->second.object())
if (marks.object())
for (const auto& [message, flags] : *marks.object()) out.state.news_marks[manager][message] = uint8_t(num(flags));
if (o->count("press_month")) {
out.state.press_month = uint32_t(num(get(modern, "press_month")));
for (const J& v : arr(get(modern, "press_month_cash"))) out.state.press_month_cash.push_back(int64_t(num(v)));
+4
View File
@@ -11,6 +11,7 @@
#include <array>
#include <cstddef>
#include <cstdint>
#include <map>
#include <string>
#include <utility>
#include <vector>
@@ -116,6 +117,9 @@ struct ModernState : GameState {
// each manager's cash at its start; the change over a month is finance.profit / finance.loss.
uint32_t press_month = 0;
std::vector<int64_t> press_month_cash;
// The interface's mailbox marks per manager id and message id (news.update): bit 0 read,
// bit 1 in the trash. Keeping a message is the original's own flag in the mailbox.
std::map<std::string, std::map<std::string, uint8_t>> news_marks;
// How and when the career ended ("" while it runs; "career_completed" when the set duration ran
// out, "last_human_resigned" when the only or last human manager gave up): the interface's
// terminationReason and the end of the employments.