Action won't execute in Java

When I run the engine and my code, initially everything starts up fine. But when it comes to executing actions the graphical window and the console shows no change in the players position.

Here is my code:


import Enums.InputCommand;
import Models.*;
import Models.Dtos.*;
import Services.*;
import com.microsoft.signalr.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        Logger logger = LoggerFactory.getLogger(Main.class);
        BotService botService = new BotService();

        String environmentIp = System.getenv("RUNNER_IPV4");
        String environmentNickname = System.getenv("BOT_NICKNAME");
        String token = "410d392c-ecf5-43b9-a228-299c0a8d224a";
        token = (token != null) ? token : System.getenv("REGISTRATION_TOKEN");

        String ip = (environmentIp != null && !environmentIp.isBlank()) ? environmentIp : "localhost";
        ip = ip.startsWith("http://") ? ip : "http://" + ip;

        String url = ip + ":" + "5000" + "/runnerhub";
        // create the connection
        HubConnection hubConnection = HubConnectionBuilder.create(url)
                .build();

        String nickname = environmentNickname != null ? environmentNickname : "JavaBot";

        hubConnection.on("Disconnect", (reason) -> {
            logger.info("Disconnected: {}",
                    reason);
            botService.setShouldQuit(true);
            hubConnection.stop();
        }, UUID.class);

        hubConnection.on("Registered", (id) -> {
            System.out.println("Registered with the runner, bot ID is: " + id);
            botService.setBotId(id);
        }, UUID.class);

        hubConnection.on("ReceiveBotState", (botStateDto) -> {
            botService.setReceivedBotState(true);
            botService.setBotState(botStateDto);
        }, BotStateDto.class);


        hubConnection.start().blockingAwait();

        Thread.sleep(1000);
        System.out.println("Registering with the runner...");
        hubConnection.send("Register", token, nickname);

        hubConnection.on("ReceiveGameComplete", (state) -> {
            System.out.println("Game complete");
            botService.setShouldQuit(true);
        }, String.class);

        GraphicalMap graphicalMap = new GraphicalMap();
        graphicalMap.showMap();

        // This is a blocking call
//        hubConnection.start().subscribe(()-> {
//
//
//        });

        while (!botService.getShouldQuit()) {
            Thread.sleep(20);

            if (botService.getReceivedBotState()) {
                //System.out.println(botService.getBotState().toString());
                int action = graphicalMap.updateMap(botService
                        .getBotState().getHeroWindow());

                int playerX = Math.floorDiv(graphicalMap.COLS, 2);
                int playerY = Math.floorDiv(graphicalMap.ROWS, 2);
                //graphicalMap.setPlayerLocation(playerX, playerY);

                System.out.println(playerX);

                if (!graphicalMap.isShowing){
                    graphicalMap.showMap();
                }

                BotCommand botCommand = botService.computeNextPlayerAction();

                hubConnection.send("SendPlayerCommand", new BotCommand(
                        botService.getBotId(), InputCommand.valueOf(new Random().nextInt(9) + 1).get()
                ));
                System.out.println(
                        new BotCommand(
                                botService.getBotId(), InputCommand.valueOf(new Random().nextInt(9) + 1).get()
                        ).toString()
                );
                botService.setReceivedBotState(false);
            }
        }

        hubConnection.stop();
    }

    public static String convertToString(int[][] array) {
        String map = "";

        for (int[] row : array) {
            for (int k : row){
                map = map + k;
            }
        }

        return map;
    }
}

I even unzipped a new engine and used the start bot code. Still the same issue. I got the output as follows:

Registering with the runner...
Registered with the runner, bot ID is: 410d392c-ecf5-43b9-a228-299c0a8d224a
Position: (23, 20), Level: 0, Collected: 1
1111100111111111111000000050000000
1111111111111111111000000050000000
1111111111111111111000000050000000
1111111111111111111000000050000000
1111111111111111111000000050000000
1111111110000000000000000050000000
1111111110000000000000000050200020
1111111110000000000000000050000000
1111111100000000000000000055444434
1111111100000000000000000055000000
1111111100000000000002000255000000
1111111110000000000000000055000000
1111111100000000444444444455000000
1111111100000000000000000000000000
1111111110000000000000000000000000
1111111110000000000000000000000000
1111111110000000000000000000000000
1111111110000000000000000000000000
1111111110000000000000000000000000
1111111110000000000000000000000000
1111111111000000000000000000000000
1111111111100001111111111111111111

Position: (23, 20), Level: 0, Collected: 1
1111100111111111111000000050000000
1111111111111111111000000050000000
1111111111111111111000000050000000
1111111111111111111000000050000000
1111111111111111111000000050000000
1111111110000000000000000050000000
1111111110000000000000000050200020
1111111110000000000000000050000000
1111111100000000000000000055444434
1111111100000000000000000055000000
1111111100000000000002000255000000
1111111110000000000000000055000000
1111111100000000444444444455000000
1111111100000000000000000000000000
1111111110000000000000000000000000
1111111110000000000000000000000000
1111111110000000000000000000000000
1111111110000000000000000000000000
1111111110000000000000000000000000
1111111110000000000000000000000000
1111111111000000000000000000000000
1111111111100001111111111111111111

Hi, have you tried to debug your code to see if you are actually receiving a gameState? and if your bot is actually sending a next action? Im not too familiar with java but your console print in the while loop will just print random values and not the actual command sent to the engine.

And it would probably be better to do a while(hubConnection != HubConnectionState.DISCONNECTED){}

that way when the game engine is terminated early you wont receive a gameComplete event and your bot would end up running until forcefully killed

Sorry, I actually gave the output of another program. The code given is my code. The output is from the starter bot. I definitely am receiving a game state considering that my visual map works.

The problem is nothing is happening whether I specify a random action or a constant.

have you tried just sending a random command from your ReceiveBotState event

hubConnection.on("ReceiveBotState", (botStateDto) -> {
            botService.setReceivedBotState(true);
            botService.setBotState(botStateDto);
hubConnection.send("SendPlayerCommand", new BotCommand(
                        botService.getBotId(), InputCommand.valueOf(new Random().nextInt(9) + 1).get()
                ));
        }, BotStateDto.class);

also how does your BotStateDto look, I know the RadarData was updated to be an int[][] instead of an string

Unfortunately, it didn’t work. I’ll just switch back to python.

Hey, in your BotCommand.java class, change from

private UUID botId;
private InputCommand action;

to

public UUID BotId;
public int Action;

and also make sure you change all occurences of botId to BotId and action to Action within that class. You might have to modify the code a bit since we are changing the action type from InputCommand to int.
(There are also other ways around where you can cast the action to an int instead when sending to the gamerunner but am just letting you know how I built my Java bot and what worked for me since initially, the Hubconnection was not quite compatible with how I was sending in Java and how the C# gamerunner was receiving)
You should be good to go after that.

1 Like

Yeah this is correct - the starter bot does not correctly convert the enum to an int needed for the actual command.