Skip to content

Commit fe19386

Browse files
author
Joachim Hofer
committed
Removed a bit of copy/paste from constructing the React message
1 parent 1051329 commit fe19386

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

Scalatron/src/scalatron/botwar/Entity.scala

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,44 +133,37 @@ object Bot {
133133
}
134134

135135
val view = state.flattenedBoard.computeView(botPos, Constants.SlaveHorizonHalfSize)
136-
view.occlude() // caution: in situ!
137-
val renderedView = view.cells.map(cell => Player.cellContentToChar(cell, bot) ).mkString
138-
val stateMapString =
139-
stateMap
140-
.filter(_._1 != Protocol.PropertyName.Debug)
141-
.map(entry => entry._1 + "=" + entry._2)
142-
.mkString(",")
143-
val controlFunctionInput =
144-
Protocol.ServerOpcode.React + "(" +
145-
Protocol.PropertyName.Generation + "=" + generation + "," + "" +
146-
Protocol.PropertyName.Name + "=" + name + "," + "" +
147-
Protocol.PropertyName.Time + "=" + state.time + "," + "" +
148-
Protocol.PropertyName.Energy + "=" + bot.energy + "," +
149-
Protocol.ServerOpcode.ParameterName.Master + "=" + deltaToMaster + "," +
150-
Protocol.PropertyName.View + "=" + renderedView +
151-
(if(stateMapString.isEmpty) "" else "," + stateMapString) +
152-
")"
153-
controlFunctionInput
136+
computeControlFunctionInput(view, Some(deltaToMaster))
154137
}
155138

156139
def computeBotInputForMaster(bot: Bot, state: State): String = {
157140
val view = state.flattenedBoard.computeView(bot.pos, Constants.MasterHorizonHalfSize)
141+
computeControlFunctionInput(view)
142+
}
143+
144+
def computeControlFunctionInput(view: FlattenedBoard, maybeDeltaToMaster: Option[XY] = None): String = {
158145
view.occlude() // caution: in situ!
159146
val renderedView = view.cells.map(cell => Player.cellContentToChar(cell, bot) ).mkString
160147
val stateMapString =
161148
stateMap
162149
.filter(_._1 != Protocol.PropertyName.Debug)
163150
.map(entry => entry._1 + "=" + entry._2)
164151
.mkString(",")
152+
val maybeMasterParameter =
153+
maybeDeltaToMaster
154+
.map(Protocol.ServerOpcode.ParameterName.Master + "=" + _ + ",")
155+
.getOrElse("")
165156
val controlFunctionInput =
166157
Protocol.ServerOpcode.React + "(" +
167158
Protocol.PropertyName.Generation + "=" + generation + "," + "" +
168159
Protocol.PropertyName.Name + "=" + name + "," + "" +
169160
Protocol.PropertyName.Time + "=" + state.time + "," +
170161
Protocol.PropertyName.View + "=" + renderedView + "," +
162+
maybeMasterParameter +
171163
Protocol.PropertyName.Energy + "=" + bot.energy +
172164
(if(stateMapString.isEmpty) "" else "," + stateMapString) +
173165
")"
166+
174167
controlFunctionInput
175168
}
176169

0 commit comments

Comments
 (0)