From cfae1b66ecc697c1c7b78f5061df307f36d32894 Mon Sep 17 00:00:00 2001 From: ItsCharaHere Date: Wed, 25 Jun 2025 21:21:14 -0700 Subject: [PATCH] Initial Unfinished Commit Essentially a baseline of how i want different sockets to be structured --- README.md | 3 + apps/server/reflector/src/index.ts | 92 +++++++++++++----------------- pnpm-lock.yaml | 31 ++++++++++ 3 files changed, 74 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 98b7424..2918b56 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ docker compose up -d # Install required dependencies pnpm install +# Build Packages +pnpm build:package + # Sync the database with the current schema pnpm db:push diff --git a/apps/server/reflector/src/index.ts b/apps/server/reflector/src/index.ts index 8b43098..37570b9 100644 --- a/apps/server/reflector/src/index.ts +++ b/apps/server/reflector/src/index.ts @@ -1,19 +1,18 @@ import { db } from '@yukiotoko/db' +import { Console } from 'console' import udp from 'dgram' -var UDPHolepunch = udp.createSocket('udp4') +var UdpMaster = udp.createSocket('udp4') -UDPHolepunch.on('error', function (error) { +UdpMaster.on('error', function (error) { console.log('Error: ' + error) - UDPHolepunch.close() + UdpMaster.close() }) -// this code is kinda bad sorry -UDPHolepunch.on('message', async function (msg, info) { +UdpMaster.on('message', async function (msg, info) { console.log("UDP Packet recieved from "+info.address+info.port+" "+msg.toString()) var ParsedJson = JSON.parse(msg.toString()) - // Find the merged room first thing - + const newroom = await db.chusanUserMatching.findFirst({ where: { roomId: Number(ParsedJson.roomId) @@ -49,53 +48,42 @@ UDPHolepunch.on('message', async function (msg, info) { } }) } - var room = await db.chusanUserRooms.findMany({ - where: { - roomId: Number(ParsedJson.roomId), - }, - distinct: ['clientId'] - }) - - var packet = Buffer.from(`{"isFinish": "false","myAddress": "udp://${info.address}:${info.port}","matchingMemberAddressList": []`) - if (memberlist?.matchingMemberInfoList.length == room.length){ // All Clients have submitted IP Information - var memberreturn = ``; - const playerlist = await db.chusanUserMatching.findFirst({// Find every unique user in the room with the same room id - where: { - roomId: Number(ParsedJson.roomId), - }, - }); - if (playerlist && playerlist.matchingMemberInfoList) { - for (const user of playerlist.matchingMemberInfoList) { - if (user!['clientId'] == null){ - return - } - const player = await db.chusanUserRooms.findFirst({ - where: { - clientId: user!['clientId'], - }, - }); - if (player!.clientId != ParsedJson.clientId ) { - memberreturn += `{"clientId": "${user!['clientId']}","ipAddress": "${player!.ipAddress}"},`; - } - } - }//fuck you lol - packet = Buffer.from( - `{"isFinish": "true","myAddress": "udp://${info.address}:${info.port}","matchingMemberAddressList": [${memberreturn.slice(0, -1)}]}`, - ); - //add all users into the final array + + mappedUDP.set(info.port, new UdpClass(info.address, info.port)) +}) + +UdpMaster.on('listening', function () { + console.log("UDPHolepunch: 50201") +}) + +// Master UDPHolePunch +UdpMaster.bind(50201) + +var mappedUDP = new Map(); + +class UdpClass { + public port: number + public ip: string + constructor(ip: string, port: number){ + this.port = port + this.ip = ip + var UDPClient = udp.createSocket('udp4') + console.log("Opening Socket on Port " + port) + UDPClient.bind(port) + UDPClient.on('message', async function (msg, info) { + UDPClient.send(msg, port, ip) + }) } - await UDPHolepunch.send(packet, info.port, info.address, function (error) { - if (error) { - console.log(error) - } - console.log("Sent "+packet.toString()+" to "+info.address+info.port) - }) -}) -UDPHolepunch.on('listening', function () { - console.log("UDPHolepunch: 50201 (Lite)") -}) +} -UDPHolepunch.bind(50201) +var UDPClient1 = udp.createSocket('udp4') +var UDPClient2 = udp.createSocket('udp4') +var UDPClient3 = udp.createSocket('udp4') +var UDPClient4 = udp.createSocket('udp4') +UDPClient1.bind(50501) +UDPClient2.bind(50502) +UDPClient3.bind(50503) +UDPClient4.bind(50504) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ded35b0..9789c68 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -135,6 +135,37 @@ importers: specifier: 4.17.21 version: 4.17.21 + apps/site/api: + dependencies: + '@yukiotoko/core': + specifier: workspace:* + version: link:../../../packages/core + '@yukiotoko/db': + specifier: workspace:* + version: link:../../../packages/db + '@yukiotoko/logger': + specifier: workspace:* + version: link:../../../packages/logger + body-parser: + specifier: 1.20.3 + version: 1.20.3 + compression: + specifier: 1.7.4 + version: 1.7.4 + express: + specifier: 4.18.2 + version: 4.18.2 + iconv-lite: + specifier: 0.6.3 + version: 0.6.3 + devDependencies: + '@types/compression': + specifier: 1.7.5 + version: 1.7.5 + '@types/express': + specifier: 4.17.21 + version: 4.17.21 + packages/core: dependencies: '@yukiotoko/db':