/** * Gets the stats (touchdowns scored/received and casualties) from a game and write it to this * user. * * @param game The game to get the stats from. */ private void getStatsFromGame(GameController game) { try { int teamIndex = game.getTeam(0).getCoach().getName().equals(getName()) ? 0 : 1; // set the index of the team where this user was coach setTouchdownsScoredWithoutWrite( getTouchdownsScored() + game.getScoreFromTeam( teamIndex)); // add the score of the team where this user was coach. setTouchdownsReceivedWithoutWrite( getTouchdownsReceived() + game.getScoreFromTeam( teamIndex == 0 ? 1 : 0)); // add the score of the team where this user was NOT coach. for (Player casualty : game.getCasualties()) if (casualty.getTeam().getCoach().getName().equals(getName())) setCasualties(getCasualties() + 1); } catch (NullPointerException e) { // the teams have not been set up yet manager .getParent() .log(Level.FINEST, "Match has not yet started. No touchdowns have happened."); } finally { writeUser(); } }
/** * Parses the parameters of a user from a string. * * @param p The string to parse from. */ public void stringToUser(String p) { if (p.startsWith("[") && p.endsWith("]")) { // p starts and ends correctly // strip [ and ] from string if there p = p.substring(1, p.length() - 1); // return if string is empty after stripping if (p.equals("")) return; // prepare position counter int c = 0; try { if (p.charAt(c) != '"') { manager .getParent() .log( Level.SEVERE, "Malformed user string. Ignoring name, password hash, op status, wins, losses, casualties and last online."); return; } c++; // skip first quote // get name name = getNextSubparameter(p, c); c += 4 + name.length(); // skip closing quote, comma and space if (p.charAt(c - 1) != '"') { manager .getParent() .log( Level.SEVERE, "Malformed user string. Ignoring password hash, op status, wins, losses, casualties, touchdowns received, touchdowns scored and last online."); return; } // get password hash passwordEncrypted = getNextSubparameter(p, c); c += 4 + passwordEncrypted.length(); // skip closing quote, comma and space if (p.charAt(c - 1) != '"') { manager .getParent() .log( Level.SEVERE, "Malformed user string. Ignoring op status, wins, losses, casualties, touchdowns received, touchdowns scored and last online."); return; } // get op status if (getNextSubparameter(p, c).toLowerCase().equals("true") || getNextSubparameter(p, c).toLowerCase().equals("yes") || getNextSubparameter(p, c).toLowerCase().equals("oui")) isOp = true; c += 5 + getNextSubparameter(p, c) .length(); // skip closing quote, comma, space and opening bracket if (p.charAt(c - 1) != '"') { manager .getParent() .log( Level.SEVERE, "Malformed user string. Ignoring wins, losses, casualties, touchdowns received, touchdowns scored and last online."); return; } // get wins wins = Integer.parseInt(getNextSubparameter(p, c)); c += 4 + getNextSubparameter(p, c).length(); // skip closing quote, comma and space if (p.charAt(c - 1) != '"') { manager .getParent() .log( Level.SEVERE, "Malformed user string. Ignoring losses, casualties, touchdowns received, touchdowns scored and last online."); return; } // get losses losses = Integer.parseInt(getNextSubparameter(p, c)); c += 4 + getNextSubparameter(p, c).length(); // skip closing quote, comma and space if (p.charAt(c - 1) != '"') { manager .getParent() .log( Level.SEVERE, "Malformed user string. Ignoring casualties, touchdowns received, touchdowns scored and last online."); return; } // get losses touchdownsScored = Integer.parseInt(getNextSubparameter(p, c)); c += 4 + getNextSubparameter(p, c).length(); // skip closing quote, comma and space if (p.charAt(c - 1) != '"') { manager .getParent() .log( Level.SEVERE, "Malformed user string. Ignoring casualties, touchdowns received and last online."); return; } // get losses touchdownsReceived = Integer.parseInt(getNextSubparameter(p, c)); c += 4 + getNextSubparameter(p, c).length(); // skip closing quote, comma and space if (p.charAt(c - 1) != '"') { manager .getParent() .log(Level.SEVERE, "Malformed user string. Ignoring casualties and last online."); return; } // get casualties casualties = Integer.parseInt(getNextSubparameter(p, c)); c += 4 + getNextSubparameter(p, c).length(); // skip closing quote, comma and space if (p.charAt(c - 1) != '"') { manager.getParent().log(Level.SEVERE, "Malformed user string. Ignoring last online."); return; } // get last online try { String lastOnlineString = getNextSubparameter(p, c); lastOnline = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(lastOnlineString); } catch (ParseException e) { manager.getParent().log(Level.SEVERE, "Malformed user string. Ignoring last online."); } } catch (IndexOutOfBoundsException e) { manager .getParent() .log( Level.SEVERE, "Malformed user string. Ignoring name, password hash, op status, wins, losses, casualties, touchdowns received, touchdowns scored and last online."); } } }