Skip to content

Commit dd865f2

Browse files
committed
Turtle API with tests
1 parent bf3b0e8 commit dd865f2

File tree

3 files changed

+382
-68
lines changed

3 files changed

+382
-68
lines changed

computercraft/subapis/turtle.py

Lines changed: 103 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,83 @@
11
from typing import Optional
22

33
from .base import BaseSubAPI
4-
from ..lua import LuaNum
54
from ..errors import LuaException
6-
from ..rproc import nil, integer, number, boolean, fact_option, any_dict
5+
from ..rproc import integer, boolean, fact_option, any_dict, flat_try_result
76

87

98
option_any_dict = fact_option(any_dict)
109

1110

11+
def inspect_result(r):
12+
assert isinstance(r, list)
13+
assert len(r) == 2
14+
success, data = r
15+
assert isinstance(success, bool)
16+
if not success:
17+
if data == 'No block to inspect':
18+
return None
19+
raise LuaException(data)
20+
else:
21+
return any_dict(data)
22+
23+
24+
def boolean_with_error_exclusion(exclude_msg):
25+
def proc(r):
26+
if r is True:
27+
return True
28+
assert isinstance(r, list)
29+
assert len(r) == 2
30+
success, msg = r
31+
assert isinstance(success, bool)
32+
if not success:
33+
if msg == exclude_msg:
34+
return False
35+
raise LuaException(msg)
36+
else:
37+
return True
38+
return proc
39+
40+
41+
dig_result = boolean_with_error_exclusion('Nothing to dig here')
42+
move_result = boolean_with_error_exclusion('Movement obstructed')
43+
place_result = boolean_with_error_exclusion('Cannot place block here')
44+
suck_result = boolean_with_error_exclusion('No items to take')
45+
drop_result = boolean_with_error_exclusion('No items to drop')
46+
transfer_result = boolean_with_error_exclusion('No space for items')
47+
attack_result = boolean_with_error_exclusion('Nothing to attack here')
48+
craft_result = boolean_with_error_exclusion('No matching recipes')
49+
50+
51+
def always_true(r):
52+
assert boolean(r) is True
53+
# return value is useless
54+
return None
55+
56+
1257
class TurtleAPI(BaseSubAPI):
13-
async def craft(self, quantity: int):
14-
return nil(await self._send('craft', quantity))
58+
async def craft(self, quantity: int = 1) -> bool:
59+
return craft_result(await self._send('craft', quantity))
1560

16-
async def forward(self):
17-
return nil(await self._send('forward'))
61+
async def forward(self) -> bool:
62+
return move_result(await self._send('forward'))
1863

19-
async def back(self):
20-
return nil(await self._send('back'))
64+
async def back(self) -> bool:
65+
return move_result(await self._send('back'))
2166

22-
async def up(self):
23-
return nil(await self._send('up'))
67+
async def up(self) -> bool:
68+
return move_result(await self._send('up'))
2469

25-
async def down(self):
26-
return nil(await self._send('down'))
70+
async def down(self) -> bool:
71+
return move_result(await self._send('down'))
2772

2873
async def turnLeft(self):
29-
return nil(await self._send('turnLeft'))
74+
return always_true(await self._send('turnLeft'))
3075

3176
async def turnRight(self):
32-
return nil(await self._send('turnRight'))
77+
return always_true(await self._send('turnRight'))
3378

3479
async def select(self, slotNum: int):
35-
return nil(await self._send('select', slotNum))
80+
return always_true(await self._send('select', slotNum))
3681

3782
async def getSelectedSlot(self) -> int:
3883
return integer(await self._send('getSelectedSlot'))
@@ -47,37 +92,37 @@ async def getItemDetail(self, slotNum: int = None) -> dict:
4792
return option_any_dict(await self._send('getItemDetail', slotNum))
4893

4994
async def equipLeft(self):
50-
return nil(await self._send('equipLeft'))
95+
return always_true(await self._send('equipLeft'))
5196

5297
async def equipRight(self):
53-
return nil(await self._send('equipRight'))
98+
return always_true(await self._send('equipRight'))
5499

55-
async def attack(self):
56-
return nil(await self._send('attack'))
100+
async def attack(self) -> bool:
101+
return attack_result(await self._send('attack'))
57102

58-
async def attackUp(self):
59-
return nil(await self._send('attackUp'))
103+
async def attackUp(self) -> bool:
104+
return attack_result(await self._send('attackUp'))
60105

61-
async def attackDown(self):
62-
return nil(await self._send('attackDown'))
106+
async def attackDown(self) -> bool:
107+
return attack_result(await self._send('attackDown'))
63108

64-
async def dig(self):
65-
return nil(await self._send('dig'))
109+
async def dig(self) -> bool:
110+
return dig_result(await self._send('dig'))
66111

67-
async def digUp(self):
68-
return nil(await self._send('digUp'))
112+
async def digUp(self) -> bool:
113+
return dig_result(await self._send('digUp'))
69114

70-
async def digDown(self):
71-
return nil(await self._send('digDown'))
115+
async def digDown(self) -> bool:
116+
return dig_result(await self._send('digDown'))
72117

73-
async def place(self, signText: str = None):
74-
return nil(await self._send('place', signText))
118+
async def place(self, signText: str = None) -> bool:
119+
return place_result(await self._send('place', signText))
75120

76-
async def placeUp(self):
77-
return nil(await self._send('placeUp'))
121+
async def placeUp(self) -> bool:
122+
return place_result(await self._send('placeUp'))
78123

79-
async def placeDown(self):
80-
return nil(await self._send('placeDown'))
124+
async def placeDown(self) -> bool:
125+
return place_result(await self._send('placeDown'))
81126

82127
async def detect(self) -> bool:
83128
return boolean(await self._send('detect'))
@@ -88,22 +133,14 @@ async def detectUp(self) -> bool:
88133
async def detectDown(self) -> bool:
89134
return boolean(await self._send('detectDown'))
90135

91-
async def _inspect(self, method):
92-
try:
93-
return any_dict(await self._send(method))
94-
except LuaException as e:
95-
if e.args == ('No block to inspect', ):
96-
return None
97-
raise e
98-
99136
async def inspect(self) -> Optional[dict]:
100-
return self._inspect('inspect')
137+
return inspect_result(await self._send('inspect'))
101138

102139
async def inspectUp(self) -> Optional[dict]:
103-
return self._inspect('inspectUp')
140+
return inspect_result(await self._send('inspectUp'))
104141

105142
async def inspectDown(self) -> Optional[dict]:
106-
return self._inspect('inspectDown')
143+
return inspect_result(await self._send('inspectDown'))
107144

108145
async def compare(self) -> bool:
109146
return boolean(await self._send('compare'))
@@ -117,32 +154,32 @@ async def compareDown(self) -> bool:
117154
async def compareTo(self, slot: int) -> bool:
118155
return boolean(await self._send('compareTo', slot))
119156

120-
async def drop(self, count: LuaNum = None):
121-
return nil(await self._send('drop', count))
157+
async def drop(self, count: int = None) -> bool:
158+
return drop_result(await self._send('drop', count))
122159

123-
async def dropUp(self, count: LuaNum = None):
124-
return nil(await self._send('dropUp', count))
160+
async def dropUp(self, count: int = None) -> bool:
161+
return drop_result(await self._send('dropUp', count))
125162

126-
async def dropDown(self, count: LuaNum = None):
127-
return nil(await self._send('dropDown', count))
163+
async def dropDown(self, count: int = None) -> bool:
164+
return drop_result(await self._send('dropDown', count))
128165

129-
async def suck(self, amount: LuaNum = None):
130-
return nil(await self._send('suck', amount))
166+
async def suck(self, amount: int = None) -> bool:
167+
return suck_result(await self._send('suck', amount))
131168

132-
async def suckUp(self, amount: LuaNum = None):
133-
return nil(await self._send('suckUp', amount))
169+
async def suckUp(self, amount: int = None) -> bool:
170+
return suck_result(await self._send('suckUp', amount))
134171

135-
async def suckDown(self, amount: LuaNum = None):
136-
return nil(await self._send('suckDown', amount))
172+
async def suckDown(self, amount: int = None) -> bool:
173+
return suck_result(await self._send('suckDown', amount))
137174

138-
async def refuel(self, quantity: LuaNum = None):
139-
return nil(await self._send('refuel', quantity))
175+
async def refuel(self, quantity: int = None):
176+
return flat_try_result(await self._send('refuel', quantity))
140177

141-
async def getFuelLevel(self) -> LuaNum:
142-
return number(await self._send('getFuelLevel'))
178+
async def getFuelLevel(self) -> int:
179+
return integer(await self._send('getFuelLevel'))
143180

144-
async def getFuelLimit(self) -> LuaNum:
145-
return number(await self._send('getFuelLimit'))
181+
async def getFuelLimit(self) -> int:
182+
return integer(await self._send('getFuelLimit'))
146183

147-
async def transferTo(self, slot: int, quantity: int = None):
148-
return nil(await self._send('transferTo', slot, quantity))
184+
async def transferTo(self, slot: int, quantity: int = None) -> bool:
185+
return transfer_result(await self._send('transferTo', slot, quantity))

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[flake8]
22
max-line-length = 120
3-
ignore = I,C812,N802,N803,N815
3+
ignore = I,C812,N802,N803,N815,W503

0 commit comments

Comments
 (0)