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

challenge sending and receiving with backend, no socket yet

parent c6778c37
......@@ -39,4 +39,28 @@ exports.getDemandes = (req, res) => {
res.send({res: false, body:'You must be logged in to see your games !'});
res.end();
}
};
exports.accept = (req, res) => {
if (req.session.loggedIn) {
Partie.accept(req.body.id_partie, (err, data) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while accepting the challenge."
});
else res.send(data);
});
}
};
exports.refuse = (req, res) => {
if (req.session.loggedIn) {
Partie.refuse(req.body.id_partie, (err, data) => {
if (err)
res.status(500).send({
message: err.message || "Some error occurred while deleting the challenge."
});
else res.send(data);
});
}
};
\ No newline at end of file
......@@ -41,4 +41,33 @@ Partie.getDemandes = (uid, result) => {
});
};
Partie.accept = (id_partie, result) => {
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.refuse = (id_partie, result) => {
sql.query("DELETE FROM partie WHERE id_partie=?", [id_partie], (err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
if (res.affectedRows == 0) {
result({ kind: "not_found" }, null);
return;
}
console.log("deleted game with id: ", id_partie);
result(null, {res: true, body: res});
});
};
module.exports = Partie;
\ No newline at end of file
......@@ -6,4 +6,8 @@ module.exports = app => {
app.get("/getparties", partie.getDemandes);
app.post("/acceptpartie", partie.accept);
app.post("/refusepartie", partie.refuse);
};
\ No newline at end of file
......@@ -20,7 +20,7 @@
<div class="players">
<h2>Challenges from other trainers</h2>
<div class="container py-3">
<div class="card" *ngFor="let partie of demandesExt">
<div class="card" *ngFor="let partie of demandesExt; let i=index">
<div class="row ">
<div class="col-md-8 px-3">
<div class="card-block px-3">
......@@ -29,8 +29,8 @@
</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>
<a class="btn btn-primary" id="accept" (click)="acceptChallenge(partie.id_partie, i)">Accept</a>
<a class="btn btn-primary" id="refuse" (click)="refuseChallenge(partie.id_partie, i)">Refuse</a>
</div>
</div>
</div>
......
import { Component, OnInit } from '@angular/core';
import { ChatService } from '../chat.service';
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { __assign } from 'tslib';
@Component({
......@@ -55,4 +55,33 @@ export class BattleMenuComponent implements OnInit {
}
}
acceptChallenge(id_partie, i){
const headers = new HttpHeaders()
.set('Content-Type', 'application/json');
this.http.post('http://localhost:3000/acceptpartie', {id_partie: id_partie}, { headers: headers, withCredentials: true}).subscribe(
(response: any) => {
console.log(response);
if(response.res) {
this.curParties.push(this.demandesExt[i]);
this.demandesExt.splice(i, 1);
}
},
(error) => console.log(error)
)
}
refuseChallenge(id_partie, i){
const headers = new HttpHeaders()
.set('Content-Type', 'application/json');
this.http.post('http://localhost:3000/refusepartie', {id_partie: id_partie}, { headers: headers, withCredentials: true}).subscribe(
(response: any) => {
console.log(response);
if(response.res) {
this.demandesExt.splice(i, 1);
}
},
(error) => console.log(error)
)
}
}
......@@ -101,6 +101,18 @@ export class ChatService {
}
}
public challengeAccepted(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); // nouvel événement .....
this.notifier.notify("success", "Challenge sent !");
console.log("challenge sent");
return true;
}
}
public recevoirChallenge(){
let observable = new Observable(observer => {
this.socket.on('demande combat', (data) => {
......
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