Add a read-only native live-ticker demo with finite fixtures (by Codex)
This commit is contained in:
1 parent
0a54b8338e
commit
8e3d9f83e0
17 files changed
+1601
-10
No files matched your search
@@ -0,0 +1,369 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_live.hpp"
|
||||
#include "modern/ui/demo_career.hpp"
|
||||
#include "modern/ui/demo_lineup.hpp"
|
||||
#include "modern/ui/demo_sports.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
namespace bmh::modern_ui {
|
||||
namespace {
|
||||
Json failure(const char *error) { return {{"ok", false}, {"error", error}}; }
|
||||
std::string fingerprint(const std::string &text) {
|
||||
// Stable recording identity, independent of std::hash or active right pane.
|
||||
uint64_t hash = 14695981039346656037ULL;
|
||||
for (unsigned char c : text) { hash ^= c; hash *= 1099511628211ULL; }
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
std::string value(16, '0');
|
||||
for (int i = 15; i >= 0; --i) { value[size_t(i)] = hex[hash & 15]; hash >>= 4; }
|
||||
return value;
|
||||
}
|
||||
Json team(const Catalog &world, const std::string &id) {
|
||||
const auto *row = world.find("clubs", id);
|
||||
return {{"id", id}, {"name", row ? world.name(*row) : "Demo-Gegner"},
|
||||
{"role", "club"}, {"catalogue", row != nullptr}};
|
||||
}
|
||||
Json owner_view(const Catalog &world, const Json &state, const Json &manager,
|
||||
bool national, bool indoor) {
|
||||
const auto club = manager.value("club", "");
|
||||
Json view = state;
|
||||
const auto active_club = demo_career_manager(state).value("club", "");
|
||||
if (club != active_club) {
|
||||
const auto views = state.value("career_views", Json::object());
|
||||
if (views.contains(club)) restore_demo_career_view(view, views.at(club));
|
||||
else {
|
||||
view["lineup"] = demo_lineup(world, club);
|
||||
view["tactics"] = demo_tactics();
|
||||
view.erase("indoor_lineup");
|
||||
}
|
||||
}
|
||||
view["career"]["active_portrait"] = manager.at("portrait");
|
||||
view["career"]["surface"] = indoor ? "indoor" : "outdoor";
|
||||
if (!migrate_demo_lineup(view, world, club))
|
||||
return {{"ok", false}, {"error", "live_owner_lineup_unavailable"}};
|
||||
auto projection = demo_lineup_rows(view, world, club);
|
||||
if (!projection.value("ok", false)) return projection;
|
||||
auto rows = projection.at("rows");
|
||||
// These are catalogue attributes, never fixture-generated fitness/strength.
|
||||
for (auto &row : rows) {
|
||||
row["caps"] = nullptr;
|
||||
row["international_goals"] = nullptr;
|
||||
if (const auto *player = world.find("players", row.at("id"))) {
|
||||
const auto attributes = player->value("attributes", Json::object());
|
||||
for (const auto *key : {"caps", "international_goals"})
|
||||
if (attributes.contains(key) && attributes.at(key).is_number_integer() &&
|
||||
attributes.at(key) >= 0) row[key] = attributes.at(key);
|
||||
}
|
||||
}
|
||||
Json edit_state = {{"lineup", view.at("lineup")},
|
||||
{"tactics", view.value("tactics", demo_tactics())},
|
||||
{"career", {{"surface", indoor ? "indoor" : "outdoor"}}},
|
||||
{"date", state.at("date")}};
|
||||
if (view.contains("indoor_lineup")) edit_state["indoor_lineup"] = view.at("indoor_lineup");
|
||||
return {{"ok", true}, {"rows", rows}, {"board", demo_lineup_visible(view)},
|
||||
{"tactics", view.value("tactics", demo_tactics())},
|
||||
{"edit_state", edit_state},
|
||||
{"club_basis", club}, {"national_demo_basis", national},
|
||||
{"field_count", indoor ? 6 : 11}, {"on_field_limit", indoor ? 6 : 11},
|
||||
{"rotation_rows", indoor ? Json(std::min(size_t(11), rows.size())) : Json()},
|
||||
{"roster_size", rows.size()}, {"roster_limit", nullptr}};
|
||||
}
|
||||
// Every outcome, statistic and minute below is authored fixture data. There is
|
||||
// no event generation, chance/goal rule, random draw or match clock simulation.
|
||||
struct Moment {
|
||||
int outdoor, indoor;
|
||||
const char *phase, *kind, *headline, *body;
|
||||
std::array<int, 2> score, chances, yellow, red;
|
||||
std::array<int, 2> points, goals_for, goals_against;
|
||||
};
|
||||
constexpr std::array<Moment, 9> recording{{
|
||||
{0, 0, "first_half", "kickoff", "Anpfiff!", "Der Ball rollt. Beide Mannschaften beginnen mit ihrer Aufstellung.", {0,0}, {0,0}, {0,0}, {0,0}, {22,19}, {20,18}, {10,12}},
|
||||
{12, 6, "first_half", "goal", "Die Heimelf geht in Führung", "Die Gastgeber nutzen ihre Gelegenheit und gehen mit 1:0 in Führung.", {1,0}, {2,1}, {0,0}, {0,0}, {24,18}, {21,18}, {10,13}},
|
||||
{23, 11, "first_half", "yellow", "Gelbe Karte für die Gäste", "Die Gäste werden verwarnt. Es bleibt beim knappen Rückstand.", {1,0}, {3,2}, {0,1}, {0,0}, {24,18}, {21,18}, {10,13}},
|
||||
{45, 20, "interval", "interval", "Halbzeit", "Mit einer knappen Führung der Gastgeber geht es in die Pause.", {1,0}, {4,3}, {0,1}, {0,0}, {24,18}, {21,18}, {10,13}},
|
||||
{46, 21, "second_half", "restart", "Die zweite Hälfte beginnt", "Die Mannschaften sind zurück. Die Heimelf beginnt mit einem Tor Vorsprung.", {1,0}, {4,3}, {0,1}, {0,0}, {24,18}, {21,18}, {10,13}},
|
||||
{62, 28, "second_half", "goal", "Ausgleich für die Gäste", "Die Gäste treffen zum 1:1. Damit ist die Begegnung wieder offen.", {1,1}, {5,4}, {0,1}, {0,0}, {22,19}, {21,19}, {11,13}},
|
||||
{70, 32, "second_half", "chance", "Eine weitere Torchance", "Die Heimelf kommt zum Abschluss. Der Spielstand bleibt unverändert.", {1,1}, {6,5}, {0,1}, {0,0}, {22,19}, {21,19}, {11,13}},
|
||||
{78, 36, "second_half", "goal", "Die Heimelf trifft erneut", "Die Gastgeber erzielen das 2:1 und liegen wieder vorn.", {2,1}, {7,5}, {0,1}, {0,0}, {24,18}, {22,19}, {11,14}},
|
||||
{90, 40, "finished", "fulltime", "Abpfiff", "Die Partie endet mit einem 2:1 für die Heimmannschaft.", {2,1}, {8,6}, {0,1}, {0,0}, {24,18}, {22,19}, {11,14}}
|
||||
}};
|
||||
Json standings(const Catalog &world, const Json &match, const Moment &moment) {
|
||||
// One declared snapshot per catalogue competition, independent of the owner
|
||||
// or watched match. Both passes are authored table ordering, not a computed
|
||||
// league ranking; all pairs in this recording share the same fixed outcomes.
|
||||
Json rows = Json::array();
|
||||
const auto *home = world.find("clubs", match.at("home").at("id"));
|
||||
if (!home) return rows;
|
||||
const auto pool = world.clubs(home->value("competition", ""));
|
||||
for (size_t side = 0; side < 2; ++side)
|
||||
for (size_t slot = side; slot < std::min(size_t(18), pool.size()); slot += 2)
|
||||
rows.push_back({{"position", rows.size() + 1}, {"team", team(world, pool[slot]->at("id"))},
|
||||
{"played", 8}, {"gf", moment.goals_for[side]},
|
||||
{"ga", moment.goals_against[side]}, {"points", moment.points[side]}});
|
||||
return rows;
|
||||
}
|
||||
bool uint_value(const Json &v, uint64_t maximum) {
|
||||
return (v.is_number_unsigned() && v.get<uint64_t>() <= maximum) ||
|
||||
(v.is_number_integer() && v.get<int64_t>() >= 0 &&
|
||||
uint64_t(v.get<int64_t>()) <= maximum);
|
||||
}
|
||||
bool string_value(const Json &v, size_t maximum = 240) {
|
||||
if (!v.is_string()) return false;
|
||||
const auto &value = v.get_ref<const std::string &>();
|
||||
return !value.empty() && value.size() <= maximum &&
|
||||
std::none_of(value.begin(), value.end(), [](unsigned char c) { return c < 32 || c == 127; });
|
||||
}
|
||||
bool team_value(const Json &v, const Json &role) {
|
||||
return v.is_object() && string_value(v.at("id")) && string_value(v.at("name")) && v.at("role") == role;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Json make_demo_live(const Catalog &world, const Json &state, uint64_t revision) {
|
||||
try {
|
||||
if (!state.is_object() || state.value("schema", "") != "openbmh-ui-demo@1")
|
||||
return failure("live_demo_state_required");
|
||||
const auto context = demo_career_context(state, world);
|
||||
if (context.value("ended", false)) return failure("live_career_ended");
|
||||
const auto role = context.value("role", ""), surface = context.value("surface", "");
|
||||
if ((role != "club" && role != "national") ||
|
||||
(surface != "outdoor" && surface != "indoor") ||
|
||||
(role == "national" && surface != "outdoor")) return failure("invalid_live_scope");
|
||||
const bool national = role == "national", indoor = surface == "indoor";
|
||||
const auto humans = context.at("active_managers");
|
||||
if (!humans.is_array() || humans.empty() || humans.size() > 4)
|
||||
return failure("invalid_live_owners");
|
||||
Json owners = Json::array(), matches = Json::array();
|
||||
std::set<int> portraits;
|
||||
for (const auto &manager : humans) {
|
||||
if (national && manager.at("portrait") != context.at("current_manager").at("portrait")) continue;
|
||||
const int portrait = manager.at("portrait");
|
||||
if (portrait < 0 || portrait > 11 || !portraits.insert(portrait).second ||
|
||||
!world.manageable(manager.value("club", ""))) return failure("invalid_live_owners");
|
||||
const auto managed = national ? context.at("current_team") : team(world, manager.at("club"));
|
||||
const auto view = owner_view(world, state, manager, national, indoor);
|
||||
if (!view.value("ok", false)) return failure("live_owner_lineup_unavailable");
|
||||
owners.push_back({{"id", "manager:" + std::to_string(portrait)}, {"portrait", portrait},
|
||||
{"name", manager.at("name")}, {"team", managed}, {"view", view}});
|
||||
}
|
||||
for (auto &owner : owners) {
|
||||
const std::string id = owner.at("team").at("id");
|
||||
auto existing = std::find_if(matches.begin(), matches.end(), [&](const auto &match) {
|
||||
return match.at("home").at("id") == id || match.at("away").at("id") == id;
|
||||
});
|
||||
if (existing != matches.end()) { owner["match_id"] = existing->at("id"); existing->at("owners").push_back(owner.at("id")); continue; }
|
||||
Json home = owner.at("team"), away;
|
||||
if (national) {
|
||||
away = {{"id", "fixture:national-opponent"}, {"name", "Demo-Nationalteam"}, {"role", "national"}, {"catalogue", false}};
|
||||
for (const auto &nation : world.rows("nations")) if (nation.value("id", "") != "nation:de") {
|
||||
away = {{"id", "national:" + nation.at("id").get<std::string>()}, {"name", world.name(nation)}, {"role", "national"}, {"nation", nation.at("id")}, {"catalogue", true}}; break;
|
||||
}
|
||||
} else {
|
||||
const auto *club = world.find("clubs", id);
|
||||
const auto pool = world.clubs(club ? club->value("competition", "") : "");
|
||||
auto at = std::find_if(pool.begin(), pool.end(), [&](const auto *c) { return c->value("id", "") == id; });
|
||||
if (at == pool.end()) return failure("live_team_unavailable");
|
||||
const size_t index = size_t(at - pool.begin()), partner = index % 2 ? index - 1 : index + 1;
|
||||
if (partner < pool.size()) {
|
||||
away = team(world, pool[partner]->at("id"));
|
||||
if (index % 2) std::swap(home, away);
|
||||
} else away = team(world, "fixture:opponent:" + id);
|
||||
}
|
||||
const auto match_id = "ui-match:" + fingerprint(Json::array({world.manifest().value("id", ""), state.at("date"), role, surface, home.at("id"), away.at("id")}).dump());
|
||||
owner["match_id"] = match_id;
|
||||
matches.push_back({{"id", match_id}, {"home", home}, {"away", away},
|
||||
{"owners", Json::array({owner.at("id")})},
|
||||
{"competition", national ? "Länderspiel · Demo" : indoor ? "Hallenpartie · Demo" : "Spieltag · Demo"}});
|
||||
}
|
||||
Json frames = Json::array(), events = Json::array();
|
||||
for (size_t sequence = 0; sequence < recording.size(); ++sequence) {
|
||||
const auto &moment = recording[sequence];
|
||||
Json scores = Json::array();
|
||||
for (const auto &match : matches) {
|
||||
const auto event_id = match.at("id").get<std::string>() + ":event:" + std::to_string(sequence);
|
||||
events.push_back({{"id", event_id}, {"sequence", sequence}, {"match_id", match.at("id")},
|
||||
{"minute", indoor ? moment.indoor : moment.outdoor}, {"kind", moment.kind},
|
||||
{"headline", moment.headline}, {"body", moment.body}, {"score", moment.score},
|
||||
{"side", std::string(moment.kind) == "yellow" || sequence == 5 ? "away" : "home"}});
|
||||
scores.push_back({{"match_id", match.at("id")}, {"score", moment.score},
|
||||
{"chances", moment.chances}, {"yellow", moment.yellow}, {"red", moment.red},
|
||||
{"players", indoor ? Json::array({6,6}) : Json::array({11,11})},
|
||||
{"standings", national ? Json::array() : standings(world, match, moment)}});
|
||||
}
|
||||
frames.push_back({{"sequence", sequence}, {"minute", indoor ? moment.indoor : moment.outdoor},
|
||||
{"phase", moment.phase}, {"matches", scores}, {"events", events}});
|
||||
}
|
||||
const auto identity = Json{{"world", world.manifest().value("id", "")}, {"date", state.at("date")},
|
||||
{"role", role}, {"surface", surface}, {"matches", matches}};
|
||||
return {{"ok", true}, {"schema", "openbmh-live-fixture@1"}, {"source", "ui-demo-fixture"},
|
||||
{"fixture_id", "ui-live:" + fingerprint(identity.dump())}, {"source_revision", revision},
|
||||
{"world", world.manifest().value("id", "")}, {"date", state.at("date")},
|
||||
{"role", role}, {"surface", surface}, {"duration", indoor ? 40 : 90},
|
||||
{"field_count", indoor ? 6 : 11}, {"owners", owners}, {"matches", matches},
|
||||
{"frames", frames}, {"notice", "UI-Demo · vorgegebene Ergebnisse, keine Spielsimulation"}};
|
||||
} catch (const std::exception &) { return failure("invalid_live_demo"); }
|
||||
}
|
||||
|
||||
bool valid_live_fixture(const Json &v) {
|
||||
try {
|
||||
if (!v.is_object() || !v.value("ok", false) || v.at("schema") != "openbmh-live-fixture@1" ||
|
||||
v.at("source") != "ui-demo-fixture" || !v.at("fixture_id").is_string() ||
|
||||
!uint_value(v.at("source_revision"), UINT64_MAX) || !v.at("owners").is_array() ||
|
||||
v.at("owners").empty() || v.at("owners").size() > 4 || !v.at("matches").is_array() ||
|
||||
v.at("matches").empty() || v.at("matches").size() > 4 || !v.at("frames").is_array() ||
|
||||
v.at("frames").size() < 2 || v.at("frames").size() > 2000) return false;
|
||||
const bool indoor = v.at("surface") == "indoor", national = v.at("role") == "national";
|
||||
if ((!national && v.at("role") != "club") || (!indoor && v.at("surface") != "outdoor") ||
|
||||
(national && (indoor || v.at("owners").size() != 1)) ||
|
||||
v.at("duration") != (indoor ? 40 : 90) || v.at("field_count") != (indoor ? 6 : 11)) return false;
|
||||
std::set<std::string> owner_ids, match_ids, teams, assigned_owners;
|
||||
std::set<int> portraits;
|
||||
for (const auto &owner : v.at("owners")) {
|
||||
if (!string_value(owner.at("id")) || !owner_ids.insert(owner.at("id")).second ||
|
||||
!uint_value(owner.at("portrait"), 11) || !portraits.insert(owner.at("portrait").get<int>()).second ||
|
||||
!string_value(owner.at("name")) || !team_value(owner.at("team"), v.at("role"))) return false;
|
||||
const auto &view = owner.at("view");
|
||||
if (!view.value("ok", false) || !view.at("rows").is_array() || view.at("rows").size() > 2000 ||
|
||||
view.at("field_count") != v.at("field_count") || view.at("on_field_limit") != v.at("field_count") ||
|
||||
view.at("roster_size") != view.at("rows").size() || !view.at("roster_limit").is_null() ||
|
||||
!view.at("board").at("order").is_array() || !view.at("board").at("points").is_array() ||
|
||||
view.at("board").at("points").size() != 11 || !view.at("edit_state").is_object() ||
|
||||
!view.at("edit_state").at("lineup").is_object() || !valid_demo_tactics(view.at("tactics")) ||
|
||||
view.at("national_demo_basis") != national) return false;
|
||||
if ((indoor && view.at("rotation_rows") != std::min(size_t(11), view.at("rows").size())) ||
|
||||
(!indoor && !view.at("rotation_rows").is_null())) return false;
|
||||
std::set<std::string> players;
|
||||
size_t slot = 0;
|
||||
for (const auto &row : view.at("rows")) {
|
||||
if (!string_value(row.at("id")) || !players.insert(row.at("id")).second || !string_value(row.at("name")) ||
|
||||
row.at("slot") != slot++ || !row.contains("strength") || !row.contains("condition")) return false;
|
||||
for (auto field : {"caps", "international_goals"})
|
||||
if (!row.at(field).is_null() && !uint_value(row.at(field), 1000000)) return false;
|
||||
}
|
||||
}
|
||||
for (const auto &match : v.at("matches")) {
|
||||
if (!string_value(match.at("id")) || !match_ids.insert(match.at("id")).second ||
|
||||
!match.at("owners").is_array() || match.at("owners").empty()) return false;
|
||||
for (auto side : {"home", "away"})
|
||||
if (!team_value(match.at(side), v.at("role")) ||
|
||||
!teams.insert(match.at(side).at("id")).second) return false;
|
||||
std::set<std::string> assigned;
|
||||
for (const auto &owner_id : match.at("owners")) {
|
||||
if (!owner_id.is_string() || !owner_ids.contains(owner_id) || !assigned.insert(owner_id).second || !assigned_owners.insert(owner_id).second) return false;
|
||||
const auto owner = std::find_if(v.at("owners").begin(), v.at("owners").end(), [&](const auto &o) { return o.at("id") == owner_id; });
|
||||
if (owner->at("match_id") != match.at("id") ||
|
||||
(owner->at("team").at("id") != match.at("home").at("id") && owner->at("team").at("id") != match.at("away").at("id"))) return false;
|
||||
}
|
||||
}
|
||||
if (assigned_owners != owner_ids) return false;
|
||||
for (const auto &owner : v.at("owners")) if (!match_ids.contains(owner.at("match_id"))) return false;
|
||||
int minute = -1; size_t sequence = 0;
|
||||
std::map<std::string, Json> history;
|
||||
for (const auto &frame : v.at("frames")) {
|
||||
if (!uint_value(frame.at("sequence"), v.at("frames").size() - 1) || frame.at("sequence") != sequence++ || !uint_value(frame.at("minute"), indoor ? 40 : 90) ||
|
||||
frame.at("minute").get<int>() < minute || !frame.at("events").is_array() ||
|
||||
frame.at("events").size() > 8000 || !frame.at("matches").is_array() ||
|
||||
frame.at("matches").size() != match_ids.size()) return false;
|
||||
const auto phase = frame.value("phase", "");
|
||||
if (phase != "first_half" && phase != "interval" && phase != "second_half" && phase != "finished") return false;
|
||||
if (phase == "finished" && sequence != v.at("frames").size()) return false;
|
||||
minute = frame.at("minute"); std::set<std::string> scored, event_ids;
|
||||
for (const auto &m : frame.at("matches")) {
|
||||
if (!match_ids.contains(m.at("match_id")) || !scored.insert(m.at("match_id")).second) return false;
|
||||
for (auto key : {"score", "chances", "yellow", "red", "players"})
|
||||
if (!m.at(key).is_array() || m.at(key).size() != 2 || !uint_value(m.at(key)[0], 1000) || !uint_value(m.at(key)[1], 1000)) return false;
|
||||
if (!m.at("standings").is_array() || m.at("standings").size() > 128 || (national && !m.at("standings").empty())) return false;
|
||||
for (const auto &row : m.at("standings")) {
|
||||
if (!team_value(row.at("team"), v.at("role"))) return false;
|
||||
for (auto key : {"position", "played", "gf", "ga", "points"}) if (!uint_value(row.at(key), 1000000)) return false;
|
||||
}
|
||||
}
|
||||
const auto previous_history = history;
|
||||
for (const auto &event : frame.at("events")) {
|
||||
if (!string_value(event.at("id")) || !event_ids.insert(event.at("id")).second ||
|
||||
!match_ids.contains(event.at("match_id")) || !uint_value(event.at("sequence"), sequence - 1) ||
|
||||
!uint_value(event.at("minute"), uint64_t(minute)) || !string_value(event.at("headline")) ||
|
||||
!string_value(event.at("body"), 2200) || !string_value(event.at("kind"), 40) ||
|
||||
(event.at("side") != "home" && event.at("side") != "away") || !event.at("score").is_array() ||
|
||||
event.at("score").size() != 2 || !uint_value(event.at("score")[0], 1000) || !uint_value(event.at("score")[1], 1000)) return false;
|
||||
const std::string id = event.at("id");
|
||||
if (history.contains(id) && history.at(id) != event) return false;
|
||||
history[id] = event;
|
||||
}
|
||||
for (const auto &[id, event] : previous_history) { (void)event; if (!event_ids.contains(id)) return false; }
|
||||
}
|
||||
return v.at("frames").front().at("minute") == 0 && minute == (indoor ? 40 : 90) &&
|
||||
v.at("frames").back().at("phase") == "finished";
|
||||
} catch (const std::exception &) { return false; }
|
||||
}
|
||||
Json live_fixture_frame(const Json &v, size_t sequence) {
|
||||
if (!valid_live_fixture(v)) return failure("invalid_live_fixture");
|
||||
if (sequence >= v.at("frames").size()) return failure("invalid_live_sequence");
|
||||
auto frame = v.at("frames")[sequence];
|
||||
frame["ok"] = true; frame["source"] = v.at("source"); frame["fixture_id"] = v.at("fixture_id");
|
||||
frame["source_revision"] = v.at("source_revision"); return frame;
|
||||
}
|
||||
Json query_demo_live(const Catalog &world, const Json &state, uint64_t revision, const Json &q) {
|
||||
try {
|
||||
if (!q.is_object()) return failure("invalid_live_query");
|
||||
for (const auto &[key, value] : q.items()) { (void)value;
|
||||
if (key != "kind" && key != "revision" && key != "sequence" && key != "fixture_id") return failure("invalid_live_query");
|
||||
}
|
||||
if (q.contains("revision") && (!uint_value(q.at("revision"), UINT64_MAX) || q.at("revision") != revision)) return failure("stale_live_revision");
|
||||
const auto kind = q.value("kind", "fixture");
|
||||
if (kind != "fixture" && kind != "frame") return failure("invalid_live_query");
|
||||
auto fixture = make_demo_live(world, state, revision);
|
||||
if (!fixture.value("ok", false)) return fixture;
|
||||
if (q.contains("fixture_id") && q.at("fixture_id") != fixture.at("fixture_id")) return failure("stale_live_fixture");
|
||||
if (kind == "fixture") return q.contains("sequence") ? failure("invalid_live_query") : fixture;
|
||||
if (!q.contains("sequence") || !uint_value(q.at("sequence"), fixture.at("frames").size() - 1)) return failure("invalid_live_sequence");
|
||||
return live_fixture_frame(fixture, q.at("sequence").get<size_t>());
|
||||
} catch (const std::exception &) { return failure("invalid_live_query"); }
|
||||
}
|
||||
|
||||
bool LivePlayback::open(const Json &fixture, const std::string &owner_id) {
|
||||
if (!valid_live_fixture(fixture)) return false;
|
||||
LivePlayback next; next.fixture_ = fixture;
|
||||
next.owner_ = fixture.at("owners").front().at("id");
|
||||
if (!owner_id.empty() && !next.owner(owner_id)) return false;
|
||||
*this = std::move(next); return true;
|
||||
}
|
||||
void LivePlayback::close() { fixture_ = nullptr; sequence_ = 0; speed_ = 0; elapsed_ = 0; owner_.clear(); }
|
||||
bool LivePlayback::finished() const { return available() && sequence_ + 1 == fixture_.at("frames").size(); }
|
||||
bool LivePlayback::next() {
|
||||
if (!available() || finished()) return false;
|
||||
++sequence_; elapsed_ = 0; if (finished()) speed_ = 0; return true;
|
||||
}
|
||||
bool LivePlayback::speed(int value) {
|
||||
if (!available() || value < 0 || value > 4 || (finished() && value)) return false;
|
||||
speed_ = value; elapsed_ = 0; return true;
|
||||
}
|
||||
bool LivePlayback::elapse(uint64_t milliseconds) {
|
||||
if (!available() || !speed_ || finished()) return false;
|
||||
constexpr std::array<uint64_t, 5> intervals{0, 2400, 1400, 800, 400};
|
||||
const auto interval = intervals[size_t(speed_)];
|
||||
// A delayed/hidden window never skips an arbitrary backlog of recorded scenes.
|
||||
elapsed_ = std::min(interval, elapsed_ + std::min(milliseconds, interval));
|
||||
return elapsed_ >= interval && next();
|
||||
}
|
||||
bool LivePlayback::owner(const std::string &id) {
|
||||
if (!available()) return false;
|
||||
for (const auto &o : fixture_.at("owners")) if (o.at("id") == id) { owner_ = id; return true; }
|
||||
return false;
|
||||
}
|
||||
bool LivePlayback::cycle_owner(int direction) {
|
||||
if (!available() || (direction != -1 && direction != 1)) return false;
|
||||
const auto &owners = fixture_.at("owners");
|
||||
for (size_t i = 0; i < owners.size(); ++i) if (owners[i].at("id") == owner_)
|
||||
return owner(owners[(i + owners.size() + direction) % owners.size()].at("id"));
|
||||
return false;
|
||||
}
|
||||
Json LivePlayback::frame() const { return available() ? live_fixture_frame(fixture_, sequence_) : failure("live_unavailable"); }
|
||||
Json LivePlayback::status() const {
|
||||
return {{"source", available() ? Json("ui-demo-fixture") : Json()}, {"available", available()},
|
||||
{"fixture_id", available() ? fixture_.at("fixture_id") : Json()},
|
||||
{"source_revision", available() ? fixture_.at("source_revision") : Json()},
|
||||
{"sequence", sequence_}, {"speed", speed_}, {"owner", owner_}, {"finished", finished()}};
|
||||
}
|
||||
} // namespace bmh::modern_ui
|
||||
Reference in new issue
Block a user