175 lines
5.7 KiB
Java
175 lines
5.7 KiB
Java
package uulm.teamname.marvelous.server;
|
|
|
|
import com.beust.jcommander.JCommander;
|
|
import com.beust.jcommander.ParameterException;
|
|
import org.tinylog.Logger;
|
|
import org.tinylog.configuration.Configuration;
|
|
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
|
|
import uulm.teamname.marvelous.gamelibrary.config.PartyConfig;
|
|
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
|
|
import uulm.teamname.marvelous.gamelibrary.json.JSON;
|
|
import uulm.teamname.marvelous.server.args.ServerArgs;
|
|
import uulm.teamname.marvelous.server.netconnector.MarvelousServer;
|
|
|
|
import java.io.*;
|
|
import java.net.InetSocketAddress;
|
|
import java.net.URISyntaxException;
|
|
import java.net.URL;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* The MainClass of the Server application. The main responsibility of this class is to parse command line arguments,
|
|
* set the log level, load the configuration files, and create and manage the threads that make up the server itself.
|
|
* To run the server edit your IntelliJ run config, and enter
|
|
* {@code -c .\configs\marvelheros.character.json -m .\configs\matchconfig_1.game.json
|
|
* -s .\configs\asgard.scenario.json -v} into the arguments field.
|
|
*/
|
|
public class Server {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
ServerArgs serverArgs = new ServerArgs();
|
|
|
|
try {
|
|
JCommander.newBuilder()
|
|
.addObject(serverArgs)
|
|
.build()
|
|
.parse(args);
|
|
} catch (ParameterException e) {
|
|
Logger.error("Invalid parameters: {}", e.getMessage());
|
|
System.exit(1);
|
|
}
|
|
|
|
System.out.println(serverArgs);
|
|
|
|
if (serverArgs.isVerbose() || serverArgs.isCheckConfig()) {
|
|
// If checkConfig, the LogLevel is also set to max, because more information
|
|
// is exactly what checking the requirements means
|
|
setLogLevel(5);
|
|
} else {
|
|
setLogLevel(serverArgs.getLogLevel());
|
|
}
|
|
|
|
|
|
ScenarioConfig scenarioConfig = readScenarioConfig(serverArgs.getScenarioConfigFile());
|
|
CharacterConfig characterConfig = readCharacterConfig(serverArgs.getCharacterConfigFile());
|
|
PartyConfig partyConfig = readPartyConfig(serverArgs.getMatchConfigFile());
|
|
|
|
// If only configurations should be checked, the server exits here
|
|
if (serverArgs.isCheckConfig()) {
|
|
Logger.info("Exiting as configuration file check is done");
|
|
System.exit(0);
|
|
}
|
|
|
|
InetSocketAddress address = new InetSocketAddress(serverArgs.getPort());
|
|
Logger.trace("Inet address {} created", address);
|
|
|
|
var json = new JSON(characterConfig);
|
|
Logger.trace("New JSON instance created with characterConfig");
|
|
|
|
MarvelousServer netConnector = new MarvelousServer(address, json);
|
|
System.out.println("Starting server");
|
|
netConnector.start();
|
|
System.out.println("Server started");
|
|
|
|
|
|
}
|
|
|
|
/** Function that sets the log level for {@link Logger Tinylog}.
|
|
* It has to be executed <b>BEFORE ANY LOGGING OPERATIONS</b> .
|
|
*/
|
|
private static void setLogLevel(int logLevel) {
|
|
// System.out.println("setting log level to " + logLevel);
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
Configuration.replace(map);
|
|
|
|
String logLevelDescriptor = switch (logLevel) {
|
|
case 0 -> "off";
|
|
case 1 -> "error";
|
|
case 2 -> "warn";
|
|
case 3 -> "info";
|
|
case 4 -> "debug";
|
|
case 5 -> "trace";
|
|
default -> "info";
|
|
};
|
|
|
|
// Add log writer 1, a console writer (which logs to the console)
|
|
map.put("writer1", "console");
|
|
map.put("writer1.level", logLevelDescriptor);
|
|
|
|
|
|
// Add log writer 2, a file writer logging to the file server.log
|
|
map.put("writer2", "file");
|
|
map.put("writer2.level", logLevelDescriptor);
|
|
map.put("writer2.file", "./logs/server.log");
|
|
|
|
Configuration.replace(map);
|
|
|
|
Logger.info("Log level set to '" + logLevelDescriptor + "'");
|
|
}
|
|
|
|
private static ScenarioConfig readScenarioConfig(File source) {
|
|
|
|
if (!source.exists()) {
|
|
Logger.error("Scenario Configuration file not found. Exiting...");
|
|
System.exit(1);
|
|
}
|
|
var config = JSON.parseScenarioConfig(source);
|
|
if (config.isEmpty()) {
|
|
Logger.error("Scenario Configuration couldn't be parsed. Exiting...");
|
|
System.exit(1);
|
|
}
|
|
|
|
// TODO: Check whether Scenario Config is valid
|
|
|
|
Logger.info("Scenario Config file loaded");
|
|
|
|
return config.get();
|
|
}
|
|
|
|
|
|
private static CharacterConfig readCharacterConfig(File source) {
|
|
|
|
if (!source.exists()) {
|
|
Logger.error("Character Configuration file not found. Exiting...");
|
|
System.exit(1);
|
|
}
|
|
var config = JSON.parseCharacterConfig(source);
|
|
if (config.isEmpty()) {
|
|
Logger.error("Character Configuration couldn't be parsed. Exiting...");
|
|
System.exit(1);
|
|
}
|
|
|
|
|
|
// TODO: Check whether Character Config is valid
|
|
|
|
Logger.info("Character Config file loaded");
|
|
|
|
return config.get();
|
|
}
|
|
|
|
|
|
private static PartyConfig readPartyConfig(File source) {
|
|
|
|
if (!source.exists()) {
|
|
Logger.error("Party Configuration file not found. Exiting...");
|
|
System.exit(1);
|
|
}
|
|
var config = JSON.parsePartyConfig(source);
|
|
if (config.isEmpty()) {
|
|
Logger.error("Party Configuration couldn't be parsed. Exiting...");
|
|
System.exit(1);
|
|
}
|
|
|
|
// TODO: Check whether Party Config is valid
|
|
|
|
Logger.info("Party Config file loaded");
|
|
|
|
return config.get();
|
|
}
|
|
}
|