-
Notifications
You must be signed in to change notification settings - Fork 2
/
commands.js
99 lines (92 loc) · 3.94 KB
/
commands.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
const GameResult = require('./gameResult');
const LeaveRoomResult = require('./leaveRoomResult');
const InventoryItem = require('./inventoryItem');
const getMatchingInteraction = (entityValue, game, context) => {
const location = game.story.map[context.currentLocation];
let interactionItem;
Object.keys(location.interactions).forEach(interaction => {
if (entityValue && entityValue.toLowerCase().includes(interaction)) {
interactionItem = interaction;
}
});
return interactionItem;
};
module.exports = {
DropItem: function(entity, context, game) {
return new GameResult('Not implemented yet.');
},
Move: function(entity, context, game) {
return new GameResult('Not implemented yet.');
},
Open: function(entity, context, game) {
// maybe we don't need this?
return new GameResult('Not implemented yet.');
},
TakeItem: function(entity, context, game) {
const location = game.story.map[context.currentLocation];
let interactionItem = getMatchingInteraction(entity.what_to_take, game, context);
// If there is no interaction item.
if (!interactionItem) {
return new GameResult(`Etrafta oyle bir sey yok!`);
}
const currentInteraction = location.interactions[interactionItem];
const takeOperation = currentInteraction.take;
// if interaction doesn't have the "use" action.
if (!takeOperation) {
return new GameResult(`"${interactionItem}" ile öyle bir sey yapilamiyor.`);
}
// if interaction is a function.
if (typeof takeOperation === "function") {
const text = takeOperation(context);
return new GameResult(text);
}
// Take the item first
context.inventory.push(new InventoryItem(interactionItem));
// None of the above means this is a text.
return new GameResult(takeOperation);
},
Use: function(entity, context, game) {
const location = game.story.map[context.currentLocation];
let interactionItem = getMatchingInteraction(entity.item, game, context);
// If there is no interaction item.
if (!interactionItem) {
return new GameResult(`Etrafta kullanilacak oyle bir sey yok!`);
}
const currentInteraction = location.interactions[interactionItem];
const useOperation = currentInteraction.use;
// if interaction doesn't have the "use" action.
if (!useOperation) {
return new GameResult(`"${interactionItem}" ile öyle bir sey yapilamiyor.`);
}
// if interaction is a function.
if (typeof useOperation === "function") {
const operationResult = useOperation(context);
if (operationResult.room) {
return new LeaveRoomResult(operationResult.room, operationResult.text);
}
return new GameResult(operationResult);
}
// None of the above means this is a text.
return new GameResult(useOperation);
},
Look: function(entity, context, game) {
const location = game.story.map[context.currentLocation];
let interactionItem = getMatchingInteraction(entity.direction, game, context);
if (!interactionItem) {
return new GameResult('Bakabileceğin bir şey yok.');
}
const currentInteraction = location.interactions[interactionItem];
const lookOperation = currentInteraction.look;
// if interaction doesn't have the "use" action.
if (!lookOperation) {
return new GameResult(`"${interactionItem}" hakkında başka bir bilgi yok.`);
}
// if interaction is a function.
if (typeof lookOperation === "function") {
const text = lookOperation(context);
return new GameResult(text);
}
// None of the above means this is a text.
return new GameResult(lookOperation);
}
};