Backend refactor, player-user decoupling
This commit is contained in:
parent
ba5f3df01b
commit
870aa12161
|
@ -1,21 +1,24 @@
|
|||
import { game } from "../game";
|
||||
import { loginResponse } from "../Classes/loginResponse";
|
||||
import { playerData } from "../Classes/playerData";
|
||||
import { userData } from "../Classes/userData";
|
||||
import { users } from "./users";
|
||||
import { players } from "../Player/players";
|
||||
|
||||
export function registerAccountCallbacks(socket){
|
||||
function emitLogin(result : userData, registered:boolean = false){
|
||||
if(result != null){
|
||||
socket.emit("account/login", JSON.stringify(userData.makeSafe(result, socket.id)));
|
||||
socket.user = result
|
||||
function emitLogin(user : userData, player:playerData, registered:boolean = false){
|
||||
if(user != null){
|
||||
socket.emit("account/login", JSON.stringify(new loginResponse(true, user, player)));
|
||||
socket.user = user
|
||||
socket.player = player
|
||||
if(registered){
|
||||
console.log(`User ${(result as userData).login} registerd and logged in`);
|
||||
console.log(`User ${(user as userData).login} registerd and logged in`);
|
||||
}
|
||||
else{
|
||||
console.log(`User ${(result as userData).login} logged in`);
|
||||
console.log(`User ${(user as userData).login} logged in`);
|
||||
}
|
||||
}
|
||||
else{
|
||||
socket.emit("account/login", "");
|
||||
socket.emit("account/login", JSON.stringify(new loginResponse(false, null, null)));
|
||||
console.log("Login Failed");
|
||||
}
|
||||
}
|
||||
|
@ -24,17 +27,27 @@ export function registerAccountCallbacks(socket){
|
|||
let buff = Buffer.from(data, 'base64');
|
||||
let json : userData = JSON.parse(buff.toString('utf-8'));
|
||||
if (!json.login || !json.password) return
|
||||
let result = users.createUser(json.login, json.password);
|
||||
|
||||
emitLogin(result, true);
|
||||
let result = users.createUser(json.login, json.password);
|
||||
let player = null
|
||||
if(result != null){
|
||||
player = players.createPlayer(result.id)
|
||||
}
|
||||
|
||||
emitLogin(result, player);
|
||||
});
|
||||
|
||||
socket.on('account/login', (data) => {
|
||||
let buff = Buffer.from(data, 'base64');
|
||||
let json : userData = JSON.parse(buff.toString('utf-8'));
|
||||
if (!json.login || !json.password) return
|
||||
let result = users.readUser(json.login, json.password);
|
||||
|
||||
emitLogin(result);
|
||||
let result = users.readUser(json.login, json.password);
|
||||
let player = null
|
||||
if(result != null){
|
||||
player = players.readPlayer(result.id)
|
||||
}
|
||||
|
||||
emitLogin(result, player);
|
||||
});
|
||||
}
|
|
@ -45,20 +45,13 @@ export class users{
|
|||
}
|
||||
|
||||
static loadUsers(){
|
||||
let maxItemId = 0;
|
||||
fs.mkdirSync(`Database/Users/`, { recursive: true })
|
||||
fs.readdir(`Database/Users/`, (err, files) => {
|
||||
files.forEach(file => {
|
||||
fs.readFile(`Database/Users/${file}`, (err, data) => {
|
||||
if (err) throw err;
|
||||
let user = JSON.parse(data);
|
||||
let user : userData = JSON.parse(data);
|
||||
game.accountsDB.push(user);
|
||||
if(user.playerData != null){
|
||||
user.playerData.inventory.items.forEach(i=>{
|
||||
if(i.id > maxItemId){
|
||||
game.itemCount = maxItemId = i.id
|
||||
}
|
||||
})
|
||||
}
|
||||
console.log(`Loaded ${user.login}`);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,17 +1,49 @@
|
|||
import { physicsObject } from "../Classes/physicsObject";
|
||||
import { playerData } from "../Classes/playerData";
|
||||
import { userData } from "./userData";
|
||||
|
||||
export class lobbyState{
|
||||
hostSocket : string
|
||||
hostId : number
|
||||
users : userData[] = [];
|
||||
players : playerData[] = [];
|
||||
objects : physicsObject[] = [];
|
||||
|
||||
copyLight(){
|
||||
let lobby = Object.assign({}, this);
|
||||
lobby.players = []
|
||||
lobby.users = []
|
||||
for (let i = 0; i < this.players.length; i++) {
|
||||
lobby.players.push(playerData.copyLight(this.players[i]));
|
||||
}
|
||||
return lobby
|
||||
}
|
||||
|
||||
addUser(userData, playerData){
|
||||
let uIdx = this.users.findIndex(u=>u.id == userData.id)
|
||||
let pIdx = this.players.findIndex(u=>u.id == playerData.id)
|
||||
if(uIdx != -1 || pIdx != -1){
|
||||
console.log("User " + userData.login + " is already in the game!")
|
||||
return false;
|
||||
}
|
||||
|
||||
this.users.push(userData)
|
||||
this.players.push(playerData)
|
||||
return true
|
||||
}
|
||||
|
||||
removeUser(id){
|
||||
let idx = this.users.findIndex(u=>u.id == id)
|
||||
if(idx != -1){
|
||||
this.users.splice(idx, 1)
|
||||
}
|
||||
|
||||
idx = this.players.findIndex(u=>u.id == id)
|
||||
if(idx != -1){
|
||||
this.players.splice(idx, 1)
|
||||
}
|
||||
}
|
||||
|
||||
findPlayer(id: number){
|
||||
return this.players.find(p=>p.id == id)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
import { playerData } from "./playerData"
|
||||
import { userData } from "./userData"
|
||||
|
||||
export class loginResponse{
|
||||
result : boolean
|
||||
userData : userData
|
||||
playerData : playerData
|
||||
|
||||
constructor(result : boolean, user : userData, player : playerData){
|
||||
this.result = result;
|
||||
if(result){
|
||||
this.userData = userData.makeSafe(user);
|
||||
this.playerData = player;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,17 +2,15 @@ import { physicsObject } from "./physicsObject";
|
|||
import { playerInventory } from "./playerInventory";
|
||||
|
||||
export class playerData{
|
||||
socketId : string
|
||||
userName : string
|
||||
id : number
|
||||
characterId : number
|
||||
inventory : playerInventory = new playerInventory()
|
||||
rigidbody : physicsObject = new physicsObject()
|
||||
|
||||
static copyLight(data : playerData){
|
||||
let player : any = Object.assign({}, data);
|
||||
delete player.userName
|
||||
delete player.characterId
|
||||
delete player.inventory
|
||||
let player : any = {}
|
||||
player.id = data.id
|
||||
player.rigidbody = data.rigidbody
|
||||
return player
|
||||
}
|
||||
}
|
|
@ -6,7 +6,6 @@ export class userData{
|
|||
checksum: number;
|
||||
login: string;
|
||||
password: string;
|
||||
playerData: playerData = null
|
||||
|
||||
constructor(id:number, login:string, password:string){
|
||||
this.id = id;
|
||||
|
@ -15,9 +14,8 @@ export class userData{
|
|||
this.checksum = 0;
|
||||
}
|
||||
|
||||
static makeSafe(user : userData, socketId : string){
|
||||
static makeSafe(user : userData){
|
||||
let safeUser : any = Object.assign({}, user);
|
||||
safeUser.socketId = socketId
|
||||
delete safeUser.password
|
||||
delete safeUser.checksum
|
||||
return safeUser
|
||||
|
|
|
@ -14,19 +14,16 @@ export function registerItemCallbacks(socket){
|
|||
let buff = Buffer.from(data, 'base64');
|
||||
let data1 : itemData = JSON.parse(buff.toString('utf-8'));
|
||||
|
||||
let player : playerData = socket.user.playerData
|
||||
let player : playerData = socket.player
|
||||
if(player != null){
|
||||
let message = socket.user.login + " picked up "
|
||||
if(data1.rarity >= 90){
|
||||
console.log("epic")
|
||||
if(data1.rarity >= 95){
|
||||
message += "<color=purple>" + data1.name + "</color> (epic)"
|
||||
}
|
||||
else if(data1.rarity >= 60){
|
||||
console.log("rare")
|
||||
else if(data1.rarity >= 75){
|
||||
message += "<color=red>" + data1.name + "</color> (rare)"
|
||||
}
|
||||
else if(data1.rarity >= 40){
|
||||
console.log("uncommon")
|
||||
else if(data1.rarity >= 60){
|
||||
message += "<color=yellow>" + data1.name + "</color> (uncommon)"
|
||||
}
|
||||
else
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import { lobbyMessage } from "../Classes/lobbyMessage";
|
||||
import { playerData } from "../Classes/playerData"
|
||||
import { playerInventory } from "../Classes/playerInventory";
|
||||
import { userData } from "../Classes/userData";
|
||||
import { game } from "../game";
|
||||
|
||||
export function registerLobbyCallbacks(socket){
|
||||
|
@ -11,16 +10,10 @@ export function registerLobbyCallbacks(socket){
|
|||
let buff = Buffer.from(data, 'base64');
|
||||
let data1 : playerData = JSON.parse(buff.toString('utf-8'));
|
||||
|
||||
let pd = new playerData()
|
||||
pd.socketId = socket.id
|
||||
pd.userName = socket.user.login
|
||||
pd.characterId = data1.characterId;
|
||||
pd.inventory = data1.inventory;
|
||||
socket.user.playerData = pd
|
||||
|
||||
game.lobbyState.players.push(socket.user.playerData)
|
||||
socket.player.characterId = data1.characterId
|
||||
game.lobbyState.addUser(socket.user, socket.player)
|
||||
game.socketIO.emit("lobby/playerJoin", JSON.stringify(game.lobbyState))
|
||||
socket.emit('lobby/messages', JSON.stringify(game.messages))
|
||||
game.addMessage(new lobbyMessage(socket.user.login + " joined"))
|
||||
});
|
||||
|
||||
socket.on('lobby/playerUpdate', (data) => {
|
||||
|
@ -28,14 +21,13 @@ export function registerLobbyCallbacks(socket){
|
|||
|
||||
let buff = Buffer.from(data, 'base64');
|
||||
let data1 : playerData = JSON.parse(buff.toString('utf-8'));
|
||||
data1.socketId = socket.id
|
||||
|
||||
let idx = game.lobbyState.players.findIndex((pl) => pl.socketId == socket.id)
|
||||
if(idx == -1){
|
||||
let player = game.lobbyState.findPlayer(socket.user.id)
|
||||
if(player == null){
|
||||
console.log("Error! Player not spawned!")
|
||||
}
|
||||
else{
|
||||
game.lobbyState.players[idx].rigidbody = data1.rigidbody
|
||||
player.rigidbody = data1.rigidbody
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -54,10 +46,11 @@ export function registerLobbyCallbacks(socket){
|
|||
let buff = Buffer.from(data, 'base64');
|
||||
let data1 : playerData = JSON.parse(buff.toString('utf-8'));
|
||||
|
||||
socket.user.playerData.inventory = data1.inventory
|
||||
playerInventory.validate(socket.user.playerData.inventory)
|
||||
let player = game.lobbyState.findPlayer(socket.user.id)
|
||||
player.inventory = data1.inventory
|
||||
playerInventory.validate(player.inventory)
|
||||
|
||||
game.socketIO.emit("lobby/loadoutChanged", JSON.stringify(socket.user.playerData))
|
||||
game.socketIO.emit("lobby/loadoutChanged", JSON.stringify(player))
|
||||
});
|
||||
|
||||
socket.on('lobby/message', (data) => {
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
import { game } from "../game";
|
||||
import { userData } from "../Classes/userData";
|
||||
import { playerData } from "../Classes/playerData";
|
||||
const fs = require('fs');
|
||||
|
||||
export class players{
|
||||
static createPlayer(id){
|
||||
if (players.readPlayer(id) != null){
|
||||
console.log(`Player ${id} alread exists`);
|
||||
return null;
|
||||
}
|
||||
|
||||
var player = new playerData();
|
||||
player.id = id
|
||||
game.playersDB.push(player);
|
||||
return player;
|
||||
}
|
||||
|
||||
static readPlayer(id : number){
|
||||
let player = game.playersDB.find(u=>u.id == id);
|
||||
return player;
|
||||
}
|
||||
|
||||
static savePlayers(){
|
||||
game.playersDB.forEach(player => {
|
||||
let data = JSON.stringify(player, null, 2);
|
||||
fs.writeFileSync(`Database/Players/${player.id}.json`, data);
|
||||
});
|
||||
}
|
||||
|
||||
static loadPlayers(){
|
||||
fs.mkdirSync(`Database/Players/`, { recursive: true })
|
||||
fs.readdir(`Database/Players/`, (err, files) => {
|
||||
files.forEach(file => {
|
||||
fs.readFile(`Database/Players/${file}`, (err, data) => {
|
||||
if (err) throw err;
|
||||
let player : playerData = JSON.parse(data);
|
||||
game.playersDB.push(player);
|
||||
player.inventory.items.forEach(i=>{
|
||||
if(i.id > game.itemCount){
|
||||
game.itemCount = i.id
|
||||
}
|
||||
})
|
||||
console.log(`Loaded pl ${player.id}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
2
game.ts
2
game.ts
|
@ -1,6 +1,7 @@
|
|||
import { userData } from "./Classes/userData";
|
||||
import { lobbyMessage } from "./Classes/lobbyMessage";
|
||||
import { lobbyState } from "./Classes/lobbyState";
|
||||
import { playerData } from "./Classes/playerData";
|
||||
const fs = require('fs');
|
||||
|
||||
export class game{
|
||||
|
@ -8,6 +9,7 @@ export class game{
|
|||
|
||||
static itemCount : number = 0
|
||||
static accountsDB : userData[] = []
|
||||
static playersDB : playerData[] = []
|
||||
|
||||
/** Key: socket ID*/
|
||||
static lobbyState : lobbyState = new lobbyState()
|
||||
|
|
12
index.ts
12
index.ts
|
@ -4,6 +4,7 @@ import { registerLobbyCallbacks } from "./Lobby/lobbySocket";
|
|||
import { playerData } from "./Classes/playerData";
|
||||
import { game } from "./game";
|
||||
import { registerItemCallbacks } from "./Items/itemSocket";
|
||||
import { players } from "./Player/players";
|
||||
|
||||
const express = require("express");
|
||||
const app = express();
|
||||
|
@ -22,6 +23,7 @@ const io = require("socket.io")(server, {
|
|||
});
|
||||
|
||||
users.loadUsers();
|
||||
players.loadPlayers();
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
console.log("Got connection!");
|
||||
|
@ -36,10 +38,7 @@ io.on('connection', (socket) => {
|
|||
socket.on("disconnect", () => {
|
||||
socket.disconnect()
|
||||
if(socket.hasOwnProperty("user")){
|
||||
let idx = game.lobbyState.players.findIndex((pl) => pl.socketId == socket.id)
|
||||
if(idx != -1){
|
||||
game.lobbyState.players.splice(idx, 1)
|
||||
}
|
||||
game.lobbyState.removeUser(socket.user.id)
|
||||
delete socket.user
|
||||
}
|
||||
console.log(socket.id + " disconnected");
|
||||
|
@ -54,13 +53,13 @@ io.on('connection', (socket) => {
|
|||
setInterval(() => {
|
||||
let userCount = game.lobbyState.players.length
|
||||
if(userCount > 0){
|
||||
game.lobbyState.hostSocket = game.lobbyState.players[0].socketId
|
||||
game.lobbyState.hostId = game.lobbyState.users[0].id
|
||||
io.emit('lobby', JSON.stringify(game.lobbyState.copyLight()))
|
||||
}
|
||||
}, 1000/30)
|
||||
|
||||
setInterval(() => {
|
||||
console.log("Connected losers: " + game.lobbyState.players.length);
|
||||
console.log("Players: " + game.lobbyState.players.length + ", users: " + game.lobbyState.users.length);
|
||||
}, 60 * 1000)
|
||||
|
||||
process.on('SIGINT', function () {
|
||||
|
@ -69,5 +68,6 @@ process.on('SIGINT', function () {
|
|||
});
|
||||
process.on('exit', function () {
|
||||
users.saveUsers();
|
||||
players.savePlayers();
|
||||
console.log("exitted");
|
||||
});
|
Loading…
Reference in New Issue