1
1
from typing import Optional
2
2
3
3
from .base import BaseSubAPI
4
- from ..lua import LuaNum
5
4
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
7
6
8
7
9
8
option_any_dict = fact_option (any_dict )
10
9
11
10
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
+
12
57
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 ))
15
60
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' ))
18
63
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' ))
21
66
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' ))
24
69
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' ))
27
72
28
73
async def turnLeft (self ):
29
- return nil (await self ._send ('turnLeft' ))
74
+ return always_true (await self ._send ('turnLeft' ))
30
75
31
76
async def turnRight (self ):
32
- return nil (await self ._send ('turnRight' ))
77
+ return always_true (await self ._send ('turnRight' ))
33
78
34
79
async def select (self , slotNum : int ):
35
- return nil (await self ._send ('select' , slotNum ))
80
+ return always_true (await self ._send ('select' , slotNum ))
36
81
37
82
async def getSelectedSlot (self ) -> int :
38
83
return integer (await self ._send ('getSelectedSlot' ))
@@ -47,37 +92,37 @@ async def getItemDetail(self, slotNum: int = None) -> dict:
47
92
return option_any_dict (await self ._send ('getItemDetail' , slotNum ))
48
93
49
94
async def equipLeft (self ):
50
- return nil (await self ._send ('equipLeft' ))
95
+ return always_true (await self ._send ('equipLeft' ))
51
96
52
97
async def equipRight (self ):
53
- return nil (await self ._send ('equipRight' ))
98
+ return always_true (await self ._send ('equipRight' ))
54
99
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' ))
57
102
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' ))
60
105
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' ))
63
108
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' ))
66
111
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' ))
69
114
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' ))
72
117
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 ))
75
120
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' ))
78
123
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' ))
81
126
82
127
async def detect (self ) -> bool :
83
128
return boolean (await self ._send ('detect' ))
@@ -88,22 +133,14 @@ async def detectUp(self) -> bool:
88
133
async def detectDown (self ) -> bool :
89
134
return boolean (await self ._send ('detectDown' ))
90
135
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
-
99
136
async def inspect (self ) -> Optional [dict ]:
100
- return self ._inspect ('inspect' )
137
+ return inspect_result ( await self ._send ('inspect' ) )
101
138
102
139
async def inspectUp (self ) -> Optional [dict ]:
103
- return self ._inspect ('inspectUp' )
140
+ return inspect_result ( await self ._send ('inspectUp' ) )
104
141
105
142
async def inspectDown (self ) -> Optional [dict ]:
106
- return self ._inspect ('inspectDown' )
143
+ return inspect_result ( await self ._send ('inspectDown' ) )
107
144
108
145
async def compare (self ) -> bool :
109
146
return boolean (await self ._send ('compare' ))
@@ -117,32 +154,32 @@ async def compareDown(self) -> bool:
117
154
async def compareTo (self , slot : int ) -> bool :
118
155
return boolean (await self ._send ('compareTo' , slot ))
119
156
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 ))
122
159
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 ))
125
162
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 ))
128
165
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 ))
131
168
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 ))
134
171
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 ))
137
174
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 ))
140
177
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' ))
143
180
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' ))
146
183
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 ))
0 commit comments