-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic.js
47 lines (37 loc) · 1.42 KB
/
basic.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
const { NethackSession } = require('nodehack');
const nethackSession = new NethackSession();
const username = 'username';
const password = 'password';
// lazy top-level await
(async () => {
await nethackSession.loginSSH('hardfoughtEU', username, password);
if (!nethackSession.connected) {
throw new Error('Couldn\'t connect!');
}
// lets check the inventory
// inject custom inventory handler: all worn items on the inventory page
const customExpressions = {
menu: {
worn: /\((being (?<worn>worn))\)/,
},
};
const screens = await nethackSession.doInput('i', customExpressions);
// get last screen
const [{ menu, map }] = screens.slice(-1);
const { items, page, numPages } = menu.data;
// all items with the attribute 'worn' matched the custom expression
const wornItems = items.filter(item => item.worn !== null).map(item => item.item);
console.log(`Worn items on inventory page ${page} of ${numPages}`);
console.log(wornItems);
// print map
// console.log(map.window.toChunkedString(' '));
// find all pets on the map by tile information
const petsOnMap = map.glyphs.filter(glyph => glyph.effects.includes('pet'));
console.log(petsOnMap);
// pressing escape to exit inventory menu
await nethackSession.doInput(String.fromCharCode(0x1b)); // escape
// yes, we want to Save
await nethackSession.doInput('S');
await nethackSession.doInput('y');
nethackSession.close();
})();