<commit_before>import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { User } from '../../../services/user.service';

@Component({
  templateUrl: '../html/admin-stats-units.component.html'
})

export class AdminStatsUnitsComponent implements OnInit {
  public armyArray:any;
  public armyFirst:any;
  public towersArray:any;
  public data:any;
  private priceHosting:any;
  private priceList:any
  
  constructor(public user: User, public translate: TranslateService) {
    this.priceList = {
      'drachma': 1,
      'food': 0.11,
      'water': 0.078,
      'wood': 0.276,
      'iron': 0.147,
      'stone': 0.799,
      'marble': 396.999,
      'grapes': 0.967,
      'wine': 1350,
      'gold': 1493
    }
  }
  
  ngOnInit() {
    this.user.checkPermissions([1]);
    
    this.armyFirst = [];
    
    //Easier to make a local copy to run tests
    this.data = this.user.info.datas;
    
    if(Object.keys(this.data).length > 0) {
      this.calculateHosting();
      this.calculateArmy();
      this.calculateArmyFirst();
      this.calculateTowers();
    }
  }
  
  calculateArmy() {
    this.armyArray = [];
    
    for(let i in this.data.army) {
      let unit = this.data.army[i];
      
      //Ignore units who don't attack
      if(!unit.attack) {
        continue;
      }
      
      unit.code = i;
      unit.globalCost = 0;
      unit.globalConsu = 0;
      unit.a_f   = 0;
      unit.d_f   = 0;
      unit.p_a = 0;
      unit.c_a = 0;
      
      for(let res in unit.cost) {
        unit.globalCost += unit.cost[res]*this.priceList[res];
      }
      for(let res in unit.consumption) {
        unit.globalConsu += unit.consumption[res]*this.priceList[res];
      }
      
      let unitInfo:any = this.unitGetCalculateHosting(unit);
      unit.field       = unitInfo.field;
      unit.globalCost += unitInfo.globalCost;
      
      if(unit.attack > 0) {
        unit.p_a = unit.globalCost/unit.attack;
        unit.c_a = unit.globalConsu/unit.attack;
      }
      
      unit.p_d = unit.globalCost/unit.defense;
      unit.c_d = unit.globalConsu/unit.defense;
      
      if(unit.field > 0) {
        unit.a_f = unit.globalCost/unit.field;
        unit.d_f = unit.defense/unit.field;
      }
      <commit_msg>Attack is always > 0, no need to check it<commit_after>import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { User } from '../../../services/user.service';

@Component({
  templateUrl: '../html/admin-stats-units.component.html'
})

export class AdminStatsUnitsComponent implements OnInit {
  public armyArray:any;
  public armyFirst:any;
  public towersArray:any;
  public data:any;
  private priceHosting:any;
  private priceList:any
  
  constructor(public user: User, public translate: TranslateService) {
    this.priceList = {
      'drachma': 1,
      'food': 0.11,
      'water': 0.078,
      'wood': 0.276,
      'iron': 0.147,
      'stone': 0.799,
      'marble': 396.999,
      'grapes': 0.967,
      'wine': 1350,
      'gold': 1493
    }
  }
  
  ngOnInit() {
    this.user.checkPermissions([1]);
    
    this.armyFirst = [];
    
    //Easier to make a local copy to run tests
    this.data = this.user.info.datas;
    
    if(Object.keys(this.data).length > 0) {
      this.calculateHosting();
      this.calculateArmy();
      this.calculateArmyFirst();
      this.calculateTowers();
    }
  }
  
  calculateArmy() {
    this.armyArray = [];
    
    for(let i in this.data.army) {
      let unit = this.data.army[i];
      
      //Ignore units who don't attack
      if(!unit.attack) {
        continue;
      }
      
      unit.code = i;
      unit.globalCost = 0;
      unit.globalConsu = 0;
      unit.a_f   = 0;
      unit.d_f   = 0;
      unit.p_a = 0;
      unit.c_a = 0;
      
      for(let res in unit.cost) {
        unit.globalCost += unit.cost[res]*this.priceList[res];
      }
      for(let res in unit.consumption) {
        unit.globalConsu += unit.consumption[res]*this.priceList[res];
      }
      
      let unitInfo:any = this.unitGetCalculateHosting(unit);
      unit.field       = unitInfo.field;
      unit.globalCost += unitInfo.globalCost;
      
      unit.p_a = unit.globalCost/unit.attack;
      unit.c_a = unit.globalConsu/unit.attack;
      unit.p_d = unit.globalCost/unit.defense;
      unit.c_d = unit.globalConsu/unit.defense;
      
      if(unit.field > 0) {
        unit.a_f = unit.globalCost/unit.field;
        unit.d_f = unit.defense/unit.field;
      }
      
{'fraction_non_alphanumeric': 0.13671107228652543, 'fraction_numerical': 0.0209652762611924, 'mean_word_length': 2.113528212100612, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '667790', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1581, 'n_words': 296}