65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
import { lobbyMessage } from "../Classes/lobbyMessage";
|
|
import { lobbyState } from "../Classes/lobbyState";
|
|
import { playerData } from "../Classes/playerData"
|
|
import { playerInventory } from "../Classes/playerInventory";
|
|
import { userData } from "../Classes/userData";
|
|
import { game } from "../game";
|
|
|
|
export function registerLobbyCallbacks(socket){
|
|
socket.on('lobby/playerJoin', (data) => {
|
|
if(!socket.hasOwnProperty("user")) return;
|
|
|
|
let buff = Buffer.from(data, 'base64');
|
|
let data1 : playerData = JSON.parse(buff.toString('utf-8'));
|
|
|
|
socket.player.characterId = data1.characterId
|
|
game.lobbyState.addUser(socket.user, socket.player)
|
|
|
|
let newLobbyState : lobbyState = Object.assign({}, game.lobbyState);
|
|
newLobbyState.users = newLobbyState.users.map(u=> userData.makeSafe(u))
|
|
|
|
game.socketIO.emit("lobby/playerJoin", JSON.stringify(newLobbyState))
|
|
game.addMessage(new lobbyMessage(socket.user.login + " joined"))
|
|
});
|
|
|
|
socket.on('lobby/playerUpdate', (data) => {
|
|
if(!socket.hasOwnProperty("user")) return;
|
|
|
|
let buff = Buffer.from(data, 'base64');
|
|
let data1 : playerData = JSON.parse(buff.toString('utf-8'));
|
|
|
|
let player : playerData = socket.player
|
|
if(player == null){
|
|
console.log("Error! Player not spawned!")
|
|
}
|
|
else{
|
|
player.room = data1.room
|
|
player.rigidbody = data1.rigidbody
|
|
player.damageInfo = player.damageInfo.concat(data1.damageInfo)
|
|
}
|
|
});
|
|
|
|
socket.on('lobby/loadoutChanged', (data) => {
|
|
if(!socket.hasOwnProperty("user")) return;
|
|
|
|
let buff = Buffer.from(data, 'base64');
|
|
let data1 : playerData = JSON.parse(buff.toString('utf-8'));
|
|
|
|
let player : playerData = socket.player
|
|
player.inventory.weapon = data1.inventory.weapon
|
|
player.inventory.equipment = data1.inventory.equipment
|
|
player.validateEquipment()
|
|
|
|
game.socketIO.emit("lobby/loadoutChanged", JSON.stringify(player))
|
|
});
|
|
|
|
socket.on('lobby/message', (data) => {
|
|
if(!socket.hasOwnProperty("user")) return;
|
|
|
|
let buff = Buffer.from(data, 'base64');
|
|
let data1 : lobbyMessage = JSON.parse(buff.toString('utf-8'));
|
|
data1.timestamp = new Date().toLocaleString();
|
|
|
|
game.addMessage(data1)
|
|
});
|
|
} |