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:
1 parent
c361dccd34
commit
179cd970ad
6 files changed
+139
-2
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user