+ // Formation management
+ if (type === 'formation_offense' || type === 'formation_defense') {
+ let id = e.formation === null ? 0 : e.formation;
+ let for_offense = (type === 'formation_offense');
+ if (offense === for_offense) {
+ formations_used_this_point.add(id);
+ } else if (for_offense) {
+ pending_offensive_formation = id;
+ } else {
+ pending_defensive_formation = id;
+ }
+ if (for_offense) {
+ last_offensive_formation = id;
+ } else {
+ last_defensive_formation = id;
+ }
+ } else if (last_offense !== offense) {
+ if (offense === true && pending_offensive_formation !== null) {
+ formations_used_this_point.add(pending_offensive_formation);
+ pending_offensive_formation = null;
+ } else if (offense === false && pending_defensive_formation !== null) {
+ formations_used_this_point.add(pending_defensive_formation);
+ pending_defensive_formation = null;
+ } else if (offense === true && last_defensive_formation !== null) {
+ if (should_reuse_last_formation(match['events'], t)) {
+ formations_used_this_point.add(last_defensive_formation);
+ }
+ } else if (offense === false && last_offensive_formation !== null) {
+ if (should_reuse_last_formation(match['events'], t)) {
+ formations_used_this_point.add(last_offensive_formation);
+ }
+ }
+ }
+