You've already forked KF-MMO-Server
Backend refactor, player-user decoupling
This commit is contained in:
49
Player/players.ts
Normal file
49
Player/players.ts
Normal file
@@ -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}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user