UNPKG

@needle-tools/networking

Version:
32 lines (28 loc) 1.09 kB
const fs = require("fs"); class DiscStorage { constructor(directory) { this.roomStateDirectory = directory; } loadRoomState(roomName) { const statePath = this.roomStateDirectory + "/" + roomName + ".json"; if (!fs.existsSync(statePath)) return { success: false, message: "file does not exist", state: null, }; const content = fs.readFileSync(statePath); const str = content.toString(); return { success: true, state: JSON.parse(str) }; } async saveRoomState(roomName, state) { if (!fs.existsSync(this.roomStateDirectory)) fs.mkdirSync(this.roomStateDirectory, { recursive: true }); const statePath = this.roomStateDirectory + "/" + roomName + ".json"; let failed = false; state = JSON.stringify(state); await fs.writeFile(statePath, state, err => { if (err) { failed = true; console.error(err); } }); return { success: !failed }; } } module.exports = DiscStorage;