Skip to content

Instantly share code, notes, and snippets.

@huytd
Created October 12, 2023 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huytd/71e7f1de289ca613c9f360f349776f9d to your computer and use it in GitHub Desktop.
Save huytd/71e7f1de289ca613c9f360f349776f9d to your computer and use it in GitHub Desktop.
Simple bot for tokyo-rs
const USERNAME = "rotate-bot";
const ws = require('ws');
const client = new ws(`wss://combat.sege.dev/socket?key=${USERNAME}&name=${USERNAME}`);
client.on('open', () => {
console.log('Connected!');
setInterval(() => {
client.send(JSON.stringify({
"e": "throttle",
"data": 1
}));
}, 500);
setInterval(() => {
client.send(JSON.stringify({
"e": "fire"
}));
}, 1000);
});
let rotate = 0;
client.on('message', (data) => {
const buffer = Buffer.from(data);
const json = JSON.parse(buffer);
console.log("TICK", Date.now());
console.log(json);
client.send(JSON.stringify({
"e": "rotate",
"data": rotate
}))
rotate += 0.2;
});
client.on('close', () => {
console.log('Disconnected!');
});
@unrealhoang
Copy link

const USERNAME = "human";

const ws = require('ws');
const client = new ws(`wss://combat.sege.dev/socket?key=${USERNAME}&name=${USERNAME}`);
const stdin = process.stdin;
var action;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', function(key){
  // ctrl-c ( end of text )
  if (key === '\u0003') {
    process.exit();
  }
  switch (key) {
    case 'a':
      action = 'turnLeft';
      break;
    case 'd':
      action = 'turnRight';
      break;
    case 'w':
      action = 'speedUp';
      break;
    case 's':
      action = 'slowDown';
      break;
    case 'q':
      action = 'stop';
      break;
    case 'e':
      action = 'fire';
      break;
  }
});

client.on('open', () => {
  console.log('Connected!');

  setInterval(() => {
    client.send(JSON.stringify({
      "e": "throttle",
      "data": 1
    }));
  }, 500);

  setInterval(() => {
    client.send(JSON.stringify({
      "e": "fire"
    }));
  }, 1000);
});

let rotate = 0;
let throttle = 0.5;

client.on('message', (data) => {
  const buffer = Buffer.from(data);
  const json = JSON.parse(buffer);

  switch (action) {
    case 'turnLeft':
      rotate -= 0.2;
      client.send(JSON.stringify({
        "e": "rotate",
        "data": rotate
      }))
      break;
    case 'turnRight':
      rotate += 0.2;
      client.send(JSON.stringify({
        "e": "rotate",
        "data": rotate
      }))
      break;
    case 'speedUp':
      throttle = Math.min(throttle + 0.2, 1);
      client.send(JSON.stringify({
        "e": "throttle",
        "data": throttle,
      }));
      break;
    case 'slowDown':
      throttle = Math.max(throttle - 0.2, 0);
      client.send(JSON.stringify({
        "e": "throttle",
        "data": throttle,
      }));
      break;
    case 'fire':
      client.send(JSON.stringify({
        "e": "fire"
      }));
      break;
    case 'stop':
      throttle = 0;
      client.send(JSON.stringify({
        "e": "throttle",
        "data": throttle,
      }));
      break;
  };
  action = '';
});

client.on('close', () => {
  console.log('Disconnected!');
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment