Advertisement
Guest User

Untitled

a guest
Apr 30th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. const { NethackSession } = require('../nodehack');
  2. // const startscumItems = require('./startscum_items');
  3.  
  4. const username = 'username';
  5. const password = 'password';
  6. const MAX_TRIES = 10;
  7. const TIMEOUT = 10000;
  8.  
  9. const startscumItems = { items: ['ring of slow digestion', 'spellbook of identify'] };
  10.  
  11. const menuExpressions = startscumItems.items.reduce((acc, cur) => {
  12. const key = cur.replace(' ', '_');
  13. const regexp = RegExp(`(?<${key}>${cur})`);
  14. return ({ ...acc, [key]: regexp });
  15. }, {});
  16.  
  17. const check = async (user, pw, tries) => {
  18. const nethackSession = new NethackSession();
  19. await nethackSession.loginSSH('hardfoughtEU', user, pw);
  20. if (!nethackSession.connected) {
  21. throw new Error('Couldn\'t connect!');
  22. }
  23. const customExpressions = {
  24. menu: menuExpressions,
  25. };
  26.  
  27. const screens = await nethackSession.doInput('i', customExpressions);
  28. const [{ menu }] = screens.slice(-1);
  29. const { items } = menu.data;
  30. const menuKeys = Object.keys(menuExpressions);
  31. const success = menuKeys.every(key => items.includes(item => item[key] !== null));
  32. await nethackSession.doInput(String.fromCharCode(0x1b)); // escape
  33. if (success) {
  34. await nethackSession.doInput('S');
  35. await nethackSession.doInput('y');
  36. console.log(`Success: Found all items after ${MAX_TRIES - tries}!`);
  37. } else {
  38. await nethackSession.doInput('#quit\n');
  39. await nethackSession.doInput('y');
  40. if (tries <= 0) {
  41. console.log(`Failure: Could not find all items after ${MAX_TRIES} tries`);
  42. } else {
  43. setTimeout(() => check(user, pw, tries - 1), TIMEOUT);
  44. }
  45. }
  46. nethackSession.close();
  47. };
  48.  
  49. check(username, password, MAX_TRIES);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement