summaryrefslogtreecommitdiff
path: root/frontend/src/actions/simulation/tick.js
blob: a629b3403ce0220b1bb7f5edacc2b7da29229d2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { getDatacenterIdOfTick } from "../../util/timeline";
import { setCurrentDatacenter } from "../topology/building";

export const GO_TO_TICK = "GO_TO_TICK";
export const SET_LAST_SIMULATED_TICK = "SET_LAST_SIMULATED_TICK";

export function incrementTick() {
  return (dispatch, getState) => {
    const { currentTick } = getState();
    dispatch(goToTick(currentTick + 1));
  };
}

export function goToTick(tick) {
  return (dispatch, getState) => {
    const state = getState();

    let sections = [];
    if (state.currentExperimentId !== -1) {
      const sectionIds =
        state.objects.path[
          state.objects.experiment[state.currentExperimentId].pathId
        ].sectionIds;

      if (sectionIds) {
        sections = sectionIds.map(
          sectionId => state.objects.section[sectionId]
        );
      }
    }

    const newDatacenterId = getDatacenterIdOfTick(tick, sections);
    if (state.currentDatacenterId !== newDatacenterId) {
      dispatch(setCurrentDatacenter(newDatacenterId));
    }

    dispatch({
      type: GO_TO_TICK,
      tick
    });
  };
}

export function setLastSimulatedTick(tick) {
  return {
    type: SET_LAST_SIMULATED_TICK,
    tick
  };
}