battle.component.ts 2.9 KB
Newer Older
Manish Kumar's avatar
Manish Kumar committed
1
import { Component, OnInit } from '@angular/core';
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
2
import { HttpClient } from '@angular/common/http';
3
4
import { ActivatedRoute } from '@angular/router';

Manish Kumar's avatar
Manish Kumar committed
5
6
7
8
9
10
11

@Component({
  selector: 'app-battle',
  templateUrl: './battle.component.html',
  styleUrls: ['./battle.component.css']
})
export class BattleComponent implements OnInit {
12
  isOpen = true;
13
14
  maxHealth: number;
  curHealthSelf: number;
15
  curHealthOpp = 100;
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
16
  pseudo: string;
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
17
  uid: any;
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
18
19
20
  log: boolean;
  level = 50;
  Power = 60;
21
22
23
24
  atk: number;
  def: number;
  typeMoi: number;
  typeMove: number;
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
25
  typeToi = 10;
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
26
  effet: any;
27
28
  private sub: any;
  private id_partie: number;
29
30
31
32
33
34
35
  selfTeam: any = [];
  curPoke: any = {};
  affichageHp: number = 0;


  constructor(private route: ActivatedRoute, private httpClient: HttpClient) { }

36

Manish Kumar's avatar
Manish Kumar committed
37
38
39
  getPokemon(){
    this.selfTeam.forEach(el => {
      if(el.stats[0].stat_value > 0) this.curPoke = el;
40
41
42
43
44
45
      this.curHealthSelf = this.curPoke.stats[0].stat_value;
      this.atk = this.curPoke.stats[1].stat_value;
      this.def = this.curPoke.stats[2].stat_value;
      this.typeMoi = this.curPoke.types[0].id_type;
      this.maxHealth = this.curPoke.base_stats[0].base_stat
      this.healthUpdate();
Manish Kumar's avatar
Manish Kumar committed
46
    });
47
48

  }
Manish Kumar's avatar
Manish Kumar committed
49

50
51
52
  healthUpdate(){
    this.affichageHp = (100*this.curHealthSelf)/this.maxHealth
  }
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
53

Manish Kumar's avatar
Manish Kumar committed
54
55
56
57
58
59
60
61
62
63
  applyDamageSelf(dmg){
    this.curHealthSelf -= dmg;
    if(this.curHealthSelf < 0) this.curHealthSelf = 0;
  }

  applyDamageOpponent(dmg){
    this.curHealthOpp -= dmg;
    if(this.curHealthOpp < 0) this.curHealthOpp = 0;
  }

64
65
  getCombatTeam(){
    this.httpClient.post("http://localhost:3000/getCombatTeam", {id_partie: this.id_partie}, { responseType: 'json', withCredentials: true }).subscribe(
66
      (res) => {this.selfTeam = res; this.getPokemon(), console.log(res)});
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
67
68
  }

Manish Kumar's avatar
Manish Kumar committed
69
70
71
72
73
74
  typeAdv(move){
    this.httpClient.post("http://localhost:3000/geteff", { typeNous: move.m_id_type, typeEux: this.typeToi}, {responseType: 'json', withCredentials: true}).subscribe((res:any) => {this.calculeDmg(res.body[0].damage_factor); move.pp--; console.log(this.curPoke.moves); this.updateDBPokemon()});
  }

  updateDBPokemon(){
    this.httpClient.post("http://localhost:3000/updatepartie", this.curPoke, {responseType: 'json', withCredentials: true}).subscribe((res:any) => (console.log(res)));
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
75
76
  }

77
  calculeDmg(eff){
78
    let dmg = ((((0.4*this.level+2)*this.atk*this.Power)/(50*this.def))+2)*(eff/100)*(Math.random()*(1 - 0.85)+ 0.85)
Manish Kumar's avatar
Manish Kumar committed
79
80
    this.applyDamageOpponent(dmg);
    console.log(dmg + " " + this.curHealthOpp)
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
81
  }
Manish Kumar's avatar
Manish Kumar committed
82
83

  ngOnInit(): void {
84
    this.httpClient.get("http://localhost:3000/checkLogin", { responseType: 'json', withCredentials: true}).subscribe( (res: any) => {
Sofia Aicha Delijaj's avatar
Sofia Aicha Delijaj committed
85
86
87
      this.log = res.res;
      this.pseudo = res.body;
      this.uid = res.uid;
88
89
90
91
92
93
      this.getCombatTeam();
    });
    this.sub = this.route.params.subscribe(params => {
      this.id_partie = +params['id']; // (+) converts string 'id' to a number
      console.log(this.id_partie);
    });
Manish Kumar's avatar
Manish Kumar committed
94
95
96
  }

}