<commit_before>/*
 Copyright (C) 2013-2015 by <NAME> and Contributors

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import * as rpg from '../game';
import {HeroModel,WeaponModel,ArmorModel,ItemModel,EntityModel,UsableModel} from './all';
import {GameWorld} from '../gameWorld';

var _gameData:pow2.GameDataResource = null;

export class GameStateModel extends pow2.Events implements pow2.IWorldObject {
  world:GameWorld;
  party:HeroModel[]; // The player's party
  inventory:ItemModel[]; // The inventory of items owned by the player.
  loader:pow2.ResourceLoader;
  keyData:{
    [key:string]:any
  } = {};
  gold:number;
  combatZone:string;

  constructor(options?:any) {
    super();
    _.defaults(this, options || {}, {
      gold: 200,
      playerPosition: new pow2.Point(),
      playerMap: "",
      combatZone: "world-plains",
      party: [],
      inventory: []
    });
  }

  initData(then?:(data:pow2.GameDataResource)=>any) {
    GameStateModel.getDataSource(then);
  }

  /**
   * Get the game data sheets from google and callback when they're loaded.
   * @param then The function to call when spreadsheet data has been fetched
   */
  static getDataSource(then?:(data:pow2.GameDataResource)=>any):pow2.GameDataResource {
    if (_gameData) {
      then && then(_gameData);
      return _gameData;
    }
    else {
      return <pow2.GameDataResource>pow2.ResourceLoader.get().loadAsType(pow2.SPREADSHEET_ID, pow2.GameDataResource, (resource:pow2.GameDataResource) => {
        _gameData = resource;
        then && then(resource);
      });
    }
  }

  setKeyData(key:string, data:any) {
    this.keyData[key] = data;
  }

  getKeyData(key:string):any {
    return this.keyData[key];
  }


  addInventory(item:ItemModel):ItemModel {
    this.inventory.push(item);
    return item;
  }

  // Remove an inventory item.  Return true if the item was removed, or false
  // if it was not found.
  removeInventory(item:ItemModel):boolean {
    for (var i = 0; i < this.inventory.length; i++) {
      if (this.inventory[i].cid === item.cid) {
        this.inventory.splice(i, 1);
        return true;
      }
    }
    return false;
  }


  addHero(model:HeroModel) {
    this.party.push(model);
    model.game = this;
  }

  addGold(amount:number) {
    this.gold += amount;
  }

  parse(data:any, options?:any) {
    if (!_gameData) {
      throw new Error("cannot instantiate inventory without valid data source.\nCall model.initData(loader) first.")
    }
    try {
      if (typeof data === 'string') {
        data = JSON.parse(data);
      }
    }
    catch (e) {
      console.log("Failed to load save game.");
      return;
    }
    if (typeof data.keyData !== 'undefined') {
      this.keyData = data.keyData;
    }

    var theChoices:any[] = [];
    theChoices = theChoices.concat(_.map(_gameData.getSheetData('weapons'), (w)=> {
      return _.extend({instanceModel: new WeaponModel(w)}, w);
    }));
    theChoices = theChoices.concat(_.map(_gameData.getSheetData('armor'), (a)=> {
      return _.extend({instanceModel: new ArmorModel(a)}, a);
    }));
    theChoices = theChoices.concat(_.map(_gameData.getSheetData('items'), (a)=> {
      return _.extend({instanceModel: new UsableModel(a)}, a);
    }));


    this.inventory = _.map(data.inventory, (item:rpg.IGameItem) => {
      return this.world.itemModelFromId(item.id);
    });
    this.party = _.map(data.party, (partyMember:string) => {
      var model = new HeroModel();
      model.fromString(partyMember, this.world);
      return model;<commit_msg>chore: remove unused item model creation code<commit_after>/*
 Copyright (C) 2013-2015 by <NAME> and Contributors

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import * as rpg from '../game';
import {HeroModel,WeaponModel,ArmorModel,ItemModel,EntityModel,UsableModel} from './all';
import {GameWorld} from '../gameWorld';

var _gameData:pow2.GameDataResource = null;

export class GameStateModel extends pow2.Events implements pow2.IWorldObject {
  world:GameWorld;
  party:HeroModel[]; // The player's party
  inventory:ItemModel[]; // The inventory of items owned by the player.
  loader:pow2.ResourceLoader;
  keyData:{
    [key:string]:any
  } = {};
  gold:number;
  combatZone:string;

  constructor(options?:any) {
    super();
    _.defaults(this, options || {}, {
      gold: 200,
      playerPosition: new pow2.Point(),
      playerMap: "",
      combatZone: "world-plains",
      party: [],
      inventory: []
    });
  }

  initData(then?:(data:pow2.GameDataResource)=>any) {
    GameStateModel.getDataSource(then);
  }

  /**
   * Get the game data sheets from google and callback when they're loaded.
   * @param then The function to call when spreadsheet data has been fetched
   */
  static getDataSource(then?:(data:pow2.GameDataResource)=>any):pow2.GameDataResource {
    if (_gameData) {
      then && then(_gameData);
      return _gameData;
    }
    else {
      return <pow2.GameDataResource>pow2.ResourceLoader.get().loadAsType(pow2.SPREADSHEET_ID, pow2.GameDataResource, (resource:pow2.GameDataResource) => {
        _gameData = resource;
        then && then(resource);
      });
    }
  }

  setKeyData(key:string, data:any) {
    this.keyData[key] = data;
  }

  getKeyData(key:string):any {
    return this.keyData[key];
  }


  addInventory(item:ItemModel):ItemModel {
    this.inventory.push(item);
    return item;
  }

  // Remove an inventory item.  Return true if the item was removed, or false
  // if it was not found.
  removeInventory(item:ItemModel):boolean {
    for (var i = 0; i < this.inventory.length; i++) {
      if (this.inventory[i].cid === item.cid) {
        this.inventory.splice(i, 1);
        return true;
      }
    }
    return false;
  }


  addHero(model:HeroModel) {
    this.party.push(model);
    model.game = this;
  }

  addGold(amount:number) {
    this.gold += amount;
  }

  parse(data:any, options?:any) {
    if (!_gameData) {
      throw new Error("cannot instantiate inventory without valid data source.\nCall model.initData(loader) first.")
    }
    try {
      if (typeof data === 'string') {
        data = JSON.parse(data);
      }
    }
    catch (e) {
      console.log("Failed to load save game.");
      return;
    }
    if (typeof data.keyData !== 'undefined') {
      this.keyData = data.keyData;
    }
    this.inventory = _.map(data.inventory, (item:rpg.IGameItem) => {
      return this.world.itemModelFromId(item.id);
    });
    this.party = _.map(data.party, (partyMember:string) => {
      var model = new HeroModel();
      model.fromString(partyMember, this.world);
      return model;
{'fraction_non_alphanumeric': 0.12873502698433592, 'fraction_numerical': 0.007897854416216928, 'mean_word_length': 3.1862258953168046, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6488893', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2349, 'n_words': 611}