{"size": 2941, "ext": "js", "max_stars_repo_path": "webapp/src/routes/home/game/elements/animalProgress.js", "max_stars_repo_name": "wojciech-panek/devtalk-pwa-app", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "webapp/src/routes/home/game/elements/animalProgress.js", "max_issues_repo_name": "wojciech-panek/devtalk-pwa-app", "max_issues_repo_issues_event_min_datetime": "2020-09-04T16:58:05.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T10:00:55.000Z", "max_forks_repo_path": "webapp/src/routes/home/game/elements/animalProgress.js", "max_forks_repo_name": "wojciech-panek/devtalk-pwa-app", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 10, "max_forks_count": null, "avg_line_length": 24.106557377, "max_line_length": 118, "alphanum_fraction": 0.6684801088}
import { Graphics, Container, Ticker } from 'pixi.js';

import { isoToTimestamp } from '../../../../shared/utils/date';
import { GameState } from '../game.state';
import { WAREHOUSE_LEVELS } from '../game.constants';


export class AnimalProgress {
  constructor({ flip, startProductionTimestamp, productionDuration, pokeCount, positionNumber }) {
    this._stage = new Container();
    this._backgroundCircle = new Graphics();
    this._innerCircle = new Graphics();
    this._ticker = new Ticker();

    this._startProductTimestamp = startProductionTimestamp;
    this._productionDuration = productionDuration;
    this._pokeCount = pokeCount;
    this._positionNumber = positionNumber;

    this.stage.x = flip ? 41 : -41;
    this.stage.y = 18;

    this.drawBackgroundCircle();
    this.drawInnerCircle();

    this._ticker.add(this.handleTick);
    this._ticker.start();

    this.stage.addChild(this._backgroundCircle, this._innerCircle);
  }

  drawBackgroundCircle = () => {
    this._backgroundCircle.lineStyle(1, 0x404040, 0.5);
    this._backgroundCircle.beginFill(0x404040, 1);
    this._backgroundCircle.drawCircle(0, 0, 9);
    this._backgroundCircle.endFill();
  };

  drawInnerCircle = () => {
    this._innerCircle.moveTo(0, 0);
    this._innerCircle.beginFill(0x45ee00, 1);
    this._innerCircle.lineStyle(0, 0x45ee00, 0.5);
    this._innerCircle.arc(0, 0, 7, 0, 2 * Math.PI * this.progress);
    this._innerCircle.endFill();
  };

  handleTick = () => {
    this._innerCircle.clear();
    this.drawInnerCircle();
  };

  get progress() {
    if (this.isWarehouseFull) {
      return 1;
    }

    const now = Date.now();
    return Math.min(
      ((now - isoToTimestamp(this.startProductTimestamp)) + this.pokeCount * 1000) / (this.productionDuration * 1000),
      1,
    );
  }

  get stage() {
    return this._stage;
  }

  get startProductTimestamp() {
    return this._startProductTimestamp;
  }

  get productionDuration() {
    return this._productionDuration;
  }

  get pokeCount() {
    return this._pokeCount;
  }

  set startProductTimestamp(value) {
    this._startProductTimestamp = value;
  }

  set productionDuration(value) {
    this._productionDuration = value;
  }

  set pokeCount(value) {
    this._pokeCount = value;
  }

  get fieldIndex() {
    if (!GameState.reduxState.fields) {
      return -1;
    }

    return GameState.reduxState.fields.findIndex((field) => field.position === this.positionNumber);
  }

  get positionNumber() {
    return this._positionNumber;
  }

  get animalData() {
    if (this.fieldIndex === -1) {
      return null;
    }
    return GameState.reduxState.fields[this.fieldIndex];
  }

  get isWarehouseFull() {
    if (!this.animalData) {
      return false;
    }

    const foodAmount = this.animalData.foodAmount;
    const warehouseCapacity = WAREHOUSE_LEVELS[this.animalData.warehouseLevel].foodMaxAmount;

    return foodAmount === warehouseCapacity;
  }
}
