Skip to content

Commit

Permalink
Make it compatible with current GameController
Browse files Browse the repository at this point in the history
... and remove a lot of stuff.
  • Loading branch information
ahasselbring committed Apr 4, 2024
1 parent 4666568 commit 4fbb4bd
Show file tree
Hide file tree
Showing 15 changed files with 27 additions and 902 deletions.
1 change: 0 additions & 1 deletion GameController.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<excludeFolder url="file://$MODULE_DIR$/bin" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/examples" />
<excludeFolder url="file://$MODULE_DIR$/resources/plugins/24" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
9 changes: 5 additions & 4 deletions examples/c/RoboCupGameControlData.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define GAMECONTROLLER_RETURN_PORT 3939

#define GAMECONTROLLER_STRUCT_HEADER "RGme"
#define GAMECONTROLLER_STRUCT_VERSION 15
#define GAMECONTROLLER_STRUCT_VERSION 16

#define MAX_NUM_PLAYERS 20

Expand All @@ -26,8 +26,8 @@
#define COMPETITION_PHASE_ROUNDROBIN 0
#define COMPETITION_PHASE_PLAYOFF 1

#define COMPETITION_TYPE_NORMAL 0
#define COMPETITION_TYPE_DYNAMIC_BALL_HANDLING 1
#define COMPETITION_TYPE_NORMAL 0
#define COMPETITION_TYPE_SHARED_AUTONOMY 1

#define GAME_PHASE_NORMAL 0
#define GAME_PHASE_PENALTYSHOOT 1
Expand Down Expand Up @@ -59,6 +59,7 @@
#define PENALTY_SPL_LOCAL_GAME_STUCK 8
#define PENALTY_SPL_ILLEGAL_POSITION_IN_SET 9
#define PENALTY_SPL_PLAYER_STANCE 10
#define PENALTY_SPL_ILLEGAL_MOTION_IN_INITIAL 11

#define PENALTY_SUBSTITUTE 14
#define PENALTY_MANUAL 15
Expand Down Expand Up @@ -89,7 +90,7 @@ struct RoboCupGameControlData
uint8_t packetNumber; // number incremented with each packet sent (with wraparound)
uint8_t playersPerTeam; // the number of players on a team
uint8_t competitionPhase; // phase of the competition (COMPETITION_PHASE_ROUNDROBIN, COMPETITION_PHASE_PLAYOFF)
uint8_t competitionType; // type of the competition (COMPETITION_TYPE_NORMAL, COMPETITION_TYPE_DYNAMIC_BALL_HANDLING)
uint8_t competitionType; // type of the competition (COMPETITION_TYPE_NORMAL, COMPETITION_TYPE_SHARED_AUTONOMY)
uint8_t gamePhase; // phase of the game (GAME_PHASE_NORMAL, GAME_PHASE_PENALTYSHOOT, etc)
uint8_t state; // state of the game (STATE_READY, STATE_PLAYING, etc)
uint8_t setPlay; // active set play (SET_PLAY_NONE, SET_PLAY_GOAL_KICK, etc)
Expand Down
33 changes: 0 additions & 33 deletions examples/c/VisualRefereeChallenge.h

This file was deleted.

7 changes: 4 additions & 3 deletions examples/python/robocup_game_control_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
GAMECONTROLLER_RETURN_PORT = 3939

GAMECONTROLLER_STRUCT_HEADER = b'RGme'
GAMECONTROLLER_STRUCT_VERSION = 15
GAMECONTROLLER_STRUCT_VERSION = 16

MAX_NUM_PLAYERS = 20

Expand All @@ -52,7 +52,7 @@
COMPETITION_PHASE_PLAYOFF = 1

COMPETITION_TYPE_NORMAL = 0
COMPETITION_TYPE_DYNAMIC_BALL_HANDLING = 1
COMPETITION_TYPE_SHARED_AUTONOMY = 1

GAME_PHASE_NORMAL = 0
GAME_PHASE_PENALTYSHOOT = 1
Expand Down Expand Up @@ -84,6 +84,7 @@
PENALTY_SPL_LOCAL_GAME_STUCK = 8
PENALTY_SPL_ILLEGAL_POSITION_IN_SET = 9
PENALTY_SPL_PLAYER_STANCE = 10
PENALTY_SPL_ILLEGAL_MOTION_IN_INITIAL = 11

PENALTY_SUBSTITUTE = 14
PENALTY_MANUAL = 15
Expand Down Expand Up @@ -111,7 +112,7 @@
'packetNumber' / Byte, # number incremented with each packet sent (with wraparound) # noqa: E501
'playersPerTeam' / Byte, # the number of players on a team
'competitionPhase' / Byte, # phase of the competition (COMPETITION_PHASE_ROUNDROBIN, COMPETITION_PHASE_PLAYOFF) # noqa: E501
'competitionType' / Byte, # type of the competition (COMPETITION_TYPE_NORMAL, COMPETITION_TYPE_DYNAMIC_BALL_HANDLING) # noqa: E501
'competitionType' / Byte, # type of the competition (COMPETITION_TYPE_NORMAL, COMPETITION_TYPE_SHARED_AUTONOMY) # noqa: E501
'gamePhase' / Byte, # phase of the game (GAME_PHASE_NORMAL, GAME_PHASE_PENALTYSHOOT, etc) # noqa: E501
'state' / Byte, # state of the game (STATE_READY, STATE_PLAYING, etc) # noqa: E501
'setPlay' / Byte, # active set play (SET_PLAY_NONE, SET_PLAY_GOAL_KICK, etc) # noqa: E501
Expand Down
204 changes: 1 addition & 203 deletions src/common/Log.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package common;

import data.AdvancedData;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;


/**
* @author Michel Bartsch
*
* This class should be used to log into a log file. A new file will be created
* every time the GameController is started.
* At the end of an actions the Log should be used to add a state into the
* timeline, that is provided by this class too.
*
* This class is a singleton!
*/
Expand All @@ -25,16 +19,10 @@ public class Log
/** The instance of the singleton. */
private static final Log instance = new Log();

/** The file to write into. */
private FileWriter file;
/** The error-file to write into. */
private FileWriter errorFile;
/** The file to write into. */
private final String errorPath = "error.txt";
/** The timeline. */
private final LinkedList<AdvancedData> states = new LinkedList<>();
/** If != null, the next log entry will use this message. */
private String message = null;

/** The format of timestamps. */
public static final SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy.M.dd-kk.mm.ss");
Expand All @@ -44,195 +32,6 @@ public class Log
*/
private Log() {}

/**
* Must be called once at the very beginning to allow Log to work.
*
* @param path The path where the log-file should be created.
*/
public synchronized static void init(String path)
{
if (instance.file != null) {
throw new IllegalStateException("logger already initialized");
}
try{
instance.file = new FileWriter(path);
} catch (IOException e) {
error("cannot write to logfile "+path);
}

// Write version number if application is GameController
try {
toFile((String)Class.forName("controller.GameController").getField("version").get(null));
} catch (ClassNotFoundException
| NoSuchFieldException
| SecurityException
| IllegalArgumentException
| IllegalAccessException ex) {
}
}

/**
* Simply writes a line, beginning with a timestamp, in the file.
* May be used to log something that should not be in the timeline.
*
* @param s The string to be written in the file.
*/
public static void toFile(String s)
{
try{
instance.file.write(timestampFormat.format(new Date(System.currentTimeMillis()))+": "+s+"\n");
instance.file.flush();
} catch (IOException e) {
error("cannot write to logfile!");
}
}

/**
* Specify the message that will be used for the next log entry. It will
* replace the one that is specified during that log entry. This allows
* to replace rather generic messages by more specific ones if an
* action calls another action to perform its task.
* @param message The message that will be used for the next log entry.
*/
public static void setNextMessage(String message)
{
instance.message = message;
}

/**
* Puts a copy of the given data into the timeline, attaching the message
* to it and writing it to the file using toFile method.
* This should be used at the very end of all actions that are meant to be
* in the timeline.
*
* @param data The current data that have just been changed and should
* go into the timeline.
* @param message A message describing what happened to the data.
*/
public static void state(AdvancedData data, String message)
{
AdvancedData state = (AdvancedData) data.clone();
if (!instance.states.isEmpty()) {
state.timeSinceCurrentGameStateBegan = state.getTime() - instance.states.getLast().whenCurrentGameStateBegan;
state.timeSinceCurrentSetPlayBegan = state.getTime() - instance.states.getLast().whenCurrentSetPlayBegan;
}
if (instance.message == null) {
state.message = message;
} else {
state.message = instance.message;
toFile(state.message);
instance.message = null;
}
instance.states.add(state);
toFile(message);
}

/**
* Changes the data used in all actions via the EventHandler to a data from
* the timeline. So this is the undo function.
* If a game state change is undone, the time when it was left is restored.
* Thereby, there whole remaining log is moved into the new timeframe.
*
* @param data The current data. Only used to determine whether
* a change of game state is reverted.
* @param states How far you want to go back, how many states.
*
* @return The message that was attached to the data you went back to.
*/
public static String goBack(AdvancedData data, int states)
{
if (states >= instance.states.size()) {
states = instance.states.size()-1;
}
long timeSinceCurrentGameStateBegan = 0;
long nextTimeSinceCurrentGameStateBegan = 0;
long timeSinceCurrentSetPlayBegan = 0;

boolean gameStateChanged = false;
byte nextGameState = data.gameState;
for (int i=0; i<states; i++) {
final AdvancedData lastState = instance.states.getLast();
if (lastState.gameState != nextGameState) {
gameStateChanged = true;
nextGameState = lastState.gameState;
timeSinceCurrentGameStateBegan = nextTimeSinceCurrentGameStateBegan;
}
if (lastState.setPlay == AdvancedData.SET_PLAY_NONE) {
timeSinceCurrentSetPlayBegan = lastState.timeSinceCurrentSetPlayBegan;
}
nextTimeSinceCurrentGameStateBegan = lastState.timeSinceCurrentGameStateBegan;
instance.states.removeLast();
}
final AdvancedData lastState = instance.states.getLast();
if (lastState.gameState != nextGameState) {
gameStateChanged = true;
timeSinceCurrentGameStateBegan = nextTimeSinceCurrentGameStateBegan;
}
if (gameStateChanged) {
long timeOffset = data.getTime() - timeSinceCurrentGameStateBegan
- lastState.whenCurrentGameStateBegan;
for (AdvancedData state : instance.states) {
state.whenCurrentGameStateBegan += timeOffset;
for (int i = 0; i < state.whenPenalized.length; i++) {
for (int j = 0; j < data.whenPenalized[i].length; j++) {
if (state.whenPenalized[i][j] != 0) {
state.whenPenalized[i][j] += timeOffset;
}
}
}
}
}
if (lastState.setPlay != AdvancedData.SET_PLAY_NONE && timeSinceCurrentSetPlayBegan != 0) {
long timeOffset = data.getTime() - timeSinceCurrentSetPlayBegan
- lastState.whenCurrentSetPlayBegan;
for (AdvancedData state : instance.states) {
state.whenCurrentSetPlayBegan += timeOffset;
}
}
AdvancedData state = (AdvancedData) lastState.clone();

// Do not undo states that have to do with the team message budget.
for (int i=0; i<2; ++i) {
state.team[i].messageBudget = data.team[i].messageBudget;
state.sentIllegalMessages[i] = data.sentIllegalMessages[i];
}

// Write state to EventHandler if application is GameController
try {
final Class<?> eventHandlerClass = Class.forName("controller.EventHandler");
eventHandlerClass.getField("data").set(eventHandlerClass.getMethod("getInstance").invoke(null), state);
} catch (ClassNotFoundException
| NoSuchFieldException
| SecurityException
| IllegalArgumentException
| IllegalAccessException
| NoSuchMethodException
| InvocationTargetException ex) {
}
return state.message;
}

/**
* Gives you the messages attached to the latest data in the timeline.
*
* @param states Of how many datas back you want to have the messages.
*
* @return The messages attached to the data, beginning with the latest.
* The arrays length equals the states parameter.
*/
public static String[] getLast(int states)
{
String[] out = new String[states];
for (int i=0; i<states; i++) {
if (instance.states.size()-1-i >= 0) {
out[i] = instance.states.get(instance.states.size()-1-i).message;
} else {
out[i] = "";
}
}
return out;
}

/**
* Writes a line, beginning with a timestamp, in the error-file and creates
* a new one, if it does not yet exist.
Expand All @@ -244,7 +43,7 @@ public static String[] getLast(int states)
public static void error(String s)
{
System.err.println(s);
try{
try {
if (instance.errorFile == null) {
instance.errorFile = new FileWriter(instance.errorPath);
}
Expand All @@ -264,6 +63,5 @@ public static void close() throws IOException {
if (instance.errorFile != null) {
instance.errorFile.close();
}
instance.file.close();
}
}
Loading

0 comments on commit 4fbb4bd

Please sign in to comment.