Commit fb8b1c15 authored by Manish Kumar's avatar Manish Kumar
Browse files

Working battle with sockets :DDDDDD

parent 92287d25
......@@ -69,13 +69,15 @@ exports.getCombatTeam = (req, res) => {
});
}
});
} else{
res.send({res: false, body: "blaa"});
}
};
exports.updatePartie = (req, res) => {
console.log(req.body);
if (req.session.loggedIn) {
Partie.updatePartieTour(req.body.id_partie, (err, data1) => {
Partie.updatePartieTour(req.body.id_partie, req.body.winner, (err, data1) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while updating pokemon state."
......@@ -92,7 +94,18 @@ exports.updatePartie = (req, res) => {
res.status(500).send({
message: err.message || "Some error occurred while updating pokemon state."
});
else res.send(data3);
else if(req.body.nextPokemon != -1){
Partie.activateNextPokemon(req.body.nextPokemon, (err, data4) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while updating pokemon state."
});
res.send(data4);
});
} else{
res.send(data3)
}
});
}
});
......
......@@ -2,27 +2,31 @@ const Pokemon = require("../models/pokemon.model.js");
exports.create = (req, res) => {
// Validate request
if (!req.body) {
res.status(400).send({
message: "Content can not be empty!"
});
}
if (req.session.loggedIn) {
if (!req.body) {
res.status(400).send({
message: "Content can not be empty!"
});
}
// Create a team pokemon
const pokemon = new Pokemon({
id_pokemon: req.body.id_pokemon,
id_joueur: req.session.uid,
courant: 1
});
// Create a team pokemon
const pokemon = new Pokemon({
id_pokemon: req.body.id_pokemon,
id_joueur: req.session.uid,
courant: 1
});
// add pokemon to team in the database
Pokemon.create(pokemon, (err, data) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while creating the Pokemon."
});
else res.send(data);
});
// add pokemon to team in the database
Pokemon.create(pokemon, (err, data) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while creating the Pokemon."
});
else res.send(data);
});
} else{
res.send({res: false, body: "Please login to use this feature !"});
}
};
exports.findAll = (req, res) => {
......
......@@ -42,7 +42,7 @@ Partie.getDemandes = (uid, result) => {
});
};
Partie.accept = (id_partie, result) => {
Partie.accept = (id_partie, resultGlobal) => {
sql.beginTransaction(function(errTransaction) {
if (errTransaction) { throw err; }
sql.query('UPDATE partie SET nbTours=1 WHERE id_partie=?', id_partie, function(err, res) {
......@@ -51,47 +51,47 @@ Partie.accept = (id_partie, result) => {
sql.rollback(function() {
throw err;
});
result(err, null);
resultGlobal(err, null);
return;
}
//INSERT INTO etatPoke (hp, def, atk, active, id_partie, id_team) SELECT p.base_hp, p.base_defense, p.base_attack, 0, 3, team.id_team FROM pokemon as p, partie as pa, team, joueur WHERE pa.id_partie = 3 AND joueur.id_joueur=pa.id_joueur1 AND team.id_joueur=joueur.id_joueur AND p.id_pokemon=team.id_pokemon AND team.courant = 1;
sql.query('INSERT INTO etatPoke (active, id_partie, id_team) SELECT 0, ?, team.id_team FROM pokemon as p, partie as pa, team, joueur WHERE pa.id_partie = ? AND (joueur.id_joueur=pa.id_joueur1 OR joueur.id_joueur=pa.id_joueur2) AND team.id_joueur=joueur.id_joueur AND p.id_pokemon=team.id_pokemon AND team.courant = 1;', [id_partie, id_partie], function(err, result) {
//INSERT INTO etatpoke (hp, def, atk, active, id_partie, id_team) SELECT p.base_hp, p.base_defense, p.base_attack, 0, 3, team.id_team FROM pokemon as p, partie as pa, team, joueur WHERE pa.id_partie = 3 AND joueur.id_joueur=pa.id_joueur1 AND team.id_joueur=joueur.id_joueur AND p.id_pokemon=team.id_pokemon AND team.courant = 1;
sql.query('INSERT INTO etatpoke (active, id_partie, id_team) SELECT 0, ?, team.id_team FROM pokemon as p, partie as pa, team, joueur WHERE pa.id_partie = ? AND (joueur.id_joueur=pa.id_joueur1 OR joueur.id_joueur=pa.id_joueur2) AND team.id_joueur=joueur.id_joueur AND p.id_pokemon=team.id_pokemon AND team.courant = 1;', [id_partie, id_partie], function(err, result) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
resultGlobal(err, null);
return;
}
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) {
sql.query('UPDATE etatpoke SET active=1 WHERE id_etat=(SELECT id_etat FROM etatpoke as ep, partie as p, team as t WHERE p.id_partie=? AND ep.id_partie=p.id_partie AND ep.id_team=t.id_team AND p.id_joueur1=t.id_joueur ORDER BY t.id_team LIMIT 1) OR id_etat=(SELECT id_etat FROM etatpoke as ep, partie as p, team as t WHERE p.id_partie=? AND ep.id_partie=p.id_partie AND ep.id_team=t.id_team AND p.id_joueur2=t.id_joueur ORDER BY t.id_team LIMIT 1) ', [id_partie, id_partie], function(err, result) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
resultGlobal(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('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() {
throw err;
});
result(err, null);
resultGlobal(err, null);
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 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() {
throw err;
});
result(err, null);
resultGlobal(err, null);
return;
}
sql.commit(function(err) {
......@@ -100,7 +100,7 @@ Partie.accept = (id_partie, result) => {
sql.rollback(function() {
throw err;
});
result(err, null);
resultGlobal(err, null);
return;
}
console.log('Transaction Complete !!!!');
......@@ -110,7 +110,7 @@ Partie.accept = (id_partie, result) => {
});
});
});
result(null, { res: true, id: res.insertId });
resultGlobal(null, { res: true, id: res.insertId });
});
});
// sql.query("UPDATE partie SET nbTours=1 WHERE id_partie=?", [id_partie], (err, res) => {
......@@ -124,7 +124,7 @@ Partie.accept = (id_partie, result) => {
};
Partie.getCombatTeam = (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) => {
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);
......@@ -143,7 +143,7 @@ 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) => {
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);
......@@ -161,22 +161,34 @@ Partie.getCombatEnemyTeam = (id_partie, id_joueur, result) => {
});
}
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.updatePartieTour = (id_partie, winner, result) => { ////////////??????????????
if(winner != undefined){
sql.query('UPDATE partie SET nbTours=nbTours+1, winner=?, joueurTour = (CASE WHEN joueurTour = id_joueur1 THEN id_joueur2 WHEN joueurTour = id_joueur2 THEN id_joueur1 ELSE joueurTour END) WHERE id_partie=?', [winner, 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 });
});
} else{
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; }
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) {
console.log("error: ", err);
sql.rollback(function() {
......@@ -191,7 +203,7 @@ Partie.updatePartie = (id_etat, active, stats, moves, result) => {
sqlStats.push(id_etat, stat.id, stat.stat_value);
});
sql.query('INSERT INTO etatStats (id_etat, stat_id, stat_value, effort) VALUES (?, ?, ?, 0), (?, ?, ?, 0), (?, ?, ?, 0), (?, ?, ?, 0), (?, ?, ?, 0), (?, ?, ?, 0) ON DUPLICATE KEY UPDATE id_etat=VALUES(id_etat), stat_id=VALUES(stat_id), stat_value=VALUES(stat_value), effort=VALUES(effort);', sqlStats, function(err, res) {
sql.query('INSERT INTO etatstats (id_etat, stat_id, stat_value, effort) VALUES (?, ?, ?, 0), (?, ?, ?, 0), (?, ?, ?, 0), (?, ?, ?, 0), (?, ?, ?, 0), (?, ?, ?, 0) ON DUPLICATE KEY UPDATE id_etat=VALUES(id_etat), stat_id=VALUES(stat_id), stat_value=VALUES(stat_value), effort=VALUES(effort);', sqlStats, function(err, res) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
......@@ -206,7 +218,7 @@ Partie.updatePartie = (id_etat, active, stats, moves, result) => {
sqlMoves.push(id_etat, move.id_move, move.pp);
});
sql.query('INSERT INTO etatMove (id_etat, id_move, pp) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?) ON DUPLICATE KEY UPDATE id_etat=VALUES(id_etat), id_move=VALUES(id_move), pp=VALUES(pp);', sqlMoves, function(err, res) {
sql.query('INSERT INTO etatmove (id_etat, id_move, pp) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?), (?, ?, ?) ON DUPLICATE KEY UPDATE id_etat=VALUES(id_etat), id_move=VALUES(id_move), pp=VALUES(pp);', sqlMoves, function(err, res) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
......@@ -223,6 +235,21 @@ Partie.updatePartie = (id_etat, active, stats, moves, result) => {
});
}
Partie.activateNextPokemon = (id_etat, result) => {
sql.query('UPDATE etatpoke SET active=1 WHERE id_etat=?', [id_etat], function(err, res) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
result(null, res);
});
}
Partie.refuse = (id_partie, result) => {
sql.query("DELETE FROM partie WHERE id_partie=?", [id_partie], (err, res) => {
if (err) {
......
......@@ -25,7 +25,7 @@ Pokemon.getAll = result => {
// Pokemon.getTeamUtil = (joueur_id, result) => {
// sql.query('SELECT p.*, m.* FROM pokemon as p, team, moveUtil as mu, move as m WHERE id_joueur= ? AND p.id_pokemon=team.id_pokemon AND mu.move_id=m.id_move AND mu.pokemon_id=p.id_pokemon;', [joueur_id], (err, res) => {
// sql.query('SELECT p.*, m.* FROM pokemon as p, team, moveutil as mu, move as m WHERE id_joueur= ? AND p.id_pokemon=team.id_pokemon AND mu.move_id=m.id_move AND mu.pokemon_id=p.id_pokemon;', [joueur_id], (err, res) => {
// if (err) {
// console.log("error: ", err);
// result(err, null);
......@@ -44,10 +44,10 @@ Pokemon.getAll = result => {
// };
Pokemon.getTeamUtil = (joueur_id, result) => {
//BEGIN; INSERT INTO team (courant ,id_joueur, id_pokemon) VALUES(1, 0, 39); INSERT INTO movePoss (id_move, id_team) SELECT mu.move_id, LAST_INSERT_ID() FROM moveUtil as mu WHERE mu.pokemon_id=39 LIMIT 4; COMMIT;
//BEGIN; INSERT INTO team (courant ,id_joueur, id_pokemon) VALUES(1, 0, 39); INSERT INTO moveposs (id_move, id_team) SELECT mu.move_id, LAST_INSERT_ID() FROM moveutil as mu WHERE mu.pokemon_id=39 LIMIT 4; COMMIT;
sql.beginTransaction(function(err) {
if (err) { throw err; }
sql.query('SELECT team.id_team, p.*, m.* FROM pokemon as p, team, moveUtil as mu, move as m WHERE id_joueur= ? AND p.id_pokemon=team.id_pokemon AND mu.move_id=m.id_move AND mu.pokemon_id=p.id_pokemon ORDER BY team.id_team;', [joueur_id], (err, res1) => {
sql.query('SELECT team.id_team, p.*, m.* FROM pokemon as p, team, moveutil as mu, move as m WHERE id_joueur= ? AND p.id_pokemon=team.id_pokemon AND mu.move_id=m.id_move AND mu.pokemon_id=p.id_pokemon ORDER BY team.id_team;', [joueur_id], (err, res1) => {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
......@@ -63,10 +63,10 @@ Pokemon.getTeamUtil = (joueur_id, result) => {
return;
}
console.log(res1);
//console.log(res1);
var dataUtil = JSONmanip.filterJSON(res1, true);
sql.query('SELECT t.id_team, p.*, m.* FROM pokemon as p, movePoss as mp INNER JOIN move as m ON mp.id_move=m.id_move RIGHT JOIN team as t ON mp.id_team=t.id_team WHERE id_joueur= ? AND p.id_pokemon=t.id_pokemon ORDER BY t.id_team;', [joueur_id], (err, res2) => {
sql.query('SELECT t.id_team, p.*, m.* FROM pokemon as p, moveposs as mp INNER JOIN move as m ON mp.id_move=m.id_move RIGHT JOIN team as t ON mp.id_team=t.id_team WHERE id_joueur= ? AND p.id_pokemon=t.id_pokemon ORDER BY t.id_team;', [joueur_id], (err, res2) => {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
......@@ -104,7 +104,7 @@ Pokemon.getTeamUtil = (joueur_id, result) => {
};
Pokemon.getTeamPoss = (joueur_id, result) => {
sql.query('SELECT p.*, m.* FROM pokemon as p, team, movePoss as mp, move as m WHERE id_joueur= ? AND p.id_pokemon=team.id_pokemon AND mp.id_move=m.id_move AND mp.id_team=team.id_team;', [joueur_id], (err, res) => {
sql.query('SELECT p.*, m.* FROM pokemon as p, team, moveposs as mp, move as m WHERE id_joueur= ? AND p.id_pokemon=team.id_pokemon AND mp.id_move=m.id_move AND mp.id_team=team.id_team;', [joueur_id], (err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
......@@ -123,7 +123,7 @@ Pokemon.getTeamPoss = (joueur_id, result) => {
};
Pokemon.updateMove = (nMove, id_team, id_move, result) => {
sql.query('UPDATE movePoss SET id_move=? WHERE id_team=? AND id_move=?;', [nMove, id_team, id_move], (err, res) => {
sql.query('UPDATE moveposs SET id_move=? WHERE id_team=? AND id_move=?;', [nMove, id_team, id_move], (err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
......@@ -142,7 +142,7 @@ Pokemon.updateMove = (nMove, id_team, id_move, result) => {
}
Pokemon.create = (newPokemon, result) => {
//BEGIN; INSERT INTO team (courant ,id_joueur, id_pokemon) VALUES(1, 0, 39); INSERT INTO movePoss (id_move, id_team) SELECT mu.move_id, LAST_INSERT_ID() FROM moveUtil as mu WHERE mu.pokemon_id=39 LIMIT 4; COMMIT;
//BEGIN; INSERT INTO team (courant ,id_joueur, id_pokemon) VALUES(1, 0, 39); INSERT INTO moveposs (id_move, id_team) SELECT mu.move_id, LAST_INSERT_ID() FROM moveutil as mu WHERE mu.pokemon_id=39 LIMIT 4; COMMIT;
sql.beginTransaction(function(err) {
if (err) { throw err; }
sql.query('INSERT INTO team SET ?', newPokemon, function(err, res) {
......@@ -155,7 +155,7 @@ Pokemon.create = (newPokemon, result) => {
return;
}
sql.query('INSERT INTO movePoss (id_move, id_team) SELECT mu.move_id, ? FROM moveUtil as mu WHERE mu.pokemon_id=? LIMIT 4', [res.insertId, newPokemon.id_pokemon], function(err, result) {
sql.query('INSERT INTO moveposs (id_move, id_team) SELECT mu.move_id, ? FROM moveutil as mu WHERE mu.pokemon_id=? LIMIT 4', [res.insertId, newPokemon.id_pokemon], function(err, result) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
......
......@@ -75,5 +75,9 @@ module.exports.listen = function(app) {
console.log(challenger + " " + guest + " " + usersMap.get(guest));
io.to(usersMap.get(challenger)).emit('challenge accepted', guest);
});
socket.on('battle data', (to, data) => {
io.to(usersMap.get(to)).emit('personal battle data', data);
});
});
};
\ No newline at end of file
......@@ -36,7 +36,7 @@ const routes: Routes = [
component: ProfilComponent
},
{
path: 'battle/:id',
path: 'battle/:id/:id2/:id3',
component: BattleComponent
},
{
......
......@@ -6,12 +6,12 @@
<div class="row ">
<div class="col-md-8 px-3">
<div class="card-block px-3">
<h4 class="card-title">{{partie.pseudo}} {{partie.id_partie}}</h4>
<h4 class="card-title">{{partie.pseudo}}</h4>
<p class="card-text">hey !</p>
</div>
</div>
<div class="col-md-4">
<a class="btn btn-primary" id="remove" [routerLink]="['/battle', partie.id_partie]">Continue</a>
<a class="btn btn-primary" id="remove" [routerLink]="['/battle', partie.id_partie, partie.joueurTour, partie.pseudo]">Continue</a>
</div>
</div>
</div>
......
......@@ -47,11 +47,13 @@ export class BattleMenuComponent implements OnInit {
filterParties(parties){
for(let partie of parties){
if(partie.nbTours == 0){
if(partie.id_joueur1 == this.uid) this.demandesInt.push(partie);
else this.demandesExt.push(partie);
} else{
this.curParties.push(partie);
if(partie.winner == null){
if(partie.nbTours == 0){
if(partie.id_joueur1 == this.uid) this.demandesInt.push(partie);
else this.demandesExt.push(partie);
} else{
this.curParties.push(partie);
}
}
}
}
......
......@@ -74,8 +74,12 @@
bottom: 80px;
}
.opponent .pokemon {
width: 20%;
.opponent .self{
width: 30%;
}
.opponent .enemy{
width: 15%;
}
.box {
......
......@@ -5,16 +5,16 @@
<div class="row1">
<div>
<span class="name">
Charizard
{{enemyPokemon.pokeName}}
</span>
<span class="level">
86
{{level}}
</span>
</div>
</div>
<div class="health-box">
<div class="health-bar-red" [style.width.%]="curHealthOpp"></div>
<div class="health-bar" [style.width.%]="curHealthOpp"></div>
<div class="health-bar-red" [style.width.%]="enemyAffichageHp"></div>
<div class="health-bar" [style.width.%]="enemyAffichageHp"></div>
<div class="health-bar-text"></div>
</div>
<div class="top">
......@@ -23,10 +23,10 @@
</div>
</div>
</div>
<img class="pokemon" src="https://projectpokemon.org/images/normal-sprite/charizard.gif" alt="A sprite of charizard" />
<img class="enemy" src="https://projectpokemon.org/images/normal-sprite/{{enemyPokemon.pokeName}}.gif" alt="A sprite of {{enemyPokemon.pokeName}}" *ngIf="displayEndgame!=1"/>
</div>
<div class="opponent">
<img class="pokemon" src="https://projectpokemon.org/images/sprites-models/normal-back/{{curPoke.pokeName}}.gif" onerror="" alt="A sprite of {{curPoke.pokeName}}" />
<img class="self" src="https://projectpokemon.org/images/sprites-models/normal-back/{{curPoke.pokeName}}.gif" onerror="" alt="A sprite of {{curPoke.pokeName}}" *ngIf="displayEndgame!=2"/>
<div class="stats">
<div class="row1">
<div>
......@@ -34,7 +34,7 @@
{{curPoke.pokeName}}
</span>
<span class="level">
50
{{level}}
</span>
</div>
<div id="apHP" class="hp-count">{{curHealthSelf}}</div>
......@@ -51,14 +51,17 @@
</div>
</div>
</div>
<div class="box">
<div class="box" *ngIf="!yourTurn">
<div id="message" class="message">
{{boxMessage}}
</div>
</div>
<div class="box" *ngIf="yourTurn">
<div id="message" class="message" >
What should {{curPoke.pokeName}} do?
</div>
<div class="actions">
<button (click)="typeAdv(move)" *ngFor="let move of curPoke.moves">{{move.identifier}}</button>
</div>
<div class="continue">
<button (click)="typeAdv(move)" *ngFor="let move of curPoke.moves">{{move.identifier}} {{move.pp}}</button>
</div>
</div>
</div>
\ No newline at end of file
......@@ -2,83 +2,204 @@ import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';
import { ChatService } from '../chat.service';
import { Pokemon } from "./pokemon.js";
@Component({
selector: 'app-battle',
templateUrl: './battle.component.html',
styleUrls: ['./battle.component.css']
})
export class BattleComponent implements OnInit {
isOpen = true;
maxHealth: number;
curHealthSelf: number;
curHealthOpp = 100;
pseudo: string;
uid: any;
log: boolean;
level = 50;
atk: number;
def: number = 0;
typeMoi: number;
typeMove: number;
typeToi = 10;
effet: any;
private sub: any;
private id_partie: number;
selfTeam: any = [];
curPoke: any = {};
def: number;
affichageHp: number = 0;
user: any;
pokemonEnemy: any = [];
enemyPokemon: any = {};
enemyTeam: any = [];
enemyType: number;
enemyCurHealth = 100;
enemyMaxHealth: number;
enemyDef: number;
enemyAtk: number;
enemyAffichageHp: number = 0;
enemyPseudo: string;
endgame: boolean = false;
displayEndgame: number = 0;
tempPokemon: any;
moveToSend: string;
dmgToSend: number;
constructor(private route: ActivatedRoute, private httpClient: HttpClient, private chatService : ChatService) { }
private sub: any;
private id_partie: number;
joueurTour: number;
yourTurn: boolean = false;
waitingMsg: string = "Waiting for the enemy...";
boxMessage: string = "Waiting for the enemy...";
curPokeIndex: number;
curEnemyPokeIndex: number;
constructor(private route: ActivatedRoute, private httpClient: HttpClient, private chatService : ChatService) { }
getPokemon(){
this.selfTeam.forEach(el => {
if(el.stats[0].stat_value > 0) this.curPoke = el;
this.curHealthSelf = this.curPoke.stats[0].stat_value;
this.atk = this.curPoke.stats[1].stat_value;
this.typeMoi = this.curPoke.types[0].id_type;
this.maxHealth = this.curPoke.base_stats[0].base_stat
this.healthUpdate();
startup(){
this.selfTeam.forEach( (el, i) => {
if(el.active == 1) {
this.curPoke = el;
this.curPokeIndex = i;
console.log(i);
}
});
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.affichageHp = this.healthUpdate(this.curHealthSelf, this.maxHealth);
this.enemyTeam.forEach( (el, i) => {
if(el.active == 1) {
this.enemyPokemon = el;
this.curEnemyPokeIndex = i;
console.log(i);
}
});
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);
}
getNextPokemon(nextPokemon){
this.curPoke = nextPokemon;
this.curHealthSelf = this.curPoke.stats[0].stat_value;
this.atk = this.curPoke.stats[1].stat_value;