Example #1
0
 public FootballManager(Console con) {
   this.con = con;
   cache =
       new MemcachedCache(jdgProperty(JDG_HOST), Integer.parseInt(jdgProperty(MEMCACHED_PORT)));
   List<String> teams = (List<String>) cache.get(teamsKey);
   if (teams == null) {
     teams = new ArrayList<String>();
     Team t = new Team("Lakers");
     t.addPlayer("Kobe Bryant");
     t.addPlayer("Pau Gasol");
     t.addPlayer("Steve Nash");
     cache.put(t.getName(), t);
     teams.add(t.getName());
   }
   cache.put(teamsKey, teams);
 }
Example #2
0
  /**
   * Adds player to the appropriate team
   *
   * @param player The player to add to the team
   */
  public void joinPlayer(Player player) {
    if (getPlayerTeam(player) == null) {
      Team teamToJoin = getTeamToJoin();
      teamToJoin.addPlayer(player);
      teleportPlayer(player);

      SnowballFight.announce(player.getName() + " has joined " + teamToJoin.getTeamName() + "!");
    } else {
      SnowballFight.log(player.getName() + " is already on " + getPlayerTeam(player) + "!");
    }
  }
Example #3
0
 public void addPlayers() {
   String teamName = con.readLine(msgEnterTeamName);
   String playerName = null;
   Team t = (Team) cache.get(encode(teamName));
   if (t != null) {
     while (!(playerName = con.readLine("Enter player's name (to stop adding, type \"q\"): "))
         .equals("q")) {
       t.addPlayer(playerName);
     }
     cache.put(encode(teamName), t);
   } else {
     con.printf(msgTeamMissing, teamName);
   }
 }
Example #4
0
  // an algorithm to sort the players into their teams, there will be <playersPerTeam> in every team
  // , returting false in case of any problem.
  public boolean sortPlayers() {
    // 12 Teams limit
    if (this.teams > 12) return false;
    for (int i = 0; i < this.teams; i++) {
      Team team = new Team(ChatColor.values()[i], teamsNames[i], i);
      for (int q = 0; q < this.playersPerTeam; q++) {
        int tries = 0;
        while (tries < 50) {
          Random random = new Random();
          int x = random.nextInt(players.length);
          Player player = players[x];
          tries++;
          if (alreadyInTeam(player)) break;
          tries = 50;
          team.addPlayer(player);
          log.put(player.getName(), teamsNames[i]);
          player.sendMessage(Strings.TEAM_JOIN_MESSAGE);
        }
      }
    }

    return true;
  }
  public static void main(String[] args) {
    Team baseballTeam = new Team(); // Creates a new Team object
    // The following print statement prints the menu
    System.out.println(
        "A) Add Player "
            + "\n"
            + "G) Get Player"
            + "\n"
            + "L) Get Leader in a stat"
            + "\n"
            + "R) Remove Player"
            + "\n"
            + "P) Print All Players"
            + "\n"
            + "S) Size"
            + "\n"
            + "H) Update Hits"
            + "\n"
            + "E) Update Errors"
            + "\n"
            + "Q) Quit ");
    Scanner scan = new Scanner(System.in);
    String menuOption = ""; // Creates a variable to hold user input
    while (!menuOption.equalsIgnoreCase(
        "Q")) // Loop will continue until user inputs "Q" for their menu option
    {
      System.out.print("\n" + "Select a menu option: "); // Prompts user for menu option
      menuOption = scan.nextLine();

      /**
       * If the selected option is "A", prompt user for name, hits, errors and position. Checks to
       * see if hits and errors are greater than or equal to zero. If condition not met, prompts
       * user for new input. Attempt to create a player using input. Throws an exception when team
       * is full or position is not within valid range.
       */
      if (menuOption.equalsIgnoreCase("A")) {
        System.out.print("\n" + "Enter the player's name: ");
        String name = scan.nextLine();
        System.out.print("Enter the number of hits: ");
        int hits = scan.nextInt();
        while (hits < 0) {
          System.out.print("Please enter a value greater than or equal to 0." + "\n");
          System.out.print("Enter the number of hits: ");
          hits = scan.nextInt();
          scan.nextLine();
        }
        System.out.print("Enter the number of errors: ");
        int errors = scan.nextInt();
        while (errors < 0) {
          System.out.println("Please enter a value greater than or equal to 0." + "\n");
          System.out.print("Enter the number of errors: ");
          errors = scan.nextInt();
          scan.nextLine();
        }
        System.out.print("Enter the position: ");
        int position = scan.nextInt();
        scan.nextLine();
        try {
          baseballTeam.addPlayer(new Player(name, hits, errors), position);
          System.out.println("Player added: " + baseballTeam.getPlayer(position));
        } catch (FullTeamException e) {
          System.out.println(e.getMessage());
        } catch (IllegalArgumentException e) {
          System.out.println(e.getMessage());
        }
      }

      /**
       * Prints the player in the position requested, prompts user for position. Throws exception
       * when position not within valid range.
       */
      if (menuOption.equalsIgnoreCase("G")) {
        System.out.print("Enter the position of the Player requested:");
        int position = scan.nextInt();
        scan.nextLine();
        try {
          System.out.println(baseballTeam.getPlayer(position));
        } catch (IllegalArgumentException e) {
          System.out.println(e.getMessage());
        }
      }

      /**
       * Prints the player who has the best score in a given statistic, prompts user for statistic
       * Throws exception when user requests stat other than "hits" or "errors" Throws exception
       * when there are no players in the Team.
       */
      if (menuOption.equalsIgnoreCase("L")) {
        System.out.print("Enter the stat:");
        String stat = scan.nextLine();
        try {
          if (stat.equalsIgnoreCase("hits"))
            System.out.println("Leader in hits: " + baseballTeam.getLeader(stat).toString());
          else System.out.println("Leader in errors: " + baseballTeam.getLeader(stat).toString());
        } catch (IllegalArgumentException e) {
          System.out.println(e.getMessage());
        } catch (NullPointerException e) {
          System.out.println("There are currently no players in the team.");
        }
      }

      /**
       * Removes a player from a given position. Prompts user for the position. Throws exception
       * when position requested is not in valid range.
       */
      if (menuOption.equalsIgnoreCase("R")) {
        System.out.print("Enter the position: ");
        int position = scan.nextInt();
        scan.nextLine();
        try {
          if (baseballTeam.getPlayer(position) == null)
            System.out.println("No player exists at that position");
          else {
            baseballTeam.removePlayer(position);
            System.out.println("Player removed at position " + position);
          }
        } catch (IllegalArgumentException e) {
          System.out.println(e.getMessage());
        }
      }

      /** Prints all the players in the team and their stats in a nice format. */
      if (menuOption.equalsIgnoreCase("P")) {
        System.out.print("\n");
        baseballTeam.PrintAllPlayers();
      }

      /** Prints the number of players currently in the team. */
      if (menuOption.equalsIgnoreCase("S")) {
        System.out.println("There are " + baseballTeam.size() + " player(s) in the current Team");
      }

      /**
       * Updates the number of hits of a player. Prompts user for player's name. Throws exception
       * when player nonexistent.
       */
      if (menuOption.equalsIgnoreCase("H")) {
        System.out.print("\n" + "Enter the player to update: ");
        String name = scan.nextLine();
        System.out.print("Enter the new number of hits:");
        int hits = scan.nextInt();
        scan.nextLine();
        while (hits < 0) {
          System.out.print("Please enter a value greater than or equal to 0");
          System.out.print("\n" + "Enter the new number of hits:");
          hits = scan.nextInt();
          scan.nextLine();
        }
        try {
          baseballTeam.getPlayerByName(name).setHits(hits);
          System.out.println("Updated " + name + " hits");
        } catch (NullPointerException e) {
          System.out.println("The player is not currently in the Team");
        }
      }

      /**
       * Updates the number of errors of a player. Prompts user for player's name. Throws exception
       * when player nonexistent.
       */
      if (menuOption.equalsIgnoreCase("E")) {
        System.out.print("\n" + "Enter the player to update: ");
        String name = scan.nextLine();
        System.out.print("Enter the new number of errors:");
        int errors = scan.nextInt();
        scan.nextLine();
        while (errors < 0) {
          System.out.print("Please enter a value greater than or equal to 0");
          System.out.print("\n" + "Enter the new number of errors:");
          errors = scan.nextInt();
          scan.nextLine();
        }

        try {
          baseballTeam.getPlayerByName(name).setErrors(errors);
          System.out.println("Updated " + name + " errors");
        } catch (NullPointerException e) {
          System.out.println("The player is not currently in the Team");
        }
      }
    }
    scan.close();
    System.out.println("Program terminated.");
  }
Example #6
0
    public ModifyTeamScreen(Team teamPassed, Screen callerScreen) {

      Eve.setLayout(new MigLayout("flowy", "[grow, fill]", "[][grow, fill]"));

      cancel.addActionListener(
          (ActionEvent e) -> {
            caller.Return();
          });

      save.addActionListener(
          (ActionEvent e) -> {
            team.name = name.getText();
            if (team.globalIndex == -1) {
              team.saveTeam(true);
            } else {
              team.saveTeam(false);
            }
            callerScreen.refresh();
            caller.Return();
          });
      addPlayers.setMargin(new Insets(0, 0, 0, 0));
      addPlayers.addActionListener(
          (ActionEvent e) -> {
            if (team.getPlayers().length < 5) {
              JButton cancelB = new JButton("Cancel");
              cancelB.addActionListener(
                  (ActionEvent f) -> {
                    inner.removeAll();
                    popup.dispose();
                  });
              inner.add(cancelB);
              JButton newPlayer = new JButton("New Player");
              newPlayer.addActionListener(
                  (ActionEvent f) -> {
                    inner.removeAll();
                    popup.dispose();
                    ModifyPlayerPopup addNewPlayer = new ModifyPlayerPopup(null, this, team);
                  });
              inner.add(newPlayer);
              inner.add(new JSeparator(), "growx");
              for (Player player : Global.Players) {
                boolean playerInCurrentTeam = false;
                for (Player teamPlayer : team.getPlayers()) {
                  if (teamPlayer.globalIndex == player.globalIndex) {
                    playerInCurrentTeam = true;
                    break;
                  }
                }
                if (!playerInCurrentTeam) {
                  JPanel oldPlayer = player.playerPreview();
                  JButton choose = new JButton("Add!");
                  choose.addActionListener(
                      (ActionEvent f) -> {
                        team.addPlayer(player);
                        inner.removeAll();
                        popup.dispose();
                        refresh();
                      });
                  oldPlayer.add(choose, "east");
                  oldPlayer.setBorder(BorderFactory.createLineBorder(Color.black));
                  inner.add(oldPlayer);
                }
              }
              innerScroll.getVerticalScrollBar().setUnitIncrement(16);
              innerScroll.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
              popup.add(innerScroll);
              popup.setSize(350, 400);
              popup.setLocation(MouseInfo.getPointerInfo().getLocation());
              popup.setVisible(true);
            } else {
              JOptionPane.showMessageDialog(
                  null,
                  "You cannot add more than 5 players per team.\nPlease remove one before adding another.");
            }
          });

      caller = callerScreen;

      drawScreen(teamPassed);
    }
Example #7
0
    public ModifyPlayerPopup(Player playerPassed, Screen callerScreen, Team team) {

      cancel.addActionListener(
          (ActionEvent e) -> {
            Cain.dispose();
          });

      save.addActionListener(
          (ActionEvent e) -> {
            player.name = name.getText();

            if (player.globalIndex == -1) {
              player.savePlayer();
            } else {
              Global.Players.set(player.globalIndex, player);
              player.writePlayer();
            }
            if (team != null) {
              team.addPlayer(player);
            }

            callerScreen.refresh();
            Cain.dispose();
          });

      if (playerPassed != null) {
        // We're editing a current player
        player = playerPassed;
        for (Hero hero : player.getPlayList()) {
          JLabel heroLabel = new JLabel(hero.portraitSmall);
          heroLabel.addMouseListener(
              new MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                  player.removeHero(hero);
                  refresh();
                }
              });
          heroes.add(heroLabel);
        }
        name.setText(player.name);
      } else {
        // This is a new Player
        Cain.setTitle("Add a player");
        player = new Player("New Player");
      }
      addHeroes.setMargin(new Insets(0, 0, 0, 0));
      addHeroes.addActionListener(
          (ActionEvent e) -> {
            pool = new PoolBuilder(false, "small", new Returner(), player.getPlayList());
            back = new JButton("Cancel");
            back.addActionListener(
                (ActionEvent p) -> {
                  inner.removeAll();
                  popup.dispose();
                });
            JLabel note = new JLabel("Right click to add multiple heroes.");
            inner.add(back, "wrap");
            inner.add(note, "wrap");
            inner.add(pool.pool, "growx");
            popup.add(inner);
            popup.setSize(700, 500);
            popup.setLocation(MouseInfo.getPointerInfo().getLocation());
            popup.setVisible(true);
          });

      name.setFont(new Font("Ariel", Font.PLAIN, 20));
      name.select(0, name.getText().length());

      heroes.setBorder(BorderFactory.createLineBorder(Color.black));

      south.add(addHeroes);
      south.add(new JLabel("Heroes"));
      south.add(heroes, "span, grow");
      south.add(cancel, "span, right, split 2");
      south.add(save, "span, right");

      Enoch.add(name);
      Enoch.add(south);

      Cain.add(Enoch);
      Cain.setMinimumSize(new Dimension(400, 350));
      Cain.setVisible(true);
    }