Newer
Older
Labyrinth / src / FileStateRepository.ts
import * as fs from "fs";
import { GameState, IStateRepository } from "./GameServer";

export class FileStateRepository implements IStateRepository
{
    private file: string;

    constructor(file: string)
    {
        this.file = file;
    }

    public loadAllGameStates(): GameState[]
    {
        if(!fs.existsSync(this.file)){
            fs.writeFileSync(this.file, JSON.stringify([]));
            return [];
        }

        const content = fs.readFileSync(this.file).toString();
        return JSON.parse(content);
    }

    public saveAllGameStates(states: GameState[]): boolean
    {
        fs.writeFileSync(this.file, JSON.stringify(states));

        return true;
    }
}