Skip to content

Commit

Permalink
Switching over to SocketServer.
Browse files Browse the repository at this point in the history
  • Loading branch information
tarehart committed Jun 12, 2019
1 parent 871f067 commit 2128c32
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 277 deletions.
4 changes: 1 addition & 3 deletions README_Tournament.md
Expand Up @@ -16,6 +16,4 @@ Advanced:

- It's fine to close and restart `ReliefBot.bat` while runner.py is active.
- You can also run the `.bat` on the command line to see stack traces for debugging purposes.
- If there is a port conflict, you can create a file called `reliefbot-port.txt`
in the bin folder with a single number in it to override the port used.
You must also modify the port.cfg located in this directory.
- If there is a port conflict, you can modify relief_bot.py's get_port method.
85 changes: 0 additions & 85 deletions src/main/java/rlbot/manager/PyAdapterManager.kt

This file was deleted.

47 changes: 3 additions & 44 deletions src/main/java/tarehart/rlbot/PyInterface.kt
@@ -1,18 +1,16 @@
package tarehart.rlbot

import rlbot.Bot
import rlbot.cppinterop.RLBotDll
import rlbot.manager.BotManager
import rlbot.manager.PyAdapterManager
import rlbot.pyinterop.DefaultPythonInterface
import rlbot.pyinterop.SocketServer
import tarehart.rlbot.bots.*
import tarehart.rlbot.ui.StatusSummary
import java.io.IOException

/**
* The public methods of this class will be called directly from the python component of the RLBot framework.
*/
class PyInterface(private val botManager: BotManager, private val statusSummary: StatusSummary) : DefaultPythonInterface(botManager) {
class PyInterface(port: Int, botManager: BotManager, private val statusSummary: StatusSummary) :
SocketServer(port, botManager) {

override fun initBot(index: Int, botType: String, team: Int): Bot {
val newBot: tarehart.rlbot.bots.BaseBot
Expand Down Expand Up @@ -41,43 +39,4 @@ class PyInterface(private val botManager: BotManager, private val statusSummary:

statusSummary.removeBot(index)
}

override fun shutdown() {
super.shutdown()
pyAdapterManager.shutdown()
}

fun ensureDllInitialized(interfaceDllPath: String) {
try {
RLBotDll.initialize(interfaceDllPath)
} catch (e: IOException) {
e.printStackTrace()
}
}


val pyAdapterManager = PyAdapterManager()

fun registerPyAdapter(index: Int, name: String, team: Int) {
pyAdapterManager.registerPyAdapter(index) { idx -> initBot(idx, name, team) }
}

fun getOutput(index: Int, secondsElapsed: Float): Array<Float> {
val floats = Array(10) { _ -> 0F }

val output = pyAdapterManager.getOutput(index, secondsElapsed)

output?.let{
floats[0] = it.throttle.toFloat()
floats[1] = it.steer.toFloat()
floats[2] = it.pitch.toFloat()
floats[3] = it.yaw.toFloat()
floats[4] = it.roll.toFloat()
floats[5] = if (it.jumpDepressed) 1F else 0F
floats[6] = if (it.boostDepressed) 1F else 0F
floats[7] = if (it.slideDepressed) 1F else 0F
}

return floats
}
}
29 changes: 12 additions & 17 deletions src/main/java/tarehart/rlbot/ReliefBotMain.kt
Expand Up @@ -31,30 +31,25 @@ fun main(args: Array<String>) {
// as you, and the match can't start because of the conflict. Because of this line, you can ask the
// organizer make a file called "port.txt" in the same directory as your .jar, and put some other number in it.
// This matches code in JavaAgent.py
val port = readPortFromFile().orElse(DEFAULT_PORT)
val port = readPortFromArgs(args) ?: DEFAULT_PORT
val botManager = BotManager()
val pythonInterface = PyInterface(botManager, statusSummary)
val pythonServer = PythonServer(pythonInterface, port)
pythonServer.start()

println(String.format("Python server started on port %s. Listening for RLBot commands!", port))

val pythonInterface = PyInterface(port, botManager, statusSummary)
Thread {pythonInterface.start()}.start()
showStatusSummary(port)
}


fun readPortFromFile(): Optional<Int> {
try {
val lines = Files.lines(Paths.get("reliefbot-port.txt"))
val firstLine = lines.findFirst()
return firstLine.map{ Integer.parseInt(it) }
fun readPortFromArgs(args: Array<String>): Int? {

if (args.isEmpty()) {
return null
}
return try {
Integer.parseInt(args[0])
} catch (e: NumberFormatException) {
println("Failed to parse port file! Will proceed with hard-coded port number.")
return Optional.empty()
} catch (e: Throwable) {
return Optional.empty()
println("Failed to get port from arguments! Will use default.")
null
}

}

private fun showStatusSummary(port: Int) {
Expand Down
Binary file removed src/main/python/port.cfg
Binary file not shown.
17 changes: 7 additions & 10 deletions src/main/python/relief_bot.py
@@ -1,22 +1,19 @@
import os

from rlbot.agents.base_agent import BOT_CONFIG_AGENT_HEADER
from rlbot.agents.base_java_agent import BaseJavaAgent
from rlbot.agents.executable_with_socket_agent import ExecutableWithSocketAgent
from rlbot.parsing.custom_config import ConfigHeader, ConfigObject


class ReliefBot(BaseJavaAgent):
class ReliefBot(ExecutableWithSocketAgent):

def get_port(self) -> int:
return 22868

def __init__(self, name, team, index):
super().__init__(name, team, index)

def get_port_file_path(self):
# Look for a port.cfg file in the same directory as THIS python file.
return os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__), 'port.cfg'))

def load_config(self, config_header: ConfigHeader):
self.java_executable_path = config_header.getpath('java_executable_path')
self.logger.info("Java executable is configured as {}".format(self.java_executable_path))
self.executable_path = config_header.getpath('java_executable_path')
self.logger.info("Java executable is configured as {}".format(self.executable_path))

@staticmethod
def create_agent_configurations(config: ConfigObject):
Expand Down
12 changes: 0 additions & 12 deletions src/main/python/relief_bot_py_adapter.cfg

This file was deleted.

98 changes: 0 additions & 98 deletions src/main/python/relief_bot_py_adapter.py

This file was deleted.

Expand Up @@ -3,13 +3,13 @@ package tarehart.rlbot.integration
import rlbot.Bot
import rlbot.cppinterop.RLBotDll
import rlbot.manager.BotManager
import rlbot.pyinterop.DefaultPythonInterface
import rlbot.pyinterop.SocketServer
import java.io.IOException

/**
* The public methods of this class will be called directly from the python component of the RLBot framework.
*/
class IntegrationPyInterface(botManager: BotManager) : DefaultPythonInterface(botManager) {
class IntegrationPyInterface(port: Int, botManager: BotManager) : SocketServer(port, botManager) {

override fun initBot(index: Int, botType: String, team: Int): Bot {
throw NotImplementedError()
Expand Down
Expand Up @@ -6,7 +6,7 @@ import rlbot.manager.BotManager
import rlbot.pyinterop.PythonServer
import tarehart.rlbot.DEFAULT_PORT
import tarehart.rlbot.integration.asserts.AssertStatus
import tarehart.rlbot.readPortFromFile
import tarehart.rlbot.readPortFromArgs

abstract class StateSettingAbstractTest {

Expand All @@ -29,10 +29,9 @@ abstract class StateSettingAbstractTest {
@JvmStatic
fun startListening() {
if (!isListening) {
val port = readPortFromFile().orElse(DEFAULT_PORT)
val pythonInterface = IntegrationPyInterface(BotManager())
val pythonServer = PythonServer(pythonInterface, port)
pythonServer.start()
val port = DEFAULT_PORT
val pythonInterface = IntegrationPyInterface(port, BotManager())
Thread {pythonInterface.start()}.start()
isListening = true
}
}
Expand Down

0 comments on commit 2128c32

Please sign in to comment.