From 3f736cd3db63f106eac02f220477b4a0f3b0eceb Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Thu, 31 Aug 2017 17:59:51 +0200 Subject: Implement room creation --- src/actions/topology.js | 112 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'src/actions/topology.js') diff --git a/src/actions/topology.js b/src/actions/topology.js index de742bb1..76b1ef27 100644 --- a/src/actions/topology.js +++ b/src/actions/topology.js @@ -1,7 +1,18 @@ +import {addIdToStoreObjectListProp, removeIdFromStoreObjectListProp} from "./objects"; + export const FETCH_TOPOLOGY_OF_DATACENTER = "FETCH_TOPOLOGY_OF_DATACENTER"; export const FETCH_TOPOLOGY_OF_DATACENTER_SUCCEEDED = "FETCH_TOPOLOGY_OF_DATACENTER_SUCCEEDED"; export const FETCH_LATEST_DATACENTER = "FETCH_LATEST_DATACENTER"; export const FETCH_LATEST_DATACENTER_SUCCEEDED = "FETCH_LATEST_DATACENTER_SUCCEEDED"; +export const START_NEW_ROOM_CONSTRUCTION = "START_NEW_ROOM_CONSTRUCTION"; +export const START_NEW_ROOM_CONSTRUCTION_SUCCEEDED = "START_NEW_ROOM_CONSTRUCTION_SUCCEEDED"; +export const FINISH_NEW_ROOM_CONSTRUCTION = "FINISH_NEW_ROOM_CONSTRUCTION"; +export const CANCEL_NEW_ROOM_CONSTRUCTION = "CANCEL_NEW_ROOM_CONSTRUCTION"; +export const CANCEL_NEW_ROOM_CONSTRUCTION_SUCCEEDED = "CANCEL_NEW_ROOM_CONSTRUCTION_SUCCEEDED"; +export const ADD_TILE = "ADD_TILE"; +export const ADD_TILE_SUCCEEDED = "ADD_TILE_SUCCEEDED"; +export const DELETE_TILE = "DELETE_TILE"; +export const DELETE_TILE_SUCCEEDED = "DELETE_TILE_SUCCEEDED"; export function fetchLatestDatacenter() { return (dispatch, getState) => { @@ -19,3 +30,104 @@ export function fetchLatestDatacenterSucceeded(datacenterId) { datacenterId }; } + +export function startNewRoomConstruction() { + return { + type: START_NEW_ROOM_CONSTRUCTION + }; +} + +export function startNewRoomConstructionSucceeded(roomId) { + return (dispatch, getState) => { + const {currentDatacenterId} = getState(); + dispatch(addIdToStoreObjectListProp("datacenter", currentDatacenterId, "roomIds", roomId)); + dispatch({ + type: START_NEW_ROOM_CONSTRUCTION_SUCCEEDED, + roomId + }); + }; +} + +export function finishNewRoomConstruction() { + return (dispatch, getState) => { + const {objects, currentRoomInConstruction} = getState(); + if (objects.room[currentRoomInConstruction].tileIds.length === 0) { + dispatch(cancelNewRoomConstruction()); + return; + } + + dispatch({ + type: FINISH_NEW_ROOM_CONSTRUCTION + }); + }; +} + +export function cancelNewRoomConstruction() { + return { + type: CANCEL_NEW_ROOM_CONSTRUCTION + }; +} + +export function cancelNewRoomConstructionSucceeded() { + return (dispatch, getState) => { + const {currentDatacenterId, currentRoomInConstruction} = getState(); + dispatch(removeIdFromStoreObjectListProp("datacenter", currentDatacenterId, "roomIds", + currentRoomInConstruction)); + dispatch({ + type: CANCEL_NEW_ROOM_CONSTRUCTION_SUCCEEDED + }); + }; +} + +export function toggleTileAtLocation(positionX, positionY) { + return (dispatch, getState) => { + const {objects, currentRoomInConstruction} = getState(); + + const tileIds = objects.room[currentRoomInConstruction].tileIds; + for (let index in tileIds) { + if (objects.tile[tileIds[index]].positionX === positionX + && objects.tile[tileIds[index]].positionY === positionY) { + dispatch(deleteTile(tileIds[index])); + return; + } + } + dispatch(addTile(positionX, positionY)); + }; +} + +export function addTile(positionX, positionY) { + return { + type: ADD_TILE, + positionX, + positionY + }; +} + +export function addTileSucceeded(tileId) { + return (dispatch, getState) => { + const {currentRoomInConstruction} = getState(); + dispatch(addIdToStoreObjectListProp("room", currentRoomInConstruction, "tileIds", tileId)); + dispatch({ + type: ADD_TILE_SUCCEEDED, + tileId + }); + }; +} + +export function deleteTile(tileId) { + return { + type: DELETE_TILE, + tileId + } +} + +export function deleteTileSucceeded(tileId) { + return (dispatch, getState) => { + const {currentRoomInConstruction} = getState(); + dispatch(removeIdFromStoreObjectListProp("room", currentRoomInConstruction, "tileIds", tileId)); + dispatch({ + type: DELETE_TILE_SUCCEEDED, + tileId + }); + }; +} -- cgit v1.2.3