Created
December 2, 2018 10:10
-
-
Save kawahara/2289befa215364e3de99134f0ed749fb to your computer and use it in GitHub Desktop.
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
const ArgumentType = require('../../extension-support/argument-type'); | |
const BlockType = require('../../extension-support/block-type'); | |
class MineCraft { | |
constructor () { | |
this.endpoint = 'http://localhost:8080/api/v5' | |
this.apiKey = null | |
this.worlds = [] | |
this.players = [] | |
this.player = null | |
this.block = null | |
} | |
setApiKey (apiKey) { | |
console.log(this) | |
this.apiKey = apiKey; | |
this.headers = { | |
'X-WebAPI-Key': this.apiKey, | |
'Content-Type': 'application/json' | |
} | |
} | |
getInfo () { | |
console.log(BlockType) | |
return { | |
id: 'minecraft', | |
name: 'MineCraft', | |
blocks: [ | |
{ | |
opcode: 'isLogin', | |
blockType: BlockType.BOOLEAN, | |
text: 'ログイン中か' | |
}, | |
{ | |
opcode: 'login', | |
blockType: BlockType.COMMAND, | |
text: 'WebAPIユーザ名[USER], パスワード[PASSWORD]でログイン', | |
arguments: { | |
USER: { | |
type: ArgumentType.STRING, | |
defaultValue: 'kawahara' | |
}, | |
PASSWORD: { | |
type: ArgumentType.STRING, | |
defaultValue: 'test' | |
} | |
} | |
}, | |
'---', | |
{ | |
opcode: 'getWorlds', | |
blockType: BlockType.COMMAND, | |
text: 'ワールド情報を取得' | |
}, | |
{ | |
opcode: 'getPlayers', | |
blockType: BlockType.COMMAND, | |
text: 'プレイヤー情報を取得' | |
}, | |
{ | |
opcode: 'say', | |
blockType: BlockType.COMMAND, | |
text: '[MSG]というメッセージを送信', | |
arguments: { | |
MSG: { | |
type: ArgumentType.STRING, | |
defaultValue: 'hello' | |
} | |
} | |
}, | |
{ | |
opcode: 'createExplosion', | |
blockType: BlockType.COMMAND, | |
text: 'x=[X], y=[Y], z=[Z]で爆発', | |
arguments: { | |
WORLD: { | |
type: ArgumentType.STRING, | |
defaultValue: 'world' | |
}, | |
X: { | |
type: ArgumentType.NUMBER, | |
defaultValue: 0 | |
}, | |
Y: { | |
type: ArgumentType.NUMBER, | |
defaultValue: 0 | |
}, | |
Z: { | |
type: ArgumentType.NUMBER, | |
defaultValue: 0 | |
} | |
} | |
}, | |
{ | |
opcode: 'getBlock', | |
blockType: BlockType.COMMAND, | |
text: '[WORLD]のx=[X], y=[Y], z=[Z]のブロックを取得', | |
arguments: { | |
WORLD: { | |
type: ArgumentType.STRING, | |
defaultValue: 'world' | |
}, | |
X: { | |
type: ArgumentType.NUMBER, | |
defaultValue: 0 | |
}, | |
Y: { | |
type: ArgumentType.NUMBER, | |
defaultValue: 0 | |
}, | |
Z: { | |
type: ArgumentType.NUMBER, | |
defaultValue: 0 | |
} | |
} | |
}, | |
{ | |
opcode: 'getBlockName', | |
blockType: BlockType.REPORTER, | |
text: 'ブロック' | |
}, | |
{ | |
opcode: 'getPlayerLength', | |
blockType: BlockType.REPORTER, | |
text: 'プレイヤー数' | |
}, | |
{ | |
opcode: 'getPlayer', | |
blockType: BlockType.COMMAND, | |
text: 'プレイヤー[PLAYER]の情報取得', | |
arguments: { | |
PLAYER: { | |
type: ArgumentType.STRING, | |
defaultValue: '' | |
} | |
} | |
}, | |
{ | |
opcode: 'getPlayerX', | |
blockType: BlockType.REPORTER, | |
text: 'プレイヤーのX' | |
}, | |
{ | |
opcode: 'getPlayerY', | |
blockType: BlockType.REPORTER, | |
text: 'プレイヤーのY' | |
}, | |
{ | |
opcode: 'getPlayerZ', | |
blockType: BlockType.REPORTER, | |
text: 'プレイヤーのZ' | |
}, | |
{ | |
opcode: 'getPlayerRotationH', | |
blockType: BlockType.REPORTER, | |
text: 'プレイヤーの向き (水平)' | |
}, | |
{ | |
opcode: 'getPlayerRotationV', | |
blockType: BlockType.REPORTER, | |
text: 'プレイヤーの向き (垂直)' | |
} | |
] | |
} | |
} | |
_delay () { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve() | |
}, 100) | |
}) | |
} | |
_executeCommand (cmd, args) { | |
return fetch(`${this.endpoint}/cmd`, { | |
method: 'POST', | |
body: JSON.stringify([{name: 'Scratch', command: cmd + ' ' + args}]), | |
headers: this.headers | |
}).then(() => { | |
return this._delay() | |
}).catch((e) => { | |
console.log(e) | |
}) | |
} | |
_getWorld (worldName) { | |
return this.worlds.find(function (w) { | |
return w.name === worldName; | |
}) | |
} | |
_getPlayer (playerName) { | |
return this.players.find(function (p) { | |
return p.name === playerName | |
}) | |
} | |
say (args) { | |
return this._executeCommand('say', args.MSG) | |
} | |
createExplosion (args) { | |
return this._executeCommand('summon', 'tnt ' + [args.X, args.Y, args.Z].join(' ') + ' {Fuse: 0}') | |
} | |
login (args) { | |
return fetch(`${this.endpoint}/user`, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({username: args.USER, password: args.PASSWORD}) | |
}).then((r) => { | |
return r.json() | |
}).then((r) => { | |
return this.setApiKey(r.key) | |
}).catch((e) => { | |
console.log(e) | |
}) | |
} | |
getWorlds () { | |
return fetch(`${this.endpoint}/world`, { | |
headers: this.headers | |
}).then((r) => { | |
console.log(r) | |
if (r.status !== 200) { | |
throw 'API call error' | |
} | |
return r.json() | |
}).then((r) => { | |
this.worlds = r | |
}).catch((e) => { | |
console.log(e) | |
}) | |
} | |
getPlayers () { | |
return fetch(`${this.endpoint}/player`, { | |
headers: this.headers | |
}).then((r) => { | |
console.log(r) | |
if (r.status !== 200) { | |
throw 'API call error' | |
} | |
return r.json() | |
}).then((r) => { | |
this.players = r | |
}).catch((e) => { | |
console.log(e) | |
}) | |
} | |
getPlayerLength () { | |
return this.players.length | |
} | |
getBlock (args) { | |
const worldObj = this._getWorld(args.WORLD) | |
if (!worldObj) { | |
return | |
} | |
return fetch(`${this.endpoint}/block/${worldObj.uuid}/${args.X}/${args.Y}/${args.Z}`, { | |
headers: this.headers | |
}).then((r) => { | |
if (r.status !== 200) { | |
throw 'API call error' | |
} | |
return r.json() | |
}).then((r) => { | |
this.block = r | |
}).catch((e) => { | |
console.log(e) | |
}) | |
} | |
getBlockName () { | |
if (!this.block) { | |
return | |
} | |
return this.block.type.id | |
} | |
isLogin () { | |
if (!this.apiKey) { | |
return false | |
} | |
return true | |
} | |
getPlayer (args) { | |
const playerObj = this._getPlayer(args.PLAYER) | |
if (!playerObj) { | |
return null | |
} | |
return fetch(`${this.endpoint}/player/${playerObj.uuid}`, { | |
headers: this.headers | |
}).then((r) => { | |
console.log(r) | |
if (r.status !== 200) { | |
throw 'API call error' | |
} | |
return r.json() | |
}).then((r) => { | |
this.player = r | |
}).catch((e) => { | |
console.log(e) | |
}) | |
} | |
getPlayerX () { | |
if (!this.player) { | |
return null; | |
} | |
return this.player.location.position.x | |
} | |
getPlayerY () { | |
if (!this.player) { | |
return null; | |
} | |
return this.player.location.position.y | |
} | |
getPlayerZ () { | |
if (!this.player) { | |
return null; | |
} | |
return this.player.location.position.z | |
} | |
getPlayerRotationH () { | |
if (!this.player) { | |
return null; | |
} | |
return this.player.rotation.y | |
} | |
getPlayerRotationV () { | |
if (!this.player) { | |
return null; | |
} | |
return this.player.rotation.x | |
} | |
} | |
module.exports = MineCraft; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment