@@ -11,7 +11,7 @@ import {
11
11
} from '../actions/statsAction.js' ;
12
12
import {
13
13
getAiScript ,
14
- getSandboxAiScriptList ,
14
+ getSandboxOpponentList ,
15
15
updateAiScript ,
16
16
renameAiScript ,
17
17
setSandboxOpponent ,
@@ -47,16 +47,17 @@ export class SandboxScreen extends React.Component {
47
47
componentDidMount ( ) {
48
48
let id = this . props . match . params . name ;
49
49
this . props . getAiScript ( id , this . props . useRemoteService ) ;
50
- this . props . getSandboxAiScriptList ( this . props . useRemoteService ) ;
50
+ this . props . getSandboxOpponentList ( this . props . useRemoteService ) ;
51
51
this . props . notifySandboxEdit ( ) ;
52
52
this . log ( 'SandboxScreen mounted' ) ;
53
53
}
54
54
55
55
shouldComponentUpdate ( nextProps ) {
56
56
if ( ! this . state . isRunning ) return true ;
57
+
57
58
if (
58
59
this . props . mode != nextProps . mode ||
59
- this . props . opponent . type != nextProps . opponent . type ||
60
+ this . props . opponent . source != nextProps . opponent . source ||
60
61
this . props . opponent . name != nextProps . opponent . name
61
62
) {
62
63
this . updateAiDefList ( nextProps . mode , nextProps . opponent ) ;
@@ -71,13 +72,16 @@ export class SandboxScreen extends React.Component {
71
72
72
73
for ( let i = 0 ; i < count ; i ++ ) {
73
74
let aiDef = JsBattle . createAiDefinition ( ) ;
74
- switch ( opponent . type ) {
75
+ switch ( opponent . source ) {
75
76
case 'bundled' :
76
77
aiDef . fromFile ( opponent . name ) ;
77
78
break ;
78
- case 'user' :
79
+ case 'local_user' :
80
+ case 'remote_user' :
79
81
aiDef . fromCode ( opponent . name , opponent . code ) ;
80
82
break ;
83
+ default :
84
+ throw new Error ( `Unsupported source: ${ opponent . source } ` ) ;
81
85
}
82
86
aiDefList . push ( aiDef ) ;
83
87
}
@@ -99,9 +103,8 @@ export class SandboxScreen extends React.Component {
99
103
this . props . updateAiScript ( this . props . script . id , code , this . props . useRemoteService ) ;
100
104
}
101
105
102
- onOpponentChange ( value ) {
103
- value = value . split ( '/' ) ;
104
- this . props . setSandboxOpponent ( value [ 0 ] , value [ 1 ] ) ;
106
+ onOpponentChange ( opponent ) {
107
+ this . props . setSandboxOpponent ( opponent . source , opponent . id ) ;
105
108
}
106
109
107
110
onBattleFinish ( result ) {
@@ -133,35 +136,20 @@ export class SandboxScreen extends React.Component {
133
136
134
137
renderSettingsTab ( ) {
135
138
let selectedOpponent ;
136
- if ( this . props . opponent . type == 'user' && this . props . opponent . name == this . props . script . scriptName ) {
137
- selectedOpponent = 'bundled/ dummy' ;
139
+ if ( ( this . props . opponent . source == 'local_user' || this . props . opponent . source == 'remote_user' ) && this . props . opponent . name == this . props . script . scriptName ) {
140
+ selectedOpponent = { source : 'bundled' , id : ' dummy'} ;
138
141
} else {
139
- selectedOpponent = this . props . opponent . type + "/" + this . props . opponent . name ;
142
+ selectedOpponent = { source : this . props . opponent . source , id : this . props . opponent . id } ;
140
143
}
141
144
142
- let userTankList = this . props . userTankList
143
- . filter ( ( script ) => script . scriptName != this . props . script . scriptName )
144
- . map ( ( row ) => ( {
145
- id : 'user/' + row . scriptName ,
146
- scriptName : row . scriptName ,
147
- bundled : false
148
- } ) ) ;
149
- let bundledTankList = this . props . bundledTankList
150
- . map ( ( name ) => ( {
151
- id : 'bundled/' + name ,
152
- scriptName : 'Bundled: ' + name ,
153
- bundled : true
154
- } ) ) ;
155
- let opponents = bundledTankList . concat ( userTankList ) ;
156
-
157
145
return < LiveCodeSandboxSettingsTab
158
146
rngSeed = { this . state . rngSeed }
159
147
isRngLocked = { this . props . lockRng }
160
148
mode = { this . props . mode }
161
- opponents = { opponents }
149
+ opponents = { this . props . opponentList }
162
150
selectedOpponent = { selectedOpponent }
163
151
onBattleModeChange = { ( isTeam ) => this . props . setSandboxBattleMode ( isTeam ) }
164
- onOpponentChange = { ( id ) => this . onOpponentChange ( id ) }
152
+ onOpponentChange = { ( opponent ) => this . onOpponentChange ( opponent ) }
165
153
onRngLock = { ( locked ) => this . props . lockSandboxRng ( locked ) }
166
154
/> ;
167
155
}
@@ -246,10 +234,9 @@ SandboxScreen.defaultProps = {
246
234
logging : true ,
247
235
simQuality : 'auto' ,
248
236
simSpeed : 1 ,
249
- userTankList : [ ] ,
250
- bundledTankList : [ ] ,
237
+ opponentList : [ ] ,
251
238
opponent : {
252
- type : 'user ' ,
239
+ source : 'local_user ' ,
253
240
name : "dummy" ,
254
241
code : "importScripts('lib/tank.js'); tank.init(function(settings, info) { }); tank.loop(function(state, control) { });" ,
255
242
} ,
@@ -266,7 +253,7 @@ SandboxScreen.defaultProps = {
266
253
setSandboxBattleMode : ( ) => { } ,
267
254
lockSandboxRng : ( ) => { } ,
268
255
notifySandboxEdit : ( ) => { } ,
269
- getSandboxAiScriptList : ( ) => { } ,
256
+ getSandboxOpponentList : ( ) => { } ,
270
257
} ;
271
258
272
259
SandboxScreen . propTypes = {
@@ -276,8 +263,7 @@ SandboxScreen.propTypes = {
276
263
useRemoteService : PropTypes . bool ,
277
264
logging : PropTypes . bool ,
278
265
simSpeed : PropTypes . number ,
279
- userTankList : PropTypes . array ,
280
- bundledTankList : PropTypes . array ,
266
+ opponentList : PropTypes . array ,
281
267
opponent : PropTypes . object ,
282
268
script : PropTypes . object ,
283
269
mode : PropTypes . oneOf ( [ 'duel' , 'team' ] ) ,
@@ -288,16 +274,15 @@ SandboxScreen.propTypes = {
288
274
setSandboxBattleMode : PropTypes . func ,
289
275
lockSandboxRng : PropTypes . func ,
290
276
notifySandboxEdit : PropTypes . func ,
291
- getSandboxAiScriptList : PropTypes . func ,
277
+ getSandboxOpponentList : PropTypes . func ,
292
278
} ;
293
279
294
280
const mapStateToProps = ( state ) => ( {
295
281
isLoading : state . loading . AI_SCRIPT || state . loading . SANDBOX_AI_SCRIPT_LIST ,
296
282
isRenameLoading : state . loading . AI_SCRIPT_RENAME ,
297
283
simQuality : state . settings . simQuality ,
298
284
simSpeed : state . settings . simSpeed ,
299
- userTankList : state . aiRepo . tankList ,
300
- bundledTankList : state . sandbox . tankList ,
285
+ opponentList : state . sandbox . opponentList ,
301
286
opponent : state . sandbox . opponent ,
302
287
mode : state . sandbox . mode ,
303
288
lockRng : state . sandbox . lockRng ,
@@ -309,8 +294,8 @@ const mapDispatchToProps = (dispatch) => ({
309
294
getAiScript : ( name , useRemoteService ) => {
310
295
dispatch ( getAiScript ( name , useRemoteService ) ) ;
311
296
} ,
312
- getSandboxAiScriptList : ( useRemoteService ) => {
313
- dispatch ( getSandboxAiScriptList ( useRemoteService ) ) ;
297
+ getSandboxOpponentList : ( useRemoteService ) => {
298
+ dispatch ( getSandboxOpponentList ( useRemoteService ) ) ;
314
299
} ,
315
300
updateAiScript : ( id , code , useRemoteService ) => {
316
301
dispatch ( updateAiScript ( id , code , useRemoteService ) ) ;
0 commit comments