Feat: Adding GUILDS! #12

Merged
snxraven merged 21 commits from guilds into main 2024-07-07 23:01:33 -04:00
Showing only changes of commit fd02cff23f - Show all commits

38
app.js
View File

@ -339,7 +339,15 @@ function handleIncomingMessage(messageObj) {
}
}
}
} else {
} else if (messageObj.type === 'guildJoin') {
const { guildTopic, guildData } = messageObj;
if (!config.guilds[guildTopic]) {
config.guilds[guildTopic] = guildData;
writeConfigToFile("./config.json");
renderGuildList();
joinGuild(guildTopic);
}
} else {
console.error('Received unknown message type:', messageObj);
}
}
@ -380,7 +388,19 @@ async function handleConnection(connection, info) {
connection.on('data', (data) => {
const messageObj = JSON.parse(data.toString());
eventEmitter.emit('onMessage', messageObj);
if (messageObj.type === 'guildJoin') {
const guildData = config.guilds[messageObj.guildTopic];
if (guildData) {
const guildJoinMessage = JSON.stringify({
type: 'guildJoin',
guildTopic: messageObj.guildTopic,
guildData: guildData
});
connection.write(guildJoinMessage);
}
} else {
eventEmitter.emit('onMessage', messageObj);
}
});
connection.on('close', () => {
@ -555,6 +575,20 @@ async function joinChatRoom(e) {
const roomTopicBuffer = b4a.from(roomTopic, 'hex');
addRoomToList(guildTopic, roomTopic);
await joinSwarm(roomTopicBuffer);
// Fetch guild data from a peer
const guildJoinMessage = JSON.stringify({
type: 'guildJoin',
guildTopic: guildTopic
});
const guildSwarm = activeRooms.find(room => room.topic === guildTopic);
if (guildSwarm) {
const peers = [...guildSwarm.swarm.connections];
for (const peer of peers) {
peer.write(guildJoinMessage);
}
}
}
async function joinSwarm(topicBuffer) {