package com square WeakLinkServer import java util ArrayList import ja

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.square.WeakLinkServer;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
public class Game {
private int ID = 0;
private TimeState currentState = null;
private int time = 0;
private int finalScore = 0;
public Game(int id) {
ID = id;
}
public int getID() {
return ID;
}
public int getTime() {
return time;
}
public void setTime(int t) {
time = t;
}
public TimeState getCurrentState() {
return currentState;
}
public void changeState(TimeState newState) {
if (currentState != null) {
currentState.deactivate(this);
}
currentState = newState;
if (currentState != null) {
currentState.activate(this);
}
}
public void start() {
time = 0;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
time++;
}
}, 1, 1000);
changeState(GameState.START);
}
public void setTimeout(int millis) {
final Timer locTimer = new Timer();
final TimeState state = currentState;
final Game game = this;
locTimer.schedule(new TimerTask() {
@Override
public void run() {
state.onTimeout(game);
locTimer.cancel();
locTimer.purge();
}
}, 1, millis);
}
//=======================================================================//
private TreeMap<Integer, Client> players = new TreeMap<Integer, Client>();
private int NumPlayers = Preferences.Game.NumPlayers;
private int currentNumPlayers = 0;
private int currentPlayerID = -1;
public synchronized void addPlayer(Client new_client) {
if (currentNumPlayers < NumPlayers) {
if (currentPlayerID == -1) {
currentPlayerID = new_client.getID();
}
players.put(new_client.getID(), new_client);
currentNumPlayers++;
}
}
public boolean isFully() {
return (currentNumPlayers == NumPlayers);
}
public Client getCurrentPlayer() {
return players.get(currentPlayerID);
}
public int getCurrentPlayerID() {
return currentPlayerID;
}
public TreeMap<Integer, Client> getAllPlayers() {
return players;
}
public void choosePlayer() {
currentPlayerID = 0;
}
public int nextPlayer() {
for (Client c : players.values()) {
if (c.isEnabled()) {
if (c.getID() == currentPlayerID) {
return -1;
}
currentPlayerID = c.getID();
return currentPlayerID;
}
}
return -2;
}
public void disablePlayer(int id) {
players.get(id).disable();
}
public void sendAll(String msg){
for (Client i : players.values()){
i.send(msg);
}
}
private int currentRound = 0;
public int getCurrentRound() {
return currentRound;
}
public void nextRound() {
if (currentRound < Preferences.Game.NumRounds - 1) {
currentRound++;
}
}
public boolean isFinal() {
return currentRound == Preferences.Game.NumRounds;
}
public Question getQuestion(){
if(isFinal()){
finalScore++;
}
Question question = new Question();
return question;
}
private Question currentQuestion = null;
public Question getCurrentQuestion() {
return currentQuestion;
}
public void setCurrentQuestion(Question question) {
currentQuestion = question;
}
private ArrayList<Integer> costs = Preferences.Game.Costs;
private int costsStackSize = Preferences.Game.CostStackSize;
private int currentCostID = 0;
public ArrayList<Integer> getCosts() {
return costs;
}
public int getCostsStackSize() {
return costsStackSize;
}
public int getCurrentCostID() {
return currentCostID;
}
public void costDown() {
currentCostID = Math.min(currentCostID - 1, costsStackSize);
currentCostID = Math.max(currentCostID - 1, 0);
}
public void costUp() {
currentCostID = Math.min(currentCostID + 1, costsStackSize);
currentCostID = Math.max(currentCostID + 1, 0);
}
public void flushCost() {
currentCostID = 0;
}
private int bankSummary = 0;
private int currentBank = 0;
private int maxRoundBank = Preferences.Game.MaxRoundBank;
private boolean flushBankEnabled = false;
public int getMaxRoundBank() {
return maxRoundBank;
}
public int getBankSummary() {
return bankSummary;
}
public int getCurrentBank() {
return currentBank;
}
public boolean flushToBank() {
if (flushBankEnabled) {
currentBank += costs.get(currentCostID);
flushCost();
if (currentBank >= maxRoundBank) {
currentBank = maxRoundBank;
return false;
}
return true;
}
return false;
}
public void saveBank() {
bankSummary += currentBank;
}
public boolean isBankEnabled() {
return flushBankEnabled;
}
public void enableBank() {
flushBankEnabled = true;
}
public void disableBank() {
flushBankEnabled = false;
}
private TreeMap<Integer, Integer> lastRate = new TreeMap<Integer, Integer>();
public TreeMap<Integer, Integer> getLastRate() {
return lastRate;
}
public void clearLastRate() {
lastRate.clear();
}
public int getDropID() {
TreeMap<Integer, Integer> r = new TreeMap<Integer, Integer>();
for (Integer id : lastRate.values()) {
if (!r.containsKey(id)) {
r.put(id, 0);
}
r.put(id, r.get(id) + 1);
}
int resID = -1;
int max = -1;
for (Integer id : r.keySet()) {
if (r.get(id) > max) {
resID = id;
max = r.get(id);
}
}
return resID;
}
public void rate(int playerID, int rateID) {
lastRate.put(playerID, rateID);
}
public int getLastStrongestID() {
int sID = 0;
int max = -1;
for (Client c : players.values()) {
if (c.isEnabled() && c.getCorrect() > max) {
sID = c.getID();
max = c.getCorrect();
}
}
return sID;
}
public int getLastWeakestID() {
int wID = 0;
int min = -1;
for (Client c : players.values()) {
if (c.isEnabled() && (c.getCorrect() < min || min < 0)) {
wID = c.getID();
min = c.getCorrect();
}
}
return wID;
}
public int getFinalScore() {
return finalScore;
}
public GameShot getShot() {
GameShot gs = new GameShot();
gs.init(this);
return gs;
}
}