This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
546 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export {Status} from './status'; | ||
export {Hostname} from './hostname'; | ||
export {Miner} from './miner'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {Command, IOHandler} from '../command'; | ||
import {Device} from '../../api/devices/device'; | ||
import {ShellApi} from '../shellapi'; | ||
|
||
export class Hostname extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('hostname', shellApi); | ||
this.addDescription('changes the name of the device'); | ||
this.addPositionalArgument('name', true); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
const args = iohandler.positionalArgs; | ||
if (args.length === 1) { | ||
const hostname = args[0]; | ||
let newDevice: Device; | ||
try { | ||
newDevice = await this.shellApi.websocket.ms('device', ['device', 'change_name'], { | ||
device_uuid: this.shellApi.activeDevice['uuid'], | ||
name: hostname | ||
}).toPromise(); | ||
} catch { | ||
iohandler.stderr('The hostname couldn\'t be changed'); | ||
return 1; | ||
} | ||
this.shellApi.activeDevice = newDevice; | ||
this.shellApi.refreshPrompt(); | ||
|
||
if (this.shellApi.activeDevice.uuid === this.shellApi.windowDelegate.device.uuid) { | ||
Object.assign(this.shellApi.windowDelegate.device, newDevice); | ||
} | ||
} else { | ||
let device: Device; | ||
try { | ||
device = await this.shellApi.websocket.ms('device', ['device', 'info'], {device_uuid: this.shellApi.activeDevice['uuid']}).toPromise(); | ||
} | ||
catch { | ||
iohandler.stdout(this.shellApi.activeDevice['name']); | ||
} | ||
if (device['name'] !== this.shellApi.activeDevice['name']) { | ||
this.shellApi.activeDevice = device; | ||
this.shellApi.refreshPrompt(); | ||
} | ||
iohandler.stdout(device['name']); | ||
} | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import {Command, IOHandler} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
|
||
export class Miner extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('miner', shellApi); | ||
this.addDescription('mangages morphcoin miners'); | ||
this.addSubcommand('look', MinerLook); | ||
this.addSubcommand('wallet', MinerWallet); | ||
this.addSubcommand('power', MinerPower); | ||
this.addSubcommand('start', MinerStart); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
this.showHelp(iohandler.stderr); | ||
return -1; | ||
} | ||
} | ||
|
||
async function getMinerService(shellApi: ShellApi): Promise<any> { | ||
const listData = await shellApi.websocket.msPromise('service', ['list'], { | ||
'device_uuid': shellApi.activeDevice['uuid'], | ||
}); | ||
for (let i = 0; i < listData.services.length; i++) { | ||
const miner: any = listData.services[i]; | ||
if (miner.name === 'miner') { | ||
return miner; | ||
} | ||
} | ||
throw new Error("miner service not found"); | ||
} | ||
|
||
class MinerLook extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('look', shellApi); | ||
this.addDescription('shows your current miner settings'); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
let miner: any; | ||
try { | ||
miner = await getMinerService(this.shellApi); | ||
} catch { | ||
iohandler.stderr('Miner service not reachable'); | ||
return 1; | ||
} | ||
const data = await this.shellApi.websocket.msPromise('service', ['miner', 'get'], { | ||
'service_uuid': miner.uuid, | ||
}); | ||
const wallet = data['wallet']; | ||
const power = Math.round(data['power'] * 100); | ||
iohandler.stdout('Wallet: ' + wallet); | ||
iohandler.stdout('Mining Speed: ' + String(Number(miner.speed) * 60 * 60) + ' MC/h'); | ||
iohandler.stdout('Power: ' + power + '%'); | ||
return 0; | ||
} | ||
} | ||
|
||
class MinerWallet extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('wallet', shellApi); | ||
this.addDescription('set the miner to a wallet'); | ||
this.addPositionalArgument('wallet-id'); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
let miner: any; | ||
try { | ||
miner = await getMinerService(this.shellApi); | ||
} catch { | ||
iohandler.stderr('Miner service not reachable'); | ||
return 1; | ||
} | ||
try { | ||
const newWallet = iohandler.positionalArgs[0]; | ||
await this.shellApi.websocket.msPromise('service', ['miner', 'wallet'], { | ||
'service_uuid': miner.uuid, | ||
'wallet_uuid': newWallet, | ||
}); | ||
iohandler.stdout(`Set wallet to ${newWallet}`); | ||
return 0; | ||
} catch { | ||
iohandler.stderr('Wallet is invalid.'); | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
class MinerPower extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('power', shellApi); | ||
this.addDescription('set the power of your miner'); | ||
this.addPositionalArgument('<0-100>'); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
const args = iohandler.positionalArgs; | ||
let miner: any; | ||
try { | ||
miner = await getMinerService(this.shellApi); | ||
} catch { | ||
iohandler.stderr('Miner service not reachable'); | ||
return 1; | ||
} | ||
if (args.length !== 1 || isNaN(Number(args[0])) || 0 > Number(args[0]) || Number(args[0]) > 100) { | ||
this.showHelp(iohandler.stderr); | ||
return 1; | ||
} | ||
await this.shellApi.websocket.msPromise('service', ['miner', 'power'], { | ||
'service_uuid': miner.uuid, | ||
'power': Number(args[1]) / 100, | ||
}); | ||
iohandler.stdout(`Set Power to ${args[0]}`); | ||
return 0; | ||
} | ||
} | ||
|
||
|
||
class MinerStart extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('start', shellApi); | ||
this.addDescription('start the miner'); | ||
this.addPositionalArgument('wallet-id'); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
const args = iohandler.positionalArgs; | ||
try { | ||
await this.shellApi.websocket.msPromise('service', ['create'], { | ||
'device_uuid': this.shellApi.activeDevice['uuid'], | ||
'name': 'miner', | ||
'wallet_uuid': args[0], | ||
}); | ||
return 0; | ||
} catch { | ||
iohandler.stderr('Invalid wallet'); | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {Command, IOHandler} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
|
||
export class Status extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('status', shellApi); | ||
this.addDescription('displays the number of online players'); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
const r = await this.shellApi.websocket.requestPromise({ | ||
action: 'info' | ||
}); | ||
iohandler.stdout('Online players: ' + r.online); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Command, IOHandler} from '../command'; | ||
import {ShellApi} from '../shellapi'; | ||
|
||
export class Template extends Command { | ||
constructor(shellApi: ShellApi) { | ||
super('COMMANDNAME', shellApi); | ||
this.addDescription(''); | ||
} | ||
|
||
async run(iohandler: IOHandler): Promise<number> { | ||
return -1; | ||
} | ||
} |
Oops, something went wrong.