-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
131 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<head> | ||
<title>Едрен батон вебсайт для websocketа</title> | ||
</head> | ||
<body> | ||
<p id="returnText">Тут будет текст после выполнения команд с бота</p> | ||
<p id="turtlesID">Тут будут id черепах когда websocket проснется</p> | ||
<p id="SelfIDText">Тут будет твой id</p> | ||
<button id="sendCmd" onclick="sendCmd()">Отправить команду</button> | ||
<button id="updateBots" onclick="updateRobots()">Обновить ботов</button> | ||
<input type="text" name="ID черепахи" id="turtleID"> | ||
<input type="text" name="Команда" id="command"> | ||
<script src="./websocketCl.js"></script> | ||
<button onclick="destroyConnection()">Отключиться от websocket'а</button> | ||
<button onclick="action('turnLeft')">Повернуть налево</button> | ||
<button onclick="action('forward')">Вперед</button> | ||
<button onclick="action('back')">Назад</button> | ||
<button onclick="action('turnRight')">Повернуть направо</button> | ||
<button onclick="action('up')">Вверх</button> | ||
<button onclick="action('down')">Вниз</button> | ||
<button onclick="action('dig')">Выкопать</button> | ||
<button onclick="action('digUp')">Выкопать вверх</button> | ||
<button onclick="action('digDown')">Выкопать вниз</button> | ||
<button onclick="action('getItemDetail')">Что в руке?</button> | ||
<button onclick="actionAdv('nextSlot')">Слот вперед</button> | ||
<button onclick="actionAdv('prevSlot')">Слот назад</button> | ||
<button onclick="action('place')">Поставить</button> | ||
<button onclick="action('placeUp')">Поставить вверх</button> | ||
<button onclick="action('placeDown')">Поставить вниз</button> | ||
</body> |
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,102 @@ | ||
let ws = new WebSocket("ws://localhost:9000") | ||
let mainText = document.getElementById('returnText') | ||
let turtlesIDT = document.getElementById('turtlesID') | ||
let turtleIDField = document.getElementById('turtleID') | ||
let commandField = document.getElementById('command') | ||
let selfIdText = document.getElementById('SelfIDText') | ||
let robots = [] | ||
let ID = "" | ||
ws.onopen = function(e) { | ||
console.log("Подключился к бебсокету") | ||
ws.send('{"message":"socketInfo","who":"comander"}') | ||
ws.send('{"message":"getRobots"}') | ||
} | ||
|
||
ws.onmessage = function(msg) { | ||
data = JSON.parse(msg.data) | ||
if(data.message == "returnBots") { | ||
console.log("Роботов накинули:") | ||
console.log(data.bots) | ||
robots = data.bots | ||
turtlesIDT.innerHTML = beautifyBots(); | ||
} else if(data.message == "logDone") { | ||
console.log("получил свой ID") | ||
ID = data.uniqueID | ||
selfIdText.innerHTML = "Ваш ID:" + ID | ||
} else if(data.message == "returnValue") { | ||
console.log("Бот что-то вернул") | ||
console.log(data.val) | ||
if(data.infoType == 'state') { | ||
if(typeof(data.val) == 'string') { | ||
trueData = eval(data.val) | ||
} else { | ||
trueData = data.val | ||
} | ||
mainText.innerHTML = "Последнее, что вернул бот: " + trueData | ||
} else if(data.infoType == 'block') { | ||
mainText.innerHTML = "Последнее, что вернул бот: " + data.val.name | ||
} else if(data.infoType == 'crit') { | ||
mainText.innerHTML = "Ошибка при исполнении команды, возможно неправильная команды" | ||
} else if(data.infoType == 'stringInfo') { | ||
if(data.val == "Movement obstructed") { | ||
mainText.innerHTML = "Движение невозможно" | ||
} else { | ||
mainText.innerHTML = "Последнее, что вернул бот: " + data.val | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
ws.onclose = function(e) { | ||
console.log("Вебсокет умрал") | ||
} | ||
|
||
function updateRobots() { | ||
ws.send('{"message":"getRobots"}') | ||
} | ||
|
||
function destroyConnection() { | ||
console.log("закрываю соединение") | ||
ws.close() | ||
} | ||
|
||
function action(act) { | ||
sendPrebuildCMD("turtle."+act+"()") | ||
} | ||
|
||
function actionAdv(advAct) { | ||
if(advAct == 'nextSlot') { | ||
sendPrebuildCMD("turtle.select(turtle.getSelectedSlot()+1)") | ||
} else if(advAct == 'prevSlot') { | ||
sendPrebuildCMD("turtle.select(turtle.getSelectedSlot()-1)") | ||
} | ||
} | ||
|
||
function beautifyBots() { | ||
magicString = "" | ||
for(var i = 0; i < robots.length;i++) { | ||
magicString += `Бот ${robots[i].id}, топливо: ${robots[i].fuel}<br>` | ||
} | ||
return magicString | ||
} | ||
|
||
function sendCmd() { | ||
if(turtleIDField.value == "") { | ||
alert("Введите ID черепашки") | ||
} else if(commandField.value == "") { | ||
alert("Введите команду") | ||
} else { | ||
ws.send(`{"message":"newCommand","toID":"${turtleIDField.value}","command":"${commandField.value}","comanderID":"${ID}"}`) | ||
ws.send('{"message":"getRobots"}') | ||
} | ||
} | ||
|
||
function sendPrebuildCMD(cmd) { | ||
if(turtleIDField.value == "") { | ||
alert("Введите ID черепашки") | ||
} else { | ||
ws.send(`{"message":"newCommand","toID":"${turtleIDField.value}","command":"${cmd}","comanderID":"${ID}"}`) | ||
ws.send('{"message":"getRobots"}') | ||
} | ||
} |