mirror of
https://gitea.tendokyu.moe/CharaLol/Yukiotoko.git
synced 2026-10-06 05:37:53 +03:00
Initial Unfinished Commit
Essentially a baseline of how i want different sockets to be structured
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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<number, UdpClass>();
|
||||
|
||||
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)
|
||||
|
||||
Generated
+31
@@ -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':
|
||||
|
||||
Reference in New Issue
Block a user