-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextAdventure.js
377 lines (338 loc) · 10.8 KB
/
textAdventure.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
class textAdventureEngine {
TBA_DEBUG = false;
#database = undefined;
#currentRoom = null;
#inventory = {};
constructor(outputFunction, clearOutputFunction) {
this.outputAddLines = outputFunction;
this.outputClear = clearOutputFunction;
}
loadDatabaseFromFile(gamedatabasePath, showGameName = true){
this.outputClear();
this.#writeOutputLines("Initializing Text Adventure Engine...");
let base = this;
$.getJSON( gamedatabasePath)
.done(function( json ) {
base.#initDatbase(json, showGameName);
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
base.outputAddLines("Failed to Load json");
base.#showRequest();
});
}
loadDatabaseFromObject(json, showGameName = true){
this.outputClear();
this.#writeOutputLines("Initializing Text Adventure Engine...");
base.#initDatbase(json, showGameName);
}
input(cmd){
this.#praseCommand(cmd);
}
#initDatbase(gameDatabaseObject, showGameName = true){
this.#database = gameDatabaseObject;
// Tell each object it's name
$.each( this.#database.verbs, function( key, val ) {
val["name"] = key;
});
$.each( this.#database.objects, function( key, val ) {
val["name"] = key;
});
$.each( this.#database.locations, function( key, val ) {
val["name"] = key;
});
if(this.TBA_DEBUG){
var base = this;
$.each( this.#database.verbs, function( i, item ) {
base.outputAddLines("Loaded Action: "+item.name);
});
$.each( this.#database.objects, function( i, item ) {
base.outputAddLines("Loaded Object: "+item.name);
});
$.each( this.#database.locations, function( i, item ) {
base.outputAddLines("Loaded Location: "+item.title);
});
this.#writeOutputLines("Loading done.");
}
if(showGameName){
this.outputClear();
this.#writeOutputLines(["", "",
"'"+this.#database.general.title+"' by "+this.#database.general.author,
"Version: "+this.#database.general.version]);
}else{
this.outputClear();
}
this.#praseCommand("welcome");
}
#showRequest(){
this.#writeOutputLines(this.#database.general.request);
}
#removeFromString(arr,str){
let regex = new RegExp("\\b"+arr.join('|')+"\\b","gi");
let removed = str.replace(regex, '');
return removed.replace(/\s\s+/g, ' ');
}
#praseCommand(cmd){
cmd = cmd.toLowerCase();
// remove ignored words from command
cmd = this.#removeFromString(this.#database.ignored_words, cmd);
cmd = cmd.trim();
console.log("Stripped command of parser: '"+cmd+"'");
if(cmd=="welcome"){
this.#inventory = {};
if(this.#database.general.start.text.length > 0){
this.#writeOutputLines(this.#database.general.start.text);
}
this.#parseActionString(undefined, this.#database.general.start.action);
}else if(cmd == "debug"){
if(this.TBA_DEBUG==true){
this.TBA_DEBUG = true;
}else{
this.TBA_DEBUG = false;
}
}else if(cmd == "help" || cmd == "?" || cmd == "what" || cmd == "how" || cmd == "what do"){
var allVerbs = "";
$.each( this.#database.verbs, function( key, val ) {
if(allVerbs != ""){
allVerbs += ", "+val.name;
}else{
allVerbs += val.name;
}
});
this.#writeOutputLines("Enter simple directions like<br /><i>look at wall</i><br />");
this.#writeOutputLines("Commonly used verbs are: "+allVerbs);
} else {
let words = cmd.split(" ");
let locationState = this.#getLocationState(this.#currentRoom);
let verb = this.#checkForVerb(words);
let object = this.#checkForObject(words);
//no action
if(verb == undefined && object != undefined){
this.#writeOutputLines("Unknown verb, please try to rephrase your command."); //TODO: Improve
this.#showRequest();
return;
}
// Look at room
if(verb != undefined && verb.name == "look" && (words.length === 1)){
locationState = this.#getLocationState(this.#currentRoom);
this.#writeLocationDescription(locationState.objects);
this.#showRequest();
return;
}
//no object
if(verb != undefined && (object == undefined)){
console.log("object is undefined");
this.#writeOutputLines(verb.failure);
this.#showRequest();
return;
}
//Do a regular Action (verb)
if(verb!=undefined && object != undefined){
console.log("Action: "+ verb.words);
console.log("Object: "+ object.name);
var result = object.actions[verb.name];
if(this.TBA_DEBUG==true){
console.log(result);
this.#writeOutputLines("Action: "+ verb.name);
this.#writeOutputLines("Object: "+ object.name);
}
if(result != undefined){
//
let objectStateVerbDefinition = object.actions[verb.name];
this.#writeOutputLines(objectStateVerbDefinition.text); // This might be a string here
if(objectStateVerbDefinition.action!=undefined){
if($.isArray(objectStateVerbDefinition.action) && objectStateVerbDefinition.action.length > 0) {
for(var i=0; i<objectStateVerbDefinition.action.length; i++){
this.#parseActionString(object, objectStateVerbDefinition.action[i]);
}
}else{
this.#parseActionString(object, objectStateVerbDefinition.action);
}
}
}else{
this.#writeOutputLines(verb.failure); //TODO: Could be improved
}
this.#showRequest();
return;
}
this.#writeOutputLines("Please try to rephrase your command."); //TODO: Replace
}
this.#showRequest();
}
#writeLocationDescription(objectsInLocation){
var fullLocationDescription = "";
for(var i =0; i< objectsInLocation.length; i++){
let thisObject = this.#database.objects[objectsInLocation[i]];
if(thisObject.locationDescription.length > 0){
if(fullLocationDescription.length !== 0){
fullLocationDescription+= " ";
}
fullLocationDescription += thisObject.locationDescription;
}
}
this.#writeOutputLines(fullLocationDescription);
if(Object.keys(this.#inventory).length > 0){
for(var index in this.#inventory) {
let currentItemDescription = this.#inventory[index].locationDescription;
if(currentItemDescription.length > 0){
this.#writeOutputLines(currentItemDescription);
}
}
}
}
#parseActionString(callingObject, actionString){
var acts = actionString.split(" ");
if(acts[0]=="objectState") {
console.error("objectState was removed. Use objectReplaceInLocation instead!");
}else if(acts[0]=="objectRemoveFromLocation"){
console.log("removing Object from Location:"+acts[1]);
var index = this.#getLocationState(this.#currentRoom).objects.indexOf(acts[1]);
if (index > -1) {
this.#getLocationState(this.#currentRoom).objects.splice(index, 1);
console.log("Removed object with index: "+index);
}else{
console.log("Object not found in location: " + acts[1]);
}
}else if(acts[0]=="objectAddToLocation"){
console.log("adding Object to Location:"+acts[1]);
this.#getLocationState(this.#currentRoom).objects.push(acts[1]);
}else if(acts[0]=="objectReplaceInLocation"){
var index = this.#getLocationState(this.#currentRoom).objects.indexOf(acts[1]);
if (index > -1) {
this.#getLocationState(this.#currentRoom).objects.splice(index, 1);
console.log("Removed object with index: "+index);
}else{
console.log("Object not found in location: " + acts[1]);
}
this.#getLocationState(this.#currentRoom).objects.push(acts[2]);
}else if(acts[0]=="gotoLocation"){
console.log("SIWTCHING LOCATION TO:"+acts[1]);
this.#currentRoom = acts[1];
var currentRoomState = this.#getLocationState(this.#currentRoom);
this.#writeLocationDescription(currentRoomState.objects);
}else if(acts[0]=="showLocationDescription"){
var currentRoomState = this.#getLocationState(this.#currentRoom);
this.#writeLocationDescription(currentRoomState.objects);
}else if(acts[0]=="inventoryAdd"){
console.log("Add or replace inventory item: " + acts[1]);
this.#inventory[acts[1]] = this.#database.objects[acts[1]];
}else if(acts[0]=="inventoryRemove"){
console.log("Remove inventory object, if it exists "+acts[1]);
delete this.#inventory[acts[1]];
}
}
#getLocationState(location){
return this.#database.locations[location];
}
#getObject(object){
if (typeof object === 'string' || object instanceof String){
return this.#database.objects[object];
}else{
return object;
}
}
#checkForVerb(words){
let value = undefined;
for(var i=0; i<words.length && value === undefined; i++){
//console.log( "Checking: "+words[i] );
$.each( this.#database.verbs, function( key, val ) {
//console.log( "Key: "+key+ " " + val.words[0] );
let test = $.inArray(words[i], val.words);
//console.log("test result: "+ test);
if(test >= 0){
value = val;
return;
}
});
}
return value;
}
#checkForObject(words){
var locationState = this.#getLocationState(this.#currentRoom);
let value = undefined;
// Check room Items
for(var i=0; i<words.length; i++){
// Check inventory item
if(Object.keys(this.#inventory).length > 0){
for(var index in this.#inventory) {
let test = $.inArray(words[i], this.#getObject(this.#inventory[index]).words);
if(test >= 0){
value = this.#inventory[index];
return value;
}
}
}
// check for objects in room
var base = this;
$.each(locationState.objects, function( index, val ) {
let test = $.inArray(words[i], base.#getObject(val).words);
if(test >= 0){
value = base.#database.objects[val];
return; // exit $.each loop
}
});
if(value!=undefined){
break;
}
}
return value;
}
#writeOutputLines(lines){
if (!Array.isArray(lines)){
this.outputAddLines(lines);
}else{
for(var i=0; i<lines.length; i++){
this.outputAddLines(lines[i]);
}
}
}
// disabled for now:
#checkForSecondObject(words){
var locationState = this.#getLocationState(this.#currentRoom);
console.log("checking for seconds object");
var value = undefined;
var founds = 0;
for(var i=0; i<words.length; i++){
let isInventoryItem = false;
// Check inventory item
if(Object.keys(this.#inventory).length > 0){
for(var index in this.#inventory) {
let test = $.inArray(words[i], this.#getObject(this.#inventory[index]).words);
if(test >= 0){
value = this.#inventory[index];
founds++;
isInventoryItem = true;
}
}
}
if(!isInventoryItem){
// check for objects in room
var base = this;
$.each(locationState.objects, function( index, val ) {
let test = $.inArray(words[i], base.#database.objects[val].words);
if(test >= 0){
value = base.#database.objects[val];
founds++;
return;
}
});
}
if(founds>=2){
break;
}
}
return value;
}
#getObjectIndexByName(name){
console.log("checking for object id");
var value = undefined;
$.each(this.#database.objects, function( index, val ) {
if(val.name==name){
value = index;
return;
}
});
return value;
}
}