Added json config
This commit is contained in:
@@ -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'
|
||||
|
||||
4
server/server-config.json
Normal file
4
server/server-config.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"tcpPort": 9090,
|
||||
"udpPort": 9091
|
||||
}
|
||||
@@ -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.");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package io.github.eldek0.config;
|
||||
|
||||
public class ServerConfig {
|
||||
public int tcpPort;
|
||||
public int udpPort;
|
||||
}
|
||||
Reference in New Issue
Block a user