Added json config
This commit is contained in:
5
assets/client-config.json
Normal file
5
assets/client-config.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"serverIP": "localhost",
|
||||||
|
"tcpPort": 9090,
|
||||||
|
"udpPort": 9091
|
||||||
|
}
|
||||||
@@ -4,11 +4,10 @@ eclipse.project.name = appName + '-core'
|
|||||||
dependencies {
|
dependencies {
|
||||||
api "com.badlogicgames.gdx:gdx:$gdxVersion"
|
api "com.badlogicgames.gdx:gdx:$gdxVersion"
|
||||||
api "com.github.crykn:kryonet:$kryoNetVersion"
|
api "com.github.crykn:kryonet:$kryoNetVersion"
|
||||||
implementation project(':shared')
|
implementation "com.google.code.gson:gson:$gsonVersion"
|
||||||
|
|
||||||
if(enableGraalNative == 'true') {
|
if(enableGraalNative == 'true') {
|
||||||
implementation "io.github.berstanio:gdx-svmhelper-annotations:$graalHelperVersion"
|
implementation "io.github.berstanio:gdx-svmhelper-annotations:$graalHelperVersion"
|
||||||
implementation(project(":shared"))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package io.github.eldek0.config;
|
||||||
|
|
||||||
|
public class ClientConfig {
|
||||||
|
public String serverIP;
|
||||||
|
public int udpPort;
|
||||||
|
public int tcpPort;
|
||||||
|
}
|
||||||
@@ -25,7 +25,7 @@ public class MultiplayerScreen implements Screen {
|
|||||||
gameClient = new GameClient();
|
gameClient = new GameClient();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
gameClient.connect("127.0.0.1", "Jugador");
|
gameClient.connect("Jugador");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,11 @@ package io.github.eldek0.network;
|
|||||||
import com.esotericsoftware.kryonet.Client;
|
import com.esotericsoftware.kryonet.Client;
|
||||||
import com.esotericsoftware.kryonet.Connection;
|
import com.esotericsoftware.kryonet.Connection;
|
||||||
import com.esotericsoftware.kryonet.Listener;
|
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.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@@ -13,8 +17,13 @@ public class GameClient {
|
|||||||
private final Client client;
|
private final Client client;
|
||||||
private final Map<Integer, NetworkPackets.PlayerState> players = new ConcurrentHashMap<>();
|
private final Map<Integer, NetworkPackets.PlayerState> players = new ConcurrentHashMap<>();
|
||||||
private int myId = -1;
|
private int myId = -1;
|
||||||
|
private int udpPort = Network.DEFAULT_UDP_PORT;
|
||||||
|
private int tcpPort = Network.DEFAULT_TCP_PORT;
|
||||||
|
private String serverIP;
|
||||||
|
|
||||||
public GameClient() {
|
public GameClient() {
|
||||||
|
this.loadClientConfig();
|
||||||
|
|
||||||
client = new Client();
|
client = new Client();
|
||||||
Network.register(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.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();
|
NetworkPackets.Login login = new NetworkPackets.Login();
|
||||||
login.name = name;
|
login.name = name;
|
||||||
@@ -92,4 +101,17 @@ public class GameClient {
|
|||||||
public void stop() {
|
public void stop() {
|
||||||
client.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import com.esotericsoftware.kryonet.EndPoint;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Network {
|
public class Network {
|
||||||
public static final int TCP_PORT = 54555;
|
public static final int DEFAULT_TCP_PORT = 54555;
|
||||||
public static final int UDP_PORT = 54777;
|
public static final int DEFAULT_UDP_PORT = 54777;
|
||||||
|
|
||||||
public static void register(EndPoint endPoint) {
|
public static void register(EndPoint endPoint) {
|
||||||
Kryo kryo = endPoint.getKryo();
|
Kryo kryo = endPoint.getKryo();
|
||||||
@@ -18,3 +18,4 @@ graalHelperVersion=2.0.1
|
|||||||
enableGraalNative=false
|
enableGraalNative=false
|
||||||
gdxVersion=1.14.0
|
gdxVersion=1.14.0
|
||||||
projectVersion=1.0.0
|
projectVersion=1.0.0
|
||||||
|
gsonVersion=2.10.1
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation project(':shared')
|
implementation project(':core')
|
||||||
|
implementation "com.google.code.gson:gson:$gsonVersion"
|
||||||
|
|
||||||
|
|
||||||
testImplementation platform('org.junit:junit-bom:5.10.0')
|
testImplementation platform('org.junit:junit-bom:5.10.0')
|
||||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
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.Connection;
|
||||||
import com.esotericsoftware.kryonet.Listener;
|
import com.esotericsoftware.kryonet.Listener;
|
||||||
import com.esotericsoftware.kryonet.Server;
|
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.Network;
|
||||||
import io.github.eldek0.network.NetworkPackets;
|
import io.github.eldek0.network.NetworkPackets;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@@ -14,11 +18,15 @@ public class GameServer {
|
|||||||
|
|
||||||
private final Server server;
|
private final Server server;
|
||||||
private final Map<Integer, NetworkPackets.PlayerState> players = new ConcurrentHashMap<>();
|
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() {
|
public GameServer() {
|
||||||
server = new Server();
|
server = new Server();
|
||||||
Network.register(server);
|
Network.register(server);
|
||||||
|
|
||||||
|
this.loadConfig();
|
||||||
|
|
||||||
server.addListener(new Listener() {
|
server.addListener(new Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void connected(Connection connection) {
|
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) {
|
private void handleLogin(Connection connection, NetworkPackets.Login login) {
|
||||||
NetworkPackets.PlayerState player = new NetworkPackets.PlayerState();
|
NetworkPackets.PlayerState player = new NetworkPackets.PlayerState();
|
||||||
player.id = connection.getID();
|
player.id = connection.getID();
|
||||||
@@ -93,7 +112,7 @@ public class GameServer {
|
|||||||
|
|
||||||
public void start() throws IOException {
|
public void start() throws IOException {
|
||||||
server.start();
|
server.start();
|
||||||
server.bind(Network.TCP_PORT, Network.UDP_PORT);
|
server.bind(this.tcpPort, this.udpPort);
|
||||||
System.out.println("Servidor iniciado.");
|
System.out.println("Servidor iniciado.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package io.github.eldek0.config;
|
||||||
|
|
||||||
|
public class ServerConfig {
|
||||||
|
public int tcpPort;
|
||||||
|
public int udpPort;
|
||||||
|
}
|
||||||
@@ -8,4 +8,3 @@ plugins {
|
|||||||
include 'lwjgl3', 'core'
|
include 'lwjgl3', 'core'
|
||||||
|
|
||||||
include 'server'
|
include 'server'
|
||||||
include 'shared'
|
|
||||||
@@ -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"
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user