Commit 6d3ecb9e authored by Manish Kumar's avatar Manish Kumar
Browse files

partie update small fix, pokeballs displayed, buttons updated, better pokemon switch transittions

parent aef61119
...@@ -95,14 +95,14 @@ exports.updatePartie = (req, res) => { ...@@ -95,14 +95,14 @@ exports.updatePartie = (req, res) => {
message: err.message || "Some error occurred while updating pokemon state." message: err.message || "Some error occurred while updating pokemon state."
}); });
else if(req.body.nextPokemon != -1){ else if(req.body.nextPokemon != -1){
Partie.activateNextPokemon(req.body.nextPokemon, (err, data4) => { console.log(req.body.nextPokemon, req.body.pokemon2.id_etat);
Partie.activateNextPokemon(req.body.nextPokemon, req.body.pokemon2.id_etat, (err, data4) => {
if (err) if (err)
res.status(500).send({ res.status(500).send({
message: err.message || "Some error occurred while updating pokemon state." message: err.message || "Some error occurred while updating pokemon state."
}); });
res.send(data4); res.send(data4);
}); });
} else{ } else{
res.send(data3) res.send(data3)
} }
......
...@@ -28,6 +28,41 @@ exports.filterJSON = function(data, type) { ...@@ -28,6 +28,41 @@ exports.filterJSON = function(data, type) {
} }
exports.filterJSONCombat = function(data) { exports.filterJSONCombat = function(data) {
var separatorsNames = ["types", "stats", "base_stats", "moves"];
var separators = ["id_type", "id", "baseStat_id", "id_move"];
var containers = [{}];
separators.forEach(sep => containers.push({}));
var dataSet = new Set();
var filteredData = [];
var i = -1;
Object.keys(data).forEach(function(key) {
var switcher = 0;
containers.forEach((el, index, containers) => containers[index] = {});
Object.keys(data[key]).forEach(function(key2) {
if (separators.includes(key2)) switcher++;
containers[switcher][key2] = data[key][key2];
});
separatorsNames.forEach(sep => containers[0][sep] = [])
if (dataSet.has(data[key]["id_pokemon"])) {
for(var j = 0; j < separators.length; j++){
filteredData[i][separatorsNames[j]].push(containers[j+1]);
}
} else {
i++;
filteredData.push(containers[0]);
dataSet.add(data[key]["id_pokemon"]);
for(var j = 0; j < separators.length; j++){
filteredData[i][separatorsNames[j]].push(containers[j+1]);
}
}
});
filteredData.forEach(pokemon => {
separatorsNames.forEach(sep => pokemon[sep] = arrUnique(pokemon[sep]));
});
return filteredData;
}
exports.filterJSONCombatOld = function(data) {
var separatorsNames = ["types", "stats", "base_stats", "moves"]; var separatorsNames = ["types", "stats", "base_stats", "moves"];
var separators = ["id_type", "id", "baseStat_id", "id_move"]; var separators = ["id_type", "id", "baseStat_id", "id_move"];
var dataSet = new Set(); var dataSet = new Set();
......
...@@ -199,7 +199,7 @@ Partie.updatePartieTour = (id_partie, winner, result) => { ////////////????????? ...@@ -199,7 +199,7 @@ Partie.updatePartieTour = (id_partie, winner, result) => { ////////////?????????
sql.rollback(function() { sql.rollback(function() {
throw err; throw err;
}); });
resultGlobal(err, null); result(err, null);
return; return;
} }
console.log('Transaction Complete for winner !!!!'); console.log('Transaction Complete for winner !!!!');
...@@ -216,15 +216,15 @@ Partie.updatePartieTour = (id_partie, winner, result) => { ////////////????????? ...@@ -216,15 +216,15 @@ Partie.updatePartieTour = (id_partie, winner, result) => { ////////////?????????
Partie.updatePartie = (id_etat, active, stats, moves, result) => { Partie.updatePartie = (id_etat, active, stats, moves, result) => {
sql.beginTransaction(function(errTransaction) { sql.beginTransaction(function(errTransaction) {
if (errTransaction) { throw err; } if (errTransaction) { throw err; }
sql.query('UPDATE etatpoke SET active=? WHERE id_etat=?', [active, id_etat], function(err, res) { // sql.query('UPDATE etatpoke SET active=? WHERE id_etat=?', [active, id_etat], function(err, res) {
if (err) { // if (err) {
console.log("error: ", err); // console.log("error: ", err);
sql.rollback(function() { // sql.rollback(function() {
throw err; // throw err;
}); // });
result(err, null); // result(err, null);
return; // return;
} // }
var sqlStats = []; var sqlStats = [];
stats.forEach(stat => { stats.forEach(stat => {
...@@ -255,26 +255,64 @@ Partie.updatePartie = (id_etat, active, stats, moves, result) => { ...@@ -255,26 +255,64 @@ Partie.updatePartie = (id_etat, active, stats, moves, result) => {
result(err, null); result(err, null);
return; return;
} }
sql.commit(function(err) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
console.log('Transaction Complete: etatPoke updated');
//sql.end();
});
}); });
result(null, { res: true, id: res.insertId });
}); });
result(null, { res: true, id: res.insertId }); // });
});
}); });
} }
Partie.activateNextPokemon = (id_etat, result) => { Partie.activateNextPokemon = (next_id_etat, dead_id_etat, result) => {
sql.query('UPDATE etatpoke SET active=1 WHERE id_etat=?', [id_etat], function(err, res) { sql.beginTransaction(function(errTransaction) {
if (err) { if (errTransaction) { throw err; }
console.log("error: ", err); sql.query('UPDATE etatpoke SET active=1 WHERE id_etat=?', [next_id_etat], function(err, res) {
sql.rollback(function() { if (err) {
throw err; console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
sql.query('UPDATE etatpoke SET active=0 WHERE id_etat=?', [dead_id_etat], function(err, res) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
sql.commit(function(err) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
console.log('Transaction complete : death of pokemon, pokemon switched', next_id_etat, dead_id_etat);
//sql.end();
});
}); });
result(err, null);
return;
}
result(null, res); result(null, res);
});
}); });
} }
......
...@@ -11,7 +11,7 @@ const Pokemon = function(pokemon) { ...@@ -11,7 +11,7 @@ const Pokemon = function(pokemon) {
Pokemon.getAll = result => { Pokemon.getAll = result => {
sql.query("SELECT * FROM pokemon WHERE id_pokemon < 175", (err, res) => { sql.query("SELECT * FROM pokemon", (err, res) => {
if (err) { if (err) {
console.log("error: ", err); console.log("error: ", err);
result(null, err); result(null, err);
......
#refuse{
background-color: rgb(219, 54, 54);
border-color: rgb(219, 54, 54);
color: beige;
}
\ No newline at end of file
...@@ -7,11 +7,11 @@ ...@@ -7,11 +7,11 @@
<div class="col-md-8 px-3"> <div class="col-md-8 px-3">
<div class="card-block px-3"> <div class="card-block px-3">
<h4 class="card-title">{{partie.pseudo}}</h4> <h4 class="card-title">{{partie.pseudo}}</h4>
<p class="card-text">hey !</p> <p class="card-text" *ngIf="partie.joueurTour==uid">It's your turn to play !</p>
</div> </div>
</div> </div>
<div class="col-md-4"> <div class="col-md-4">
<a class="btn btn-primary" id="remove" [routerLink]="['/battle', partie.id_partie, partie.joueurTour, partie.pseudo]" skipLocationChange>Continue</a> <button [ngClass]="partie.joueurTour==uid ? 'btn btn-success':'btn btn-primary'" id="remove" [routerLink]="['/battle', partie.id_partie, partie.joueurTour, partie.pseudo]" skipLocationChange>Continue</button>
</div> </div>
</div> </div>
</div> </div>
...@@ -29,8 +29,8 @@ ...@@ -29,8 +29,8 @@
</div> </div>
</div> </div>
<div class="col-md-4"> <div class="col-md-4">
<a class="btn btn-primary" id="accept" (click)="acceptChallenge(partie, i)">Accept</a> <button class="btn btn-primary" id="accept" (click)="acceptChallenge(partie, i)">Accept</button >
<a class="btn btn-primary" id="refuse" (click)="refuseChallenge(partie.id_partie, i)">Refuse</a> <button class="btn btn-success" id="refuse" (click)="refuseChallenge(partie.id_partie, i)">Refuse</button>
</div> </div>
</div> </div>
</div> </div>
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
</div> </div>
<div class="top"> <div class="top">
<div class="pokeballs"> <div class="pokeballs">
<div class="pokeball" *ngFor="let poke of selfTeam"></div> <div [ngClass]="poke.stats[0].stat_value>0 ? 'pokeball pokeballAlive' : 'pokeball pokeballKo'" *ngFor="let poke of enemyTeam"></div>
</div> </div>
</div> </div>
</div> </div>
......
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute, Router } from '@angular/router';
import { ChatService } from '../chat.service'; import { ChatService } from '../chat.service';
@Component({ @Component({
selector: 'app-battle', selector: 'app-battle',
...@@ -35,6 +35,8 @@ export class BattleComponent implements OnInit { ...@@ -35,6 +35,8 @@ export class BattleComponent implements OnInit {
enemySpecDEf: number; enemySpecDEf: number;
enemyAtk: number; enemyAtk: number;
enemyAffichageHp: number = 0; enemyAffichageHp: number = 0;
tempPoke: any; //to alternate between two pokemon when death
oldPoke: any; //to deactivate when death
enemyPseudo: string; enemyPseudo: string;
endgame: boolean = false; endgame: boolean = false;
...@@ -52,7 +54,7 @@ export class BattleComponent implements OnInit { ...@@ -52,7 +54,7 @@ export class BattleComponent implements OnInit {
curPokeIndex: number; curPokeIndex: number;
curEnemyPokeIndex: number; curEnemyPokeIndex: number;
constructor(private route: ActivatedRoute, private httpClient: HttpClient, private chatService : ChatService) { } constructor(private router: Router, private route: ActivatedRoute, private httpClient: HttpClient, private chatService : ChatService) { }
startup(){ startup(){
this.selfTeam.forEach( (el, i) => { this.selfTeam.forEach( (el, i) => {
...@@ -97,9 +99,10 @@ export class BattleComponent implements OnInit { ...@@ -97,9 +99,10 @@ export class BattleComponent implements OnInit {
getNextEnemyPokemon(){ getNextEnemyPokemon(){
this.endgame = true; this.endgame = true;
this.oldPoke = this.enemyPokemon
this.enemyTeam.forEach(el => { this.enemyTeam.forEach(el => {
if(el.stats[0].stat_value > 0) { if(el.stats[0].stat_value > 0) {
this.enemyPokemon = el; this.tempPoke = el;
this.endgame = false; this.endgame = false;
} }
else this.endgame = this.endgame && true; else this.endgame = this.endgame && true;
...@@ -110,16 +113,28 @@ export class BattleComponent implements OnInit { ...@@ -110,16 +113,28 @@ export class BattleComponent implements OnInit {
setTimeout(() => this.boxMessage = "You won !", 3000); setTimeout(() => this.boxMessage = "You won !", 3000);
return false; return false;
} else{ } else{
this.enemyCurHealth = this.enemyPokemon.stats[0].stat_value; this.selfKOMessage(this.tempPoke);
this.enemyAtk = this.enemyPokemon.stats[1].stat_value;
this.enemyDef = this.enemyPokemon.stats[2].stat_value;
this.enemyType = this.enemyPokemon.types[0].id_type;
this.enemyMaxHealth = this.enemyPokemon.base_stats[0].base_stat;
this.enemyAffichageHp = this.healthUpdate(this.enemyCurHealth, this.enemyMaxHealth);
return true; return true;
} }
} }
selfKOMessage(tempPoke){
setTimeout(() => this.boxMessage = this.enemyPokemon.pokeName + " is K.O. !", 2000);
setTimeout(() => this.boxMessage = this.enemyPseudo + " summons " + tempPoke.pokeName + " !", 4000);
setTimeout(() => this.affectEnemyStats(tempPoke), 5000);
setTimeout(() => this.boxMessage = this.waitingMsg, 7000);
}
affectEnemyStats(tempPoke){
this.enemyPokemon = tempPoke;
this.enemyCurHealth = this.enemyPokemon.stats[0].stat_value;
this.enemyAtk = this.enemyPokemon.stats[1].stat_value;
this.enemyDef = this.enemyPokemon.stats[2].stat_value;
this.enemyType = this.enemyPokemon.types[0].id_type;
this.enemyMaxHealth = this.enemyPokemon.base_stats[0].base_stat;
this.enemyAffichageHp = this.healthUpdate(this.enemyCurHealth, this.enemyMaxHealth);
}
healthUpdate(currentHealth, maxHealth){ healthUpdate(currentHealth, maxHealth){
return (100*currentHealth)/maxHealth; return (100*currentHealth)/maxHealth;
} }
...@@ -128,6 +143,7 @@ export class BattleComponent implements OnInit { ...@@ -128,6 +143,7 @@ export class BattleComponent implements OnInit {
this.curHealthSelf -= dmg; this.curHealthSelf -= dmg;
if(this.curHealthSelf < 0) this.curHealthSelf = 0; if(this.curHealthSelf < 0) this.curHealthSelf = 0;
this.affichageHp = this.healthUpdate(this.curHealthSelf, this.maxHealth); this.affichageHp = this.healthUpdate(this.curHealthSelf, this.maxHealth);
this.updatePokemonJSON();
} }
applyDamageOpponent(dmg){ applyDamageOpponent(dmg){
...@@ -153,12 +169,11 @@ export class BattleComponent implements OnInit { ...@@ -153,12 +169,11 @@ export class BattleComponent implements OnInit {
typeAdv(move){ typeAdv(move){
console.log(move); console.log(move);
this.dmgClasse = move.id_dmg_classe;
this.httpClient.post("http://localhost:3000/geteff", { typeNous: move.m_id_type, typeEux: this.enemyType}, {responseType: 'json', withCredentials: true}).subscribe((res:any) => { this.httpClient.post("http://localhost:3000/geteff", { typeNous: move.m_id_type, typeEux: this.enemyType}, {responseType: 'json', withCredentials: true}).subscribe((res:any) => {
console.log(res); console.log(res);
this.moveToSend = move; this.moveToSend = move;
this.boxMessage = this.curPoke.pokeName + " used " + move.identifier + " !"; this.boxMessage = this.curPoke.pokeName + " used " + move.identifier + " !";
setTimeout(() => this.boxMessage = this.waitingMsg, 1000); this.dmgClasse = move.id_dmg_classe;
this.dmgToSend = this.calculeDmg(res.body[0].damage_factor, move.power); this.dmgToSend = this.calculeDmg(res.body[0].damage_factor, move.power);
move.pp--; move.pp--;
this.yourTurn = false; this.yourTurn = false;
...@@ -166,25 +181,18 @@ export class BattleComponent implements OnInit { ...@@ -166,25 +181,18 @@ export class BattleComponent implements OnInit {
} }
updateDBPokemon(death){ updateDBPokemon(death){
this.enemyPokemon.stats[0].stat_value = this.enemyCurHealth; this.updatePokemonJSON()
this.enemyPokemon.stats[1].stat_value = this.enemyAtk;
this.enemyPokemon.stats[2].stat_value = this.enemyDef;
this.curPoke.stats[0].stat_value = this.curHealthSelf;
this.curPoke.stats[1].stat_value = this.atk;
this.curPoke.stats[2].stat_value = this.def;
this.selfTeam[this.curPokeIndex] = this.curPoke;
this.enemyTeam[this.curEnemyPokeIndex] = this.enemyPokemon;
if(death){ if(death){
this.enemyPokemon.active = 0; this.enemyPokemon.active = 0;
if(this.getNextEnemyPokemon()){ if(this.getNextEnemyPokemon()){
console.log("death note"); console.log("death note");
this.httpClient.post("http://localhost:3000/updatepartie", {nextPokemon: this.enemyPokemon.id_etat, pokemon1: this.curPoke, pokemon2: this.enemyPokemon, id_partie: this.id_partie, winner: undefined}, {responseType: 'json', withCredentials: true}).subscribe((res:any) => { this.httpClient.post("http://localhost:3000/updatepartie", {nextPokemon: this.tempPoke.id_etat, pokemon1: this.curPoke, pokemon2: this.oldPoke, id_partie: this.id_partie, winner: undefined}, {responseType: 'json', withCredentials: true}).subscribe((res:any) => {
console.log(res); console.log(res);
this.chatService.sendBattleExchcange(this.enemyPseudo, {move: this.moveToSend, dmg: this.dmgToSend, nextPokemon: this.enemyPokemon, endgame: false, sender: this.pseudo}); this.chatService.sendBattleExchcange(this.enemyPseudo, {move: this.moveToSend, dmg: this.dmgToSend, nextPokemon: this.tempPoke, endgame: false, sender: this.pseudo});
}); });
} else{ } else{
console.log("winning note"); console.log("winning note");
this.httpClient.post("http://localhost:3000/updatepartie", {nextPokemon: this.enemyPokemon.id_etat, pokemon1: this.curPoke, pokemon2: this.enemyPokemon, id_partie: this.id_partie, winner: this.uid}, {responseType: 'json', withCredentials: true}).subscribe((res:any) => { this.httpClient.post("http://localhost:3000/updatepartie", {nextPokemon: -1, pokemon1: this.curPoke, pokemon2: this.oldPoke, id_partie: this.id_partie, winner: this.uid}, {responseType: 'json', withCredentials: true}).subscribe((res:any) => {
console.log(res); console.log(res);
this.chatService.sendBattleExchcange(this.enemyPseudo, {move: this.moveToSend, dmg: this.dmgToSend, nextPokemon: this.enemyPokemon, endgame: true, sender: this.pseudo}); this.chatService.sendBattleExchcange(this.enemyPseudo, {move: this.moveToSend, dmg: this.dmgToSend, nextPokemon: this.enemyPokemon, endgame: true, sender: this.pseudo});
}); });
...@@ -195,16 +203,29 @@ export class BattleComponent implements OnInit { ...@@ -195,16 +203,29 @@ export class BattleComponent implements OnInit {
console.log(res); console.log(res);
this.chatService.sendBattleExchcange(this.enemyPseudo, {move: this.moveToSend, dmg: this.dmgToSend, nextPokemon: undefined, endgame: false, sender: this.pseudo}); this.chatService.sendBattleExchcange(this.enemyPseudo, {move: this.moveToSend, dmg: this.dmgToSend, nextPokemon: undefined, endgame: false, sender: this.pseudo});
}); });
setTimeout(() => this.boxMessage = this.waitingMsg, 1000);
} }
} }
updatePokemonJSON(){
this.enemyPokemon.stats[0].stat_value = this.enemyCurHealth;
this.enemyPokemon.stats[1].stat_value = this.enemyAtk;
this.enemyPokemon.stats[2].stat_value = this.enemyDef;
this.curPoke.stats[0].stat_value = this.curHealthSelf;
this.curPoke.stats[1].stat_value = this.atk;
this.curPoke.stats[2].stat_value = this.def;
this.selfTeam[this.curPokeIndex] = this.curPoke;
this.enemyTeam[this.curEnemyPokeIndex] = this.enemyPokemon;
}
calculeDmg(eff, power){ calculeDmg(eff, power){
console.log(eff, power); console.log(eff, power, this.dmgClasse);
console.log(this.enemyDef + " " + this.atk); console.log(this.enemyDef + " " + this.atk);
if(this.dmgClasse == 2){ if(this.dmgClasse == 2){
let dmg = Math.round(((((0.4*this.level+2)*this.atk*power)/(50*this.enemyDef))+2)*(eff/100)*(Math.random()*(1 - 0.85)+ 0.85)); let dmg = Math.round(((((0.4*this.level+2)*this.atk*power)/(50*this.enemyDef))+2)*(eff/100)*(Math.random()*(1 - 0.85)+ 0.85));
this.applyDamageOpponent(dmg); console.log(this.level, this.atk, power, this.enemyDef, )
console.log(dmg + " " + this.enemyCurHealth); console.log(dmg + " " + this.enemyCurHealth);
this.applyDamageOpponent(dmg);
return dmg; return dmg;
} else if (this.dmgClasse == 3){ } else if (this.dmgClasse == 3){
let dmg = Math.round(((((0.4*this.level+2)*this.atkspec*power)/(50*this.enemySpecDEf))+2)*(eff/100)*(Math.random()*(1 - 0.85)+ 0.85)); let dmg = Math.round(((((0.4*this.level+2)*this.atkspec*power)/(50*this.enemySpecDEf))+2)*(eff/100)*(Math.random()*(1 - 0.85)+ 0.85));
......
#remove{
background-color: rgb(219, 54, 54);
border-color: rgb(219, 54, 54);
color: beige;
}
.dropydown{ .dropydown{
overflow-y: scroll; overflow-y: scroll;
height: 10rem; height: 10rem;
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
</div> </div>
</div> </div>
<div class="col-md-4"> <div class="col-md-4">
<a class="btn btn-primary" id="remove" (click)="removePokemon(pokemon)">Remove</a> <button type="button" class="btn btn-danger" (click)="removePokemon(pokemon)">Remove</button >
</div> </div>
</div> </div>
</div> </div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment