// Wenn Plugin gestartet wird (reload) und Spieler auf den Server waren, // werden Gamer für diese erstellt public void onPluginEnabled() { for (Player p : Main.i.getServer().getOnlinePlayers()) { Gamer g = new Gamer(p); g.loadYAML(); if (getGamer(g.name) == null) gamers.add(g); } }
// Lösche den Gamer für den Spieler, der gerade geleavt ist public void onPlayerQuit(PlayerQuitEvent e) { for (Gamer g : gamers) { if (g.name.equals(e.getPlayer().getName())) { g.saveYAML(); gamers.remove(g); if (g.joinedRoom != null) Main.i.rooms.quitRoom(g.joinedRoom, g); if (g.dummies != null) { for (Gamer d : g.dummies) { d.saveYAML(); gamers.remove(d); if (d.joinedRoom != null) Main.i.rooms.quitRoom(d.joinedRoom, d); } } sendLobbyMessage( ChatColor.YELLOW + Main.i.saves.config.getString("tr.lobby.quit").replaceAll("#name", g.longName)); break; } } Main.i.saves.savePlayers(); }
public void sendLobbyMessage(String msg) { // Check: Chatten ist erlaubt in der Lobby if (!Main.i.saves.allow_chat_in_lobby) return; for (Gamer r : gamers) { if (r.state == 0) { r.sendMessage(msg); } } }
// Method that allows the user to create a new game in the database public void CreateGame( ScreenFrame frame, ServerConnection server, User currentUser, Parsers parser) { try { // Defining variables from typed values in the CreateScreen panel String gamename = frame.getCreate().getTfGameName().getText(); int mapSize = frame.getCreate().getTfMapSize(); String controls = frame.getCreate().getTfControls().getText(); // Checks whether the typed in values are legitimate // Strings controls and gamename can't be empty and the map size can't be 0 if (!controls.equals("") && mapSize != 0 && !gamename.equals("")) { // Creates objects of/instansialize Game and Gamer classes Game game = new Game(); Gamer gamer = new Gamer(); // Sets controls equal to the typed controls gamer.setControls(controls); // Sets id equal to the logged in user (the current user) gamer.setId(currentUser.getId()); // Sets the above defined id as the host of the game game.setHost(gamer); // Sets the gamename and mapsize equal to the typed values game.setName(gamename); game.setMapSize(mapSize); // All of the above setters are used to @post the variables into the database // String json = new Gson().toJson(game); String message = parser.createParser(server.post(json, "games/")); // Checks if the received list of games contains the newly created game // If so a confirmation will be shown to the user if (message.equals(game.getName())) { JOptionPane.showMessageDialog( frame, "Game was created!\nIt's called " + game.getName(), "Success!", JOptionPane.INFORMATION_MESSAGE); } } // Prints a stacktrace when catching an unforeseen error } catch (Exception e) { e.printStackTrace(); } }
public void addNewGamer(SocketThread st, String name) { System.out.println("Do gry " + this.name + " do��czy� si� " + name); synchronized (gamers) { Gamer g = gamers.get(countGamers); g.st = st; g.name = name; // gamers.add(countGamers, g); countGamers++; } if (countGamers == 3) { broadcast(jsonRedy()); return; } broadcast(jsonWaitingForGamer(countGamers + 1)); }
@Override public void actionPerformed(ActionEvent e) { switch (e.getActionCommand()) { case "Cancel": screen.show("menu"); break; case "Start game": Game startGame = null; // 1. if tjekker værdien af hvad der er valgt i comboBoxen og sammenligner med de spil som // for-loopet kører igennem // 2. if gør så hosten ikke kan joine sit eget spil for (Game g : games) { if (g.getName().equals(screen.getFindGamePanel().getSelectedGame())) { if (g.getHost().getId() != currentUser.getId()) startGame = g; } } if (startGame != null) { Gamer opponent = new Gamer(); opponent.setId(currentUser.getId()); opponent.setControls(screen.getFindGamePanel().getDirectionsTextfield()); startGame.setOpponent(opponent); String joinGamemessage = api.joinGame(startGame); String startGamemessage = api.startGame(startGame); System.out.println(startGamemessage); String winnerName = ""; for (User u : users) { try { if (u.getId() == Integer.parseInt(startGamemessage)) { winnerName = u.getUsername(); } } catch (NumberFormatException e1) { e1.printStackTrace(); } } JOptionPane.showMessageDialog( screen, joinGamemessage + ". The winner was:" + winnerName); } else { JOptionPane.showMessageDialog(screen, "You can't join a game where you're the host"); } break; } }
// Befehl: Ändere seriösen Name public boolean onCommandCHGNM( CommandSender sender, Command command, String label, String[] args) { Gamer g = Main.i.mylib.getGamerFromSender(sender); if (g == null) return true; if (g.state != 0) { Main.i.mylib.sendError(g, "ingame"); return false; } g.sendMessage( ChatColor.BLUE.toString() + ChatColor.BOLD + Main.i.saves.config.getString("tr.lobby.change")); g.inputLongName = true; return true; }
// Erstelle den Gamer für einen Spieler, der gerade gejoint ist public void onPlayerJoin(PlayerJoinEvent e) { Gamer g = new Gamer(e.getPlayer()); g.loadYAML(); if (getGamer(g.name) == null) gamers.add(g); sendLobbyMessage( ChatColor.YELLOW + Main.i.saves.config.getString("tr.lobby.join").replaceAll("#name", g.longName)); g.sendWelcomeMessage(); if (Main.i.saves.spawnPoint != null) g.player.teleport(Main.i.saves.spawnPoint); if (!g.player.hasPermission("sh.admin")) { g.player.getInventory().clear(); } g.player.setGameMode(GameMode.ADVENTURE); g.player.setLevel(0); g.player.setExp(0); g.player.removePotionEffect(PotionEffectType.INVISIBILITY); }
// ChatManager! public void onAsyncPlayerChat(AsyncPlayerChatEvent e) { Gamer g = getGamer(e.getPlayer().getName()); if (g == null) return; e.setCancelled(true); // Check Flags if (g.inputLongName) { g.longName = e.getMessage(); g.inputLongName = false; Main.i.mylib.sendInfo(g, "chgnm"); return; } // In der Lobby if (g.state == 0) { sendLobbyMessage("<" + g.longName + "> " + e.getMessage()); } else if (g.state == 1) { if (g.joinedRoom == null) return; g.joinedRoom.sendMessage("<" + g.longName + "> " + e.getMessage(), ChatColor.WHITE); } }
@Override public void actionPerformed(ActionEvent e) { switch (e.getActionCommand()) { case "Cancel": screen.show("menu"); break; case "Create": Game startGame = new Game(); startGame.setName(screen.getStartGamePanel().getGameName()); startGame.setMapSize(25); Gamer opponent = new Gamer(); // For-loop kører brugerne igennem og viser dem i en comboBox // 1. if tjekker værdien af hvad der er valgt i comboBoxen og sammenligner med de brugere // som for-loopet kører igennem for (User u : users) { if (u.getUsername().equals(screen.getStartGamePanel().getSelectedUSer())) { opponent.setId(u.getId()); } } // If tjekker om hosten vil udfordre sig selv og giver fejlmeddelelse og ellers er spillet // oprettet. if (opponent.getId() == currentUser.getId()) { JOptionPane.showMessageDialog( screen, "Error: You need to choose a different opponent than yourself"); } else { Gamer host = new Gamer(); host.setId(currentUser.getId()); host.setControls(screen.getStartGamePanel().getControlsToSnake()); startGame.setHost(host); startGame.setOpponent(opponent); String message = api.createGame(startGame); JOptionPane.showMessageDialog(screen, message); screen.show("menu"); } break; } }
// Method that allows the user to join a game and at the same time starting, determining who's the // winner public void JoinGame( ScreenFrame frame, ServerConnection server, User currentUser, Parsers parser) { // Try/catch for error handling through exceptions try { // Defines the variables gameId for the game and controls for the joining user // GamId is set as type long because the server apparently sends back long type variables long gameId = frame.getJoin().getTfGameId(); String controls = frame.getJoin().getTfControls().getText(); // Checks whether the opponent typed controls are legitimate if (!controls.equals("")) { // Instansierer/create new objects of game and gamer class Game game = new Game(); Gamer gamer = new Gamer(); // Sets the gamer id equal to the current users id gamer.setId(currentUser.getId()); // Sets the typed in controls equal to gamer controls gamer.setControls(controls); // Sets the gameId equal to the typed gameId game.setGameId(gameId); // Sets the gamer to being the opponent game.setOpponent(gamer); String json = new Gson().toJson(game); String message = parser.messageParser(server.put("games/join/", json)); // Checks whether the PUT-request succeeded if (message.equals("Game was joined")) { // Game joined = parser.joinParser(server.put("games/start/", json)); joined = parser.joinParser(server.get("game/" + joined.getGameId() + "/")); // If the SnakeMasterId is equal to the opponent (current user) // the panel shows a message confirming that the opponent won if (joined.getSnakeMasterId() == currentUser.getId()) { JOptionPane.showMessageDialog( frame, "You joined the game and won!", "WINNER!", JOptionPane.INFORMATION_MESSAGE); // If the SnakeMasterId isn't equal to the current users // the panel shows a message confirming that the host won } else if (joined.getSnakeMasterId() != currentUser.getId()) { JOptionPane.showMessageDialog( frame, "You joined the game and lost!\nBuhuu..", "LOSER", JOptionPane.INFORMATION_MESSAGE); } // If the PUT-request message equals "Game closed" // the panel shows error message to the user } else if (message.equals("Game closed")) { JOptionPane.showMessageDialog( frame, "The Game is closed. Check your Game Id again", "NOPE", JOptionPane.ERROR_MESSAGE); } } } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) throws Exception { Gamer gamerA = (Gamer) Class.forName(System.getProperty("a")).newInstance(); Gamer gamerB = (Gamer) Class.forName(System.getProperty("b")).newInstance(); String gamerAName = gamerA.who(); String gamerBName = gamerB.who(); gamerA.init(); gamerB.init(); int aScore = 0; int bScore = 0; int result = 0; for (int i = 0; i < 200; i++) { if (i > 100) { Thread.currentThread().sleep(100); System.out.print("."); if ((i % 20) == 0) System.out.println(); } result = evaluate(gamerA.play(), gamerB.play()); if (result == 1) { if (i >= 100) { aScore++; bScore--; } gamerA.sendResult(Gamer.WIN); gamerB.sendResult(Gamer.LOSE); } else if (result == -1) { if (i >= 100) { aScore--; bScore++; } gamerA.sendResult(Gamer.LOSE); gamerB.sendResult(Gamer.WIN); } else { gamerA.sendResult(Gamer.DRAW); gamerB.sendResult(Gamer.DRAW); } } System.out.printf("\n%s(%d) : %s(%d)", gamerAName, aScore, gamerBName, bScore); }
// Speichere alle Spieler ab, wird gemacht, wenn das Plugin beendet wird public void saveAllGamers() { for (Gamer g : gamers) { g.saveYAML(); } Main.i.saves.savePlayers(); }