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

team remove fix, small errors fix

parent edb41d83
......@@ -75,7 +75,7 @@ exports.updateMove = (req, res) => {
};
exports.remove = (req, res) => {
Pokemon.remove(req.session.uid, req.body.id_pokemon, (err, data) => {
Pokemon.remove(req.body.id_team, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.status(404).send({
......
......@@ -28,36 +28,36 @@ exports.filterJSON = function(data, type) {
}
exports.filterJSONCombat = function(data) {
var separatorsNames = ["types", "stats", "base_stats", "moves"];
var separators = ["id_type", "id", "baseStat_id", "id_move"];
var containers = [{}];
var separatorsNames = ["types", "stats", "base_stats", "moves"]; // nom des catégories finales
var separators = ["id_type", "id", "baseStat_id", "id_move"]; // nom des colonnes annonçant le début d'une nouvelle catégorie dans le JSON de base
var containers = [{}]; // initialisation du container temporaire pour toutes les catégories d'un Pokemon
separators.forEach(sep => containers.push({}));
var dataSet = new Set();
var filteredData = [];
var i = -1;
var dataSet = new Set(); // stockage des ids des Pokemon, permet de vérifier rapidement si nouveau Pokemon
var filteredData = []; // resultat final
var i = -1; // index courant du JSON final et donc du Pokemon courant
Object.keys(data).forEach(function(key) {
var switcher = 0;
containers.forEach((el, index, containers) => containers[index] = {});
var switcher = 0; // switcher entre categories
containers.forEach((el, index, containers) => containers[index] = {}); // vider les categories pour le prochain element
Object.keys(data[key]).forEach(function(key2) {
if (separators.includes(key2)) switcher++;
containers[switcher][key2] = data[key][key2];
containers[switcher][key2] = data[key][key2]; // assigner les elements a chaque categorie
});
separatorsNames.forEach(sep => containers[0][sep] = [])
if (dataSet.has(data[key]["id_pokemon"])) {
separatorsNames.forEach(sep => containers[0][sep] = []); // Le premier element du tableau contiendra toutes les categories du tableaux, utile pour un nouveau Pokemon
if (dataSet.has(data[key]["id_pokemon"])) { //Pokemon deja present dans JSON final
for(var j = 0; j < separators.length; j++){
filteredData[i][separatorsNames[j]].push(containers[j+1]);
filteredData[i][separatorsNames[j]].push(containers[j+1]); // ajouter les nouveaux elements tableaux des categories au Pokémon
}
} else {
i++;
filteredData.push(containers[0]);
i++; // nouveau Pokemon, donc nouvelle case
filteredData.push(containers[0]); // assigne le conteneur temporaire
dataSet.add(data[key]["id_pokemon"]);
for(var j = 0; j < separators.length; j++){
filteredData[i][separatorsNames[j]].push(containers[j+1]);
filteredData[i][separatorsNames[j]].push(containers[j+1]); // ajouter les elements
}
}
});
filteredData.forEach(pokemon => {
separatorsNames.forEach(sep => pokemon[sep] = arrUnique(pokemon[sep]));
separatorsNames.forEach(sep => pokemon[sep] = arrUnique(pokemon[sep])); // eleminer la redondance dans chaque categorie
});
return filteredData;
}
......
......@@ -58,7 +58,7 @@ 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;
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 team.courant=1 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() {
......@@ -77,7 +77,7 @@ Pokemon.getTeamUtil = (joueur_id, result) => {
//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 AND t.courant=1 ORDER BY t.id_team;', [joueur_id], (err, res2) => {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
......@@ -194,8 +194,8 @@ Pokemon.create = (newPokemon, result) => {
};
Pokemon.remove = (id_joueur, id_pokemon, result) => {
sql.query('DELETE FROM team WHERE id_joueur= ? AND id_pokemon= ? LIMIT 1', [id_joueur, id_pokemon], (err, res) => {
Pokemon.remove = (id_team, result) => {
sql.query('UPDATE team SET courant=0 WHERE id_team=?', [id_team], (err, res) => {
if (err) {
console.log("error: ", err);
result(null, err);
......@@ -208,7 +208,7 @@ Pokemon.remove = (id_joueur, id_pokemon, result) => {
return;
}
console.log("deleted Pokemon with id: ", id_pokemon, "from team of user with id: ", id_joueur);
console.log("deleted Pokemon with team id: ", id_team);
result(null, res);
});
};
......
......@@ -30,7 +30,7 @@
</div>
<div class="col-md-4">
<button class="btn btn-primary" id="accept" (click)="acceptChallenge(partie, i)">Accept</button >
<button class="btn btn-success" id="refuse" (click)="refuseChallenge(partie.id_partie, i)">Refuse</button>
<button class="btn btn-danger" id="refuse" (click)="refuseChallenge(partie.id_partie, i)">Refuse</button>
</div>
</div>
</div>
......
......@@ -10,8 +10,8 @@ export class LeaderboardComponent implements OnInit {
constructor(private httpClient: HttpClient) { }
resultat: any = {};
poke:any = {};
resultat: any = [];
poke:any = [];
ngOnInit(): void {
this.httpClient.get('http://localhost:3000/winners', { responseType: 'json', withCredentials:true }).subscribe(res => {this.resultat = res})
......
......@@ -20,7 +20,7 @@ export class TeamMenuComponent implements OnInit {
removePokemon(pokemon){
const headers = new HttpHeaders()
.set('Content-Type', 'application/json');
this.http.post('http://localhost:3000/removepokemon', {id_pokemon: pokemon.id_pokemon} , { headers: headers, withCredentials: true}).subscribe(
this.http.post('http://localhost:3000/removepokemon', {id_team: pokemon.id_team} , { headers: headers, withCredentials: true}).subscribe(
(response) => {console.log(response); this.ngOnInit();},
(error) => console.log(error)
)
......
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