View Javadoc

1   /**
2    * This program is free software: you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
13   */
14  
15  package net.curre.prefcount.service;
16  
17  import java.util.List;
18  import java.util.logging.Level;
19  import java.util.logging.Logger;
20  
21  import net.curre.prefcount.bean.GameResultBean;
22  import net.curre.prefcount.bean.PlayerStatistics;
23  
24  /**
25   * This service bean is responsible for
26   * computing game results.
27   * <p/>
28   * Created date: Jul 29, 2007
29   *
30   * @author Yevgeny Nyden
31   */
32  public class ResultService {
33  
34    /** Private class logger. */
35    private static Logger log = Logger.getLogger(ResultService.class.toString());
36  
37    /**
38     * Generates the final player scores. Note that the target
39     * bullet must be set to a valid (positive) value.
40     *
41     * @param rBean <code>GameResultBean</code> object that contains
42     *              all necessary game data.
43     */
44    public static void generateFinalResults(GameResultBean rBean) {
45  
46      if (log.isLoggable(Level.FINE)) {
47        log.fine("Generating final results for Bean: " + rBean);
48      }
49  
50      // computing the average and minimum mountain
51      int min = Integer.MAX_VALUE;
52      int sum = 0;
53      List<PlayerStatistics> playerStats = rBean.getPlayerStats();
54      for (PlayerStatistics stats : playerStats) {
55        int currMountain = stats.getNewMountain();
56        sum += currMountain;
57        if (currMountain < min) {
58          min = currMountain;
59        }
60      }
61      rBean.setAverageMountain((float) sum / (float) playerStats.size() - min);
62      rBean.setMinMountain(min);
63  
64      // computing the vists saldo/balances
65      for (int playerInd = 0; playerInd < playerStats.size(); ++playerInd) {
66        PlayerStatistics stats = playerStats.get(playerInd);
67        int totalSaldo = 0;
68        for (int otherInd = 0; otherInd < playerStats.size(); ++otherInd) {
69          if (playerInd != otherInd) {
70            Integer computedSaldo = stats.getVistSaldoMap().get(otherInd);
71            if (computedSaldo == null) {
72              PlayerStatistics statsOther = playerStats.get(otherInd);
73              final int currPlayerVists = stats.getVistsForPlayer(otherInd);
74              final int otherPlayerVists = statsOther.getVistsForPlayer(playerInd);
75              computedSaldo = currPlayerVists - otherPlayerVists;
76              stats.getVistSaldoMap().put(otherInd, computedSaldo);
77              statsOther.getVistSaldoMap().put(playerInd, -1 * computedSaldo);
78            }
79            totalSaldo += computedSaldo;
80          }
81        }
82        // saving total saldo under the same (as the player index) key
83        stats.getVistSaldoMap().put(playerInd, totalSaldo);
84      }
85      rBean.setFinalScoresReady(true);
86    }
87  
88    /**
89     * Clears the final player scores.
90     *
91     * @param rBean <code>GameResultBean</code> object that contains
92     *              all necessary game data.
93     */
94    public static void clearFinalResults(GameResultBean rBean) {
95      // clearing the player vist saldo maps
96      for (PlayerStatistics stats : rBean.getPlayerStats()) {
97        stats.getVistSaldoMap().clear();
98      }
99      rBean.setFinalScoresReady(false);
100   }
101 
102   /** Private methods ***********************/
103 
104 }