Skip to content

Commit

Permalink
refactor: Refactor screen handling, networking & game simulation (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
crykn authored Oct 30, 2023
1 parent c2ae02e commit af23e52
Show file tree
Hide file tree
Showing 243 changed files with 6,257 additions and 8,153 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
submodules: 'true'
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/create-release
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
submodules: 'true'

- name: Set up JDK 11
uses: actions/setup-java@v3
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "pancake"]
path = pancake
url = https://github.com/eskalon/pancake.git
28 changes: 16 additions & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ buildscript {
allprojects {
apply plugin: "eclipse"
apply plugin: "idea"
apply plugin: "java-library"

version = "0.1.0"

ext {
appName = "Projekt GG"
//aiVersion = "1.8.2"
discordIPCVersion = "a8d6631cc9" // = 0.5
gdxVersion = "1.12.0"
gsonVersion = "2.10.1"
guacamoleVersion = "631bdc0ae7" // TODO: remove, when the proper version is pulled in via screenmanager
guacamoleVersion = "9c55fa3f05" // TODO: remove, when the proper version is pulled in via screenmanager
imguiVersion = "1.86.10";
kryonetVersion = "c767618c5d" // = 2.22.9
pancakeVersion = "9706e72262" // = 0.1.2
kryonetVersion = "2.22.9"
lombok = "1.18.30"
//quakemonkeyVersion = "master-SNAPSHOT"
vfxVersion = "0.6.1"

Expand All @@ -42,8 +44,6 @@ allprojects {
}

project(":desktop") {
apply plugin: "java-library"

dependencies {
api project(":game")
implementation "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
Expand All @@ -57,17 +57,18 @@ project(":desktop") {
}

project(":engine") {
apply plugin: "java-library"

dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
api "com.github.eskalon.pancake:core:$pancakeVersion"
api "com.github.eskalon.pancake:desktop:$pancakeVersion"
api "com.github.eskalon.pancake:core"
api "com.github.eskalon.pancake:lwjgl3"
api "com.github.crykn:kryonet:$kryonetVersion"
api "com.google.code.gson:gson:$gsonVersion"
api "com.github.jagrosh:DiscordIPC:$discordIPCVersion"
//api files('../libs/commons-0.1.0.jar')

compileOnly "org.projectlombok:lombok:$lombok"
annotationProcessor "org.projectlombok:lombok:$lombok"

testImplementation "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
}
Expand All @@ -78,12 +79,12 @@ project(":engine") {
}

project(":game") {
apply plugin: "java-library"

dependencies {
api project(":engine")
api "com.github.crykn.gdx-vfx:gdx-vfx-effects:$vfxVersion"
api "com.github.crykn.guacamole:gdx:$guacamoleVersion"
//api ("com.github.crykn.guacamole:gdx:$guacamoleVersion") {
// force = true
//}
//api "com.github.crykn:quakemonkey:$quakemonkeyVersion"

api "io.github.spair:imgui-java-binding:$imguiVersion"
Expand All @@ -93,6 +94,9 @@ project(":game") {
api "io.github.spair:imgui-java-natives-windows:$imguiVersion"
api "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"

compileOnly "org.projectlombok:lombok:$lombok"
annotationProcessor "org.projectlombok:lombok:$lombok"

//testImplementation project(':engine').sourceSets.test.output
testImplementation "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
testImplementation "net.jodah:concurrentunit:$concurrentUnitVersion"
Expand Down
2 changes: 1 addition & 1 deletion desktop/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ targetCompatibility = JavaVersion.VERSION_11

import org.gradle.internal.os.OperatingSystem

project.ext.mainClassName = "de.gg.desktop.DesktopLauncher"
project.ext.mainClassName = "de.eskalon.gg.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../game/assets");

tasks.register('run', JavaExec) {
Expand Down
68 changes: 68 additions & 0 deletions desktop/src/main/java/de/eskalon/gg/desktop/DesktopLauncher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package de.eskalon.gg.desktop;

import com.badlogic.gdx.Files.FileType;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;

import de.damios.guacamole.gdx.StartOnFirstThreadHelper;
import de.eskalon.commons.core.EskalonApplicationStarter;
import de.eskalon.commons.core.StartArguments;
import de.eskalon.commons.core.StartArguments.StartArgumentsBuilder;
import de.eskalon.gg.core.ProjektGGApplication;

/**
* Starts the application for the desktop-based builds.
*/
public class DesktopLauncher {

public static void main(String[] args) {
StartOnFirstThreadHelper.executeOnValidJVM(() -> {
/* Start arguments */
StartArgumentsBuilder startArgs = StartArguments.create();

if (args != null) {
for (int i = 0; i < args.length; i++) {
if (args[i].equalsIgnoreCase("--debug")) {
startArgs.enableDebugLogging();
continue;
}

if (args[i].equalsIgnoreCase("--trace")) {
startArgs.enableTraceLogging();
continue;
}

if (args[i].equalsIgnoreCase("--skip")) {
startArgs.skipSplashScreen();
continue;
}
}
}

/* libGDX config */
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setTitle(ProjektGGApplication.GAME_NAME);
config.setWindowedMode(1280, 720);
config.setResizable(false);
config.useVsync(false);
config.setForegroundFPS(120);
config.setWindowIcon(FileType.Internal, "icon16.png", "icon32.png",
"icon48.png");

/* Start the app */
try {
new Lwjgl3Application(
new EskalonApplicationStarter(
ProjektGGApplication.GAME_NAME,
ProjektGGApplication.class, startArgs.build()),
config);
} catch (Exception e) {
System.err.println(
"An unexpected error occurred while starting the game:");
e.printStackTrace();
System.exit(-1);
}
});
}

}
37 changes: 0 additions & 37 deletions desktop/src/main/java/de/gg/desktop/DesktopLauncher.java

This file was deleted.

40 changes: 30 additions & 10 deletions engine/src/main/java/com/esotericsoftware/minlog/Log.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

package com.esotericsoftware.minlog;

import java.util.concurrent.TimeUnit;

import de.damios.guacamole.gdx.log.Logger;
import de.damios.guacamole.Exceptions;
import de.damios.guacamole.gdx.log.LoggerService;

/**
Expand Down Expand Up @@ -209,20 +207,42 @@ public void log(int level, String category, String message,

switch (level) {
case LEVEL_ERROR:
LOG.error("[" + category + "] " + message, ex);
if (ex == null)
LOG.error("[" + category + "] " + message);
else
LOG.error("[" + category + "] " + message + ": %s",
Exceptions.getStackTraceAsString(ex));
break;
case LEVEL_WARN:
LOG.info("[" + category + "] " + message, ex);
if (ex == null)
LOG.warn("[" + category + "] " + message);
else
LOG.warn("[" + category + "] " + message + ": %s",
Exceptions.getStackTraceAsString(ex));
break;
case LEVEL_INFO:
LOG.debug("[" + category + "] " + message, ex);
if (ex == null)
LOG.info("[" + category + "] " + message);
else
LOG.info("[" + category + "] " + message + ": %s",
Exceptions.getStackTraceAsString(ex));
break;
case LEVEL_DEBUG:
LOG.debug("[" + category + "] " + message, ex);
case LEVEL_DEBUG: // log this to TRACE to avoid spamming the log
if (ex == null)
LOG.trace("[" + category + "] " + message);
else
LOG.trace("[" + category + "] " + message + ": %s",
Exceptions.getStackTraceAsString(ex));
break;
case LEVEL_TRACE:
LOG.debug("[" + category + "] " + message, ex);
break;
//@formatter:off
// if (ex == null)
// LOG.trace("[" + category + "] " + message);
// else
// LOG.trace("[" + category + "] " + message + ": %s",
// Exceptions.getStackTraceAsString(ex));
// break;
//@formatter:on
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.eskalon.commons.net;

import java.util.List;

import com.esotericsoftware.kryonet.Listener.TypeListener;

import de.eskalon.commons.net.packets.data.IPlayerAction;
import de.eskalon.commons.net.packets.data.IReadyable;
import de.eskalon.commons.net.packets.data.PlayerActionsWrapper;
import de.eskalon.commons.net.packets.lockstep.C2SSendPlayerActionsPacket;
import de.eskalon.commons.net.packets.lockstep.S2CActionsDistributionPacket;

public abstract class LockstepGameClient<G, S, P extends IReadyable>
extends ReadyableGameClient<G, S, P> {

public LockstepGameClient() {
super();

TypeListener typeListener = new TypeListener();
typeListener.addTypeHandler(S2CActionsDistributionPacket.class,
(con, msg) -> {
onAllActionsReceived(msg.getTurn(), msg.getActions());
});
client.addListener(typeListener);
}

public void sendActions(int turn, List<IPlayerAction> actions) {
client.sendTCP(new C2SSendPlayerActionsPacket(turn, actions));
}

/* --- METHODS FOR CHILD CLASSES --- */
public abstract void onAllActionsReceived(int turn,
List<PlayerActionsWrapper> list);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package de.eskalon.commons.net;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.badlogic.gdx.utils.IntMap;
import com.esotericsoftware.kryonet.Listener.TypeListener;

import de.damios.guacamole.gdx.log.Logger;
import de.damios.guacamole.gdx.log.LoggerService;
import de.eskalon.commons.net.data.ServerSettings;
import de.eskalon.commons.net.packets.data.IReadyable;
import de.eskalon.commons.net.packets.data.LobbyData;
import de.eskalon.commons.net.packets.data.PlayerActionsWrapper;
import de.eskalon.commons.net.packets.lockstep.C2SSendPlayerActionsPacket;
import de.eskalon.commons.net.packets.lockstep.S2CActionsDistributionPacket;

public abstract class LockstepGameServer<G, S, P extends IReadyable>
extends ReadyableGameServer<G, S, P> {

private static final Logger LOG = LoggerService
.getLogger(LockstepGameServer.class);

IntMap<List<PlayerActionsWrapper>> commandsForTurn = new IntMap<>();

public LockstepGameServer(ServerSettings serverSettings,
LobbyData lobbyData) {
super(serverSettings, lobbyData);

TypeListener typeListener = new TypeListener();
typeListener.addTypeHandler(C2SSendPlayerActionsPacket.class,
(con, msg) -> {
PlayerActionsWrapper actionsWrapper = new PlayerActionsWrapper(
(short) con.getArbitraryData(), msg.getCommands());

List<PlayerActionsWrapper> list = commandsForTurn
.get(msg.getTurn());
if (list == null) {
list = new ArrayList<>();
commandsForTurn.put(msg.getTurn(), list);
}

list.add(actionsWrapper);

if (LoggerService.isTraceEnabled()) {
LOG.trace(
"[SERVER] Received actions of player %d for turn %d: %s",
actionsWrapper.getPlayerId(), msg.getTurn(),
actionsWrapper.getActions().stream().map(
(o) -> o.getClass().getSimpleName())
.collect(Collectors.joining(", ")));
}

if (list.size() == lobbyData.getPlayers().size) {
LOG.trace("[SERVER] All actions received for turn %d",
msg.getTurn());

server.sendToAllTCP(new S2CActionsDistributionPacket(
msg.getTurn(), list));
onAllActionsReceived(msg.getTurn(), list);
commandsForTurn.remove(msg.getTurn());
}
});
server.addListener(typeListener);
}

/* --- METHODS FOR CHILD CLASSES --- */
public abstract void onAllActionsReceived(int turn,
List<PlayerActionsWrapper> list);

}
Loading

0 comments on commit af23e52

Please sign in to comment.