Added json config

This commit is contained in:
2026-04-05 19:29:30 -03:00
parent 2293dc29fb
commit 1bc0ae71bf
15 changed files with 74 additions and 43 deletions

View File

@@ -0,0 +1,5 @@
{
"serverIP": "localhost",
"tcpPort": 9090,
"udpPort": 9091
}

View File

@@ -4,11 +4,10 @@ eclipse.project.name = appName + '-core'
dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.github.crykn:kryonet:$kryoNetVersion"
implementation project(':shared')
implementation "com.google.code.gson:gson:$gsonVersion"
if(enableGraalNative == 'true') {
implementation "io.github.berstanio:gdx-svmhelper-annotations:$graalHelperVersion"
implementation(project(":shared"))
}
}

View File

@@ -0,0 +1,7 @@
package io.github.eldek0.config;
public class ClientConfig {
public String serverIP;
public int udpPort;
public int tcpPort;
}

View File

@@ -25,7 +25,7 @@ public class MultiplayerScreen implements Screen {
gameClient = new GameClient();
try {
gameClient.connect("127.0.0.1", "Jugador");
gameClient.connect("Jugador");
} catch (Exception e) {
e.printStackTrace();
}

View File

@@ -3,7 +3,11 @@ package io.github.eldek0.network;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.google.gson.Gson;
import io.github.eldek0.config.ClientConfig;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -13,8 +17,13 @@ public class GameClient {
private final Client client;
private final Map<Integer, NetworkPackets.PlayerState> players = new ConcurrentHashMap<>();
private int myId = -1;
private int udpPort = Network.DEFAULT_UDP_PORT;
private int tcpPort = Network.DEFAULT_TCP_PORT;
private String serverIP;
public GameClient() {
this.loadClientConfig();
client = new Client();
Network.register(client);
@@ -57,9 +66,9 @@ public class GameClient {
});
}
public void connect(String host, String name) throws IOException {
public void connect(String name) throws IOException {
client.start();
client.connect(5000, host, Network.TCP_PORT, Network.UDP_PORT);
client.connect(5000, this.serverIP, this.tcpPort, this.udpPort);
NetworkPackets.Login login = new NetworkPackets.Login();
login.name = name;
@@ -92,4 +101,17 @@ public class GameClient {
public void stop() {
client.stop();
}
private void loadClientConfig(){
ClientConfig config = null;
try {
config = new Gson().fromJson(new FileReader("client-config.json"), ClientConfig.class);
this.udpPort = config.udpPort;
this.tcpPort = config.tcpPort;
this.serverIP = config.serverIP;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -5,8 +5,8 @@ import com.esotericsoftware.kryonet.EndPoint;
import java.util.ArrayList;
public class Network {
public static final int TCP_PORT = 54555;
public static final int UDP_PORT = 54777;
public static final int DEFAULT_TCP_PORT = 54555;
public static final int DEFAULT_UDP_PORT = 54777;
public static void register(EndPoint endPoint) {
Kryo kryo = endPoint.getKryo();

View File

@@ -18,3 +18,4 @@ graalHelperVersion=2.0.1
enableGraalNative=false
gdxVersion=1.14.0
projectVersion=1.0.0
gsonVersion=2.10.1

View File

@@ -13,7 +13,9 @@ repositories {
}
dependencies {
implementation project(':shared')
implementation project(':core')
implementation "com.google.code.gson:gson:$gsonVersion"
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'

View File

@@ -0,0 +1,4 @@
{
"tcpPort": 9090,
"udpPort": 9091
}

View File

@@ -3,9 +3,13 @@ package io.github.eldek0;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.google.gson.Gson;
import io.github.eldek0.config.ServerConfig;
import io.github.eldek0.network.Network;
import io.github.eldek0.network.NetworkPackets;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -14,11 +18,15 @@ public class GameServer {
private final Server server;
private final Map<Integer, NetworkPackets.PlayerState> players = new ConcurrentHashMap<>();
private int udpPort = Network.DEFAULT_UDP_PORT;
private int tcpPort = Network.DEFAULT_TCP_PORT;
public GameServer() {
server = new Server();
Network.register(server);
this.loadConfig();
server.addListener(new Listener() {
@Override
public void connected(Connection connection) {
@@ -50,6 +58,17 @@ public class GameServer {
});
}
private void loadConfig() {
ServerConfig config = null;
try {
config = new Gson().fromJson(new FileReader("server-config.json"), ServerConfig.class);
this.udpPort = config.udpPort;
this.tcpPort = config.tcpPort;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
private void handleLogin(Connection connection, NetworkPackets.Login login) {
NetworkPackets.PlayerState player = new NetworkPackets.PlayerState();
player.id = connection.getID();
@@ -93,7 +112,7 @@ public class GameServer {
public void start() throws IOException {
server.start();
server.bind(Network.TCP_PORT, Network.UDP_PORT);
server.bind(this.tcpPort, this.udpPort);
System.out.println("Servidor iniciado.");
}

View File

@@ -0,0 +1,6 @@
package io.github.eldek0.config;
public class ServerConfig {
public int tcpPort;
public int udpPort;
}

View File

@@ -8,4 +8,3 @@ plugins {
include 'lwjgl3', 'core'
include 'server'
include 'shared'

View File

@@ -1,16 +0,0 @@
plugins {
id 'java-library'
}
group = 'io.github.eldek0'
version = "$projectVersion"
repositories {
mavenCentral()
mavenLocal()
maven { url = 'https://jitpack.io' }
}
dependencies {
api "com.github.crykn:kryonet:$kryoNetVersion"
}

View File

@@ -1,17 +0,0 @@
package io.github.eldek0;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}