Block fixed, looking for invite in game
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import { navigateTo } from "../../index.js";
|
||||
import {create_popup} from "../../utils/noticeUtils.js";
|
||||
|
||||
class Notice {
|
||||
@ -7,9 +8,8 @@ class Notice {
|
||||
|
||||
// users online, invited by, asked friend by,
|
||||
let data_variable = ["online", "invited", "asked"];
|
||||
for (let i in data_variable) {
|
||||
for (let i in data_variable)
|
||||
this.data[data_variable[i]] = [];
|
||||
}
|
||||
|
||||
this.connect();
|
||||
|
||||
@ -20,12 +20,14 @@ class Notice {
|
||||
|
||||
this.chatSocket = new WebSocket(url);
|
||||
this.chatSocket.onmessage = (event) =>{
|
||||
let data = JSON.parse(event.data);
|
||||
//console.log("notice: ", data);
|
||||
if (data.type == "invite")
|
||||
this.receiveInvite(data);
|
||||
else if (data.type == "online_users" || data.type == "disconnect")
|
||||
this.receiveOnlineUser(data);
|
||||
let send = JSON.parse(event.data);
|
||||
console.log("notice: ", send);
|
||||
if (send.type == "invite")
|
||||
this.receiveInvite(send);
|
||||
else if (send.type == "online_users" || send.type == "disconnect")
|
||||
this.receiveOnlineUser(send);
|
||||
else if (send.type == "accept_invite")
|
||||
this.receiveAcceptInvite(send);
|
||||
}
|
||||
this.chatSocket.onopen = (event) => {
|
||||
this.getOnlineUser();
|
||||
@ -40,61 +42,96 @@ class Notice {
|
||||
|
||||
}
|
||||
|
||||
async sendInvite(id_inviter, id_inviteds) {
|
||||
|
||||
if (this.chatSocket == undefined)
|
||||
return;
|
||||
async acceptInvite(invitedBy) {
|
||||
|
||||
this.chatSocket.send(JSON.stringify({
|
||||
type: "invite",
|
||||
targets: id_inviteds,
|
||||
}));
|
||||
this.sendRequest({
|
||||
type: "accept_invite",
|
||||
targets: [invitedBy],
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
async receiveInvite(data) {
|
||||
async receiveAcceptInvite(send) {
|
||||
|
||||
let id_game = send["result"];
|
||||
navigateTo("/game/" + id_game);
|
||||
|
||||
}
|
||||
|
||||
async refuseInvite(invitedBy) {
|
||||
|
||||
this.sendRequest({
|
||||
type: "refuse_invite",
|
||||
targets: [invitedBy],
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
async sendInvite(id_inviteds) {
|
||||
|
||||
this.sendRequest({
|
||||
type: "invite",
|
||||
targets: id_inviteds,
|
||||
time: new Date().getTime(),
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
async receiveInvite(send) {
|
||||
|
||||
if (data.content === "notice return") {
|
||||
if (data.status == 200) {
|
||||
for (let target in data.targets)
|
||||
this.data["invited"].push(target);
|
||||
if (this.client.me == undefined)
|
||||
return ;
|
||||
|
||||
let content = send.content;
|
||||
|
||||
if (send.author_id == this.client.me.id) {
|
||||
if (send.status == 200) {
|
||||
for (let target in send.targets)
|
||||
return create_popup("Invitation send");
|
||||
}
|
||||
else if (data.status == 404)
|
||||
else if (send.status == 404)
|
||||
return create_popup("User not connected");
|
||||
else if (data.status == 409)
|
||||
else if (send.status == 409)
|
||||
return create_popup("Already invited");
|
||||
}
|
||||
else {
|
||||
let sender = await this.client.profiles.getProfile(data.author_id);
|
||||
|
||||
this.inviter.push(data.author_id);
|
||||
if (!content.includes(send.author_id) ||
|
||||
this.data["invited"].includes(send.author_id))
|
||||
return;
|
||||
|
||||
this.data["invited"] = content;
|
||||
let sender = await this.client.profiles.getProfile(send.author_id);
|
||||
|
||||
create_popup("Invitation received by " + sender.username);
|
||||
|
||||
if (this.rewrite_invite !== undefined)
|
||||
this.rewrite_invite();
|
||||
|
||||
// Géré la reception de l'invitation
|
||||
}
|
||||
}
|
||||
|
||||
async getOnlineUser() {
|
||||
|
||||
if (this.chatSocket == undefined)
|
||||
return;
|
||||
|
||||
this.online_users = {};
|
||||
|
||||
this.chatSocket.send(JSON.stringify({
|
||||
this.sendRequest({
|
||||
type: "online_users",
|
||||
targets: "all",
|
||||
}));
|
||||
targets: [],
|
||||
time: new Date().getTime(),
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
async receiveOnlineUser(data) {
|
||||
if (data.content !== undefined) {
|
||||
async receiveOnlineUser(send) {
|
||||
let content = send.content;
|
||||
if (content !== undefined) {
|
||||
|
||||
if (this.online_users.length > 0) {
|
||||
if (this.data["online"].length > 0) {
|
||||
// get all disconnect user
|
||||
let disconnects = this.online_users.filter(id => !Object.keys(data.content).includes(id));
|
||||
let disconnects = this.data["online"].filter(id => !Object.keys(content).includes(id));
|
||||
|
||||
// delete invite
|
||||
this.data["invited"] = this.data["invited"].filter(id => !disconnects.includes(id));
|
||||
@ -102,12 +139,19 @@ class Notice {
|
||||
//console.log(this.data["invited"]);
|
||||
}
|
||||
|
||||
this.data["online"] = Object.keys(data.content);
|
||||
this.data["online"] = Object.keys(content);
|
||||
|
||||
if (this.rewrite_usernames !== undefined)
|
||||
this.rewrite_usernames();
|
||||
}
|
||||
}
|
||||
|
||||
async sendRequest(content) {
|
||||
if (this.chatSocket == undefined)
|
||||
return;
|
||||
|
||||
this.chatSocket.send(JSON.stringify(content));
|
||||
}
|
||||
}
|
||||
|
||||
export {Notice}
|
||||
|
Reference in New Issue
Block a user