Commit 8595b56a authored by Manish Kumar's avatar Manish Kumar
Browse files

preparing db switch

parent 1ae17883
......@@ -54,12 +54,20 @@ exports.accept = (req, res) => {
exports.getCombatTeam = (req, res) => {
if (req.session.loggedIn) {
Partie.getCombatTeam(req.body.id_partie, req.session.uid, (err, data) => {
Partie.getCombatTeam(req.body.id_partie, req.session.uid, (err, data1) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while getting combat team."
});
else res.send(data);
else {
Partie.getCombatEnemyTeam(req.body.id_partie, req.session.uid, (err, data2) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while getting enemy combat team."
});
else res.send({self : data1, enemy: data2});
});
}
});
}
};
......@@ -67,12 +75,28 @@ exports.getCombatTeam = (req, res) => {
exports.updatePartie = (req, res) => {
console.log(req.body);
if (req.session.loggedIn) {
Partie.updatePartie(req.body.id_etat, req.body.active, req.body.stats, req.body.moves, (err, data) => {
Partie.updatePartieTour(req.body.id_partie, (err, data1) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while updating pokemon state."
});
else res.send(data);
else {
Partie.updatePartie(req.body.pokemon1.id_etat, req.body.pokemon1.active, req.body.pokemon1.stats, req.body.pokemon1.moves, (err, data2) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while updating pokemon state."
});
else {
Partie.updatePartie(req.body.pokemon2.id_etat, req.body.pokemon2.active, req.body.pokemon2.stats, req.body.pokemon2.moves, (err, data3) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while updating pokemon state."
});
else res.send(data3);
});
}
});
}
});
}
};
......@@ -99,4 +123,14 @@ exports.eff = (req, res) => {
else res.send(data);
});
}
};
exports.getTables = (req, res) => {
Partie.getTables((err, data) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while accepting the challenge."
});
else res.send(data);
});
};
\ No newline at end of file
......@@ -64,7 +64,8 @@ Partie.accept = (id_partie, result) => {
result(err, null);
return;
}
sql.query('INSERT INTO etatStats (id_etat, stat_id, stat_value, effort) SELECT ep.id_etat ,s.stat_id, s.base_stat, s.effort FROM etatPoke as ep, team as t, pokemon as p, stats as s WHERE ep.id_partie = ? AND ep.id_team=t.id_team AND t.id_pokemon=p.id_pokemon AND s.pokemon_id=p.id_pokemon;', [id_partie], function(err, result) {
sql.query('UPDATE etatPoke SET active=1 WHERE id_etat=(SELECT id_etat FROM etatPoke as ep, partie as p, team as t WHERE ep.id_partie=p.id_partie AND ep.team_id=t.team_id AND p.id_joueur1=t.id_joueur ORDER BY t.team_id LIMIT 1) OR id_etat=(SELECT id_etat FROM etatPoke as ep, partie as p, team as t WHERE ep.id_partie=p.id_partie AND ep.team_id=t.team_id AND p.id_joueur2=t.id_joueur ORDER BY t.team_id LIMIT 1)', [id_partie, id_partie], function(err, result) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
......@@ -74,7 +75,7 @@ Partie.accept = (id_partie, result) => {
return;
}
sql.query('INSERT INTO etatMove (id_etat, id_move, pp) SELECT ep.id_etat, mp.id_move, m.pp FROM etatPoke as ep, movePoss as mp, move as m, team, partie WHERE partie.id_partie = ? AND ep.id_partie = partie.id_partie AND ep.id_team=team.id_team AND mp.id_team=team.id_team AND mp.id_move=m.id_move;', [id_partie], function(err, result) {
sql.query('INSERT INTO etatStats (id_etat, stat_id, stat_value, effort) SELECT ep.id_etat ,s.stat_id, s.base_stat, s.effort FROM etatPoke as ep, team as t, pokemon as p, stats as s WHERE ep.id_partie = ? AND ep.id_team=t.id_team AND t.id_pokemon=p.id_pokemon AND s.pokemon_id=p.id_pokemon;', [id_partie], function(err, result) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
......@@ -83,7 +84,8 @@ Partie.accept = (id_partie, result) => {
result(err, null);
return;
}
sql.commit(function(err) {
sql.query('INSERT INTO etatMove (id_etat, id_move, pp) SELECT ep.id_etat, mp.id_move, m.pp FROM etatPoke as ep, movePoss as mp, move as m, team, partie WHERE partie.id_partie = ? AND ep.id_partie = partie.id_partie AND ep.id_team=team.id_team AND mp.id_team=team.id_team AND mp.id_move=m.id_move;', [id_partie], function(err, result) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
......@@ -92,8 +94,18 @@ Partie.accept = (id_partie, result) => {
result(err, null);
return;
}
console.log('Transaction Complete !!!!');
//sql.end();
sql.commit(function(err) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
console.log('Transaction Complete !!!!');
//sql.end();
});
});
});
});
......@@ -130,6 +142,37 @@ Partie.getCombatTeam = (id_partie, id_joueur, result) => {
});
}
Partie.getCombatEnemyTeam = (id_partie, id_joueur, result) => {
sql.query("SELECT DISTINCT p.id_pokemon, p.pokeName, ep.id_etat, ep.active, typePoke.id_type, typePoke.nomType as typeNom, gs.id, gs.identifier as statName, es.stat_value, es.effort, s.stat_id as baseStat_id, gsBase.identifier as baseStatName, s.base_stat, m.id_move, m.identifier, m.power, m.accuracy, typeMove.id_type as m_id_type, typeMove.nomType, em.pp, dc.id_dmg_classe, dc.identifier as dmg_classe FROM etatPoke as ep, etatStats as es, etatMove as em, team as t, joueur as j, pokemon as p, move as m, genState as gs, type as typePoke, type as typeMove, pokemon_type as pty, stats as s, genState as gsBase, damage_classe as dc WHERE ep.id_partie=? AND ep.id_team=t.id_team AND t.id_joueur != ? AND ep.id_etat=es.id_etat AND ep.id_etat=em.id_etat AND t.id_pokemon=p.id_pokemon AND m.id_move=em.id_move AND gs.id=es.stat_id AND p.id_pokemon=pty.pokemon_id AND pty.type_id=typePoke.id_type AND m.type_id=typeMove.id_type AND p.id_pokemon=s.pokemon_id AND gsBase.id=s.stat_id AND m.damage_class_id=dc.id_dmg_classe ORDER BY p.id_pokemon, gs.id, s.stat_id", [id_partie, id_joueur], (err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
if (res.length) {
var filteredData = JSONmanip.filterJSONCombat(res);
result(null, filteredData);
return;
}
// not found combat team with the id
result({ kind: "not_found" }, null);
});
}
Partie.updatePartieTour = (id_partie, result) => { ////////////??????????????
sql.query('UPDATE partie SET nbTours=nbTours+1 joueurTour= CASE WHEN joueurTour = id_joueur1 THEN id_joueur2 WHEN joueurTour = id_joueur2 THEN id_joueur1 ELSE joueurTour END WHERE id_partie=?', id_partie, function(err, res) {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
// not found combat team with the id
result(null, { res: true, id: res.insertId });
});
}
Partie.updatePartie = (id_etat, active, stats, moves, result) => {
sql.beginTransaction(function(errTransaction) {
if (errTransaction) { throw err; }
......@@ -209,4 +252,17 @@ Partie.efficacite = (typeNous, typeEux, result) => {
};
Partie.getTables = result => {
sql.query("show tables", (err, res) => {
if (err) {
console.log("error: ", err);
result(null, err);
return;
}
//console.log("all good, here is the first Pokemon: ", res[0]);
result(null, res);
});
};
module.exports = Partie;
\ No newline at end of file
......@@ -16,4 +16,7 @@ module.exports = app => {
app.post("/updatepartie", partie.updatePartie);
//////////
app.get("/gettables", partie.getTables);
};
\ No newline at end of file
......@@ -69,6 +69,11 @@ module.exports.listen = function(app) {
socket.on('battle me', (challenger, guest) => {
console.log(challenger + " " + guest + " " + usersMap.get(guest));
io.to(usersMap.get(guest)).emit('demande combat', challenger);
})
});
socket.on('challenge accepted', (guest, challenger) => {
console.log(challenger + " " + guest + " " + usersMap.get(guest));
io.to(usersMap.get(challenger)).emit('challenge accepted', guest);
});
});
};
\ No newline at end of file
......@@ -6,7 +6,7 @@
<div class="row ">
<div class="col-md-8 px-3">
<div class="card-block px-3">
<h4 class="card-title">{{partie.pseudo}}</h4>
<h4 class="card-title">{{partie.pseudo}} {{partie.id_partie}}</h4>
<p class="card-text">hey !</p>
</div>
</div>
......@@ -29,7 +29,7 @@
</div>
</div>
<div class="col-md-4">
<a class="btn btn-primary" id="accept" (click)="acceptChallenge(partie.id_partie, i)">Accept</a>
<a class="btn btn-primary" id="accept" (click)="acceptChallenge(partie, i)">Accept</a>
<a class="btn btn-primary" id="refuse" (click)="refuseChallenge(partie.id_partie, i)">Refuse</a>
</div>
</div>
......
import { Component, OnInit } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { __assign } from 'tslib';
import { ChatService } from "../chat.service";
@Component({
selector: 'app-battle-menu',
......@@ -17,7 +18,7 @@ export class BattleMenuComponent implements OnInit {
demandesExt: any = [];
demandesInt: any = [];
constructor(private http: HttpClient){}
constructor(private chatservice: ChatService, private http: HttpClient){}
ngOnInit() {
this.http.get('http://localhost:3000/checkLogin', { responseType: 'json', withCredentials: true}).subscribe( (res: any) => {
......@@ -55,15 +56,16 @@ export class BattleMenuComponent implements OnInit {
}
}
acceptChallenge(id_partie, i){
acceptChallenge(partie, i){
const headers = new HttpHeaders()
.set('Content-Type', 'application/json');
this.http.post('http://localhost:3000/acceptpartie', {id_partie: id_partie}, { headers: headers, withCredentials: true}).subscribe(
this.http.post('http://localhost:3000/acceptpartie', {id_partie: partie.id_partie}, { headers: headers, withCredentials: true}).subscribe(
(response: any) => {
console.log(response);
if(response.res) {
this.curParties.push(this.demandesExt[i]);
this.demandesExt.splice(i, 1);
this.chatservice.challengeAccepted(partie.id_joueur1)
}
},
(error) => console.log(error)
......
......@@ -30,6 +30,7 @@ export class BattleComponent implements OnInit {
curPoke: any = {};
affichageHp: number = 0;
user: any;
pokemonEnemy: any = [];
constructor(private route: ActivatedRoute, private httpClient: HttpClient, private chatService : ChatService) { }
......@@ -63,7 +64,7 @@ export class BattleComponent implements OnInit {
getCombatTeam(){
this.httpClient.post("http://localhost:3000/getCombatTeam", {id_partie: this.id_partie}, { responseType: 'json', withCredentials: true }).subscribe(
(res) => {this.selfTeam = res; this.getPokemon(), console.log(res)});
(res: any) => {this.selfTeam = res.self; this.pokemonEnemy = res.enemy; this.getPokemon(), console.log(res)});
}
typeAdv(move){
......@@ -71,7 +72,7 @@ export class BattleComponent implements OnInit {
}
updateDBPokemon(){
this.httpClient.post("http://localhost:3000/updatepartie", this.curPoke, {responseType: 'json', withCredentials: true}).subscribe((res:any) => (console.log(res)));
this.httpClient.post("http://localhost:3000/updatepartie", {pokemon1: this.curPoke, pokemon2: this.pokemonEnemy, id_partie: this.id_partie}, {responseType: 'json', withCredentials: true}).subscribe((res:any) => (console.log(res)));
}
calculeDmg(eff, power){
......
......@@ -33,6 +33,10 @@ export class ChatService {
this.notifier.notify("success", challenger + " wants to battle !");
console.log("challenge from : " + challenger);
});
this.recevoirChallengeAccepted().subscribe((guest: string) =>{
this.notifier.notify("success", guest + " accepted challenge !");
console.log("challenge accepted by : " + guest);
});
}
private onlineUsers(){
......@@ -101,17 +105,28 @@ export class ChatService {
}
}
// public challengeAccepted(guest){
// if(this.pseudo == undefined){
// this.notifier.notify("warning", "You must login to battle !");
// return false;
// } else{
// this.socket.emit('challenge accepted', this.pseudo, guest); // nouvel événement .....
// this.notifier.notify("success", "Challenge sent !");
// console.log("challenge sent");
// return true;
// }
// }
public challengeAccepted(challenger){
if(this.pseudo == undefined){
this.notifier.notify("warning", "You must login to battle !");
return false;
} else{
this.socket.emit('challenge accepted', this.pseudo, challenger); // nouvel événement .....
this.notifier.notify("success", "Challenge sent !");
console.log("challenge sent");
return true;
}
}
public recevoirChallengeAccepted(){
let observable = new Observable(observer => {
this.socket.on('challenge accepted', (data) => {
console.log("challenge accepted " + data);
observer.next(data);
});
return () => {this.socket.disconnect();};
})
return observable;
}
public recevoirChallenge(){
let observable = new Observable(observer => {
......
Markdown is supported
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