about summary refs log tree commit diff stats
path: root/js/story-interpreter/interpreter.js
blob: be11f0ec8fd4c84530222a36839e7fbd36b723e1 (plain) (blame)
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
const fs = require('fs');


class Room {
    constructor(name, description, items, animals, connections) {
        this.name = name;
        this.description = description;
        this.items = items;
        this.animals = animals;
        this.connections = connections;
    }
}

class Item {
    constructor(name, description, action) {
        this.name = name;
        this.description = description;
        this.action = action;
    }
}

class Animal {
    constructor(name, description, action) {
        this.name = name;
        this.description = description;
        this.action = action;
    }
}

class Interpreter {
    constructor() {
        this.rooms = {};
        this.currentRoom = null;
        this.history = [];
    }

    parseDSLFile(filePath) {
        const dsl = fs.readFileSync(filePath, 'utf8');
        this.parseDSL(dsl);
    }

    parseDSL(dsl) {
        const lines = dsl.split('\n');
        let currentRoom = null;
        let currentItem = null;
        let currentAnimal = null;

        for (const line of lines) {
            const trimmed = line.trim();

            if (trimmed.startsWith('room')) {
                const name = trimmed.split('"')[1];
                currentRoom = new Room(name);
                this.rooms[name] = currentRoom;
            } else if (trimmed.startsWith('description')) {
                const description = trimmed.split('"')[1];
                if (currentItem) {
                    currentItem.description = description;
                } else if (currentAnimal) {
                    currentAnimal.description = description;
                } else {
                    currentRoom.description = description;
                }
            } else if (trimmed.startsWith('item')) {
                const name = trimmed.split('"')[1];
                currentItem = new Item(name);
                currentRoom.items[name] = currentItem;
            } else if (trimmed.startsWith('animal')) {
                const name = trimmed.split('"')[1];
                currentAnimal = new Animal(name);
                currentRoom.animals[name] = currentAnimal;
            } else if (trimmed.startsWith('action')) {
                const action = trimmed.split('"')[1];
                if (currentItem) {
                    currentItem.action = action;
                } else {
                    currentAnimal.action = action;
                }
            } else if (trimmed.startsWith('connection')) {
                const direction = trimmed.split('"')[1];
                const roomName = trimmed.split('"')[3];
                currentRoom.connections[direction] = roomName;
            } 
        }
    }

    interpretCommand(command) {
        const tokens = command.split(' ');
        const action = tokens[0];
        const object = tokens[1];

        if (action === 'go') {
            this.currentRoom = this.rooms[this.currentRoom.connections[object]];
        } else if (action === 'take') {
            if (this.currentRoom.items[object]) {
                console.log(this.currentRoom.items[object].action);
                this.inventory.push(this.currentRoom.items[object]);
                delete this.currentRoom.items[object];
            }
        } else if (action === 'drop') {
            if (this.inventory.includes(object)) {
                this.currentRoom.items[object] = object;
                this.inventory = this.inventory.filter(item => item !== object);
                console.log(`You dropped the ${object}.`);
            }
        } else if (action === 'examine') {
            if (this.currentRoom.items[object]) {
                console.log(this.currentRoom.items[object].description);
            } else if (this.inventory.includes(object)) {
                console.log(object.description);
            } else {
                console.log(`There is no ${object} here to examine.`);
            }
        } else if (this.currentRoom.animals[object]) {
            console.log(this.currentRoom.animals[object].action);
        }

        this.history.push(command);
    }
}

// Create a new interpreter
const interpreter = new Interpreter();

// Parse the DSL file
interpreter.parseDSLFile(process.argv[2]);

// Start a command line interface
const readline = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
});

readline.on('line', (line) => {
    interpreter.interpretCommand(line);
});