Skip to content
This repository has been archived by the owner on Feb 9, 2020. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rigwild committed Jan 7, 2020
1 parent e15514b commit fc9a7a9
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 rigwild
Copyright (c) 2019 rigwild <me@rigwild.dev> (https://rigwild.dev/)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 12 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const serverIp = '127.0.0.1'
const serverPort = '8769'
const serverURI = `ws://${serverIp}:${serverPort}`

const packetDelayMs = 100

module.exports = {
serverIp,
serverPort,
serverURI,
packetDelayMs
}
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const WebSocket = require('ws')

const config = require('./config')
const { startScriptLoaderShell } = require('./utils')

const ws = new WebSocket(config.serverURI)

ws.on('open', async () => {
ws.send('hello')
startScriptLoaderShell(ws)
})

ws.on('message', data => {
// Log sniffing data
console.log(data)
})

9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "js",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"ws": "^7.2.1"
}
}
9 changes: 9 additions & 0 deletions scripts/turn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const { pause, receivePacket, sendPacket } = require('../utils')

const run = async wsClient => {
// Turn your player
for (let i = 0; i <= 30; i++)
await sendPacket(wsClient, `eD${i}`)
}

module.exports = { run }
72 changes: 72 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const readline = require('readline')
const fs = require('fs')
const path = require('path')

const config = require('./config')

/**
* Start a shell `RPC>` which will load any scripts from the `scripts` directory
* @param {any} wsClient WebSocket client
*/
const startScriptLoaderShell = (wsClient) => {
let shell = readline.createInterface(process.stdin, process.stdout)
shell.setPrompt('RPC> ')
shell.prompt()
shell.on('line', async line => {
// A script was asked, read the scripts directory
const scripts = await fs.promises.readdir(path.resolve(__dirname, 'scripts'))

// The script was found, run it
if (scripts.find(x => x.toLowerCase() === line.toLowerCase())) {
console.log(`Running the script "${line}".`)
// TODO: loaded script is cached so can't be edited on-the-fly
const loadedScript = require(path.resolve(__dirname, 'scripts', line))
loadedScript.run(wsClient)
}
else console.log(`The script "${line}" was not found in the "scripts" directory.`)
shell.prompt()
}).on('close', () => process.exit(0))
}


/**
* Pause execution
* @param {number} ms Number of milliseconds to wait
*/
const pause = ms => new Promise(res => setTimeout(res, ms))

/**
* RPC call
* @param {any} wsClient WebSocket client
* @param {string[]} packetList List of packets to send
* @param {number} delayBetweenPackets Delay between each packet sending
*/
const call = (wsClient, command, packet, delayBetweenPackets = config.packetDelayMs) => {
wsClient.send(`${command} ${packet}`)
return pause(delayBetweenPackets)
}

/**
* RPC ask to send a packet
* @param {any} wsClient WebSocket client
* @param {string[]} packetList List of packets to send
* @param {number} delayBetweenPackets Delay between each packet sending
*/
const sendPacket = (wsClient, packet, delayBetweenPackets = config.packetDelayMs) =>
call(wsClient, 'send', packet, delayBetweenPackets)

/**
* RPC ask to receive a packet
* @param {any} wsClient WebSocket client
* @param {string[]} packetList List of packets to send
* @param {number} delayBetweenPackets Delay between each packet sending
*/
const receivePacket = (wsClient, packet, delayBetweenPackets = config.packetDelayMs) =>
call(wsClient, 'receive', packet, delayBetweenPackets)

module.exports = {
startScriptLoaderShell,
pause,
sendPacket,
receivePacket
}
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


ws@^7.2.1:
version "7.2.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.2.1.tgz#03ed52423cd744084b2cf42ed197c8b65a936b8e"
integrity sha512-sucePNSafamSKoOqoNfBd8V0StlkzJKL2ZAhGQinCfNQ+oacw+Pk7lcdAElecBF2VkLNZRiIb5Oi1Q5lVUVt2A==

0 comments on commit fc9a7a9

Please sign in to comment.