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}`);
}
}