Block fixed, looking for invite in game
This commit is contained in:
@ -78,7 +78,6 @@
|
||||
border: none;
|
||||
outline: none;
|
||||
border-bottom: 0.15em solid green;
|
||||
caret-color: green;
|
||||
color: green;
|
||||
font-size: 0.8em;
|
||||
}
|
||||
@ -106,9 +105,8 @@
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
#app #invite {
|
||||
#app #invite, #app #yes, #app #no {
|
||||
position: relative;
|
||||
background-color: green;
|
||||
border: none;
|
||||
color: white;
|
||||
text-align: center;
|
||||
@ -118,3 +116,15 @@
|
||||
width: 4em;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#app #yes, #app #no {
|
||||
position: relative;
|
||||
border: none;
|
||||
color: white;
|
||||
text-align: center;
|
||||
text-decoration: none;
|
||||
font-size: 0.8em;
|
||||
height: 2em;
|
||||
width: 2em;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
@ -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}
|
||||
|
@ -30,7 +30,7 @@ class Client
|
||||
|
||||
this.channels = new Channels(this);
|
||||
this.channel = undefined;
|
||||
|
||||
|
||||
this.notice = new Notice(this);
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,7 @@ class Profile
|
||||
|
||||
let block_response = await this.client._get("/api/profiles/block");
|
||||
|
||||
|
||||
if (block_response.status != 200)
|
||||
return
|
||||
|
||||
@ -42,7 +43,7 @@ class Profile
|
||||
block_list.forEach(block => {
|
||||
let blocker = block.fields.blocker;
|
||||
let blocked = block.fields.blocked;
|
||||
if (blocker == this.client.me.user_id && blocked == user_id)
|
||||
if (blocker == this.client.me.id && blocked == this.id)
|
||||
return this.isBlocked = true;
|
||||
});
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ class Profiles
|
||||
|
||||
// blocker & blocked
|
||||
let response = await this.client._post("/api/profiles/block", {
|
||||
users_id:[this.client.me.user_id, user_id],
|
||||
users_id:[this.client.me.id, user_id],
|
||||
});
|
||||
|
||||
let data = await response.json();
|
||||
@ -49,7 +49,7 @@ class Profiles
|
||||
|
||||
// blocker & blocked
|
||||
let response = await this.client._delete("/api/profiles/block", {
|
||||
users_id:[this.client.me.user_id, user_id],
|
||||
users_id:[this.client.me.id, user_id],
|
||||
});
|
||||
|
||||
let data = await response.json();
|
||||
|
@ -8,6 +8,7 @@ export default class extends AbstractView {
|
||||
async getHtml() {
|
||||
return `
|
||||
<h1>404 Bozo</h1>
|
||||
<img src="https://media.giphy.com/media/pm0BKtuBFpdM4/giphy.gif">
|
||||
<p>Git gud</p>
|
||||
`;
|
||||
}
|
||||
|
@ -31,9 +31,11 @@ export default class extends AbstractView {
|
||||
//await client.notice.getOnlineUser();
|
||||
await this.wait_get_online_users();
|
||||
client.notice.rewrite_usernames = this.rewrite_usernames;
|
||||
client.notice.rewrite_invite = this.display_invite;
|
||||
|
||||
let search = document.getElementById("input_user");
|
||||
search.oninput = () => this.display_users(logged, profiles);
|
||||
if (search != undefined)
|
||||
search.oninput = () => this.display_users(logged, profiles);
|
||||
|
||||
let chat_input = document.getElementById("input_chat");
|
||||
//chat_input.addEventListener("keydown", this.display_chat_manager)
|
||||
@ -175,13 +177,15 @@ export default class extends AbstractView {
|
||||
chat_input.maxLength=255;
|
||||
chat.appendChild(chat_input);
|
||||
|
||||
let members_id = client.channels.channel.members_id;
|
||||
|
||||
chat_input.onkeydown = async () => {
|
||||
if (event.keyCode == 13 && client.channels.channel != undefined) {
|
||||
//let chat_input = document.getElementById("input_chat");
|
||||
let chat_text = chat_input.value;
|
||||
|
||||
let receivers_id = [];
|
||||
client.channels.channel.members_id.forEach((member_id) => {
|
||||
members_id.forEach((member_id) => {
|
||||
if (member_id != client.me.id)
|
||||
receivers_id.push(profiles.filter(user => user.id == member_id)[0].id);
|
||||
});
|
||||
@ -196,7 +200,7 @@ export default class extends AbstractView {
|
||||
// Scroll to the bottom of messages
|
||||
messages.scrollTop = messages.scrollHeight;
|
||||
|
||||
this.display_invite(chat);
|
||||
this.display_invite();
|
||||
|
||||
}
|
||||
|
||||
@ -242,10 +246,13 @@ export default class extends AbstractView {
|
||||
}
|
||||
|
||||
async display_members(chat, profiles) {
|
||||
|
||||
let members_id = client.channels.channel.members_id;
|
||||
|
||||
let members = document.createElement("h2");
|
||||
members.id = "members";
|
||||
let usernames = "";
|
||||
client.channels.channel.members_id.forEach((member_id) => {
|
||||
members_id.forEach((member_id) => {
|
||||
if (member_id != client.me.id) {
|
||||
if (usernames.length > 0)
|
||||
usernames += ", ";
|
||||
@ -258,17 +265,66 @@ export default class extends AbstractView {
|
||||
return members
|
||||
}
|
||||
|
||||
async display_invite(chat, profiles) {
|
||||
async display_invite() {
|
||||
|
||||
let chat = document.getElementById("chat");
|
||||
|
||||
if (chat == undefined)
|
||||
return ;
|
||||
|
||||
let members_id = client.channels.channel.members_id;
|
||||
let others = members_id.filter(id => id !== client.me.id);
|
||||
|
||||
// Button to send invite to play
|
||||
let invite = document.getElementById("invite") || document.createElement("button");
|
||||
invite.id = "invite";
|
||||
invite.innerText = "invite";
|
||||
invite.onclick = async () => {
|
||||
await client.notice.sendInvite(client.me.id,
|
||||
client.channels.channel.members_id.filter(id => id !== client.me.id));
|
||||
};
|
||||
chat.appendChild(invite);
|
||||
let yes = document.getElementById("yes") || document.createElement("button");
|
||||
let no = document.getElementById("no") || document.createElement("button");
|
||||
|
||||
let invitedBy = undefined;
|
||||
for (let x in others) {
|
||||
if (client.notice.data["invited"].includes(others[x])) {
|
||||
invitedBy = others[x];
|
||||
}
|
||||
}
|
||||
|
||||
if (invitedBy == undefined) {
|
||||
|
||||
if (yes && no) {
|
||||
yes.remove();
|
||||
no.remove();
|
||||
}
|
||||
|
||||
// Button to send invite to play
|
||||
invite.id = "invite";
|
||||
invite.style.background = "orange";
|
||||
invite.innerText = "invite";
|
||||
invite.title = "Invite to play a game"
|
||||
invite.onclick = async () => {
|
||||
await client.notice.sendInvite(others);
|
||||
};
|
||||
chat.appendChild(invite);
|
||||
}
|
||||
else {
|
||||
|
||||
if (invite)
|
||||
invite.remove()
|
||||
|
||||
yes.id = "yes";
|
||||
yes.style.background = "green";
|
||||
yes.title = "Accept to play a game"
|
||||
yes.onclick = async () => {
|
||||
await client.notice.acceptInvite(invitedBy);
|
||||
};
|
||||
|
||||
no.id = "no";
|
||||
no.style.background = "red";
|
||||
no.title = "Refuse to play a game"
|
||||
no.onclick = async () => {
|
||||
await client.notice.refuseInvite(invitedBy);
|
||||
};
|
||||
|
||||
chat.appendChild(yes);
|
||||
chat.appendChild(no);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user