-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (49 loc) · 1.53 KB
/
main.py
File metadata and controls
64 lines (49 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import fastapi
import uvicorn
import asyncio
from mcturtle import MCTurtle
turtles: dict[int, MCTurtle] = {}
app = fastapi.FastAPI()
@app.websocket("/turtle")
async def turtle_websocket_endpoint(websocket: fastapi.WebSocket):
turtle_id = websocket.query_params.get("turtle_id")
if turtle_id is None:
await websocket.close(code=1008, reason="Turtle ID is required")
return
await websocket.accept()
print(f"connection open")
turtle = MCTurtle(int(turtle_id), websocket)
turtles[turtle.id] = turtle
asyncio.create_task(on_turtle(turtle))
await turtle.loop()
try:
await turtle.loop()
finally:
turtles.pop(turtle.id, None)
@app.get("/")
async def turtles_endpoint():
data = {}
for turtle in turtles.values():
blocks, inv = await asyncio.gather(
turtle.get_blocks(),
turtle.get_inventory()
)
data[turtle.id] = {
"blocks": blocks,
"inventory": inv,
}
return data
# just a simple example function on how
# you could use this API, this script
# connects to turtle #0, waits 5s, then
# selects sandstone and places it
async def on_turtle(turtle: MCTurtle):
if turtle.id != 0:
return
await asyncio.sleep(5)
await turtle.select_item("sandstone")
#await turtle.place() # TODO: impliment place
#await turtle.up(10)
#print('done moving')
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8080, reload=True)