E.W.O. Next / All files / src/engine/Characters reducers.ts

96.3% Statements 26/27
90.91% Branches 10/11
100% Functions 3/3
96.15% Lines 25/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66          4x     4x   4x 4x       4x 4x       4x 15x   1x   1x 1x   2x     2x   4x     4x   6x   4x                 3x 3x   3x 3x   3x   1x   1x     7x      
/**
 * @module Engine.Characters
 * Characters reducers
 */
 
import * as Tasks from '@engine/Commands/tasks';
import { IMapsState } from '@engine/Maps/reducers';
import { Character, CharacterFrontend, CharacterDatabase } from '@models';
import { Map } from 'immutable';
import { AnyAction } from 'redux';
import { CharacterActions, CharactersActions } from './actions';
import { CharactersTools } from './CharacterTools';
import { Plans } from '@engine/resources';
 
// tslint:disable-next-line: no-unnecessary-type-annotation
const INITIAL_STATE: ICharactersState = Map();
const DATABASE = 'characters';
 
export type ICharactersState = Map<string, Character>;
 
export const charactersReducer = (state: ICharactersState = INITIAL_STATE, action: AnyAction): ICharactersState => {
  switch (action.type) {
    case CharactersActions.LOAD_DATABASE:
      return Tasks.loadDatabaseMap(DATABASE, CharactersTools.factory);
    case CharactersActions.SAVE_DATABASE:
      Tasks.saveDatabaseMap(DATABASE, state);
      return state;
    case CharactersActions.LINK_TO_MAP:
      const maps: IMapsState = action.maps;
 
      // Technically, we can threat
      return state.map(character => {
 
        Iif (character.position === undefined || character.position.plan === undefined) {
          return character;
        }
        const map = maps.get(character.position.plan.id);
 
        const coord = map.find(m => m.character.mat === character.mat);
 
        return {
          ...character, position: {
            plan: character.position.plan,
            coord,
          },
        };
      });
 
    case CharacterActions.MOVE:
      const char: Character = action.character;
      char.currentSpeed -= action.cost;
 
      char.position.coord.x = action.newX;
      char.position.coord.y = action.newY;
 
      return state.set(String(char.mat), char);
    case CharacterActions.CREATE:
      const newChar = CharactersTools.factory({ mat: state.size + 1, name: action.name, race: action.race, owner: action.owner, genre: action.genre });
 
      return state.set(String(newChar.mat), newChar);
 
    default:
      return state;
  }
};