Commit cceafc93 authored by Sofia Aicha Delijaj's avatar Sofia Aicha Delijaj
Browse files

sotp

parents b6c8215c 8aa30e73
......@@ -26,7 +26,7 @@ exports.auth = (req, res) => {
exports.checkLogin = (req, response) => {
if (req.session.loggedIn) {
response.send({ res: true, body: req.session.pseudo });
response.send({ res: true, body: req.session.pseudo, uid: req.session.uid });
} else {
response.send({ res: false, body: 'You are not logged in' });
console.log("Session ID: ", req.sessionID);
......
const Partie = require("../models/partie.model.js");
exports.create = (req, res) => {
// Validate request
if (!req.body) {
res.status(400).send({
message: "Content can not be empty!"
});
}
// add game to db
Partie.create(req.session.uid, req.body.pseudo, (err, data) => {
if (err)
res.status(500).send({
message:
err.message || "Some error occurred while creating the Pokemon."
});
else res.send(data);
});
};
exports.getDemandes = (req, res) => {
console.log("Is logged in: " + req.session.loggedIn);
if(req.session.loggedIn){
Partie.getDemandes(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 games with id " + req.session.uid
});
}
} else{
res.send({res: true, body: data});
}
});
} else{
res.send({res: false, body:'You must be logged in to see your games !'});
res.end();
}
};
\ No newline at end of file
const sql = require("./db.js");
// constructor
const Partie = function(partie) {
this.nbTours = partie.nbTours;
this.joueurTour = partie.joueurTour;
this.id_joueur1 = partie.id_joueur1;
this.id_joueur2 = partie.id_joueur2;
};
Partie.create = (uid, pseudo, result) => {
sql.query('INSERT INTO partie (nbTours, joueurTour, id_joueur1, id_joueur2) SELECT 0, id_joueur, ?, id_joueur FROM joueur WHERE pseudo = ?;', [uid, pseudo], (err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
console.log("created request: " + pseudo + " " + uid);
result(null, {id: res.insertId});
});
};
Partie.getDemandes = (uid, result) => {
sql.query('SELECT p.*, j.pseudo FROM partie as p, joueur as j WHERE (id_joueur1 = ? AND j.id_joueur = p.id_joueur2) OR (id_joueur2 = ? AND j.id_joueur = p.id_joueur1)', [uid, uid], (err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
if (res.length) {
console.log("found games: ", res);
result(null, res);
return;
}
// not found team with the id
result({ kind: "not_found" }, null);
});
};
module.exports = Partie;
\ No newline at end of file
module.exports = app => {
const partie = require("../controllers/partie.controller.js");
// Add request for game
app.post("/addpartie", partie.create);
app.get("/getparties", partie.getDemandes);
};
\ No newline at end of file
......@@ -38,7 +38,7 @@ app.get("/", (req, res) => {
require("./app/routes/pokemon.routes.js")(app);
require("./app/routes/joueur.routes.js")(app);
require("./app/routes/partie.routes.js")(app);
// set port, listen for requests
var serv = app.listen(3000, () => {
......
#refuse{
background-color: rgb(219, 54, 54);
border-color: rgb(219, 54, 54);
color: beige;
}
\ No newline at end of file
<section *ngIf="loggedIn">
<div class="container py-3">
<div class="card" *ngFor="let user of users">
<div class="row ">
<div class="col-md-8 px-3">
<div class="card-block px-3">
<h4 class="card-title">{{user}}</h4>
<p class="card-text">hey !</p>
<div class="players">
<h2>Current matches</h2>
<div class="container py-3">
<div class="card" *ngFor="let partie of curParties">
<div class="row ">
<div class="col-md-8 px-3">
<div class="card-block px-3">
<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">Continue</a>
</div>
</div>
</div>
</div>
</div>
<div class="players">
<h2>Challenges from other trainers</h2>
<div class="container py-3">
<div class="card" *ngFor="let partie of demandesExt">
<div class="row ">
<div class="col-md-8 px-3">
<div class="card-block px-3">
<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">Accept</a>
<a class="btn btn-primary" id="refuse">Refuse</a>
</div>
</div>
<div class="col-md-4">
<a class="btn btn-primary" id="remove" (click)="challenge(user)">Battle</a>
</div>
</div>
</div>
<div class="players">
<h2>Pending requests</h2>
<div class="container py-3">
<div class="card" *ngFor="let partie of demandesInt">
<div class="row ">
<div class="col-md-8 px-3">
<div class="card-block px-3">
<h4 class="card-title">{{partie.pseudo}}</h4>
<p class="card-text">hey !</p>
</div>
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
......
......@@ -10,43 +10,49 @@ import { __assign } from 'tslib';
})
export class BattleMenuComponent implements OnInit {
user: string;
users: any = [];
pseudo: string;
uid;
loggedIn: boolean;
challenges: any = []
parties: any = [];
curParties: any = [];
demandesExt: any = [];
demandesInt: any = [];
constructor(private chatService : ChatService, private http: HttpClient){}
ngOnInit() {
this.http.get('http://localhost:3000/checkLogin', { responseType: 'json', withCredentials: true}).subscribe(res => {
var temp : any = res;
this.loggedIn = temp.res;
this.pseudo = temp.body;
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.initiateSockets();
this.pageInit();
}else{
console.log("not logged in");
}
});
}
initiateSockets(){
this.chatService.recevoirChallenge().subscribe((challenger: string) =>{
this.challenges.push(challenger);
console.log("challenges " + this.challenges);
pageInit(){
this.http.get('http://localhost:3000/getparties', { responseType: 'json', withCredentials: true}).subscribe((res: any) => {
if(res.res) {
this.parties = res.body;
this.filterParties(res.body);
}
else console.log(res.body);
});
}
sendMessage() {
this.chatService.sendMessage(this.user);
this.user = '';
}
challenge(user){
this.chatService.attaquer(user);
console.log("sending challenge");
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);
}
}
}
}
......@@ -92,10 +92,12 @@ export class ChatService {
public attaquer(guest){
if(this.pseudo == undefined){
this.notifier.notify("warning", "You must login to battle !");
return false;
} else{
this.socket.emit('battle me', this.pseudo, guest);
this.notifier.notify("success", "Challenge sent !");
console.log("challenge sent");
return true;
}
}
......
......@@ -2,6 +2,6 @@
<div class="list-group">
<a class="list-group-item list-group-item-action" *ngFor="let user of users">
{{user}}
<a class="btn btn-primary" id="remove" (click)="challenge(user)">Battle</a>
<a class="btn btn-primary" id="challenge" (click)="challenge(user)">Battle</a>
</a>
</div>
\ No newline at end of file
import { Component, OnInit } from '@angular/core';
import { ChatService } from '../../chat.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Component({
......@@ -11,14 +12,25 @@ export class OnlinePlayersComponent implements OnInit {
users = new Set();
constructor(private chatService: ChatService) { }
constructor(private httpClient: HttpClient, private chatService: ChatService) { }
ngOnInit(): void {
this.chatService.currentUsers.subscribe(users => this.users = users);
}
challenge(user){
this.chatService.attaquer(user);
console.log("sending challenge");
if(this.chatService.attaquer(user)){
this.addPartie(user);
console.log("sending challenge");
}
}
addPartie(user){
const headers = new HttpHeaders()
.set('Content-Type', 'application/json');
this.httpClient.post('http://localhost:3000/addpartie', {pseudo: user}, { headers: headers, withCredentials: true}).subscribe(
(response) => console.log(response),
(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