1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.curre.prefcount.service;
16
17 import java.util.Map;
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 import net.curre.prefcount.gui.type.Place;
24 import net.curre.prefcount.PrefCountRegistry;
25
26
27
28
29
30
31
32
33
34 public class ResultService {
35
36
37 private static Logger log = Logger.getLogger(ResultService.class.toString());
38
39
40
41
42
43
44
45
46 public static void generateFinalResults(GameResultBean rBean) {
47
48 if (log.isLoggable(Level.FINE)) {
49 log.fine("Generating final results for Bean: " + rBean);
50 }
51
52
53 int min = Integer.MAX_VALUE;
54 int sum = 0;
55 int playersNum = rBean.getPlayerStats().size();
56 for (Map.Entry<Place, PlayerStatistics> entry : rBean.getPlayerStats().entrySet()) {
57 PlayerStatistics stats = entry.getValue();
58 int currMountain = stats.getNewMountain();
59 sum += currMountain;
60 if (currMountain < min) {
61 min = currMountain;
62 }
63
64
65 stats.setMountFix(null);
66 stats.getWhistFixesMap().clear();
67 stats.getWhistSaldoMap().clear();
68 }
69
70 rBean.setMinMountain(min);
71
72
73 int mountFix = 0;
74 if (rBean.isMountDivisibleByN()) {
75 final int remainder = (sum - min * playersNum) % playersNum;
76
77 if (remainder != 0) {
78 askForAdjustmentPlayer();
79 }
80
81 mountFix = doFixForDivisibleByN(remainder, rBean.getDivisibleByNPlayer(), rBean.getPlayerStats());
82 }
83 rBean.setAverageMountain((float) (sum + mountFix) / (float) playersNum - min);
84
85
86 for (Map.Entry<Place, PlayerStatistics> currEntry : rBean.getPlayerStats().entrySet()) {
87 PlayerStatistics currStats = currEntry.getValue();
88 Place currPlace = currEntry.getKey();
89 int totalSaldo = 0;
90 for (Map.Entry<Place, PlayerStatistics> otherEntry : rBean.getPlayerStats().entrySet()) {
91 Place otherPlace = otherEntry.getKey();
92 PlayerStatistics otherStats = otherEntry.getValue();
93 if (currPlace != otherPlace) {
94 Integer whistSaldo = currStats.getWhistSaldoMap().get(otherPlace);
95 if (whistSaldo == null) {
96 int currWhist = currStats.getWhistsAgainstPlayer(otherPlace);
97 Integer currwhistFix = currStats.getWhistFixAgainstPlayer(otherPlace);
98 if (currwhistFix != null) {
99 currWhist += currwhistFix;
100 }
101 int whistAgainst = otherStats.getWhistsAgainstPlayer(currPlace);
102 Integer whistFixAgainst = otherStats.getWhistFixAgainstPlayer(currPlace);
103 if (whistFixAgainst != null) {
104 whistAgainst += whistFixAgainst;
105 }
106 whistSaldo = currWhist - whistAgainst;
107 currStats.getWhistSaldoMap().put(otherPlace, whistSaldo);
108 otherStats.getWhistSaldoMap().put(currPlace, -1 * whistSaldo);
109 }
110 totalSaldo += whistSaldo;
111 }
112 }
113
114 currStats.getWhistSaldoMap().put(currEntry.getKey(), totalSaldo);
115 }
116 rBean.setFinalScoresReady();
117 }
118
119
120
121
122
123
124
125 public static void clearFinalResults(GameResultBean rBean) {
126
127 for (PlayerStatistics stats : rBean.getPlayerStats().values()) {
128 stats.getWhistSaldoMap().clear();
129 }
130 rBean.clearResults();
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144 private static int doFixForDivisibleByN(int remainder, Place adjustPlace,
145 Map<Place, PlayerStatistics> playerStats) {
146 int mountFix = 0;
147
148 if (remainder != 0) {
149 final int playersNum = playerStats.size();
150
151 if (playersNum == 4) {
152 if (remainder == 1) {
153
154 PlayerStatistics stats = playerStats.get(adjustPlace);
155 stats.setMountFix(-1);
156 mountFix = -1;
157
158 addWhistsFixAgainstSelf(playerStats, adjustPlace, 3);
159
160 } else if (remainder == 2) {
161
162
163 } else if (remainder == 3) {
164
165 PlayerStatistics stats = playerStats.get(adjustPlace);
166 stats.setMountFix(1);
167 mountFix = 1;
168
169 addWhistsFixAgainstOthers(playerStats, adjustPlace, 3);
170 }
171
172 } else {
173 if (remainder == 1) {
174
175 PlayerStatistics stats = playerStats.get(adjustPlace);
176 stats.setMountFix(-1);
177 mountFix = -1;
178
179 addWhistsFixAgainstSelf(playerStats, adjustPlace, 3);
180
181 } else if (remainder == 2) {
182
183 PlayerStatistics stats = playerStats.get(adjustPlace);
184 stats.setMountFix(1);
185 mountFix = 1;
186
187 addWhistsFixAgainstOthers(playerStats, adjustPlace, 3);
188 }
189 }
190 }
191 return mountFix;
192 }
193
194
195
196
197
198
199
200 private static void askForAdjustmentPlayer() {
201 PrefCountRegistry reg = PrefCountRegistry.getInstance();
202 boolean notDebug = reg.getMainWindow() != null && reg.getMainWindow().isVisible();
203 if (notDebug) {
204 PrefCountRegistry.getInstance().getChoosePlayerDialog();
205 }
206 }
207
208
209
210
211
212
213
214
215
216 private static void addWhistsFixAgainstOthers(Map<Place, PlayerStatistics> playerStats,
217 Place place, int value) {
218 PlayerStatistics stats = playerStats.get(place);
219 for (Map.Entry<Place, PlayerStatistics> entry : playerStats.entrySet()) {
220 if (entry.getKey() != place) {
221 stats.getWhistFixesMap().put(entry.getKey(), value);
222 }
223 }
224 }
225
226
227
228
229
230
231
232
233
234 private static void addWhistsFixAgainstSelf(Map<Place, PlayerStatistics> playerStats,
235 Place place, int value) {
236 for (Map.Entry<Place, PlayerStatistics> entry : playerStats.entrySet()) {
237 if (entry.getKey() != place) {
238 entry.getValue().getWhistFixesMap().put(place, value);
239 }
240 }
241 }
242
243 }