1075 lines
43 KiB
C++
1075 lines
43 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
||
#include "modern/ui/news_catalog.hpp"
|
||
#include <algorithm>
|
||
#include <array>
|
||
#include <charconv>
|
||
#include <chrono>
|
||
#include <cmath>
|
||
#include <iomanip>
|
||
#include <limits>
|
||
#include <map>
|
||
#include <set>
|
||
#include <sstream>
|
||
|
||
namespace bmh::modern_ui {
|
||
namespace {
|
||
constexpr int64_t max_money = 900000000000000LL;
|
||
Json fail(const std::string &code, const Json &fields = Json::array()) {
|
||
return {{"ok", false}, {"error", {{"code", code}, {"fields", fields}}}};
|
||
}
|
||
bool integer(const Json &v, int64_t lo, int64_t hi) {
|
||
if (v.is_number_float()) {
|
||
const auto n = v.get<double>();
|
||
return std::isfinite(n) && n == std::floor(n) && n >= double(lo) &&
|
||
n <= double(hi);
|
||
}
|
||
if (v.is_number_unsigned())
|
||
return lo <= 0 ? v.get<uint64_t>() <= uint64_t(hi)
|
||
: v.get<uint64_t>() >= uint64_t(lo) &&
|
||
v.get<uint64_t>() <= uint64_t(hi);
|
||
return v.is_number_integer() && v.get<int64_t>() >= lo &&
|
||
v.get<int64_t>() <= hi;
|
||
}
|
||
bool currency(const std::string &v) {
|
||
return v == "EUR" || v == "GBP" || v == "USD" || v == "CHF" || v == "DEM";
|
||
}
|
||
bool whitespace(uint32_t n) {
|
||
return n == 32 || n == 160 || n == 0x1680 || (n >= 0x2000 && n <= 0x200a) ||
|
||
n == 0x2028 || n == 0x2029 || n == 0x202f || n == 0x205f ||
|
||
n == 0x3000 || n == 0xfeff;
|
||
}
|
||
std::string text(const Json &value, size_t maximum = 120) {
|
||
if (!value.is_string())
|
||
throw editor::Error("invalid_news_text");
|
||
const auto &s = value.get_ref<const std::string &>();
|
||
size_t begin = s.size(), end = 0, units = 0;
|
||
for (size_t i = 0; i < s.size();) {
|
||
const size_t start = i;
|
||
const auto first = static_cast<unsigned char>(s[i++]);
|
||
uint32_t n = 0;
|
||
int count = 0;
|
||
if (first < 0x80)
|
||
n = first;
|
||
else if (first >= 0xc2 && first <= 0xdf) {
|
||
n = first & 31;
|
||
count = 1;
|
||
} else if (first >= 0xe0 && first <= 0xef) {
|
||
n = first & 15;
|
||
count = 2;
|
||
} else if (first >= 0xf0 && first <= 0xf4) {
|
||
n = first & 7;
|
||
count = 3;
|
||
} else
|
||
throw editor::Error("invalid_news_utf8");
|
||
for (int k = 0; k < count; ++k) {
|
||
if (i >= s.size() || (static_cast<unsigned char>(s[i]) & 0xc0) != 0x80)
|
||
throw editor::Error("invalid_news_utf8");
|
||
n = (n << 6) | (static_cast<unsigned char>(s[i++]) & 63);
|
||
}
|
||
if ((count == 1 && n < 0x80) || (count == 2 && n < 0x800) ||
|
||
(count == 3 && n < 0x10000) || n > 0x10ffff ||
|
||
(n >= 0xd800 && n <= 0xdfff) || n < 32 || (n >= 127 && n <= 159))
|
||
throw editor::Error("invalid_news_utf8");
|
||
units += n > 0xffff ? 2 : 1;
|
||
if (!whitespace(n)) {
|
||
begin = std::min(begin, start);
|
||
end = i;
|
||
}
|
||
}
|
||
if (begin == s.size() || units > maximum)
|
||
throw editor::Error("invalid_news_text");
|
||
return s.substr(begin, end - begin);
|
||
}
|
||
std::string identity(const Json &v) {
|
||
if (integer(v, 0, 9007199254740991LL))
|
||
return std::to_string(v.get<int64_t>());
|
||
if (!v.is_string())
|
||
throw editor::Error("invalid_news_id");
|
||
const auto id = v.get<std::string>();
|
||
if (id.empty() || id.size() > 140 ||
|
||
id.find_first_not_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy"
|
||
"z0123456789:_./-") != id.npos)
|
||
throw editor::Error("invalid_news_id");
|
||
return id;
|
||
}
|
||
Json entity(const Json &v, bool scope = false) {
|
||
if (!v.is_object())
|
||
throw editor::Error("invalid_news_entity");
|
||
Json result = {{"id", identity(v.at("id"))}, {"name", text(v.at("name"))}};
|
||
if (v.contains("kind"))
|
||
result["kind"] = text(v.at("kind"), 24);
|
||
if (scope && (!result.contains("kind") || (result.at("kind") != "club" &&
|
||
result.at("kind") != "national" &&
|
||
result.at("kind") != "global")))
|
||
throw editor::Error("invalid_news_scope");
|
||
if (v.contains("names")) {
|
||
if (!v.at("names").is_object())
|
||
throw editor::Error("invalid_news_names");
|
||
result["names"] = {{"de", text(v.at("names").at("de"))},
|
||
{"en", text(v.at("names").at("en"))}};
|
||
}
|
||
return result;
|
||
}
|
||
const std::map<std::string, std::map<std::string, std::array<const char *, 2>>>
|
||
terms{{"focus",
|
||
{{"fitness", {"Kondition", "fitness"}},
|
||
{"technique", {"Technik", "technique"}},
|
||
{"passing", {"Passspiel", "passing"}},
|
||
{"shooting", {"Torabschluss", "finishing"}},
|
||
{"teamwork", {"Teamplay", "teamwork"}},
|
||
{"set_pieces", {"Standardsituationen", "set pieces"}}}},
|
||
{"diagnosis",
|
||
{{"muscle", {"Muskelprobleme", "muscle problems"}},
|
||
{"ankle", {"Sprunggelenksprobleme", "ankle problems"}},
|
||
{"knee", {"Knieprobleme", "knee problems"}},
|
||
{"illness", {"Erkrankung", "illness"}},
|
||
{"unspecified",
|
||
{"nicht näher bezeichnete Beschwerden", "unspecified symptoms"}}}},
|
||
{"tournament",
|
||
{{"world_cup", {"WM", "World Cup"}},
|
||
{"european_championship", {"EM", "European Championship"}}}},
|
||
{"facility",
|
||
{{"training_ground", {"Trainingsgelände", "the training ground"}},
|
||
{"youth", {"Nachwuchsarbeit", "youth development"}},
|
||
{"medical", {"medizinische Betreuung", "medical facilities"}},
|
||
{"floodlights", {"Flutlichtanlage", "the floodlights"}},
|
||
{"parking", {"Parkmöglichkeiten", "parking facilities"}},
|
||
{"dressing_rooms", {"Umkleidebereich", "the dressing rooms"}}}}};
|
||
const std::map<std::string, std::pair<int64_t, int64_t>> bounds{
|
||
{"integer", {0, 1000000}}, {"positive_integer", {1, 1000000}},
|
||
{"matches", {1, 999}}, {"days", {1, 3650}},
|
||
{"months", {1, 1200}}, {"ordinal", {1, 125}},
|
||
{"year", {1, 9999}}, {"minute", {1, 120}},
|
||
{"players", {1, 60}}, {"shares", {1, 1000000}},
|
||
{"seats", {1, 500000}}};
|
||
Json normalize(const std::string &type, const Json &v) {
|
||
if (type == "entity" || type == "person")
|
||
return entity(v);
|
||
if (type == "entities") {
|
||
if (!v.is_array() || v.empty() || v.size() > 16)
|
||
throw editor::Error("entities");
|
||
Json out = Json::array();
|
||
for (const auto &e : v)
|
||
out.push_back(entity(e));
|
||
return out;
|
||
}
|
||
if (type == "score") {
|
||
if (!v.is_object() || !integer(v.at("for"), 0, 50) ||
|
||
!integer(v.at("against"), 0, 50))
|
||
throw editor::Error("score");
|
||
return {{"for", v.at("for")}, {"against", v.at("against")}};
|
||
}
|
||
if (type == "money") {
|
||
if (!v.is_object() || v.size() != 2 ||
|
||
!integer(v.at("minor"), 0, max_money) ||
|
||
!v.at("currency").is_string() || !currency(v.at("currency")))
|
||
throw editor::Error("money");
|
||
return {{"minor", v.at("minor").get<int64_t>()},
|
||
{"currency", v.at("currency")}};
|
||
}
|
||
if (type == "date") {
|
||
if (!v.is_string() || !news_iso_date(v))
|
||
throw editor::Error("date");
|
||
return v;
|
||
}
|
||
if (type == "season") {
|
||
std::string s;
|
||
if (integer(v, 1, 9998)) {
|
||
std::ostringstream out;
|
||
out << std::setfill('0') << std::setw(4) << v.get<int>() << '/'
|
||
<< std::setw(2) << ((v.get<int>() + 1) % 100);
|
||
s = out.str();
|
||
} else if (v.is_string())
|
||
s = v.get<std::string>();
|
||
if (s.size() != 7 || s[4] != '/' ||
|
||
s.find_first_not_of("0123456789/") != s.npos ||
|
||
std::stoi(s.substr(5)) != (std::stoi(s.substr(0, 4)) + 1) % 100)
|
||
throw editor::Error("season");
|
||
return s;
|
||
}
|
||
if (type == "boolean") {
|
||
if (!v.is_boolean())
|
||
throw editor::Error("boolean");
|
||
return v;
|
||
}
|
||
if (type == "venue") {
|
||
if (v != "home" && v != "away")
|
||
throw editor::Error("venue");
|
||
return v;
|
||
}
|
||
if (terms.contains(type)) {
|
||
if (!v.is_string() || !terms.at(type).contains(v.get<std::string>()))
|
||
throw editor::Error("term");
|
||
return v;
|
||
}
|
||
if (type == "percent") {
|
||
if (!v.is_number() || !std::isfinite(v.get<double>()) || v < 0 || v > 100)
|
||
throw editor::Error("percent");
|
||
const auto n = v.get<double>();
|
||
return n == std::floor(n) ? Json(int64_t(n)) : v;
|
||
}
|
||
if (!bounds.contains(type) ||
|
||
!integer(v, bounds.at(type).first, bounds.at(type).second))
|
||
throw editor::Error("integer");
|
||
return v.get<int64_t>();
|
||
}
|
||
std::string grouped(int64_t value, bool en) {
|
||
auto out = number(value);
|
||
if (en)
|
||
std::replace(out.begin(), out.end(), '.', ',');
|
||
return out;
|
||
}
|
||
std::string date_text(const std::string &v, bool en) {
|
||
static const std::array<const char *, 12> de{
|
||
"Januar", "Februar", "März", "April", "Mai", "Juni",
|
||
"Juli", "August", "September", "Oktober", "November", "Dezember"},
|
||
english{"January", "February", "March", "April",
|
||
"May", "June", "July", "August",
|
||
"September", "October", "November", "December"};
|
||
return std::to_string(std::stoi(v.substr(8))) + (en ? " " : ". ") +
|
||
(en ? english : de)[size_t(std::stoi(v.substr(5, 2)) - 1)] + " " +
|
||
std::to_string(std::max(1, std::stoi(v.substr(0, 4))));
|
||
}
|
||
std::string decimal(double value, bool en) {
|
||
// Only bounded nonnegative percentages use fractional numbers. Currency never
|
||
// passes through double. Decimal rounding uses the shortest JSON spelling.
|
||
auto s = Json(value / 100.0).dump();
|
||
int exponent = 2;
|
||
const auto exp = s.find_first_of("eE");
|
||
if (exp != s.npos) {
|
||
exponent += std::stoi(s.substr(exp + 1));
|
||
s = s.substr(0, exp);
|
||
}
|
||
const auto dot = s.find('.');
|
||
const int point = int(dot == s.npos ? s.size() : dot) + exponent;
|
||
if (dot != s.npos)
|
||
s.erase(dot, 1);
|
||
s = point <= 0 ? "0." + std::string(size_t(-point), '0') + s
|
||
: point >= int(s.size())
|
||
? s + std::string(size_t(point - int(s.size())), '0')
|
||
: s.substr(0, size_t(point)) + "." + s.substr(size_t(point));
|
||
const auto at = s.find('.');
|
||
auto whole = at == s.npos ? s : s.substr(0, at),
|
||
fraction = at == s.npos ? std::string() : s.substr(at + 1);
|
||
fraction.append(3, '0');
|
||
int64_t cents = std::stoll(whole) * 100 + std::stoi(fraction.substr(0, 2));
|
||
if (fraction[2] >= '5')
|
||
++cents;
|
||
std::string out = grouped(cents / 100, en);
|
||
const int tail = int(cents % 100);
|
||
if (tail) {
|
||
out += en ? '.' : ',';
|
||
out += char('0' + tail / 10);
|
||
if (tail % 10)
|
||
out += char('0' + tail % 10);
|
||
}
|
||
return out;
|
||
}
|
||
std::string format(const std::string &type, const Json &v, bool en) {
|
||
const auto language = en ? "en" : "de";
|
||
if (type == "entity" || type == "person")
|
||
return v.contains("names") ? v.at("names").at(language).get<std::string>()
|
||
: v.at("name").get<std::string>();
|
||
if (type == "entities") {
|
||
std::string out;
|
||
for (size_t i = 0; i < v.size(); ++i) {
|
||
if (i)
|
||
out += i + 1 == v.size() ? (en ? " and " : " und ") : ", ";
|
||
out += format("entity", v[i], en);
|
||
}
|
||
return out;
|
||
}
|
||
if (type == "score")
|
||
return grouped(v.at("for"), en) + (en ? "–" : ":") +
|
||
grouped(v.at("against"), en);
|
||
if (type == "money") {
|
||
const int64_t n = v.at("minor");
|
||
const std::string code = v.at("currency");
|
||
std::string amount = grouped(n / 100, en);
|
||
if (n % 100) {
|
||
amount += en ? '.' : ',';
|
||
amount += char('0' + (n % 100) / 10);
|
||
amount += char('0' + n % 10);
|
||
}
|
||
const std::string symbol = code == "EUR" ? "€"
|
||
: code == "GBP" ? "£"
|
||
: code == "USD" ? (en ? "US$" : "$")
|
||
: code == "DEM" ? "DEM"
|
||
: code;
|
||
return en ? ((code == "CHF" || code == "DEM") ? symbol + " " + amount
|
||
: symbol + amount)
|
||
: amount + " " + symbol;
|
||
}
|
||
if (type == "percent")
|
||
return decimal(v.get<double>(), en) + (en ? "%" : " %");
|
||
if (type == "date")
|
||
return date_text(v, en);
|
||
if (type == "season" || type == "venue")
|
||
return v.get<std::string>();
|
||
if (terms.contains(type))
|
||
return terms.at(type).at(v.get<std::string>())[en ? 1 : 0];
|
||
const int64_t n = v.get<int64_t>();
|
||
if (type == "year")
|
||
return std::to_string(n);
|
||
if (type == "ordinal")
|
||
return grouped(n, en) + (en ? ((n % 100 >= 11 && n % 100 <= 13) ? "th"
|
||
: n % 10 == 1 ? "st"
|
||
: n % 10 == 2 ? "nd"
|
||
: n % 10 == 3 ? "rd"
|
||
: "th")
|
||
: ".");
|
||
const std::map<std::string, std::array<const char *, 4>> units{
|
||
{"days", {"Tag", "Tage", "day", "days"}},
|
||
{"months", {"Monat", "Monate", "month", "months"}},
|
||
{"matches", {"Partie", "Partien", "match", "matches"}},
|
||
{"players", {"Spieler", "Spieler", "player", "players"}},
|
||
{"shares", {"Aktie", "Aktien", "share", "shares"}},
|
||
{"seats", {"Platz", "Plätze", "seat", "seats"}}};
|
||
return grouped(n, en) +
|
||
(units.contains(type)
|
||
? " " +
|
||
std::string(units.at(type)[(en ? 2 : 0) + (n == 1 ? 0 : 1)])
|
||
: "");
|
||
}
|
||
bool guard(const std::string &g, const Json &p, const Json &event) {
|
||
const auto has = [&](const char *k) { return p.contains(k); };
|
||
const int diff = has("score") ? p.at("score").at("for").get<int>() -
|
||
p.at("score").at("against").get<int>()
|
||
: -100;
|
||
if (g == "win")
|
||
return has("score") && diff > 0;
|
||
if (g == "loss")
|
||
return has("score") && diff < 0;
|
||
if (g == "draw")
|
||
return has("score") && diff == 0;
|
||
if (g == "narrowWin")
|
||
return diff == 1;
|
||
if (g == "narrowLoss")
|
||
return diff == -1;
|
||
if (g == "bigWin")
|
||
return diff >= 3;
|
||
if (g == "heavyLoss")
|
||
return has("score") && diff <= -3;
|
||
if (g == "goalless")
|
||
return has("score") && p.at("score").at("for") == 0 &&
|
||
p.at("score").at("against") == 0;
|
||
if (g == "clean")
|
||
return has("score") && p.at("score").at("against") == 0;
|
||
if (g == "derby" || g == "trailed")
|
||
return has(g.c_str()) && p.at(g) == true;
|
||
if (g == "home" || g == "away")
|
||
return has("venue") && p.at("venue") == g;
|
||
if (g == "late")
|
||
return has("minute") && p.at("minute") >= 80;
|
||
if (g == "shootoutWin" || g == "shootoutLoss")
|
||
return has("shootout") &&
|
||
(g == "shootoutWin"
|
||
? p.at("shootout").at("for") > p.at("shootout").at("against")
|
||
: p.at("shootout").at("for") < p.at("shootout").at("against"));
|
||
if (g == "streak")
|
||
return has("matches") && p.at("matches") >= 2;
|
||
if (g == "positiveAmount")
|
||
return has("amount") && p.at("amount").at("minor") > 0;
|
||
if (g == "positiveRate")
|
||
return has("rate") && p.at("rate") > 0;
|
||
if (g == "feeAbsent")
|
||
return !has("fee");
|
||
if (g == "wageAbsent")
|
||
return !has("wage");
|
||
if (g == "untilAbsent")
|
||
return !has("until");
|
||
if (g == "yearMatchesDate")
|
||
return has("year") &&
|
||
p.at("year") ==
|
||
std::stoi(event.at("date").get<std::string>().substr(0, 4));
|
||
return false;
|
||
}
|
||
std::string replace(const std::string &source, const Json ¶meters,
|
||
const Json &values, bool en) {
|
||
std::string out;
|
||
size_t at = 0;
|
||
while (true) {
|
||
const auto start = source.find("{{", at);
|
||
if (start == source.npos) {
|
||
out += source.substr(at);
|
||
break;
|
||
}
|
||
out += source.substr(at, start - at);
|
||
const auto end = source.find("}}", start + 2);
|
||
if (end == source.npos)
|
||
throw editor::Error("invalid_news_template");
|
||
const auto key = source.substr(start + 2, end - start - 2);
|
||
out += format(parameters.at(key).at("type"), values.at(key), en);
|
||
at = end + 2;
|
||
}
|
||
return out;
|
||
}
|
||
const Json *select_template(const std::vector<const Json *> &list,
|
||
const Json &event, const Json &p) {
|
||
const auto find = [&](const std::string &variant) -> const Json * {
|
||
for (const auto *t : list)
|
||
if (t->at("variant") == variant)
|
||
return t;
|
||
return nullptr;
|
||
};
|
||
if (event.contains("variant"))
|
||
return find(event.at("variant"));
|
||
const auto type = event.at("type").get<std::string>();
|
||
if (type == "match.result") {
|
||
if (!p.contains("score"))
|
||
return find("loss");
|
||
const int d = p.at("score").at("for").get<int>() -
|
||
p.at("score").at("against").get<int>();
|
||
return find(d == 0 ? (p.at("score").at("for") == 0 ? "goalless" : "draw")
|
||
: d >= 3 ? "big-win"
|
||
: d == 1 ? "narrow-win"
|
||
: d > 0 ? "win"
|
||
: d <= -3 ? "heavy-loss"
|
||
: d == -1 ? "narrow-loss"
|
||
: "loss");
|
||
}
|
||
if (type == "cup.round_result" || type == "cup.final_result" ||
|
||
type == "cup.extra_time" || type == "indoor.result")
|
||
return find(p.contains("score") &&
|
||
p.at("score").at("for") > p.at("score").at("against")
|
||
? "win"
|
||
: "loss");
|
||
if (type == "cup.penalties")
|
||
return find(p.contains("shootout") && p.at("shootout").at("for") >
|
||
p.at("shootout").at("against")
|
||
? "win"
|
||
: "loss");
|
||
auto sorted = list;
|
||
std::stable_sort(
|
||
sorted.begin(), sorted.end(), [](const auto *a, const auto *b) {
|
||
return a->at("parameters").size() > b->at("parameters").size();
|
||
});
|
||
for (const auto *t : sorted) {
|
||
bool good = true;
|
||
for (const auto &[key, s] : t->at("parameters").items())
|
||
good &= s.value("source", "") == "scope" || p.contains(key);
|
||
for (const auto &g : t->at("guards"))
|
||
good &= guard(g, p, event);
|
||
if (good)
|
||
return t;
|
||
}
|
||
if (const auto *t = find("default"))
|
||
return t;
|
||
return list.empty() ? nullptr : list.front();
|
||
}
|
||
} // namespace
|
||
|
||
std::string news_hash(const std::string &value) {
|
||
uint64_t n = 14695981039346656037ULL;
|
||
for (unsigned char c : value) {
|
||
n ^= c;
|
||
n *= 1099511628211ULL;
|
||
}
|
||
std::ostringstream out;
|
||
out << std::hex << std::setfill('0') << std::setw(16) << n;
|
||
return out.str();
|
||
}
|
||
std::string news_canonical(const Json &v) {
|
||
if (v.is_object()) {
|
||
std::map<std::string, Json> sorted;
|
||
for (const auto &[k, n] : v.items())
|
||
sorted[k] = n;
|
||
std::string out = "{";
|
||
for (const auto &[k, n] : sorted) {
|
||
if (out.size() > 1)
|
||
out += ',';
|
||
out += Json(k).dump() + ':' + news_canonical(n);
|
||
}
|
||
return out + '}';
|
||
}
|
||
if (v.is_array()) {
|
||
std::string out = "[";
|
||
for (const auto &n : v) {
|
||
if (out.size() > 1)
|
||
out += ',';
|
||
out += news_canonical(n);
|
||
}
|
||
return out + ']';
|
||
}
|
||
if (v.is_number_float() && v.get<double>() == std::floor(v.get<double>()) &&
|
||
std::abs(v.get<double>()) <= 9007199254740991.0)
|
||
return std::to_string(v.get<int64_t>());
|
||
auto out = v.dump();
|
||
if (v.is_number_float()) {
|
||
const auto at = out.find('e');
|
||
if (at != out.npos) {
|
||
const int n = std::stoi(out.substr(at + 1));
|
||
auto mantissa = out.substr(0, at);
|
||
if (n >= -6 && n < 21) {
|
||
const auto dot = mantissa.find('.');
|
||
const int point = int(dot == mantissa.npos ? mantissa.size() : dot) + n;
|
||
if (dot != mantissa.npos)
|
||
mantissa.erase(dot, 1);
|
||
out = point <= 0 ? "0." + std::string(size_t(-point), '0') + mantissa
|
||
: point >= int(mantissa.size())
|
||
? mantissa +
|
||
std::string(size_t(point - int(mantissa.size())), '0')
|
||
: mantissa.substr(0, size_t(point)) + "." +
|
||
mantissa.substr(size_t(point));
|
||
} else
|
||
out = mantissa + "e" + (n >= 0 ? "+" : "") + std::to_string(n);
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
bool news_iso_date(const std::string &s) {
|
||
if (s.size() != 10 || s[4] != '-' || s[7] != '-')
|
||
return false;
|
||
for (size_t i = 0; i < s.size(); ++i)
|
||
if (i != 4 && i != 7 && (s[i] < '0' || s[i] > '9'))
|
||
return false;
|
||
using namespace std::chrono;
|
||
return year_month_day{year{std::stoi(s.substr(0, 4))},
|
||
month{unsigned(std::stoi(s.substr(5, 2)))},
|
||
day{unsigned(std::stoi(s.substr(8, 2)))}}
|
||
.ok();
|
||
}
|
||
bool news_scope_matches(const Json &a, const Json &b) {
|
||
return a.is_object() && b.is_object() && a.contains("kind") &&
|
||
a.contains("id") && b.contains("kind") && b.contains("id") &&
|
||
(a.at("kind") == "global" ||
|
||
(a.at("kind") == b.at("kind") && a.at("id") == b.at("id")));
|
||
}
|
||
Json NewsEntity::json() const {
|
||
Json out = {{"id", id}, {"name", name}};
|
||
if (localized_names)
|
||
out["names"] = {{"de", localized_names->first},
|
||
{"en", localized_names->second}};
|
||
return out;
|
||
}
|
||
Json NewsScope::json() const {
|
||
if (kind != NewsScopeKind::club && kind != NewsScopeKind::national &&
|
||
kind != NewsScopeKind::global)
|
||
throw editor::Error("invalid_news_scope");
|
||
auto out = team.json();
|
||
out["kind"] = kind == NewsScopeKind::club ? "club"
|
||
: kind == NewsScopeKind::national ? "national"
|
||
: "global";
|
||
return out;
|
||
}
|
||
Json NewsMinorMoney::json() const {
|
||
return normalize("money", {{"minor", minor}, {"currency", currency}});
|
||
}
|
||
std::optional<NewsMinorMoney> news_money_from_dto(const Json &v,
|
||
std::string &error) {
|
||
error.clear();
|
||
try {
|
||
if (!v.is_object() || v.size() != 3 || !v.at("minor").is_string() ||
|
||
!v.at("currency").is_string() || !integer(v.at("minorUnits"), 0, 4))
|
||
throw editor::Error("invalid_dto_money");
|
||
const auto raw = v.at("minor").get<std::string>();
|
||
if (raw.empty() || raw.size() > 20 || (raw.size() > 1 && raw[0] == '0') ||
|
||
raw.find_first_not_of("0123456789") != raw.npos)
|
||
throw editor::Error("invalid_dto_money");
|
||
int64_t amount = 0;
|
||
const auto parsed =
|
||
std::from_chars(raw.data(), raw.data() + raw.size(), amount);
|
||
if (parsed.ec != std::errc() || parsed.ptr != raw.data() + raw.size())
|
||
throw editor::Error("news_money_overflow");
|
||
const int precision = v.at("minorUnits");
|
||
if (precision < 2) {
|
||
for (int i = precision; i < 2; ++i) {
|
||
if (amount > max_money / 10)
|
||
throw editor::Error("news_money_overflow");
|
||
amount *= 10;
|
||
}
|
||
} else
|
||
for (int i = 2; i < precision; ++i) {
|
||
if (amount % 10)
|
||
throw editor::Error("inexact_news_money");
|
||
amount /= 10;
|
||
}
|
||
if (!currency(v.at("currency")))
|
||
throw editor::Error("unsupported_news_currency");
|
||
NewsMinorMoney result{amount, v.at("currency")};
|
||
(void)result.json();
|
||
return result;
|
||
} catch (const std::exception &e) {
|
||
error = e.what();
|
||
return {};
|
||
}
|
||
}
|
||
Json NewsEvent::json() const {
|
||
Json out = {{"id", id}, {"type", type},
|
||
{"date", date}, {"scope", scope.json()},
|
||
{"data", data}, {"demo", demo}};
|
||
if (variant)
|
||
out["variant"] = *variant;
|
||
return out;
|
||
}
|
||
Json NewsRenderContext::json() const {
|
||
if (language != NewsLanguage::de && language != NewsLanguage::en)
|
||
throw editor::Error("invalid_news_language");
|
||
Json out = {{"language", language == NewsLanguage::de ? "de" : "en"}};
|
||
if (manager)
|
||
out["manager"] = manager->json();
|
||
if (as_of)
|
||
out["asOf"] = *as_of;
|
||
return out;
|
||
}
|
||
|
||
NewsCatalog::NewsCatalog(Json data) : catalogue_(std::move(data)) {
|
||
if (!catalogue_.is_object() ||
|
||
catalogue_.at("schema") != "bmh-news-catalog@1" ||
|
||
!integer(catalogue_.at("version"), 1, 1000) ||
|
||
!catalogue_.at("templates").is_array() ||
|
||
catalogue_.at("templates").empty() ||
|
||
catalogue_.at("templates").size() > 1000)
|
||
throw editor::Error("invalid_news_catalogue");
|
||
std::set<std::string> ids, variants;
|
||
for (const auto &t : catalogue_.at("templates")) {
|
||
const auto id = identity(t.at("id"));
|
||
if (!ids.insert(id).second || !t.at("event").is_string() ||
|
||
!t.at("variant").is_string() ||
|
||
!variants
|
||
.insert(t.at("event").get<std::string>() + ":" +
|
||
t.at("variant").get<std::string>())
|
||
.second ||
|
||
!t.at("parameters").is_object() || !t.at("scopes").is_array() ||
|
||
t.at("scopes").empty() || !t.at("guards").is_array())
|
||
throw editor::Error("invalid_news_catalogue");
|
||
for (const auto &scope : t.at("scopes"))
|
||
if (scope != "club" && scope != "national" && scope != "global")
|
||
throw editor::Error("invalid_news_catalogue");
|
||
if (t.at("tone") != "positive" && t.at("tone") != "neutral" &&
|
||
t.at("tone") != "negative")
|
||
throw editor::Error("invalid_news_catalogue");
|
||
for (const auto &[key, p] : t.at("parameters").items()) {
|
||
const std::string type = p.at("type");
|
||
if (type != "entity" && type != "person" && type != "entities" &&
|
||
type != "score" && type != "money" && type != "date" &&
|
||
type != "season" && type != "boolean" && type != "venue" &&
|
||
type != "percent" && !bounds.contains(type) && !terms.contains(type))
|
||
throw editor::Error("invalid_news_parameter_type");
|
||
if (p.contains("source") &&
|
||
!((key == "team" && p.at("source") == "scope") ||
|
||
(key == "manager" && p.at("source") == "event-or-context")))
|
||
throw editor::Error("invalid_news_parameter_source");
|
||
}
|
||
const std::set<std::string> guards{
|
||
"win", "loss", "draw", "narrowWin",
|
||
"narrowLoss", "bigWin", "heavyLoss", "goalless",
|
||
"clean", "derby", "trailed", "home",
|
||
"away", "late", "shootoutWin", "shootoutLoss",
|
||
"streak", "positiveAmount", "positiveRate", "feeAbsent",
|
||
"wageAbsent", "untilAbsent", "yearMatchesDate"};
|
||
for (const auto &g : t.at("guards"))
|
||
if (!g.is_string() || !guards.contains(g.get<std::string>()))
|
||
throw editor::Error("invalid_news_guard");
|
||
for (auto language : {"de", "en"}) {
|
||
Json parts = t.at(language).at("sentences");
|
||
parts.push_back(t.at(language).at("headline"));
|
||
for (const auto &part : parts) {
|
||
const std::string body = part;
|
||
size_t at = 0;
|
||
while ((at = body.find("{{", at)) != std::string::npos) {
|
||
const auto end = body.find("}}", at + 2);
|
||
if (end == std::string::npos ||
|
||
!t.at("parameters").contains(body.substr(at + 2, end - at - 2)))
|
||
throw editor::Error("invalid_news_placeholder");
|
||
at = end + 2;
|
||
}
|
||
}
|
||
}
|
||
|
||
for (const auto *lang : {"de", "en"}) {
|
||
const auto &v = t.at(lang);
|
||
(void)text(v.at("headline"), 240);
|
||
if (!v.at("sentences").is_array() || v.at("sentences").size() < 2 ||
|
||
v.at("sentences").size() > 4)
|
||
throw editor::Error("invalid_news_catalogue");
|
||
for (const auto &s : v.at("sentences"))
|
||
(void)text(s, 2200);
|
||
}
|
||
}
|
||
}
|
||
NewsCatalog NewsCatalog::load(const fs::path &path) {
|
||
return NewsCatalog(Json::parse(editor::read_file(path)));
|
||
}
|
||
Json NewsCatalog::registry() const {
|
||
Json out = Json::array();
|
||
for (const auto &t : catalogue_.at("templates")) {
|
||
Json row;
|
||
for (const auto *k : {"id", "event", "variant", "kind", "scopes", "tone",
|
||
"parameters", "guards"})
|
||
row[k] = t.at(k);
|
||
out.push_back(row);
|
||
}
|
||
return out;
|
||
}
|
||
Json NewsCatalog::from_event(const NewsEvent &event,
|
||
const NewsRenderContext &context) const {
|
||
try {
|
||
return from_event(event.json(), context.json());
|
||
} catch (...) {
|
||
return fail("invalid_news_event");
|
||
}
|
||
}
|
||
Json NewsCatalog::from_event(const Json &input, const Json &context) const {
|
||
try {
|
||
if (!input.is_object() || !context.is_object() ||
|
||
(input.contains("data") && !input.at("data").is_object() &&
|
||
!input.at("data").is_null()))
|
||
return fail("invalid_news_event");
|
||
const auto language = context.value("language", std::string("de"));
|
||
if (language != "de" && language != "en")
|
||
return fail("invalid_news_context", {"language"});
|
||
Json event = {{"id", identity(input.at("id"))},
|
||
{"type", input.value("type", Json())},
|
||
{"date", input.value("date", Json())}};
|
||
std::vector<const Json *> all, list;
|
||
for (const auto &t : catalogue_.at("templates"))
|
||
if (t.at("event") == event.at("type"))
|
||
all.push_back(&t);
|
||
if (all.empty())
|
||
return fail("unknown_news_event", {"type"});
|
||
if (!event.at("date").is_string() || !news_iso_date(event.at("date")))
|
||
return fail("invalid_news_date", {"date"});
|
||
if (context.contains("asOf") &&
|
||
(!context.at("asOf").is_string() || !news_iso_date(context.at("asOf"))))
|
||
return fail("invalid_news_context", {"asOf"});
|
||
if (context.contains("asOf") && event.at("date") > context.at("asOf"))
|
||
return fail("future_news_event", {"date"});
|
||
const auto scope = entity(input.at("scope"), true);
|
||
for (const auto *t : all)
|
||
if (std::find(t->at("scopes").begin(), t->at("scopes").end(),
|
||
scope.at("kind")) != t->at("scopes").end())
|
||
list.push_back(t);
|
||
if (list.empty())
|
||
return fail("unsupported_news_scope", {"scope"});
|
||
if (input.contains("variant")) {
|
||
if (!input.at("variant").is_string())
|
||
return fail("unknown_news_variant", {"variant"});
|
||
event["variant"] = input.at("variant");
|
||
}
|
||
Json schema = Json::object(), p = Json::object();
|
||
for (const auto *t : all)
|
||
for (const auto &[k, s] : t->at("parameters").items())
|
||
schema[k] = s;
|
||
const auto data = input.value("data", Json::object());
|
||
if (data.is_object())
|
||
for (const auto &[key, value] : data.items()) {
|
||
if (!schema.contains(key) || key == "team")
|
||
return fail("unknown_news_parameter", {key});
|
||
if (value.is_null())
|
||
continue;
|
||
try {
|
||
p[key] = normalize(schema.at(key).at("type"), value);
|
||
} catch (...) {
|
||
return fail("invalid_news_parameter", {key});
|
||
}
|
||
}
|
||
if (schema.contains("manager") && !p.contains("manager") &&
|
||
context.contains("manager")) {
|
||
try {
|
||
p["manager"] = normalize("person", context.at("manager"));
|
||
} catch (...) {
|
||
return fail("invalid_news_parameter", {"manager"});
|
||
}
|
||
}
|
||
const auto *t = select_template(list, event, p);
|
||
if (!t)
|
||
return fail("unknown_news_variant", {"variant"});
|
||
Json missing = Json::array();
|
||
for (const auto &[key, s] : t->at("parameters").items())
|
||
if (s.value("source", "") != "scope" && !p.contains(key))
|
||
missing.push_back(key);
|
||
if (!missing.empty())
|
||
return fail("missing_news_parameter", missing);
|
||
for (const auto &g : t->at("guards"))
|
||
if (!guard(g, p, event))
|
||
return fail("inconsistent_news_event", {g});
|
||
if (p.contains("opponent") && p.at("opponent").contains("kind") &&
|
||
(p.at("opponent").at("kind") == "club" ||
|
||
p.at("opponent").at("kind") == "national") &&
|
||
p.at("opponent").at("kind") != scope.at("kind"))
|
||
return fail("unsupported_news_scope", {"opponent.kind"});
|
||
auto values = p;
|
||
values["team"] = scope;
|
||
Json translations = Json::object();
|
||
for (const auto *lang : {"de", "en"}) {
|
||
const bool en = std::string(lang) == "en";
|
||
const auto headline =
|
||
replace(t->at(lang).at("headline"), t->at("parameters"), values, en);
|
||
std::string body;
|
||
for (const auto &s : t->at(lang).at("sentences")) {
|
||
if (!body.empty())
|
||
body += ' ';
|
||
body += replace(s, t->at("parameters"), values, en);
|
||
}
|
||
try {
|
||
(void)text(headline, 240);
|
||
(void)text(body, 2200);
|
||
} catch (...) {
|
||
return fail("news_article_too_long");
|
||
}
|
||
translations[lang] = {{"headline", headline}, {"body", body}};
|
||
}
|
||
const bool demo = input.value("demo", Json(false)) == true;
|
||
const Json normalized = {{"id", event.at("id")},
|
||
{"type", event.at("type")},
|
||
{"date", event.at("date")},
|
||
{"scope", scope},
|
||
{"data", p},
|
||
{"variant", t->at("variant")},
|
||
{"templateId", t->at("id")},
|
||
{"demo", demo}};
|
||
const auto key = news_canonical(
|
||
Json::array({scope.at("kind"), scope.at("id"), event.at("id")}));
|
||
return {{"ok", true},
|
||
{"article",
|
||
{{"schema", "bmh-news-article@1"},
|
||
{"catalogueVersion", catalogue_.at("version")},
|
||
{"id", "evt-" + news_hash(key)},
|
||
{"eventId", event.at("id")},
|
||
{"eventType", event.at("type")},
|
||
{"templateId", t->at("id")},
|
||
{"date", event.at("date")},
|
||
{"kind", t->at("kind")},
|
||
{"tone", t->at("tone")},
|
||
{"scope", scope},
|
||
{"dedupeKey", key},
|
||
{"fingerprint", news_hash(news_canonical(normalized))},
|
||
{"event", normalized},
|
||
{"language", language},
|
||
{"headline", translations.at(language).at("headline")},
|
||
{"body", translations.at(language).at("body")},
|
||
{"translations", translations},
|
||
{"demo", demo}}}};
|
||
} catch (...) {
|
||
return fail("invalid_news_event");
|
||
}
|
||
}
|
||
Json NewsCatalog::validate_article(const Json &raw) const {
|
||
try {
|
||
if (!raw.is_object() || raw.at("schema") != "bmh-news-article@1" ||
|
||
!raw.at("date").is_string() || !news_iso_date(raw.at("date")) ||
|
||
!raw.at("translations").is_object() ||
|
||
(raw.at("language") != "de" && raw.at("language") != "en") ||
|
||
!raw.at("demo").is_boolean())
|
||
return fail("invalid_news_article");
|
||
const auto scope = entity(raw.at("scope"), true);
|
||
const auto key = news_canonical(Json::array(
|
||
{scope.at("kind"), scope.at("id"), identity(raw.at("eventId"))}));
|
||
if (raw.at("id") != "evt-" + news_hash(key) || raw.at("dedupeKey") != key ||
|
||
raw.at("fingerprint") != news_hash(news_canonical(raw.at("event"))))
|
||
return fail("invalid_news_article_id");
|
||
for (const auto *lang : {"de", "en"}) {
|
||
(void)text(raw.at("translations").at(lang).at("headline"), 240);
|
||
(void)text(raw.at("translations").at(lang).at("body"), 2200);
|
||
}
|
||
const std::string language = raw.at("language");
|
||
if (raw.at("headline") !=
|
||
raw.at("translations").at(language).at("headline") ||
|
||
raw.at("body") != raw.at("translations").at(language).at("body"))
|
||
return fail("invalid_news_article");
|
||
if (raw.at("event").at("id") != raw.at("eventId") ||
|
||
raw.at("event").at("type") != raw.at("eventType") ||
|
||
raw.at("event").at("date") != raw.at("date") ||
|
||
news_canonical(raw.at("event").at("scope")) != news_canonical(scope) ||
|
||
raw.at("event").at("templateId") != raw.at("templateId"))
|
||
return fail("invalid_news_article_event");
|
||
if (!integer(raw.at("catalogueVersion"), 1, catalogue_.at("version")))
|
||
return fail("unsupported_news_catalogue");
|
||
if (raw.at("catalogueVersion") == catalogue_.at("version")) {
|
||
const auto result = from_event(raw.at("event"), {{"language", language}});
|
||
if (!result.value("ok", false) ||
|
||
result.at("article").at("fingerprint") != raw.at("fingerprint") ||
|
||
result.at("article").at("kind") != raw.at("kind") ||
|
||
result.at("article").at("tone") != raw.at("tone"))
|
||
return fail("invalid_news_article_event");
|
||
}
|
||
return {{"ok", true},
|
||
{"article", raw}}; // Archived wording is never regenerated.
|
||
} catch (...) {
|
||
return fail("invalid_news_article");
|
||
}
|
||
}
|
||
Json NewsCatalog::example(const std::string &id, const Json &options) const {
|
||
try {
|
||
const Json *t = nullptr;
|
||
for (const auto &v : catalogue_.at("templates"))
|
||
if (v.at("id") == id) {
|
||
t = &v;
|
||
break;
|
||
}
|
||
if (!t)
|
||
return fail("unknown_news_template", {"templateId"});
|
||
auto scope = options.value("scope", Json{{"kind", t->at("scopes")[0]},
|
||
{"id", "example-nordstadt"},
|
||
{"name", "FC Nordstadt"}});
|
||
const auto kind = scope.at("kind");
|
||
if (!options.contains("scope") && kind == "national")
|
||
scope = {{"kind", "national"},
|
||
{"id", "national-de"},
|
||
{"name", "Deutschland"},
|
||
{"names", {{"de", "Deutschland"}, {"en", "Germany"}}}};
|
||
if (!options.contains("scope") && kind == "global")
|
||
scope = {{"kind", "global"}, {"id", "world"}, {"name", "Fußball"}};
|
||
if (std::find(t->at("scopes").begin(), t->at("scopes").end(), kind) ==
|
||
t->at("scopes").end())
|
||
return fail("unsupported_news_scope", {"scope"});
|
||
Json data = Json::object();
|
||
for (const auto &[key, s] : t->at("parameters").items())
|
||
if (s.value("source", "") != "scope")
|
||
data[key] = catalogue_.at("examples").at(key);
|
||
for (const auto &[key, v] : t->at("fixture").items())
|
||
data[key] = v;
|
||
const auto day =
|
||
options.value("date", data.value("$date", std::string("2027-07-07")));
|
||
data.erase("$date");
|
||
if (!news_iso_date(day))
|
||
return fail("invalid_news_date", {"date"});
|
||
const auto shift = [&](int count) {
|
||
using namespace std::chrono;
|
||
auto n = sys_days(year_month_day{
|
||
year{std::stoi(day.substr(0, 4))},
|
||
month{unsigned(std::stoi(day.substr(5, 2)))},
|
||
std::chrono::day{unsigned(std::stoi(day.substr(8, 2)))}}) +
|
||
days{count};
|
||
year_month_day d{n};
|
||
std::ostringstream out;
|
||
out << std::setfill('0') << std::setw(4) << int(d.year()) << '-'
|
||
<< std::setw(2) << unsigned(d.month()) << '-' << std::setw(2)
|
||
<< unsigned(d.day());
|
||
return out.str();
|
||
};
|
||
const std::string event = t->at("event");
|
||
if (data.contains("on") && !t->at("fixture").contains("on"))
|
||
data["on"] = shift(7);
|
||
if (data.contains("until") && !t->at("fixture").contains("until"))
|
||
data["until"] = shift(event == "training.camp_start" ? 10
|
||
: event.starts_with("loan.") ? 180
|
||
: event == "advertising.ending" ? 30
|
||
: 730);
|
||
if (std::find(t->at("guards").begin(), t->at("guards").end(),
|
||
"yearMatchesDate") != t->at("guards").end())
|
||
data["year"] = std::stoi(day.substr(0, 4));
|
||
if (data.contains("season") && options.contains("date")) {
|
||
const int y = std::stoi(day.substr(0, 4)) -
|
||
(std::stoi(day.substr(5, 2)) < 7 ? 1 : 0);
|
||
data["season"] = normalize("season", y);
|
||
}
|
||
if (kind == "national") {
|
||
if (data.contains("competition"))
|
||
data["competition"] = {
|
||
{"id", "example-international"},
|
||
{"name", "Länderspiel"},
|
||
{"names", {{"de", "Länderspiel"}, {"en", "International Match"}}},
|
||
{"kind", "competition"}};
|
||
const Json a = {{"kind", "national"},
|
||
{"id", "national-at"},
|
||
{"name", "Österreich"},
|
||
{"names", {{"de", "Österreich"}, {"en", "Austria"}}}},
|
||
b = {{"kind", "national"},
|
||
{"id", "national-dk"},
|
||
{"name", "Dänemark"},
|
||
{"names", {{"de", "Dänemark"}, {"en", "Denmark"}}}};
|
||
if (data.contains("opponent"))
|
||
data["opponent"] = a;
|
||
if (data.contains("opponents"))
|
||
data["opponents"] = Json::array({a, b});
|
||
}
|
||
if (event.starts_with("cup."))
|
||
data["competition"] =
|
||
kind == "national"
|
||
? Json{{"id", "example-international-cup"},
|
||
{"name", "Länderpokal"},
|
||
{"names",
|
||
{{"de", "Länderpokal"}, {"en", "International Cup"}}},
|
||
{"kind", "cup"}}
|
||
: Json{{"id", "example-cup"},
|
||
{"name", "Landespokal"},
|
||
{"names", {{"de", "Landespokal"}, {"en", "Regional Cup"}}},
|
||
{"kind", "cup"}};
|
||
if (event.starts_with("indoor."))
|
||
data["competition"] = {
|
||
{"id", "example-indoor"},
|
||
{"name", "Hallenpokal"},
|
||
{"names", {{"de", "Hallenpokal"}, {"en", "Indoor Cup"}}},
|
||
{"kind", "cup"}};
|
||
Json filtered = Json::object();
|
||
for (const auto &[key, v] : data.items())
|
||
if (t->at("parameters").contains(key))
|
||
filtered[key] = v;
|
||
return {
|
||
{"ok", true},
|
||
{"event",
|
||
{{"id", options.value(
|
||
"id", "demo:" + id + ":" +
|
||
news_hash(kind.get<std::string>() + ":" +
|
||
scope.at("id").get<std::string>()))},
|
||
{"type", event},
|
||
{"variant", t->at("variant")},
|
||
{"date", day},
|
||
{"scope", scope},
|
||
{"data", filtered},
|
||
{"demo", true}}}};
|
||
} catch (...) {
|
||
return fail("invalid_news_example");
|
||
}
|
||
}
|
||
Json NewsCatalog::from_almanac(const Json &record, const Json &context) const {
|
||
try {
|
||
const auto scope = entity(context.at("scope"), true);
|
||
(void)identity(record.at("id"));
|
||
(void)entity(record.at("home"));
|
||
(void)entity(record.at("away"));
|
||
(void)entity(record.at("competition"));
|
||
const auto &result = record.at("result");
|
||
if (!result.at("final").is_array() || result.at("final").size() != 2)
|
||
return fail("invalid_almanac_news");
|
||
const std::string side =
|
||
record.at("home").at("id") == scope.at("id") ? "home"
|
||
: record.at("away").at("id") == scope.at("id") ? "away"
|
||
: "";
|
||
if (side.empty())
|
||
return fail("not_almanac_participant", {"scope.id"});
|
||
const auto &own = record.at(side);
|
||
const auto &other = record.at(side == "home" ? "away" : "home");
|
||
if (own.value("kind", std::string("club")) !=
|
||
scope.at("kind").get<std::string>())
|
||
return fail("unsupported_news_scope", {"scope.kind"});
|
||
const size_t i = side == "home" ? 0 : 1;
|
||
const auto pair = [&](const Json &v) {
|
||
return Json{{"for", v[i]}, {"against", v[1 - i]}};
|
||
};
|
||
const auto rid = identity(record.at("id"));
|
||
Json event = {{"id", "almanac:" + rid + ":result"},
|
||
{"demo", record.value("demo", false) ||
|
||
rid.starts_with("demo:almanac:")},
|
||
{"date", record.at("date")},
|
||
{"type", "match.result"},
|
||
{"scope",
|
||
{{"kind", scope.at("kind")},
|
||
{"id", own.at("id")},
|
||
{"name", own.at("name")}}},
|
||
{"data",
|
||
{{"opponent",
|
||
{{"id", other.at("id")},
|
||
{"name", other.at("name")},
|
||
{"kind", scope.at("kind")}}},
|
||
{"score", pair(result.at("final"))},
|
||
{"competition", record.at("competition")}}}};
|
||
if (result.value("penalties_played", false)) {
|
||
if (!result.at("penalties").is_array() ||
|
||
result.at("penalties").size() != 2)
|
||
return fail("invalid_almanac_news");
|
||
event["type"] = "cup.penalties";
|
||
event["data"]["shootout"] = pair(result.at("penalties"));
|
||
} else if (result.value("extra_played", false) &&
|
||
event.at("data").at("score").at("for") !=
|
||
event.at("data").at("score").at("against"))
|
||
event["type"] = "cup.extra_time";
|
||
return from_event(event, context);
|
||
} catch (...) {
|
||
return fail("invalid_almanac_news");
|
||
}
|
||
}
|
||
} // namespace bmh::modern_ui
|