can invite to play a game in chat

This commit is contained in:
2024-05-14 14:36:04 +02:00
parent 96a5094fd2
commit 1135014d0f
7 changed files with 69 additions and 52 deletions

View File

@ -1,6 +1,6 @@
import {Client} from './Client.js';
import {createNotification} from '../utils/noticeUtils.js'
import { lastView } from '../index.js';
import { lastView, navigateTo } from '../index.js';
import ProfilePageView from '../views/ProfilePageView.js';
import Search from '../views/Search.js';
@ -17,13 +17,12 @@ export default class Notice {
this.url = location.origin.replace('http', 'ws') + '/ws/notice';
}
start() {
async start() {
this._socket = new WebSocket(this.url);
this._socket.onclose = _ => this._socket = undefined;
this._socket.onmessage = message => {
this._socket.onmessage = async message => {
const data = JSON.parse(message.data);
console.log(data)
if (data.type === 'friend_request') {
this.friend_request(data.author);
@ -38,13 +37,11 @@ export default class Notice {
} else if (data.type === 'offline') {
this.offline(data.user)
} else if (data.type == 'game_asked') {
} else if (data.type == 'game_canceled') {
this.game_asked(data.asker);
} else if (data.type == 'game_accepted') {
this.game_accepted(data.asked, data.id_game);
} else if (data.type == 'game_refused') {
this.game_refused(data.asked);
}
};
}
@ -106,4 +103,24 @@ export default class Notice {
lastView.loadFriendshipStatus();
}
}
game_asked(asker) {
createNotification('Game Invite', `<b>${asker}</b> ask to play a 1vs1 pong`);
if (lastView instanceof Search)
lastView.display_invite();
}
game_refused(asked) {
createNotification('Game Refused', `<b>${asked}</b> refuse your proposition to play`);
if (lastView instanceof Search)
lastView.display_invite();
}
async game_accepted(asked, id_game) {
createNotification('Game Accepted', `<b>${asked}</b> accept your proposition to play`);
if (lastView instanceof Search)
lastView.display_invite();
await navigateTo(`/games/pong/${id_game}`);
}
}

View File

@ -1,3 +1,5 @@
import { lastView, navigateTo } from '../../index.js';
import Search from '../../views/Search.js';
export default class Ask {
constructor(client) {
@ -10,16 +12,19 @@ export default class Ask {
});
}
async ask_game_canceled() {
}
async ask_game_accepted(asker) {
let response = await this.client._post(`/api/chat/ask/accept`, {
let response = await this.client._post(`/api/chat/ask/accept/`, {
asker:asker,
});
console.log(response.status);
const statu = response.status;
if (statu == 404 || statu == 204)
return
if (lastView instanceof Search)
lastView.display_invite();
const data = await response.json();
await navigateTo(`/games/pong/${data.id_game}`);
}
async ask_game_refused(asker) {
@ -27,14 +32,17 @@ export default class Ask {
asker:asker,
});
console.log(response.status);
const statu = response.status;
if (statu == 404 || statu == 204)
return
if (lastView instanceof Search)
lastView.display_invite();
}
async is_asked(asked) {
let response = await this.client._get(`/api/chat/ask/${asked}`);
const statu = response.status;
console.log(statu);
if (statu == 404 || statu == 204)
return false;
return true;

View File

@ -69,7 +69,7 @@ export default class extends AbstractView {
// Permet de savoir si c'est le même channel
// Check to know if it's the same channel
this.channelManager.channel.members_id.forEach((member_id) => {
this.channelManager.channel.members.forEach((member_id) => {
if (member_id == user.id)
this.channelManager.channel = undefined;
});