Added remove. Updated friend list. Fixed friend accept notification.

This commit is contained in:
MaksSlyzar 2026-04-03 01:25:20 +03:00
parent 69f5523454
commit d245eebac1
5 changed files with 25 additions and 122 deletions

View File

@ -30,7 +30,7 @@ const NotificationsTab = () => {
const handleAction = (id, action, data) => {
if (action === "accept_friend") {
socket.emit("friend:accept", { friendId: data.fromId });
socket.emit("friend:accept", { id, friendId: data.fromId });
socket.emit("notification:read", { id });
} else if (action === "dismiss") {
socket.emit("notification:dismiss", { id });

View File

@ -21,7 +21,19 @@ class SocialManager {
attributes: ["id", "username", "level"],
});
}
async removeFriend(myId, friendId) {
await Friend.destroy({
where: {
[Op.or]: [
{ playerId: myId, friendId: friendId },
{ playerId: friendId, friendId: myId },
],
},
});
await this.broadcastFriendListUpdate(myId);
await this.broadcastFriendListUpdate(friendId);
}
async sendFriendRequest(sender, targetId) {
await notificationManager.send({
playerId: targetId,
@ -43,7 +55,6 @@ class SocialManager {
{ playerId: myId, friendId: friendId },
{ playerId: friendId, friendId: myId },
]);
await notificationManager.delete(notificationId, myId);
await this.broadcastFriendListUpdate(myId);

View File

@ -3,6 +3,7 @@ const Player = require("./Player");
const Inventory = require("./Inventory");
const setupAssociations = require("./associations");
const Notification = require("./Notification");
const Friend = require("./Friend.js");
Player.hasMany(Inventory, { foreignKey: "playerId", as: "inventory" });
Inventory.belongsTo(Player, { foreignKey: "playerId" });
@ -13,4 +14,5 @@ module.exports = {
Player,
Inventory,
Notification,
Friend,
};

View File

@ -1,114 +0,0 @@
const Player = require("../../models/Player");
const Friend = require("../../models/Friend");
const NotificationManager = require("../../game/NotificationManager");
const Notification = require("../../models/Notification");
module.exports = (io, socket) => {
socket.on("player:search", async ({ query }) => {
try {
const players = await Player.findAll({
where: {
username: { [require("sequelize").Op.like]: `%${query}%` },
id: { [require("sequelize").Op.ne]: socket.user.id },
},
limit: 5,
attributes: ["id", "username", "level"],
});
socket.emit("player:search_results", players);
} catch (e) {
console.error(e);
}
});
socket.on("friend:add", async ({ friendId }) => {
try {
const player = socket.user;
await NotificationManager.createNotification({
playerId: friendId,
type: "friend_request",
title: "NEW FRIEND REQUEST",
message: `${player.username} wants to add you as a friend.`,
data: { fromId: player.id },
});
io.to(friendId).emit("notification:new", {
type: "friend_request",
title: "NEW FRIEND REQUEST",
message: `${player.username} wants to add you as a friend.`,
data: { fromId: player.id },
createdAt: new Date(),
});
} catch (e) {
socket.emit("error", { message: "FAILED_TO_SEND_REQUEST" });
}
});
socket.on("friend:accept", async ({ friendId, notificationId }) => {
try {
const myId = socket.user.id;
const exists = await Friend.findOne({
where: { playerId: myId, friendId: friendId },
});
console.log(myId, friendId);
if (!exists) {
await Friend.bulkCreate([
{ playerId: myId, friendId: friendId },
{ playerId: friendId, friendId: myId },
]);
await Notification.destroy({
where: { id: notificationId, playerId: myId },
});
const myUpdated = await Player.findByPk(myId, {
include: [
{
model: Player,
as: "Friends",
attributes: ["id", "username", "level"],
},
],
});
socket.emit("friend:list", myUpdated.Friends || []);
const friendUpdated = await Player.findByPk(friendId, {
include: [
{
model: Player,
as: "Friends",
attributes: ["id", "username", "level"],
},
],
});
io.to(friendId).emit("friend:list", friendUpdated.Friends || []);
const unreadCount = await Notification.count({
where: { playerId: myId, isRead: false },
});
socket.emit("notifications:unread_count", unreadCount);
}
} catch (e) {
console.error(e);
socket.emit("error", { message: "FAILED_TO_ACCEPT_FRIEND" });
}
});
socket.on("friend:get_list", async () => {
try {
const player = await Player.findByPk(socket.user.id, {
include: [
{
model: Player,
as: "Friends",
attributes: ["id", "username", "level"],
},
],
});
socket.emit("friend:list", player.Friends || []);
} catch (e) {
console.error(e);
}
});
};

View File

@ -20,13 +20,17 @@ module.exports = (io, socket) => {
}
});
socket.on("friend:accept", async ({ friendId, notificationId }) => {
socket.on("friend:remove", async ({ friendId }) => {
try {
await socialManager.acceptFriendRequest(
socket.user.id,
friendId,
notificationId,
);
await socialManager.removeFriend(socket.user.id, friendId);
} catch (e) {
console.error(e);
socket.emit("error", { message: "FAILED_TO_REMOVE_FRIEND" });
}
});
socket.on("friend:accept", async ({ friendId, id }) => {
try {
await socialManager.acceptFriendRequest(socket.user.id, friendId, id);
} catch (e) {
console.error(e);
socket.emit("error", { message: "FAILED_TO_ACCEPT_FRIEND" });