45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import { game } from "../game";
|
|
import { playerData } from "../Classes/playerData";
|
|
|
|
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(fs){
|
|
game.playersDB.forEach(player => {player.save(fs)});
|
|
}
|
|
|
|
static loadPlayers(fs){
|
|
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 = new playerData(JSON.parse(data));
|
|
game.playersDB.push(player);
|
|
player.inventory.items.forEach(i=>{
|
|
if(i.id > game.itemCount){
|
|
game.itemCount = i.id
|
|
}
|
|
})
|
|
player.validateEquipment()
|
|
console.log(`Loaded pl ${player.id}`);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
} |