|
| 1 | +package sc.player2021; |
| 2 | + |
| 3 | +import jargs.gnu.CmdLineParser; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | +import sc.player2021.logic.Logic; |
| 7 | +import sc.plugin2021.AbstractClient; |
| 8 | +import sc.plugin2021.IGameHandler; |
| 9 | +import sc.shared.SharedConfiguration; |
| 10 | + |
| 11 | +import java.io.File; |
| 12 | + |
| 13 | +/** |
| 14 | + * Hauptklasse des Clients, die über Konsolenargumente gesteuert werden kann. |
| 15 | + * Sie veranlasst eine Verbindung zum Spielserver. |
| 16 | + */ |
| 17 | +public class Starter extends AbstractClient { |
| 18 | + private static final Logger logger = LoggerFactory.getLogger(Starter.class); |
| 19 | + |
| 20 | + public Starter(String host, int port, String reservation) throws Exception { |
| 21 | + // client starten |
| 22 | + super(host, port); |
| 23 | + |
| 24 | + // Strategie zuweisen |
| 25 | + IGameHandler logic = new Logic(this); |
| 26 | + setHandler(logic); |
| 27 | + |
| 28 | + // einem Spiel beitreten |
| 29 | + if (reservation == null || reservation.isEmpty()) { |
| 30 | + joinAnyGame(); |
| 31 | + } else { |
| 32 | + joinPreparedGame(reservation); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static void main(String[] args) { |
| 37 | + System.setProperty("file.encoding", "UTF-8"); |
| 38 | + |
| 39 | + // parameter definieren |
| 40 | + CmdLineParser parser = new CmdLineParser(); |
| 41 | + CmdLineParser.Option hostOption = parser.addStringOption('h', "host"); |
| 42 | + CmdLineParser.Option portOption = parser.addIntegerOption('p', "port"); |
| 43 | + CmdLineParser.Option reservationOption = parser.addStringOption('r', "reservation"); |
| 44 | + |
| 45 | + try { |
| 46 | + // parameter auslesen |
| 47 | + parser.parse(args); |
| 48 | + } catch (CmdLineParser.OptionException e) { |
| 49 | + // bei Fehler die Hilfe anzeigen |
| 50 | + showHelp(e.getMessage()); |
| 51 | + System.exit(2); |
| 52 | + } |
| 53 | + |
| 54 | + // parameter laden |
| 55 | + String host = (String) parser.getOptionValue(hostOption, "localhost"); |
| 56 | + int port = (Integer) parser.getOptionValue(portOption, SharedConfiguration.DEFAULT_PORT); |
| 57 | + String reservation = (String) parser.getOptionValue(reservationOption, ""); |
| 58 | + |
| 59 | + // einen neuen client erzeugen |
| 60 | + try { |
| 61 | + new Starter(host, port, reservation); |
| 62 | + } catch (Exception e) { |
| 63 | + logger.error("Beim Starten den Clients ist ein Fehler aufgetreten:", e); |
| 64 | + e.printStackTrace(); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + private static void showHelp(String errorMsg) { |
| 70 | + String jarName = new File(Starter.class.getProtectionDomain().getCodeSource().getLocation().getFile()).getName(); |
| 71 | + System.out.println("\n" + errorMsg); |
| 72 | + System.out.println("\nBitte das Programm mit folgenden Parametern (optional) aufrufen: \n" |
| 73 | + + "java -jar " + jarName + " [{-h,--host} hostname]\n" |
| 74 | + + " [{-p,--port} port]\n" |
| 75 | + + " [{-r,--reservation} reservierung]"); |
| 76 | + System.out.println("\nBeispiel: \n" |
| 77 | + + "java -jar " + jarName + " --host 127.0.0.1 --port 10500 --reservation 1234\n"); |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments