Skip to content

Commit

Permalink
better friend requests
Browse files Browse the repository at this point in the history
  • Loading branch information
evolutionleo committed Dec 25, 2023
1 parent cea453a commit 33c9f66
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
6 changes: 0 additions & 6 deletions Client/options/extensions/double_window.json

This file was deleted.

2 changes: 0 additions & 2 deletions Client/scripts/HandlePacket/handlepacket.gml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
global.cmd_handlers = {}

///@function addHandler(cmd, handler)
///@param {String} cmd
///@param {Function} handler
Expand Down
10 changes: 9 additions & 1 deletion Client/scripts/friendHandlers/friendHandlers.gml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
addHandler("friend list", function(data) {
trace(data.friends)
trace("friends:", data.friends)
global.friends = data.friends
})

Expand All @@ -16,4 +16,12 @@ addHandler("friend req out", function(data) {
addHandler("friend req new", function(data) {
trace(data.from)
array_push(global.friend_requests_inc, data.from)
})

addHandler("friend added", function(data) {
trace("friend added!")
})

addHandler("friend req accepted", function(data) {
trace("friend request from % accepted!", data.name)
})
23 changes: 14 additions & 9 deletions TypescriptServer/src/cmd/handlers/friends.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import addHandler from "#cmd/handlePacket";
import FriendRequest from "#schemas/friend_request";
import { IProfile } from "#schemas/profile";

addHandler('friend req inc', async (c, data) => {
let from_profiles = await c.getIncomingFriendRequests();
Expand All @@ -16,18 +18,20 @@ addHandler('friend list', async (c, data) => {
});

addHandler('friend req send', (c, data) => {
c.friendRequestSend(data.friend);
if (!c.profile.friends.includes((data.friend as IProfile)._id) && !FriendRequest.exists({ sender: data.friend._id }))
c.friendRequestSend(data.friend);

c.send({ cmd: 'friend req sent', to: data.friend.name });
});

addHandler('friend req accept', (c, data) => {
c.friendRequestAccept(data.friend);
c.send({ cmd: 'friend req accepted', from: data.friend.name });
addHandler('friend req accept', async (c, data) => {
if (await c.friendRequestAccept(data.friend))
c.send({ cmd: 'friend req accepted', from: data.friend.name });
});

addHandler('friend req reject', (c, data) => {
c.friendRequestReject(data.friend);
c.send({ cmd: 'friend req rejected', from: data.friend.name });
addHandler('friend req reject', async (c, data) => {
if (await c.friendRequestReject(data.friend))
c.send({ cmd: 'friend req rejected', from: data.friend.name });
});

addHandler('friend req cancel', (c, data) => {
Expand All @@ -36,8 +40,9 @@ addHandler('friend req cancel', (c, data) => {
});

addHandler('friend add', async (c, data) => {
c.friendAdd(data.friend);
c.send({ cmd: 'friend added', name: data.friend.name });
let res = await c.friendAdd(data.friend);
if (res)
c.send({ cmd: 'friend added', name: data.friend.name });
});

addHandler('friend remove', async (c, data) => {
Expand Down
31 changes: 26 additions & 5 deletions TypescriptServer/src/concepts/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,29 @@ export default class Client extends SendStuff implements IClient {
return await FriendRequest.findOutgoing(this.profile._id);
}

async friendCanAdd(friend: Client|IProfile): Promise<boolean> {
friend = friend instanceof Client ? friend.profile : friend;
if (!this.logged_in) return false;

let this_id = this.profile._id;
let friend_id = friend._id;

return this_id != friend_id && !(await this.getFriendIds()).includes(friend_id);
}

/**
* Send a new friend request or accept an existing one from the user
* @param friend {IProfile|Client}
*/
async friendAdd(friend: Client | IProfile): Promise<boolean> {
friend = friend instanceof Client ? friend.profile : friend;
if (!this.logged_in) return false;
if (!await this.friendCanAdd(friend)) return false;

let sender_id = this.profile._id;
let receiver_id = friend._id;

let friend_exists = this.profile.friends.includes(receiver_id);
let friend_exists = this.profile.friends.some(friend_id => friend_id === receiver_id);
if (friend_exists) { // already friends
trace('already friends');
return false;
Expand Down Expand Up @@ -354,56 +365,66 @@ export default class Client extends SendStuff implements IClient {
*/
async friendRequestAccept(user_from:Client|IProfile) {
user_from = user_from instanceof Client ? user_from.profile : user_from;
if (!this.logged_in) return;
if (!this.logged_in) return false;

// find a request FROM the user
let inc_request_id = await this.friendRequestFind(user_from, this);
if (inc_request_id) {
await FriendRequest.accept(inc_request_id); // this method also updates the .friends arrays
return true;
}

return false;
}

/**
* @param {Client|IProfile} user_from
*/
async friendRequestReject(user_from:Client|IProfile) {
user_from = user_from instanceof Client ? user_from.profile : user_from;
if (!this.logged_in) return;
if (!this.logged_in) return false;

// find a request FROM the user
let inc_request_id = await this.friendRequestFind(user_from, this);
if (inc_request_id) {
await FriendRequest.reject(inc_request_id);
return true;
}

return false;
}

/**
* @param {Client|IProfile} user_to
*/
async friendRequestCancel(user_to:Client|IProfile) {
user_to = user_to instanceof Client ? user_to.profile : user_to;
if (!this.logged_in) return null;
if (!this.logged_in) return false;

// find a request from us TO the user
let req_id = await this.friendRequestFind(this, user_to);
if (req_id) {
await FriendRequest.cancel(req_id);
return true;
}
return false;
}

/**
* @param {Client|IProfile} friend
*/
async friendRemove(friend:Client|IProfile) {
friend = friend instanceof Client ? friend.profile : friend;
if (!this.logged_in) return;
if (!this.logged_in) return false;

let my_id = this.profile._id;
let friend_id = friend._id;

// delete from each others' profiles
await Profile.findByIdAndUpdate(my_id, { $pull: { friends: friend_id }});
await Profile.findByIdAndUpdate(friend_id, { $pull: { friends: my_id }});

return true;
}


Expand Down

0 comments on commit 33c9f66

Please sign in to comment.