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

fully created games + reception of game team

parent c13a4311
const Joueur = require("../models/joueur.model.js");
exports.auth = (req, res) => {
console.log(req);
Joueur.auth(req.body.email, req.body.mdp, (err, data) => {
if (err) {
if (err.kind === "not_found") {
......
......@@ -53,6 +53,18 @@ exports.accept = (req, res) => {
}
};
exports.getCombatTeam = (req, res) => {
if (req.session.loggedIn) {
Partie.getCombatTeam(req.body.id_partie, req.session.uid, (err, data) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while getting combat team."
});
else res.send(data);
});
}
};
exports.refuse = (req, res) => {
if (req.session.loggedIn) {
Partie.refuse(req.body.id_partie, (err, data) => {
......
var _ = require('underscore');
exports.filterJSON = function (data, type){
var moveNom = "movesPoss";
if(type) moveNom = "movesUtil";
......@@ -20,6 +22,58 @@ exports.filterJSON = function (data, type){
return filteredData;
}
exports.filterJSONCombat = function (data){
var separatorsNames = ["types", "stats", "moves"];
var separators = ["id_type", "id", "id_move"];
var dataSet = new Set();
var filteredData = [];
var i = -1;
Object.keys(data).forEach(function(key) {
var switcher = 0;
var tempArray = {};
var typesSet = new Map();
var statsSet = new Map();
var movesSet = new Map();
var typesContainer = {};
var statsContainer = {};
var movesContainer = {};
Object.keys(data[key]).forEach(function(key2) {
if(separators.includes(key2)) switcher++;
switch(switcher) {
case 1:
typesContainer[key2] = data[key][key2];
break;
case 2:
statsContainer[key2] = data[key][key2];
break;
case 3:
movesContainer[key2] = data[key][key2];
break;
default:
tempArray[key2] = data[key][key2];
}
// subContainer[key2] = data[key][key2];
});
//tempArray[moveNom] = [];
separatorsNames.forEach(sep => tempArray[sep] = [])
if(dataSet.has(data[key]["id_pokemon"])) {
//filteredData[i][moveNom].push(subContainer)
filteredData[i][separatorsNames[0]].push(typesContainer);
filteredData[i][separatorsNames[1]].push(statsContainer);
filteredData[i][separatorsNames[2]].push(movesContainer);
} else {
i++;
filteredData.push(tempArray);
dataSet.add(data[key]["id_pokemon"]);
filteredData[i][separatorsNames[0]].push(typesContainer);
filteredData[i][separatorsNames[1]].push(statsContainer);
filteredData[i][separatorsNames[2]].push(movesContainer);
}
});
filteredData.forEach(pokemon => {pokemon.types = arrUnique(pokemon.types); pokemon.moves = arrUnique(pokemon.moves); pokemon.stats = arrUnique(pokemon.stats);});
return filteredData;
}
exports.fuseJSON = function (dataUtil, dataPoss){
var fusedJSON = dataUtil;
Object.keys(fusedJSON).forEach(function(key) {
......@@ -37,4 +91,16 @@ exports.fuseJSON = function (dataUtil, dataPoss){
exports.test = function (){
return true;
}
function arrUnique(arr) {
var cleaned = [];
arr.forEach(function(itm) {
var unique = true;
cleaned.forEach(function(itm2) {
if (_.isEqual(itm, itm2)) unique = false;
});
if (unique) cleaned.push(itm);
});
return cleaned;
}
\ No newline at end of file
const sql = require("./db.js");
const JSONmanip = require("../methods/JSONmanip");
// constructor
const Partie = function(partie) {
......@@ -42,15 +43,94 @@ Partie.getDemandes = (uid, result) => {
};
Partie.accept = (id_partie, result) => {
sql.query("UPDATE partie SET nbTours=1 WHERE id_partie=?", [id_partie], (err, res) => {
sql.beginTransaction(function(errTransaction) {
if (errTransaction) { throw err; }
sql.query('UPDATE partie SET nbTours=1 WHERE id_partie=?', id_partie, function(err, res) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(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) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
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) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(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) {
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 !!!!');
//sql.end();
});
});
});
});
result(null, {res: true, id: res.insertId});
});
});
// sql.query("UPDATE partie SET nbTours=1 WHERE id_partie=?", [id_partie], (err, res) => {
// if (err) {
// console.log("error: ", err);
// result(err, null);
// return;
// }
// result(null, {res: true, id: res.insertId});
// });
};
Partie.getCombatTeam = (id_partie, id_joueur, result) =>{
sql.query("SELECT DISTINCT p.id_pokemon, p.pokeName, ep.active, typePoke.id_type, typePoke.nomType as typeNom, gs.id, gs.identifier as statName, es.stat_value, es.effort, m.id_move, m.identifier, m.power, m.accuracy, typeMove.id_type as m_id_type, typeMove.nomType, em.pp 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 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", [id_partie, id_joueur], (err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
result(null, {res: true, id: res.insertId});
if (res.length) {
console.log("found EVERYTHING: ", res);
var filteredData = JSONmanip.filterJSONCombat(res);
console.log(filteredData);
result(null, filteredData);
return;
}
// not found combat team with the id
result({ kind: "not_found" }, null);
});
};
}
Partie.refuse = (id_partie, result) => {
sql.query("DELETE FROM partie WHERE id_partie=?", [id_partie], (err, res) => {
......
const sql = require("./db.js");
const JSONmanip = require("../methods/JSONmanip");
// constructor
const Pokemon = function(pokemon) {
......@@ -62,7 +63,7 @@ Pokemon.getTeamUtil = (joueur_id, result) => {
}
console.log(res1);
var dataUtil = filterJSON(res1, true);
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) => {
if (err) {
......@@ -89,8 +90,8 @@ Pokemon.getTeamUtil = (joueur_id, result) => {
return;
}
var dataPoss = filterJSON(res2, false);
var fusedData = fuseJSON(dataUtil, dataPoss);
var dataPoss = JSONmanip.filterJSON(res2, false);
var fusedData = JSONmanip.fuseJSON(dataUtil, dataPoss);
console.log('Transaction Complete.');
//console.log(fusedData);
result(null, fusedData);
......@@ -201,43 +202,4 @@ Pokemon.remove = (id_joueur, id_pokemon, result) => {
};
function filterJSON(data, type){
var moveNom = "movesPoss";
if(type) moveNom = "movesUtil";
var dataSet = new Set();
var filteredData = [];
var i = -1;
Object.keys(data).forEach(function(key) {
var switcher = false;
var temp = {};
var move = {};
Object.keys(data[key]).forEach(function(key2) {
if(key2 == "id_move") switcher = true;
if(switcher) move[key2] = data[key][key2];
else {temp[key2] = data[key][key2]; };
});
temp[moveNom] = [];
if(dataSet.has(data[key]["id_pokemon"])) filteredData[i][moveNom].push(move);
else {i++;filteredData.push(temp); dataSet.add(data[key]["id_pokemon"]); filteredData[i][moveNom].push(move);}
});
return filteredData;
}
function fuseJSON(dataUtil, dataPoss){
var fusedJSON = dataUtil;
Object.keys(fusedJSON).forEach(function(key) {
var tempTab = fusedJSON[key]["movesUtil"];
tempTab = tempTab.filter(value =>{
var contain = false;
dataPoss[key]["movesPoss"].forEach(value2 => {if(value.id_move == value2.id_move)contain = true});
return !contain;
});
//console.log(tempTab);
fusedJSON[key]["movesUtil"] = tempTab;
fusedJSON[key]["movesPoss"] = dataPoss[key]["movesPoss"];
});
return fusedJSON;
}
module.exports = Pokemon;
\ No newline at end of file
......@@ -10,4 +10,6 @@ module.exports = app => {
app.post("/refusepartie", partie.refuse);
app.post("/getCombatTeam", partie.getCombatTeam);
};
\ No newline at end of file
var assert = require('assert');
var chai = require("chai");
var expect = chai.expect;
var manip = require("../app/methods/JSONmanip");
let should = chai.should();
var testVar = require("./testJSON.var");
describe("JSON transformation", function () {
describe("JSON restructuring", function () {
it("removes duplicates and adds moves to the correct pokemon array", function () {
var data = [{
id_team: 53,
id_pokemon: 1,
pokeName: 'squirtle',
height: 6,
weight: 85,
base_experience: 62,
level: 0,
is_default: 1,
base_defense: 0,
base_attack: 0,
base_hp: 0,
id_type: 0,
id_move: 497,
identifier: 'echoed-voice',
description: '',
action: 0,
power: '40',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 1
},
{
id_team: 51,
id_pokemon: 4,
pokeName: 'charmander',
height: 6,
weight: 85,
base_experience: 62,
level: 0,
is_default: 1,
base_defense: 0,
base_attack: 0,
base_hp: 0,
id_type: 0,
id_move: 497,
identifier: 'echoed-voice',
description: '',
action: 0,
power: '40',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 1
},
{
id_team: 51,
id_pokemon: 4,
pokeName: 'charmander',
height: 6,
weight: 85,
base_experience: 62,
level: 0,
is_default: 1,
base_defense: 0,
base_attack: 0,
base_hp: 0,
id_type: 0,
id_move: 510,
identifier: 'incinerate',
description: '',
action: 0,
power: '60',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 10
},
{
id_team: 51,
id_pokemon: 4,
pokeName: 'charmander',
height: 6,
weight: 85,
base_experience: 62,
level: 0,
is_default: 1,
base_defense: 0,
base_attack: 0,
base_hp: 0,
id_type: 0,
id_move: 517,
identifier: 'inferno',
description: '',
action: 0,
power: '100',
pp: '5',
accuracy: '50',
priority: 0,
type_id: 10
}]
var result = [
{
id_team: 53,
id_pokemon: 1,
pokeName: 'squirtle',
height: 6,
weight: 85,
base_experience: 62,
level: 0,
is_default: 1,
base_defense: 0,
base_attack: 0,
base_hp: 0,
id_type: 0,
movesUtil: [{
id_move: 497,
identifier: 'echoed-voice',
description: '',
action: 0,
power: '40',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 1
}]
},
{
id_team: 51,
id_pokemon: 4,
pokeName: 'charmander',
height: 6,
weight: 85,
base_experience: 62,
level: 0,
is_default: 1,
base_defense: 0,
base_attack: 0,
base_hp: 0,
id_type: 0,
movesUtil: [{
id_move: 497,
identifier: 'echoed-voice',
description: '',
action: 0,
power: '40',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 1
}, {
id_move: 510,
identifier: 'incinerate',
description: '',
action: 0,
power: '60',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 10
}, {
id_move: 517,
identifier: 'inferno',
description: '',
action: 0,
power: '100',
pp: '5',
accuracy: '50',
priority: 0,
type_id: 10
}]
}]
var restructured = []
restructured = manip.filterJSON(data, true);
expect(restructured).to.deep.equal(result);
restructured = manip.filterJSON(testVar.test1Data, true);
expect(restructured).to.deep.equal(testVar.test1Result);
});
});
describe("JSON fusion", function () {
it("fuses two JSON and removes usable moves according to moves already in the possession of a pokemon", function () {
var data1 = [
{
id_team: 53,
id_pokemon: 1,
pokeName: 'squirtle',
height: 6,
weight: 85,
base_experience: 62,
level: 0,
is_default: 1,
base_defense: 0,
base_attack: 0,
base_hp: 0,
id_type: 0,
movesUtil: [{
id_move: 497,
identifier: 'echoed-voice',
description: '',
action: 0,
power: '40',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 1
}]
},
{
id_team: 51,
id_pokemon: 4,
pokeName: 'charmander',
height: 6,
weight: 85,
base_experience: 62,
level: 0,
is_default: 1,
base_defense: 0,
base_attack: 0,
base_hp: 0,
id_type: 0,
movesUtil: [{
id_move: 497,
identifier: 'echoed-voice',
description: '',
action: 0,
power: '40',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 1
}, {
id_move: 510,
identifier: 'incinerate',
description: '',
action: 0,
power: '60',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 10
}, {
id_move: 517,
identifier: 'inferno',
description: '',
action: 0,
power: '100',
pp: '5',
accuracy: '50',
priority: 0,
type_id: 10
}]
}];
var data2 = [
{
id_team: 53,
id_pokemon: 1,
pokeName: 'squirtle',
height: 6,
weight: 85,
base_experience: 62,
level: 0,
is_default: 1,
base_defense: 0,
base_attack: 0,
base_hp: 0,
id_type: 0,
movesPoss: [{
id_move: 497,
identifier: 'echoed-voice',
description: '',
action: 0,
power: '40',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 1
}]
},
{
id_team: 51,
id_pokemon: 4,
pokeName: 'charmander',
height: 6,
weight: 85,
base_experience: 62,
level: 0,
is_default: 1,
base_defense: 0,
base_attack: 0,
base_hp: 0,
id_type: 0,
movesPoss: [{
id_move: 497,
identifier: 'echoed-voice',
description: '',
action: 0,
power: '40',
pp: '15',
accuracy: '100',
priority: 0,
type_id: 1
},{
id_move: 517,
identifier: 'inferno',
description: '',
action: 0,