-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathgenerateLevels.js
More file actions
76 lines (64 loc) · 2.09 KB
/
generateLevels.js
File metadata and controls
76 lines (64 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const scriptStartTime = new Date()
// This script tests the level generation algorithm by generating levels with random parameters and saving the results.
// Example usage:
// nodemon scripts/generateLevels.js --dry --debug
require('coffee-script').register()
const _ = require('lodash')
_.string = require('underscore.string')
_.mixin(_.str.exports())
const fs = require('fs')
const path = require('path')
const PWD = __dirname
const program = require('commander')
const moment = require('moment')
const levelGeneration = require('../app/lib/level-generation')
async function main () {
const options = parseOptions()
for (let i = 0; i < 1; ++i) {
const parameters = {}
const level = await levelGeneration.generateLevel({ parameters })
debug(level)
await saveLevel(level, options)
}
}
async function saveLevel (level, options) {
const fileName = `level-${level.parametersKey}.json`
// Make the levels output directory
const levelsDir = path.join(PWD, 'levels')
if (!fs.existsSync(levelsDir)) {
fs.mkdirSync(levelsDir)
}
// Write the level to a file
const levelPath = path.join(levelsDir, fileName)
fs.writeFileSync(levelPath, JSON.stringify(level, null, 2))
console.log(`Wrote level to ${levelPath}`)
if (!options.dry) {
// Write the level to the database
// TODO
}
}
function parseOptions () {
console.log()
program
.description('Creates test levels to exercise the level-generation library')
.option('-d, --debug', 'Debug logging') // TODO: differentiate normal, debug, and verbose logging levels
.option('--dry', 'Dry run--does not write levels')
.parse(process.argv)
const options = program.opts()
if (options.debug) {
debugOutput = true
}
return options
}
let debugOutput = false
function debug (...args) {
if (!debugOutput) return
console.log('\x1b[36m%s\x1b[0m', moment().format('YYYY-MM-DD HH:mm:ss.SSS'), ...args)
}
main().then(() => {
console.log('Finished in', moment.duration(new Date() - scriptStartTime).asSeconds(), 'seconds')
process.exit(0)
}).catch((err) => {
console.error('Error: ', err)
process.exit(1)
})