8000 fixed compatibility · neumond/python-computer-craft@1e28ff9 · GitHub
[go: up one dir, main page]

Skip to 8000 content

Commit 1e28ff9

Browse files
committed
fixed compatibility
1 parent 902e2e5 commit 1e28ff9

File tree

7 files changed

+137
-10
lines changed

7 files changed

+137
-10
lines changed

computercraft/back.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local genv = getfenv()
22
local temp = {}
33
genv.temp = temp
4-
local url = 'http://127.0.0.1:4343/'
4+
local url = 'http://127.0.0.1:8080/'
55
local args = {...}
66
local tasks = {}
77

@@ -67,13 +67,13 @@ local function event_queue(task_id, event)
6767
end
6868

6969
local function fetch_fn()
70-
local r = http.post(url..'start/'..os.getComputerID()..'/'..args[1]..'/')
70+
local r = http.post(url..'start/'..os.getComputerID()..'/'..args[1]..'/', "")
7171
if (r == nil or r.getResponseCode() ~= 200) then
7272
print('Failed to start program '..args[1])
7373
return
7474
end
7575
while true do
76-
r = http.post(url..'gettask/'..os.getComputerID()..'/', answer)
76+
r = http.post(url..'gettask/'..os.getComputerID()..'/', "")
7777
if (r == nil or r.getResponseCode() ~= 200) then
7878
print('Connection broken')
7979
return

computercraft/server.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,11 @@ async def taskresult(self, request):
193193
def backdoor(request):
194194
with open(LUA_FILE, 'r') as f:
195195
fcont = f.read()
196-
fcont = fcont.replace(
197-
"local url = 'http://127.0.0.1:4343/'",
198-
"local url = '{}://{}/'".format(request.scheme, request.host)
199-
)
196+
new_url = "local url = '{}://{}/'".format(request.scheme, request.host)
197+
# fcont = fcont.replace(
198+
# "local url = 'http://127.0.0.1:8080/'",
199+
# new_url
200+
# )
200201
return web.Response(text=fcont)
201202

202203
def initialize(self, source_module):

examples/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
# wget http://127.0.0.1:8080/ py
12
from .hello import program as hello
3+
from .dig import dig
4+
from .test import test
25

3-
4-
__all__ = (hello, )
6+
__all__ = (hello, dig, test)

examples/dig.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
from unit import Unit
3+
import computercraft.errors
4+
5+
async def dig(api):
6+
unit = Unit(api)
7+
dim = np.array([10, 5, 10])
8+
9+
await api.print('Digging begun')
10+
for y in range(dim[1]):
11+
for z in range(dim[2]):
12+
for x in range(dim[0]):
13+
await unit.go_to([x, -y+1, z])
14+
try:
15+
await api.turtle.digDown()
16+
except computercraft.errors.CommandException:
17+
continue
18+
19+

examples/hello.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
2+
import time
3+
14
async def program(api):
2-
await api.print('Hello world!')
5+
print("moo")

examples/test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from unit import Unit
2+
3+
#http://127.0.0.1:8080/start/0/test/
4+
5+
async def test(api):
6+
unit = Unit(api)
7+
await api.print("moo")
8+
print("moo")

examples/unit.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import numpy as np
2+
import computercraft.errors
3+
4+
5+
class Unit:
6+
left = np.array([
7+
[0, 0, 1],
8+
[0, 1, 0],
9+
[-1, 0, 0]
10+
])
11+
12+
right = np.array([
13+
[0, 0, -1],
14+
[0, 1, 0],
15+
[1, 0, 0]
16+
])
17+
18+
def __init__(self, api):
19+
self.api = api
20+
self.cods = np.array([0, 0, 0])
21+
self.dire = np.array([1, 0, 0])
22+
23+
async def go_to(self, new_cods):
24+
if self.cods[0] != new_cods[0]: # X
25+
if self.cods[0] < new_cods[0]:
26+
await self.face([1, 0, 0])
27+
else:
28+
await self.face([-1, 0, 0])
29+
for _ in range(abs(self.cods[0] - new_cods[0])):
30+
await self.forward()
31+
32+
if self.cods[2] != new_cods[2]: # Z
33+
if self.cods[2] < new_cods[2]:
34+
await self.face([0, 0, 1])
35+
else:
36+
await self.face([0, 0, -1])
37+
for x in range(abs(self.cods[2] - new_cods[2])):
38+
await self.forward()
39+
40+
if self.cods[1] != new_cods[1]: # Y
41+
asce = self.cods[1] < new_cods[1]
42+
for _ in range(abs(self.cods[1] - new_cods[1])):
43+
if asce:
44+
await self.up()
45+
else:
46+
await self.down()
47+
48+
async def forward(self):
49+
await self.fuel_check()
50+
while True:
51+
try:
52+
await self.api.turtle.forward()
53+
break
54+
except computercraft.errors.CommandException:
55+
await self.api.turtle.dig()
56+
self.cods += self.dire
57+
58+
async def up(self):
59+
await self.fuel_check()
60+
while True:
61+
try:
62+
await self.api.turtle.up()
63+
break
64+
except computercraft.errors.CommandException:
65+
await self.api.turtle.digup()
66+
self.cods += [0, 1, 0]
67+
68+
async def down(self):
69+
await self.fuel_check()
70+
while True:
71+
try:
72+
await self.api.turtle.down()
73+
break
74+
except computercraft.errors.CommandException:
75+
await self.api.turtle.digDown()
76+
self.cods += [0, -1, 0]
77+
78+
async def fuel_check(self):
79+
level = await self.api.turtle.getFuelLevel()
80+
if level == 0:
81+
for i in range(1, 17):
82+
await self.api.turtle.select(i)
83+
try:
84+
await self.api.turtle.refuel()
85+
return
86+
except computercraft.errors.CommandException:
87+
continue
88+
self.api.print("Fuel depleted")
89+
90+
async def face(self, new_dire):
91+
new_dire = np.array(new_dire)
92+
while not (self.dire == new_dire).all():
93+
await self.api.turtle.turnRight()
94+
self.dire = self.dire @ self.right

0 commit comments

Comments
 (0)
0