Commit 77cf961a authored by Manish Kumar's avatar Manish Kumar
Browse files

pokedex add team with attacks and attack selection for team

parent d4977a61
......@@ -37,28 +37,40 @@ exports.findAll = (req, res) => {
});
};
exports.getTeam = (req, res) => {
console.log("Is logged in: " + req.session.loggedIn);
if(req.session.loggedIn){
Pokemon.getTeam(req.session.uid, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.send({res: true, body: data});
} else {
res.status(500).send({
message: "Error retrieving Team with id " + req.session.uid
});
}
} else{
exports.getTeamUtil = (req, res) => {
Pokemon.getTeamUtil(req.body.uid, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.send({res: true, body: data});
} else {
res.status(500).send({
message: "Error retrieving Team with id " + req.body.uid
});
}
});
} else{
res.send({res: false, body:'You must be logged in to manage a team !'});
res.end();
}
} else{
res.send({res: true, body: data});
}
});
};
exports.updateMove = (req, res) => {
Pokemon.updateMove(req.body.nMove, req.body.id_team, req.body.id_move, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.send({res: true, body: data});
} else {
res.status(500).send({
message: "Error updating moves",
received: req.body
});
}
} else{
res.send({res: true, body: data});
}
});
};
exports.remove = (req, res) => {
Pokemon.remove(req.session.uid ,req.body.id_pokemon, (err, data) => {
......@@ -75,3 +87,5 @@ exports.remove = (req, res) => {
} else res.send({ message: `Pokemon was deleted successfully from Team!` });
});
};
......@@ -22,8 +22,86 @@ Pokemon.getAll = result => {
};
Pokemon.getTeam = (joueur_id, result) => {
sql.query('SELECT p.* FROM pokemon as p, team WHERE id_joueur= ? AND p.id_pokemon=team.id_pokemon', [joueur_id], (err, res) => {
// 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) => {
// if (err) {
// console.log("error: ", err);
// result(err, null);
// return;
// }
// if (res.length) {
// //console.log("found team: ", res);
// result(null, res);
// return;
// }
// // not found team with the id
// result({ kind: "not_found" }, null);
// });
// };
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) => {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
return;
}
if (!res1.length) {
//console.log("found team: ", res);
result({ kind: "not_found" }, null);
return;
}
var dataUtil = 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) {
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;
}
if (!res2.length) {
//console.log("found team: ", res);
result({ kind: "not_found" }, null);
return;
}
var dataPoss = filterJSON(res2, false);
var fusedData = fuseJSON(dataUtil, dataPoss);
console.log('Transaction Complete.');
//console.log(fusedData);
result(null, fusedData);
//sql.end();
});
});
});
});
};
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) => {
if (err) {
console.log("error: ", err);
result(err, null);
......@@ -31,7 +109,7 @@ Pokemon.getTeam = (joueur_id, result) => {
}
if (res.length) {
console.log("found team: ", res);
//console.log("found team: ", res);
result(null, res);
return;
}
......@@ -41,19 +119,64 @@ Pokemon.getTeam = (joueur_id, result) => {
});
};
Pokemon.create = (newPokemon, result) => {
sql.query('INSERT INTO team SET ?', newPokemon, (err, res) => {
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) => {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
console.log("added new Pokemon to team: ", { id: res.insertId, ...newPokemon });
result(null, { id: res.insertId, ...newPokemon });
if (res.affectedRows == 0) {
// not found team Pokemon with the id
result({ kind: "not_found" }, null);
return;
}
console.log("updated move with id: ", id_move, " from team pokemon with id: ", id_team, " with new id : ", nMove);
result(null, res);
});
}
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;
sql.beginTransaction(function(err) {
if (err) { throw err; }
sql.query('INSERT INTO team SET ?', newPokemon, function(err, res) {
if (err) {
console.log("error: ", err);
sql.rollback(function() {
throw err;
});
result(err, null);
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) {
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, { id: res.insertId, ...newPokemon });
});
});
};
......@@ -76,4 +199,49 @@ 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);}
});
//console.log(filteredData);
return filteredData;
}
function fuseJSON(dataUtil, dataPoss){
var fusedJSON = dataUtil;
//console.log(dataPoss);
//console.log(dataUtil);
Object.keys(fusedJSON).forEach(function(key) {
//console.log(key);
//console.log(dataPoss);
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
......@@ -8,9 +8,11 @@ module.exports = app => {
app.post("/addpokemon", pokemon.create);
// Retrieve a team
app.get("/getteam", pokemon.getTeam);
app.post("/getteam", pokemon.getTeamUtil);
// Remove a pokemon from a team
app.post("/removepokemon", pokemon.remove);
app.post("/updatemove", pokemon.updateMove);
};
\ No newline at end of file
This diff is collapsed.
......@@ -16,9 +16,11 @@
"@angular/compiler": "~9.1.0",
"@angular/core": "~9.1.0",
"@angular/forms": "~9.1.0",
"@angular/localize": "~9.1.0",
"@angular/platform-browser": "~9.1.0",
"@angular/platform-browser-dynamic": "~9.1.0",
"@angular/router": "~9.1.0",
"@ng-bootstrap/ng-bootstrap": "^6.1.0",
"angular-notifier": "^6.0.1",
"bootstrap": "^4.4.1",
"jquery": "^3.5.1",
......
......@@ -22,6 +22,8 @@ import { HomeComponent } from './home/home.component';
import { OnlinePlayersComponent } from './sub-components/online-players/online-players.component';
import { NotifierModule } from "angular-notifier";
import { BattleComponent } from './battle/battle.component';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
@NgModule({
declarations: [
......@@ -44,7 +46,8 @@ import { BattleComponent } from './battle/battle.component';
HttpClientModule,
FormsModule,
ReactiveFormsModule,
NotifierModule
NotifierModule,
NgbModule
],
providers: [
ChatService,
......
......@@ -2,4 +2,9 @@
background-color: rgb(219, 54, 54);
border-color: rgb(219, 54, 54);
color: beige;
}
.dropydown{
overflow-y: scroll;
height: 10rem;
}
\ No newline at end of file
<section *ngIf="team.res">
<div class="container py-3" *ngIf="team.body!=null">
<div class="card" *ngFor="let pokemon of team.body; let i=index">
<section *ngIf="loggedIn">
<div class="container py-3" *ngIf="team!=null">
<div class="card" *ngFor="let pokemon of team; let i=index">
<div class="row ">
<div class="col-md-4">
<img src="https://pokeres.bastionbot.org/images/pokemon/{{pokemon.id_pokemon}}.png" alt="" onerror="this.src='https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/0.png';" class="w-100">
......@@ -8,7 +8,28 @@
<div class="col-md-8 px-3">
<div class="card-block px-3">
<h4 class="card-title">{{pokemon.pokeName}}</h4>
<p class="card-text">Attacks</p>
<p class="card-text">Attacks : {{showAttacks(pokemon)}}</p>
</div>
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
<a [ngClass]="first ? 'nav-item nav-link active' : 'nav-item nav-link'" id="nav-{{pokemon.pokeName}}-attack{{nb}}-tab" data-toggle="tab" href="#nav-{{pokemon.pokeName}}-attack{{nb}}" role="tab" aria-selected="false" *ngFor="let nb of nbAttacks;let first = first;">attack {{nb}}</a>
</div>
</nav>
<div class="tab-content" id="nav-tabContent">
<div [ngClass]="first ? 'tab-pane fade show active' : 'tab-pane fade show'" id="nav-{{pokemon.pokeName}}-attack{{item}}" role="tabpanel" *ngFor="let item of nbAttacks; let first = first; let j = index">
<a>{{chosenAttacks.get(pokemon.pokeName)[j]}}</a>
<div class="row">
<div class="col text-right">
<div ngbDropdown class="d-inline-block">
<button class="btn btn-outline-primary" id="dropdownBasic1" ngbDropdownToggle>Change attack {{item}}</button>
<div ngbDropdownMenu aria-labelledby="dropdownBasic1" class="dropydown">
<button ngbDropdownItem *ngFor="let move of pokemon.movesUtil; let k=index" (click)="updateAttacks(pokemon, move, j, k)">{{move.identifier}}</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-4">
......@@ -18,10 +39,10 @@
</div>
</div>
</section>
<div class="alert alert-warning alert-dismissible fade show" role="alert" *ngIf="!team.res">
<div class="alert alert-warning alert-dismissible fade show" role="alert" *ngIf="!loggedIn">
You need to <strong>create an account</strong> to use this feature.
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<p *ngIf="team.body==null">You don't have any Pokemon :(</p>
\ No newline at end of file
<p *ngIf="team==null">You don't have any Pokemon :(</p>
\ No newline at end of file
......@@ -9,7 +9,12 @@ import { NgLocalization } from '@angular/common';
})
export class TeamMenuComponent implements OnInit {
team: any = {};
loggedIn: boolean;
nbAttacks: Number[] = [1, 2, 3, 4];
chosenAttacks = new Map();
pseudo: string;
uid: Number;
team: any = [];
constructor(private http: HttpClient) { }
removePokemon(pokemon){
......@@ -21,11 +26,65 @@ export class TeamMenuComponent implements OnInit {
)
}
ngOnInit(): void {
this.http.get("http://localhost:3000/getteam", { responseType: 'json', withCredentials: true }).subscribe(
(response) => {console.log(response); this.team = response;},
pageInit(){
this.http.post("http://localhost:3000/getteam", {uid: this.uid}, { responseType: 'json', withCredentials: true }).subscribe(
(response: any) => {
console.log(response);
if(response.res) {
this.team = response.body;
this.team.forEach(pokemon => {
var temp = [];
pokemon.movesPoss.forEach(move => {
temp.push(move.identifier);
});
this.chosenAttacks.set(pokemon.pokeName, temp);
});
console.log(this.team);
}
else console.log("error retrieving team");
},
(error) => {console.log(error)});
console.log(this.team.body);
}
updateAttacks(pokemon, move, j, k){
var temp = this.chosenAttacks.get(pokemon.pokeName);
temp[j] = move.identifier;
this.chosenAttacks.set(pokemon.pokeName, temp);
this.http.post("http://localhost:3000/updateMove", {nMove: move.id_move, id_team: pokemon.id_team, id_move: pokemon.movesPoss[j].id_move}, { responseType: 'json', withCredentials: true }).subscribe(
(response: any) => {},
(error) => {console.log(error)}
);
var tempMove = move;
pokemon.movesUtil[k] = pokemon.movesPoss[j];
pokemon.movesPoss[j] = tempMove;
var tempTab = this.chosenAttacks.get(pokemon.pokeName);
tempTab[j] = tempMove.identifier;
this.chosenAttacks.set(pokemon.pokeName, tempTab);
console.log(this.chosenAttacks);
}
showAttacks(pokemon){
var temp = "";
this.chosenAttacks.get(pokemon.pokeName).forEach(attack => {
temp += attack + ", ";
});
return temp.slice(0, -1);
}
ngOnInit(): void {
this.http.get('http://localhost:3000/checkLogin', { responseType: 'json', withCredentials: true}).subscribe( (res: any) => {
this.loggedIn = res.res;
this.pseudo = res.body;
this.uid = res.uid;
console.log(res);
if(this.loggedIn){
this.pageInit();
}else{
console.log("not logged in");
}
});
}
}
/***************************************************************************************************
* Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
*/
import '@angular/localize/init';
/**
* This file includes polyfills needed by Angular and is loaded before the app.
* You can add your own extra polyfills to this file.
......
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