+
+void EventsModel::set_team_at(uint64_t t, const set<int> &new_team)
+{
+ // Backdate to the last goal or stoppage, _or_ the last time someone
+ // going out is mentioned. (We don't really track injuries yet;
+ // do we want an explicit injury type? If we had one, it would probably
+ // be the simplest.)
+ uint64_t backdate_point = 0;
+ for (const Event &e : events) {
+ if (e.t > t) {
+ break;
+ }
+ if (e.type == "goal" || e.type == "their_goal" || e.type == "stoppage" || e.type == "reset" || e.type == "set_offense" || e.type == "set_defense") {
+ backdate_point = e.t + 1;
+ }
+ if (e.player_id.has_value() && !new_team.count(*e.player_id)) {
+ backdate_point = e.t + 1;
+ }
+ }
+
+ // Delete all in/outs already at the backdate point.
+ for (unsigned i = 0; i < events.size(); ) {
+ if (events[i].t > backdate_point) {
+ break;
+ }
+ if (events[i].t == backdate_point && (events[i].type == "in" || events[i].type == "out")) {
+ delete_event(i);
+ } else {
+ ++i;
+ }
+ }
+
+ // Finally make the subs we need.
+ set<int> old_team = get_team_at(backdate_point);
+ for (int player_id : old_team) {
+ if (!new_team.count(player_id)) {
+ insert_event(backdate_point, player_id, nullopt, "out");
+ }
+ }
+ for (int player_id : new_team) {
+ if (!old_team.count(player_id)) {
+ insert_event(backdate_point, player_id, nullopt, "in");
+ }
+ }
+}
+
+void EventsModel::set_formation_at(uint64_t t, bool offense, unsigned formation)
+{
+ // If there's another goal/stoppage/turnover no more than 20 seconds ago,
+ // we assume that the formation started at that point (it just took
+ // the operator a bit of time to see it). If not, we assume we
+ // changed in the middle of a point.
+ uint64_t backdate_point = 0;
+ for (const Event &e : events) {
+ if (e.t > t) {
+ break;
+ }
+ if (e.type == "goal" || e.type == "their_goal" ||
+ e.type == "in" || e.type == "out" ||
+ e.type == "stoppage" || e.type == "reset" ||
+ e.type == "set_defense" || e.type == "set_offense" ||
+ e.type == "throwaway" || e.type == "their_throwaway" ||
+ e.type == "drop" || e.type == "defense" || e.type == "interception" ||
+ e.type == "pull" || e.type == "pull_landed" || e.type == "pull_oob" || e.type == "their_pull" ||
+ e.type == "formation_offense" || e.type == "formation_defense") {
+ backdate_point = e.t + 1;
+ }
+ if (e.type == "formation_offense" || e.type == "formation_defense") {
+ backdate_point = 0;
+ }
+ }
+ if (backdate_point != 0 && t - backdate_point < 20000) {
+ t = backdate_point;
+ }
+ if (offense) {
+ insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_offense");
+ } else {
+ insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_defense");
+ }
+}
+
+vector<int> EventsModel::sort_team(const set<int> &team) const
+{
+ vector<int> ret(team.begin(), team.end());
+ std::sort(ret.begin(), ret.end(), [this](int a, int b) {
+ return player_ordering.find(a)->second < player_ordering.find(b)->second;
+ });
+ return ret;
+}