<commit_before>const PanelsActionCreator = require('../actions/panels-action-creator');
const DisksActionCreator = require('../actions/disks-action-creator');
const SculptureActionCreator = require('../actions/sculpture-action-creator');

const Disk = require('../utils/disk');

const DEFAULT_LEVEL = 0;

export default class DiskGameLogic {
  // These are automatically added to the sculpture store
  static trackedProperties = {
    level: DEFAULT_LEVEL
  };

  constructor(store, config) {
    this.store = store;
    this.config = config;
    this.gameConfig = config.DISK_GAME;

    this._complete = false;
  }

  get data() {
    return this.store.data.get('disk');
  }

  start() {
    this._level = DEFAULT_LEVEL;
  }

  handleActionPayload(payload) {
    const actionHandlers = {
      [PanelsActionCreator.PANEL_PRESSED]: this._actionPanelPressed.bind(this),
      [DisksActionCreator.DISK_UPDATE]: this._actionDiskUpdate.bind(this),
      [SculptureActionCreator.FINISH_STATUS_ANIMATION]: this._actionFinishStatusAnimation.bind(this)
    };

    const actionHandler = actionHandlers[payload.actionType];
    if (actionHandler) {
      actionHandler(payload);
    }
  }

  _actionPanelPressed(payload) {
    let {stripId, panelId, pressed} = payload;

    const controlMappings = this.gameConfig.CONTROL_MAPPINGS;
    if (this._complete || !controlMappings.hasOwnProperty(stripId) || !controlMappings[stripId].hasOwnProperty(panelId)) {
      return;
    }

    const disks = this.store.data.get('disks');
    const targetDisks = controlMappings[stripId][panelId];
    
    for (let diskId of Object.keys(targetDisks)) {
      const direction = targetDisks[diskId];
      const disk = disks.get(diskId);
      if (pressed) {
        disk.setDirection(direction);
      }
      else if (!disk.isStopped) {
        disk.unsetDirection(direction);
      }
    }
  }

  _actionDiskUpdate(payload) {
    const {diskId, position, direction, state} = payload;

    const disks = this.store.data.get('disks');
    const disk = disks.get(diskId);

    if (typeof position !== 'undefined') {
      disk.rotateTo(position);
    }
    if (typeof direction !== 'undefined') {
      disk.setDirection(direction);
    }
    if (typeof state !== 'undefined') {
      disk.setState(state);
    }

    if (!this.store.isStatusSuccess) {
      this._checkWinConditions(disks);
    }
  }

  _actionFinishStatusAnimation(payload) {
    if (this._complete) {
      this.store.moveToNextGame();
    }
  }

  _checkWinConditions(disks) {
    for (let diskId of Object.keys(this._targetPositions)) {
      const targetPosition = this._targetPositions[diskId];
      if (Math.abs(disks.get(diskId).getPosition() - targetPosition) > this.gameConfig.TOLERANCE) {
        return false;
      }
    }

    this._winGame();
  }

  _winGame() {
    this.store.data.get('lights').deactivateAll();
    this._stopAllDisks();

    this.store.setSuccessStatus();

    let level = this._level + 1;
    if (level >= this._levels) {
      this._complete = true;
    }

    this._level = level;
  }

  _stopAllDisks() {
    const disks = this.store.data.get('disks');

    for (let diskId of disks) {
      disks.get(diskId).stop();
    }
  }

  get _targetPositions() {
    const level = this._level;
    return this.gameConfig.TARGET_POSITIONS_LEVELS[level];
  }

  get _levels() {
    return this.gameConfig.TARGET_POSITIONS_LEVELS.length;
  }

  get _level() {
    return this.data.get('level');
  }

  set _level(value) {
    return this.data.set('level', value);
  }
}
<commit_msg>Bugfix: Tag as not completed every time the game starts<commit_after>const PanelsActionCreator = require('../actions/panels-action-creator');
const DisksActionCreator = require('../actions/disks-action-creator');
const SculptureActionCreator = require('../actions/sculpture-action-creator');

const Disk = require('../utils/disk');

const DEFAULT_LEVEL = 0;

export default class DiskGameLogic {
  // These are automatically added to the sculpture store
  static trackedProperties = {
    level: DEFAULT_LEVEL
  };

  constructor(store, config) {
    this.store = store;
    this.config = config;
    this.gameConfig = config.DISK_GAME;

    this._complete = false;
  }

  get data() {
    return this.store.data.get('disk');
  }

  start() {
    this._level = DEFAULT_LEVEL;
    this._complete = false;
  }

  handleActionPayload(payload) {
    const actionHandlers = {
      [PanelsActionCreator.PANEL_PRESSED]: this._actionPanelPressed.bind(this),
      [DisksActionCreator.DISK_UPDATE]: this._actionDiskUpdate.bind(this),
      [SculptureActionCreator.FINISH_STATUS_ANIMATION]: this._actionFinishStatusAnimation.bind(this)
    };

    const actionHandler = actionHandlers[payload.actionType];
    if (actionHandler) {
      actionHandler(payload);
    }
  }

  _actionPanelPressed(payload) {
    let {stripId, panelId, pressed} = payload;

    const controlMappings = this.gameConfig.CONTROL_MAPPINGS;
    if (this._complete || !controlMappings.hasOwnProperty(stripId) || !controlMappings[stripId].hasOwnProperty(panelId)) {
      return;
    }

    const disks = this.store.data.get('disks');
    const targetDisks = controlMappings[stripId][panelId];
    
    for (let diskId of Object.keys(targetDisks)) {
      const direction = targetDisks[diskId];
      const disk = disks.get(diskId);
      if (pressed) {
        disk.setDirection(direction);
      }
      else if (!disk.isStopped) {
        disk.unsetDirection(direction);
      }
    }
  }

  _actionDiskUpdate(payload) {
    const {diskId, position, direction, state} = payload;

    const disks = this.store.data.get('disks');
    const disk = disks.get(diskId);

    if (typeof position !== 'undefined') {
      disk.rotateTo(position);
    }
    if (typeof direction !== 'undefined') {
      disk.setDirection(direction);
    }
    if (typeof state !== 'undefined') {
      disk.setState(state);
    }

    if (!this.store.isStatusSuccess) {
      this._checkWinConditions(disks);
    }
  }

  _actionFinishStatusAnimation(payload) {
    if (this._complete) {
      this.store.moveToNextGame();
    }
  }

  _checkWinConditions(disks) {
    for (let diskId of Object.keys(this._targetPositions)) {
      const targetPosition = this._targetPositions[diskId];
      if (Math.abs(disks.get(diskId).getPosition() - targetPosition) > this.gameConfig.TOLERANCE) {
        return false;
      }
    }

    this._winGame();
  }

  _winGame() {
    this.store.data.get('lights').deactivateAll();
    this._stopAllDisks();

    this.store.setSuccessStatus();

    let level = this._level + 1;
    if (level >= this._levels) {
      this._complete = true;
    }

    this._level = level;
  }

  _stopAllDisks() {
    const disks = this.store.data.get('disks');

    for (let diskId of disks) {
      disks.get(diskId).stop();
    }
  }

  get _targetPositions() {
    const level = this._level;
    return this.gameConfig.TARGET_POSITIONS_LEVELS[level];
  }

  get _levels() {
    return this.gameConfig.TARGET_POSITIONS_LEVELS.length;
  }

  get _level() {
    return this.data.get('level');
  }

  set _level(value) {
    return this.data.set('level', value);
  }
}

{'fraction_non_alphanumeric': 0.1292119186856029, 'fraction_numerical': 0.000556947925368978, 'mean_word_length': 3.171312427409988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2107493', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2269, 'n_words': 405}