Fix issues with joining
This commit is contained in:
1 file changed
+99
-70
+99
-70
@@ -109,7 +109,6 @@ interface ClientInfoData {
|
|||||||
|
|
||||||
interface MultiplayerClient {
|
interface MultiplayerClient {
|
||||||
Id: string
|
Id: string
|
||||||
Key: string
|
|
||||||
PosX: number
|
PosX: number
|
||||||
PosY: number
|
PosY: number
|
||||||
Flipped: boolean
|
Flipped: boolean
|
||||||
@@ -289,6 +288,7 @@ app.ws('/ws', {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const { connection: connection0, db: db0 } = dbInfo0
|
const { connection: connection0, db: db0 } = dbInfo0
|
||||||
const { connection: connection1, db: db1 } = dbInfo1
|
const { connection: connection1, db: db1 } = dbInfo1
|
||||||
|
|
||||||
@@ -298,6 +298,7 @@ app.ws('/ws', {
|
|||||||
db0,
|
db0,
|
||||||
ip
|
ip
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!authResult.valid) {
|
if (!authResult.valid) {
|
||||||
connection0.end()
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
@@ -310,9 +311,10 @@ app.ws('/ws', {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const userId = authResult.id
|
const userId = authResult.id
|
||||||
|
|
||||||
const result = await db0
|
const userResult = await db0
|
||||||
.select({
|
.select({
|
||||||
username: users.username,
|
username: users.username,
|
||||||
admin: users.admin,
|
admin: users.admin,
|
||||||
@@ -322,7 +324,7 @@ app.ws('/ws', {
|
|||||||
.where(eq(users.id, userId))
|
.where(eq(users.id, userId))
|
||||||
.execute()
|
.execute()
|
||||||
|
|
||||||
if (result.length != 1) {
|
if (userResult.length !== 1) {
|
||||||
connection0.end()
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
ws.send({
|
ws.send({
|
||||||
@@ -335,68 +337,120 @@ app.ws('/ws', {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const players: any = []
|
const currentUserData = await db1
|
||||||
for (const marketplaceInfo of multiplayerClients) {
|
|
||||||
const userData = await db1
|
|
||||||
.select({
|
.select({
|
||||||
saveData: berryDashUserData.saveData
|
saveData: berryDashUserData.saveData
|
||||||
})
|
})
|
||||||
.from(berryDashUserData)
|
.from(berryDashUserData)
|
||||||
.where(and(eq(berryDashUserData.id, marketplaceInfo.UserID)))
|
.where(eq(berryDashUserData.id, userId))
|
||||||
.execute()
|
.execute()
|
||||||
|
|
||||||
const savedata = userData[0].saveData
|
const currentSavedata = currentUserData[0]?.saveData
|
||||||
? JSON.parse(userData[0].saveData)
|
? JSON.parse(currentUserData[0].saveData)
|
||||||
: null
|
: null
|
||||||
if (!savedata) {
|
|
||||||
|
if (!currentSavedata) {
|
||||||
connection0.end()
|
connection0.end()
|
||||||
connection1.end()
|
connection1.end()
|
||||||
|
ws.send({
|
||||||
|
type: 'multiplayer_join_response',
|
||||||
|
accepted: false,
|
||||||
|
denyReason: 'Unable to load save data',
|
||||||
|
id: null,
|
||||||
|
players: null
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const customIcon = savedata.bird?.customIcon?.selected ?? null
|
const currentCustomIcon =
|
||||||
const icon = customIcon ? 0 : savedata?.icon ?? 1
|
currentSavedata.bird?.customIcon?.selected ?? null
|
||||||
const overlay = customIcon ? 1 : savedata?.overlay ?? 0
|
const currentIcon = currentCustomIcon
|
||||||
const iconColor = customIcon
|
? 0
|
||||||
|
: currentSavedata.bird?.icon ?? 1
|
||||||
|
const currentOverlay = currentCustomIcon
|
||||||
|
? 1
|
||||||
|
: currentSavedata.bird?.overlay ?? 0
|
||||||
|
const currentIconColor = currentCustomIcon
|
||||||
? [255, 255, 255]
|
? [255, 255, 255]
|
||||||
: savedata?.settings?.colors?.icon ?? [255, 255, 255]
|
: currentSavedata?.settings?.colors?.icon ?? [255, 255, 255]
|
||||||
const overlayColor = customIcon
|
const currentOverlayColor = currentCustomIcon
|
||||||
? [255, 255, 255]
|
? [255, 255, 255]
|
||||||
: savedata?.settings?.colors?.overlay ?? [255, 255, 255]
|
: currentSavedata?.settings?.colors?.overlay ?? [255, 255, 255]
|
||||||
|
|
||||||
|
const players: any = []
|
||||||
|
for (const marketplaceInfo of multiplayerClients) {
|
||||||
|
const playerUserData = await db1
|
||||||
|
.select({
|
||||||
|
saveData: berryDashUserData.saveData
|
||||||
|
})
|
||||||
|
.from(berryDashUserData)
|
||||||
|
.where(eq(berryDashUserData.id, marketplaceInfo.UserID))
|
||||||
|
.execute()
|
||||||
|
|
||||||
|
const playerSavedata = playerUserData[0]?.saveData
|
||||||
|
? JSON.parse(playerUserData[0].saveData)
|
||||||
|
: null
|
||||||
|
|
||||||
|
if (!playerSavedata) continue
|
||||||
|
|
||||||
|
const playerInfo = await db0
|
||||||
|
.select({
|
||||||
|
username: users.username,
|
||||||
|
admin: users.admin
|
||||||
|
})
|
||||||
|
.from(users)
|
||||||
|
.where(eq(users.id, marketplaceInfo.UserID))
|
||||||
|
.execute()
|
||||||
|
|
||||||
|
if (playerInfo.length !== 1) continue
|
||||||
|
|
||||||
|
const playerCustomIcon =
|
||||||
|
playerSavedata.bird?.customIcon?.selected ?? null
|
||||||
|
const playerIcon = playerCustomIcon
|
||||||
|
? 0
|
||||||
|
: playerSavedata.bird?.icon ?? 1
|
||||||
|
const playerOverlay = playerCustomIcon
|
||||||
|
? 1
|
||||||
|
: playerSavedata.bird?.overlay ?? 0
|
||||||
|
const playerIconColor = playerCustomIcon
|
||||||
|
? [255, 255, 255]
|
||||||
|
: playerSavedata?.settings?.colors?.icon ?? [255, 255, 255]
|
||||||
|
const playerOverlayColor = playerCustomIcon
|
||||||
|
? [255, 255, 255]
|
||||||
|
: playerSavedata?.settings?.colors?.overlay ?? [255, 255, 255]
|
||||||
|
|
||||||
players.push({
|
players.push({
|
||||||
id: marketplaceInfo.UserID,
|
id: marketplaceInfo.UserID,
|
||||||
username: result[0].username,
|
username: playerInfo[0].username,
|
||||||
admin: result[0].admin,
|
admin: playerInfo[0].admin,
|
||||||
timestamp: marketplaceInfo.JoinTimestamp,
|
timestamp: marketplaceInfo.JoinTimestamp,
|
||||||
iconData: {
|
iconData: {
|
||||||
icon,
|
icon: playerIcon,
|
||||||
overlay,
|
overlay: playerOverlay,
|
||||||
iconColor,
|
iconColor: playerIconColor,
|
||||||
overlayColor,
|
overlayColor: playerOverlayColor,
|
||||||
customIcon: customIcon ?? ''
|
customIcon: playerCustomIcon ?? ''
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = randomBytes(16).toString('hex')
|
|
||||||
const time = Date.now()
|
const time = Date.now()
|
||||||
const data: MultiplayerClient = {
|
const newClient: MultiplayerClient = {
|
||||||
Id: ws.id,
|
Id: ws.id,
|
||||||
Key: key,
|
|
||||||
PosX: 0,
|
PosX: 0,
|
||||||
PosY: -1,
|
PosY: -1,
|
||||||
Flipped: false,
|
Flipped: false,
|
||||||
Paused: false,
|
Paused: false,
|
||||||
FlaggedReason: null,
|
FlaggedReason: null,
|
||||||
Token: result[0].token, //Just to be safe
|
Token: userResult[0].token,
|
||||||
Username: result[0].username,
|
Username: userResult[0].username,
|
||||||
UserID: userId,
|
UserID: userId,
|
||||||
Admin: result[0].admin,
|
Admin: userResult[0].admin,
|
||||||
Platform: null,
|
Platform: null,
|
||||||
JoinTimestamp: time
|
JoinTimestamp: time
|
||||||
}
|
}
|
||||||
multiplayerClients.push(data)
|
multiplayerClients.push(newClient)
|
||||||
|
|
||||||
ws.send({
|
ws.send({
|
||||||
type: 'multiplayer_join_response',
|
type: 'multiplayer_join_response',
|
||||||
accepted: true,
|
accepted: true,
|
||||||
@@ -404,51 +458,26 @@ app.ws('/ws', {
|
|||||||
players
|
players
|
||||||
})
|
})
|
||||||
|
|
||||||
const userData = await db1
|
for (const existingClient of multiplayerClients) {
|
||||||
.select({
|
if (ws.id === existingClient.Id) continue
|
||||||
saveData: berryDashUserData.saveData
|
|
||||||
})
|
|
||||||
.from(berryDashUserData)
|
|
||||||
.where(and(eq(berryDashUserData.id, userId)))
|
|
||||||
.execute()
|
|
||||||
|
|
||||||
const savedata = userData[0].saveData
|
const clientSocket = Array.from(clients).find(
|
||||||
? JSON.parse(userData[0].saveData)
|
client => client.id === existingClient.Id
|
||||||
: null
|
|
||||||
if (!savedata) {
|
|
||||||
connection0.end()
|
|
||||||
connection1.end()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const customIcon = savedata.bird?.customIcon?.selected ?? null
|
|
||||||
const icon = customIcon ? 0 : savedata?.icon ?? 1
|
|
||||||
const overlay = customIcon ? 1 : savedata?.overlay ?? 0
|
|
||||||
const iconColor = customIcon
|
|
||||||
? [255, 255, 255]
|
|
||||||
: savedata?.settings?.colors?.icon ?? [255, 255, 255]
|
|
||||||
const overlayColor = customIcon
|
|
||||||
? [255, 255, 255]
|
|
||||||
: savedata?.settings?.colors?.overlay ?? [255, 255, 255]
|
|
||||||
|
|
||||||
for (const multiplayerClientInfo of multiplayerClients) {
|
|
||||||
if (ws.id == multiplayerClientInfo.Id) continue
|
|
||||||
const client = Array.from(clients).find(
|
|
||||||
client => client.id === multiplayerClientInfo.Id
|
|
||||||
)
|
)
|
||||||
if (!client) continue
|
if (!clientSocket) continue
|
||||||
client.send({
|
|
||||||
|
clientSocket.send({
|
||||||
type: 'multiplayer_player_join',
|
type: 'multiplayer_player_join',
|
||||||
id: userId,
|
id: userId,
|
||||||
username: result[0].username,
|
username: userResult[0].username,
|
||||||
admin: result[0].admin,
|
admin: userResult[0].admin,
|
||||||
timestamp: time,
|
timestamp: time,
|
||||||
iconData: {
|
iconData: {
|
||||||
icon,
|
icon: currentIcon,
|
||||||
overlay,
|
overlay: currentOverlay,
|
||||||
iconColor,
|
iconColor: currentIconColor,
|
||||||
overlayColor,
|
overlayColor: currentOverlayColor,
|
||||||
customIcon: customIcon ?? ''
|
customIcon: currentCustomIcon ?? ''
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user