E.W.O. Next / All files / src/client/Game/Views Views.tsx

93.26% Statements 83/89
75% Branches 18/24
76.92% Functions 10/13
94.05% Lines 79/84

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230            1x 1x 1x 1x 1x 1x 1x                                   1x     5x   5x         11x 4x     7x                 5x 5x 5x 5x 5x   5x 95x 95x     5x 95x 95x     5x           40x   40x 40x   40x   40x   5x 5x   5x 5x   5x 5x   5x 5x   5x 5x   5x 5x   5x 5x   5x 5x     40x                 40x               5x 5x 5x 5x 5x 5x 5x 5x   5x       428x   428x 380x   40x 40x                 8x                 7x   7x 2x     5x 5x   5x             5x             5x   5x   5x       428x 428x 428x                                       10x         4x       1x        
/**
 * @module Client.Game.Views
 * Views (checkerboard) module for game page
 */
 
import { IStateFrontend } from '@client/reducers';
import { getSelectedCharacter, getSelectedView } from '@client/selector';
import * as Actions from '@client/socket/actions';
import { MapsTools } from '@engine/Maps/MapsTools';
import { CharacterFrontend, CoordCharacterFrontend, CoordFrontend, Direction, POVState, ViewFrontend } from '@models';
import React from 'react';
import { connect } from 'react-redux';
import { Action, bindActionCreators, Dispatch } from 'redux';
 
export interface ViewsProps {
  character: CharacterFrontend;
  view: ViewFrontend;
  actions: any;
}
 
export interface CoordLabel extends CoordFrontend {
  label: string;
}
 
export interface CoordMeta extends CoordFrontend {
  category: string;
  ui: string;
  meta: any;
}
 
export class ViewsComponent extends React.Component<ViewsProps> {
 
  constructor(props: ViewsProps) {
    super(props);
 
    this.clickDirection = this.clickDirection.bind(this);
  }
 
  public render() {
 
    if (this.props.character === undefined) {
      return null;
    }
 
    return (
      <div className="Views Game__Container">
        <div className="Title">Views</div>
        {this.renderGrid()}
      </div>
    );
  }
 
  private generateCoordsBorder(centerX: number, centerY: number, insight: number) {
    const minX = centerX - insight;
    const maxX = centerX + insight;
    const minY = centerY - insight;
    const maxY = centerY + insight;
    const borders: CoordLabel[] = [];
 
    for (let x = minX + 1; x < maxX; x += 1) {
      borders.push({ x, y: minY, label: String(x), type: 'lab' });
      borders.push({ x, y: maxY, label: String(x), type: 'lab' });
    }
 
    for (let y = minY + 1; y < maxY; y += 1) {
      borders.push({ x: minX, y, label: String(y), type: 'lab' });
      borders.push({ x: maxX, y, label: String(y), type: 'lab' });
    }
 
    return borders;
 
  }
 
  private generateMoveCoord(direction: number, pos: { x, y }, coords: any[]): any[] {
 
    const { view } = this.props;
 
    const found = view.pov.find(c => c.x === pos.x && c.y === pos.y && c.state === POVState.Block);
    Eif (found === undefined) {
 
      let className = '';
 
      switch (direction) {
        case Direction.North:
          className = 'fas fa-arrow-up';
          break;
        case Direction.NorthEast:
          className = 'fas fa-arrow-up turn45';
          break;
        case Direction.East:
          className = 'fas fa-arrow-right';
          break;
        case Direction.SouthEast:
          className = 'fas fa-arrow-right turn45';
          break;
        case Direction.South:
          className = 'fas fa-arrow-down';
          break;
        case Direction.SouthWest:
          className = 'fas fa-arrow-down turn45';
          break;
        case Direction.West:
          className = 'fas fa-arrow-left';
          break;
        case Direction.NorthWest:
          className = 'fas fa-arrow-left turn45';
          break;
      }
 
      const move: CoordMeta = {
        x: pos.x,
        y: pos.y,
        type: 'met',
        category: 'move',
        ui: className,
        meta: direction,
      };
 
      return [...coords, move];
    }
 
    return coords;
  }
 
  private generateMoveCoords(x: number, y: number, coords: any[]) {
    // tslint:disable: no-parameter-reassignment
    coords = this.generateMoveCoord(Direction.North, MapsTools.getRelativePosition(x, y, Direction.North), coords);
    coords = this.generateMoveCoord(Direction.NorthEast, MapsTools.getRelativePosition(x, y, Direction.NorthEast), coords);
    coords = this.generateMoveCoord(Direction.East, MapsTools.getRelativePosition(x, y, Direction.East), coords);
    coords = this.generateMoveCoord(Direction.SouthEast, MapsTools.getRelativePosition(x, y, Direction.SouthEast), coords);
    coords = this.generateMoveCoord(Direction.South, MapsTools.getRelativePosition(x, y, Direction.South), coords);
    coords = this.generateMoveCoord(Direction.SouthWest, MapsTools.getRelativePosition(x, y, Direction.SouthWest), coords);
    coords = this.generateMoveCoord(Direction.West, MapsTools.getRelativePosition(x, y, Direction.West), coords);
    coords = this.generateMoveCoord(Direction.NorthWest, MapsTools.getRelativePosition(x, y, Direction.NorthWest), coords);
 
    return coords;
  }
 
  private renderCase(coord: any): JSX.Element {
    Eif (coord.type !== undefined) {
 
      switch (coord.type) {
        case 'lab': return <div className="item--label">{coord.label}</div>;
        case 'met':
          Eif (coord.category === 'move') {
            return (
              <div
                role="button"
                className={`item--move ${coord.ui}`}
                onClick={() => { this.clickDirection(coord.meta); }}
              />
            );
          }
          break;
        case 'cha': return <div className="item--character">{coord.mat}</div>;
      }
 
    }
    return null;
  }
 
  private renderGrid() {
 
    const { character, view } = this.props;
 
    if (character.coord === undefined) {
      return <section>Votre personnage n'est plus de ce monde.</section>;
    }
 
    const centerX = character.coord.x;
    const centerY = character.coord.y;
 
    const coordCharacter: CoordCharacterFrontend = {
      x: centerX,
      y: centerY,
      character,
      type: 'cha',
    };
 
    let gridCoords = [
      coordCharacter,
      ...view.characters,
      ...view.pov,
      ...this.generateCoordsBorder(centerX, centerY, character.currentInsight),
    ];
 
    gridCoords = this.generateMoveCoords(centerX, centerY, gridCoords);
 
    console.log({ gridCoords });
 
    return (
      <section className={`grid grid-${character.currentInsight}`}>
        {gridCoords.map((coord, index) => {
          // +1 for centering, and another +1 for the ruler row and column
          const offsetX = coord.x - centerX + character.currentInsight + 2;
          const offsetY = coord.y - centerY + character.currentInsight + 2;
          return <div
            key={index}
            className="item"
            style={{
              gridColumn: offsetX,
              gridRow: offsetY,
            }}
          >
            {this.renderCase(coord)}
          </div>;
        })}
      </section>
    ); // {character.mat} ({coord.x}/{coord.y})
  }
 
  private clickDirection(direction) {
    this.props.actions.socketActionMovement(this.props.character.mat, direction);
  }
}
 
const mapStateToProps = (state: IStateFrontend) => ({
  character: getSelectedCharacter(state),
  view: getSelectedView(state),
});
 
const mapDispatchToProps = (dispatch: Dispatch<Action>) => ({
  actions: bindActionCreators(Actions, dispatch),
});
 
export const Views = connect(
  mapStateToProps,
  mapDispatchToProps,
)(ViewsComponent);