1
1
const net = require ( 'net' ) ;
2
2
const InventoryData = require ( '../shared/InventoryData' ) ;
3
3
const MapData = require ( '../shared/MapData' ) ;
4
- const validators = require ( '../shared/fromRobotSchemas.js' ) ;
4
+ const keyToValidatorMap = require ( '../shared/fromRobotSchemas.js' ) . keyToValidatorMap ;
5
5
6
6
/**
7
7
* Used to make sure the server is working properly. Attempts to replicate
@@ -32,7 +32,12 @@ class TestClient {
32
32
this . equipped ;
33
33
this . map = new MapData ( ) ;
34
34
this . map . setFromMapData ( this . testData . map ) ;
35
- this . position = this . testData . position ;
35
+ this . position = {
36
+ x : this . testData . config . posX ,
37
+ y : this . testData . config . posY ,
38
+ z : this . testData . config . posZ ,
39
+ } ;
40
+ this . config = this . testData . config ;
36
41
this . map . set ( this . position . x , this . position . y , this . position . z , { "hardness" : 2 } ) ;
37
42
this . components = this . testData . components ;
38
43
@@ -57,12 +62,12 @@ class TestClient {
57
62
58
63
this . commandMap = {
59
64
60
- scanArea : ( scanLevel ) => {
65
+ scanArea : ( scanLevel , times ) => {
61
66
for ( let i = - 2 ; i <= 5 ; i ++ ) {
62
67
let scan = this . geolyzerScan ( - 3 , - 3 , i , 8 , 8 , 1 ) ;
63
68
this . send ( 'map data' , scan ) ;
64
69
}
65
- this . sendWithCost ( 'command result' , [ true , 'area scanned' ] ) ;
70
+ this . sendWithCost ( 'command result' , [ 'map data' , true ] ) ;
66
71
} ,
67
72
68
73
viewInventory : ( ) => {
@@ -71,11 +76,11 @@ class TestClient {
71
76
this . send ( 'inventory data' , inventoryMeta ) ;
72
77
73
78
for ( let slotNum in this . inventory . slots ) {
74
- let slotData = this . inventory . serializeSlot ( slotNum ) ;
79
+ let slotData = this . inventory . serializeSlot ( parseInt ( slotNum ) ) ;
75
80
this . send ( 'slot data' , slotData ) ;
76
81
}
77
82
78
- this . sendWithCost ( 'command result' , [ true , 'all slot data sent' ] ) ;
83
+ this . sendWithCost ( 'command result' , [ 'inventory data' , true ] ) ;
79
84
80
85
} ,
81
86
@@ -89,31 +94,56 @@ class TestClient {
89
94
let slotData = this . inventory . serializeSlot ( this . inventory . selected ) ;
90
95
this . send ( 'slot data' , slotData ) ;
91
96
92
- this . sendWithCost ( 'command result' , [ true , { label : 'test object' , size : 4 } ] ) ;
97
+ this . sendWithCost ( 'command result' , [ 'equip' , { label : 'test object' , size : 4 } ] ) ;
93
98
94
99
} ,
95
100
96
- dig : ( x1 , y1 , z1 , x2 , y2 , z2 , selectionIndex , scanLevel ) => {
101
+ dig : ( x1 , y1 , z1 , x2 , y2 , z2 , relative , scanLevel , selectionIndex , ) => {
102
+ if ( relative ) {
103
+ x1 += this . position . x ;
104
+ y1 += this . position . y ;
105
+ z1 += this . position . z ;
106
+ x2 += this . position . x ;
107
+ y2 += this . position . y ;
108
+ z2 += this . position . z ;
109
+ }
97
110
let points = this . getBoxPoints ( x1 , y1 , z1 , x2 , y2 , z2 ) ;
98
111
for ( let point of points ) {
99
112
this . dig ( point . x , point . y , point . z ) ;
100
113
this . send ( 'dig success' , point ) ;
101
114
}
102
- this . send ( 'delete selection' , selectionIndex ) ;
103
- this . sendWithCost ( 'command result' , [ true , 'digging done' ] ) ;
115
+ if ( selectionIndex !== undefined ) {
116
+ this . send ( 'delete selection' , selectionIndex ) ;
117
+ }
118
+ this . sendWithCost ( 'command result' , [ 'dig' , true ] ) ;
104
119
} ,
105
120
106
- place : ( x1 , y1 , z1 , x2 , y2 , z2 , selectionIndex , scanLevel ) => {
121
+ place : ( x1 , y1 , z1 , x2 , y2 , z2 , relative , scanLevel , selectionIndex , ) => {
122
+ if ( relative ) {
123
+ x1 += this . position . x ;
124
+ y1 += this . position . y ;
125
+ z1 += this . position . z ;
126
+ x2 += this . position . x ;
127
+ y2 += this . position . y ;
128
+ z2 += this . position . z ;
129
+ }
107
130
let points = this . getBoxPoints ( x1 , y1 , z1 , x2 , y2 , z2 ) ;
108
131
for ( let point of points ) {
109
132
let blockData = this . place ( point . x , point . y , point . z ) ;
110
133
this . send ( 'block data' , blockData ) ;
111
134
}
112
- this . send ( 'delete selection' , selectionIndex ) ;
113
- this . sendWithCost ( 'command result' , [ true , 'placing done' ] ) ;
135
+ if ( selectionIndex !== undefined ) {
136
+ this . send ( 'delete selection' , selectionIndex ) ;
137
+ }
138
+ this . sendWithCost ( 'command result' , [ 'place' , true ] ) ;
114
139
} ,
115
140
116
- move : ( x , y , z , scanLevel ) => {
141
+ move : ( x , y , z , relative , scanLevel ) => {
142
+ if ( relative ) {
143
+ x += this . position . x ;
144
+ y += this . position . y ;
145
+ z += this . position . z ;
146
+ }
117
147
let result = this . move ( x , y , z ) ;
118
148
if ( result ) {
119
149
this . commandMap . scanArea ( ) ;
@@ -126,26 +156,36 @@ class TestClient {
126
156
}
127
157
} ,
128
158
129
- interact : ( x , y , z , scanLevel ) => {
159
+ interact : ( x , y , z , relative , scanLevel ) => {
160
+ if ( relative ) {
161
+ x += this . position . x ;
162
+ y += this . position . y ;
163
+ z += this . position . z ;
164
+ }
130
165
let blockData = this . inspect ( x , y , z ) ;
131
166
if ( blockData . name == 'minecraft:chest' ) {
132
167
this . send ( 'inventory data' , this . testData . externalInventory . meta ) ;
133
168
for ( let slotNum in this . inventories [ 3 ] . slots ) {
134
- let slot = this . inventories [ 3 ] . serializeSlot ( slotNum ) ;
169
+ let slot = this . inventories [ 3 ] . serializeSlot ( parseInt ( slotNum ) ) ;
135
170
this . send ( 'slot data' , slot ) ;
136
171
}
137
172
}
138
- this . sendWithCost ( 'command result' , [ true , 'interact complete' ] ) ;
173
+ this . sendWithCost ( 'command result' , [ 'interact' , true ] ) ;
139
174
} ,
140
175
141
- inspect : ( x , y , z , scanLevel ) => {
176
+ inspect : ( x , y , z , relative , scanLevel ) => {
177
+ if ( relative ) {
178
+ x += this . position . x ;
179
+ y += this . position . y ;
180
+ z += this . position . z ;
181
+ }
142
182
let blockData = this . inspect ( x , y , z ) ;
143
183
this . sendWithCost ( 'block data' , blockData ) ;
144
184
} ,
145
185
146
186
select : ( slotNum ) => {
147
187
this . select ( slotNum ) ;
148
- this . sendWithCost ( 'command result' , [ true , 'select complete' ] ) ;
188
+ this . sendWithCost ( 'command result' , [ 'select' , true ] ) ;
149
189
} ,
150
190
151
191
transfer : ( fromSlotIndex , fromSide , toSlotIndex , toSide , amount ) => {
@@ -169,20 +209,20 @@ class TestClient {
169
209
let toSlotData = toInv . serializeSlot ( toSlotIndex ) ;
170
210
this . send ( 'slot data' , toSlotData ) ;
171
211
172
- this . sendWithCost ( 'command result' , [ true , "transfer successful" ] ) ;
212
+ this . sendWithCost ( 'command result' , [ 'transfer' , true ] ) ;
173
213
}
174
214
else {
175
- this . sendWithCost ( 'command result' , [ false , "transfer failed" ] ) ;
215
+ this . sendWithCost ( 'command result' , [ 'transfer' , false ] ) ;
176
216
}
177
217
178
218
} ,
179
219
180
220
craft : ( itemName ) => {
181
- this . sendWithCost ( 'command result' , [ false , 'crafting not implemented' ] ) ;
221
+ this . sendWithCost ( 'command result' , [ 'craft' , 'crafting not implemented' ] ) ;
182
222
} ,
183
223
184
224
raw : ( commandString ) => {
185
- let resultData = [ true , 'received command: ' + commandString ] ;
225
+ let resultData = [ 'raw' , 'received command: ' + commandString ] ;
186
226
this . sendWithCost ( 'command result' , resultData ) ;
187
227
} ,
188
228
@@ -196,6 +236,21 @@ class TestClient {
196
236
this . sendWithCost ( 'available components' , components ) ;
197
237
} ,
198
238
239
+ config : ( optionName , optionValue ) => {
240
+ if ( optionName && ( optionValue !== undefined ) ) {
241
+ this . config [ optionName ] = optionValue ;
242
+ this . sendWithCost ( 'command result' , [ 'config' , true ] ) ;
243
+ }
244
+ else if ( optionName ) {
245
+ let result = { } ;
246
+ result [ optionName ] = this . config [ optionName ] ;
247
+ this . sendWithCost ( 'config' , result ) ;
248
+ }
249
+ else {
250
+ this . sendWithCost ( 'config' , this . config ) ;
251
+ }
252
+ } ,
253
+
199
254
} ;
200
255
201
256
this . socket . on ( 'data' , ( rawMessages ) => {
@@ -458,21 +513,7 @@ class TestClient {
458
513
* @param {any } value
459
514
*/
460
515
validate ( key , value ) {
461
- let keyToValidatorMap = {
462
- 'inventory data' : validators . inventoryMeta ,
463
- 'slot data' : validators . inventorySlot ,
464
- 'command result' : validators . commandResult ,
465
- 'robot position' : validators . position ,
466
- 'available components' : validators . components ,
467
- 'map data' : validators . geolyzerScan ,
468
- 'id' : validators . id ,
469
- 'message' : validators . message ,
470
- 'power level' : validators . powerLevel ,
471
- 'dig success' : validators . digSuccess ,
472
- 'delete selection' : validators . deleteSelection ,
473
- 'block data' : validators . blockData ,
474
- } ;
475
- keyToValidatorMap [ key ] ( value ) ;
516
+ return keyToValidatorMap [ key ] ( value ) ;
476
517
}
477
518
478
519
/**
@@ -482,7 +523,9 @@ class TestClient {
482
523
*/
483
524
send ( key , value ) {
484
525
485
- this . validate ( key , value ) ;
526
+ if ( ! this . validate ( key , value ) ) {
527
+ throw Error ( `command ${ key } failed to validate with value ${ JSON . stringify ( value ) } ` ) ;
528
+ } ;
486
529
487
530
const data = {
488
531
[ key ] : value ,
@@ -520,14 +563,14 @@ class TestClient {
520
563
* so it can be checked in the unit tests.
521
564
*/
522
565
getID ( ) {
523
- return { robot : this . testData . robotName , account : this . testData . accountName } ;
566
+ return { robot : this . testData . config . robotName , account : this . testData . config . accountName } ;
524
567
}
525
568
526
569
/**
527
570
* Used to identify the test client to the server and open the socket connection.
528
571
*/
529
572
connect ( ) {
530
- this . socket . connect ( this . testData . port , this . testData . host ) ;
573
+ this . socket . connect ( this . testData . config . tcpPort , this . testData . config . serverIP ) ;
531
574
}
532
575
533
576
/**
0 commit comments