46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { lobbyJoin } from "../Classes/Incoming/lobbyJoin";
|
|
import { levelState } from "../Classes/levelState";
|
|
import { lobbyMessage } from "../Classes/lobbyMessage";
|
|
import { playerData } from "../Classes/playerData";
|
|
import { userData } from "../Classes/userData";
|
|
import { game } from "../game";
|
|
|
|
export function playerJoin(socket, data){
|
|
if(!socket.hasOwnProperty("player")) return;
|
|
|
|
let buff = Buffer.from(data, 'base64');
|
|
let data1 : lobbyJoin = JSON.parse(buff.toString('utf-8'));
|
|
let player : playerData = socket.player
|
|
|
|
player.room = "0_0"
|
|
player.characterId = data1.player.characterId
|
|
game.lobbyState.addUser(socket.user, player)
|
|
let spawnRoom = game.lobbyState.roomEnter(new levelState("0_0", -1), player)
|
|
socket.join("0_0")
|
|
|
|
let newUser = {
|
|
users:[userData.makeSafe(socket.user)],
|
|
players:[player.copyStart()],
|
|
rooms:[]
|
|
};
|
|
let existingUsers = {
|
|
users:game.lobbyState.users.map(u=> userData.makeSafe(u)),
|
|
players:game.lobbyState.players.map(p=>p.copyStart()),
|
|
rooms:[]
|
|
}
|
|
|
|
game.lobbyState.rooms.forEach(room=>{
|
|
if(room == spawnRoom)
|
|
existingUsers.rooms.push(spawnRoom.copyStart())
|
|
else
|
|
existingUsers.rooms.push(room.copyState())
|
|
})
|
|
|
|
//send only player to all existing players
|
|
socket.broadcast.emit("lobby/playerJoin", JSON.stringify(newUser));
|
|
|
|
//send all users to player (including player)
|
|
socket.emit("lobby/playerJoin", JSON.stringify(existingUsers))
|
|
|
|
game.addMessage(new lobbyMessage(socket.user.login + " joined"))
|
|
} |