UNPKG

warrior-code-api

Version:
59 lines (44 loc) 1.43 kB
'use strict'; const fs = require('fs'); const path = require('path'); const Promise = require('bluebird'); const mongoose = require('mongoose'); const MAPS_DIR = process.env.WARRIORJS_MAPS_DIR || path.join('..', 'maps') const MapSchema = new mongoose.Schema({ // Group the maps by a specific world. world: { type: String, required: true }, // Name for the map. Example: "Level 1". name: { type: String, required: true }, // Optional description. description: { type: String }, // Weight to order the maps in a world. Works as levels. weight: { type: Number, default: 0 }, // All the data to evaluate the map. // data: { type: mongoose.Schema.Mixed, required: true } // The route to the map data. dataFile: { type: String, required: true } }); /** * Read and return the map data as a JavaScript object. * @return {Promise} promise. */ MapSchema.methods.getMapData = () => { // Build and return the promise. return new Promise((resolve, reject) => { // Open the file. fs.readFile(path.join(MAPS_DIR, this.dataFile), 'utf-8', (err, data) => { // Pass any errors. if (err) reject(err); try { // Parse the data. data = JSON.parse(data); // Resolve with the parse map. resolve(data); } catch(err) { // Pass any errors. reject(err); } }); }); }; module.exports = mongoose.model('Map', MapSchema);