forked from dfabulich/choicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmygamegenerator.js
140 lines (120 loc) · 3.95 KB
/
mygamegenerator.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
if (typeof load == "undefined") {
fs = require("fs");
vm = require("vm");
vm.runInThisContext(fs.readFileSync("headless.js"), "headless.js");
args = process.argv;
args.shift();
args.shift();
} else {
load("headless.js");
args = arguments;
console = {log: print};
}
gameDir = args[0] || "mygame";
function parseSceneList(lines, lineNum) {
var nextIndent = null;
var scenes = [];
var purchases = {};
var line;
while(typeof (line = lines[++lineNum]) != "undefined") {
if (!line.trim()) continue;
var indent = /^(\s*)/.exec(line)[1].length;
if (nextIndent === null || nextIndent === undefined) {
// initialize nextIndent with whatever indentation the line turns out to be
// ...unless it's not indented at all
if (indent === 0) throw new Error("invalid scene_list indent, expected at least one row");
this.indent = nextIndent = indent;
}
if (indent === 0) break;
if (indent != this.indent) {
// all scenes are supposed to be at the same indentation level
throw new Error("invalid scene_list indent, expected "+this.indent+", was " + indent);
}
line = line.trim();
var purchaseMatch = /^\$(\w*)\s+(.*)/.exec(line);
if (purchaseMatch) {
line = purchaseMatch[2];
var product = purchaseMatch[1].trim() || "adfree";
purchases[line] = product;
}
if (!scenes.length && "startup" != line) scenes.push("startup");
scenes.push(line);
}
return {scenes:scenes, purchases:purchases, lineNum:lineNum-1};
}
function parseAchievement(data, lines, lineNum) {
var nextIndent = null;
var parsed = /(\S+)\s+(\S+)\s+(\S+)\s+(.*)/.exec(data);
var achievementName = parsed[1].toLowerCase();
var visibility = parsed[2];
var visible = (visibility != "hidden");
parsed[2] = visible;
var pointString = parsed[3];
parsed[3] = pointString*1;
var title = parsed[4];
var line = lines[++lineNum];
var preEarnedDescription = line.trim();
parsed[6] = preEarnedDescription;
var postEarnedDescription = null;
while(typeof(line = lines[++lineNum]) != "undefined") {
if (line.trim()) break;
}
if (/^\s/.test(line)) {
postEarnedDescription = line.trim();
} else {
// No indent means the next line is not a post-earned description
lineNum--;
}
if (postEarnedDescription === null) postEarnedDescription = preEarnedDescription;
parsed[5] = postEarnedDescription;
parsed.shift();
achievements.push(parsed);
return lineNum;
}
var lines = slurpFileLines("web/"+gameDir+"/scenes/startup.txt");
var stats = {}, purchases = {};
var scenes = ["startup"];
var create = /^\*create +(\w+) +(.*)/;
var result, variable, value;
var achievements = [];
var ignoredInitialCommands = {"comment":1, "title":1, "author":1};
for (var i = 0; i < lines.length; i++) {
var line = (""+lines[i]).trim();
if (!line) { continue; }
var result = /^\s*\*(\w+)(.*)/.exec(line);
if (!result) break;
var command = result[1].toLowerCase();
var data = result[2].trim();
if (ignoredInitialCommands[command]) { continue; }
else if (command == "create") {
var result = /^(\w*)(.*)/.exec(data);
variable = result[1];
value = JSON.parse(result[2]);
stats[variable.toLowerCase()] = value;
} else if (command == "scene_list") {
result = parseSceneList(lines, i);
scenes = result.scenes;
purchases = result.purchases;
i = result.lineNum;
} else if (command == "achievement") {
i = parseAchievement(data, lines, i);
} else {
break;
}
}
console.log("\ufeffnav = new SceneNavigator(");
function logJson(x) {
var json = JSON.stringify(x, null, " ");
json = json.replace(/[\u007f-\uffff]/g, function(c) {
return '\\u'+('0000'+c.charCodeAt(0).toString(16)).slice(-4);
});
console.log(json);
}
logJson(scenes);
console.log(");\nstats = ");
logJson(stats);
console.log(";\npurchases = ");
logJson(purchases);
console.log(";\nachievements = ");
logJson(achievements);
console.log(";");