/
ClientUser.java
302 lines (266 loc) · 9.31 KB
/
ClientUser.java
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JOptionPane;
public class ClientUser extends User implements Runnable{
Socket mySocket;
ObjectInputStream in;
ObjectOutputStream out;
PokemonFrame pk;
String opponentPokemon;
int opponentHealth;
double opponentStrength;
boolean myTurn = false;
public ClientUser(String ipOfServer){
super();
try {
//TODO: DO NOT HARDCODE LOCALHOST
mySocket = new Socket(ipOfServer, 1234);
in = new ObjectInputStream(mySocket.getInputStream());
out = new ObjectOutputStream(mySocket.getOutputStream());
} catch (IOException e) {
System.out.println("Could not connect to server");
}
}
public void setPokemonFrame(PokemonFrame pk) {
this.pk = pk;
}
public ObjectOutputStream getOutputStream(){
return out;
}
public void purchaseSteroids() {
try {
out.writeObject(new PurchaseUpdate(this.getID(), 0, true, 1, 0, 0));
} catch (IOException e) {
e.printStackTrace();
}
}
public void purchaseMorphine() {
try {
out.writeObject(new PurchaseUpdate(this.getID(), 0, true, 0, 1, 0));
} catch (IOException e) {
e.printStackTrace();
}
}
public void purchaseEpinephrine() {
try {
out.writeObject(new PurchaseUpdate(this.getID(), 0, true, 0, 0, 1));
} catch (IOException e) {
e.printStackTrace();
}
}
private void update(UserUpdate user) {
this.setID(user.getID());
this.setUsername(user.getUsername());
this.setMoney(user.getMoney());
System.out.println("money now:" + user.getMoney());
this.setWins(user.getWins());
this.setLosses(user.getLosses());
this.setOpponentID(user.getOpponentID());
if(this.getItems() == null) {
this.setItems(new HashMap<String, Integer>());
}
this.setItemQuantity("steroids", user.getSteroids());
this.setItemQuantity("morphine", user.getMorphine());
this.setItemQuantity("epinephrine", user.getEpinephrine());
}
private void processBattleData(BattleData bd) {
if(!this.isInBattle()) {
this.setInBattle(true);
if(bd.getId() == this.getID()) {
myTurn = true;
}
this.opponentPokemon = bd.getOpponentPokemon();
this.opponentHealth = bd.getOpponentHealth();
this.opponentStrength = bd.getOpponentStrength();
this.setCurrentPokemon(this.getPokemon(bd.getMyPokemon()));
this.pk.showBattle();
if(this.myTurn) {
this.pk.currentBattle.toggle();
pk.currentBattle.setStatus("What will you do?");
}
} else {
if(bd.getId() == this.getID()) {
processOwnMove(bd);
} else {
processOpponentMove(bd);
}
pk.currentBattle.updateBattleUI();
}
}
private void processOpponentMove(BattleData bd) {
Pokemon myP = this.getCurrentPokemon();
if(bd.getType().equals("attack")) {
boolean attackMissed = false;
if(myP.getHealthPoints() == bd.getOpponentHealth()){
attackMissed = true;
}
myP.setHealthPoints(bd.getOpponentHealth());
myP.setStrength(bd.getOpponentStrength());
pk.currentBattle.updateBattleUI();
if(attackMissed){
pk.currentBattle.setStatus(bd.getOpponentPokemon() + " used " + bd.getAttackName() + " but it missed!");
}
else{
pk.currentBattle.setStatus(bd.getOpponentPokemon() + " used " + bd.getAttackName());
}
if(myP.getHealthPoints() == 0) {
this.getCurrentPokemon().setDead(true);
pk.currentBattle.setStatus(this.getCurrentPokemon().getName() + " fainted! Choose a pokemon");
pk.currentBattle.forceSwitchPokemon();
}
} else if(bd.getType().equals("item")) {
if(bd.getItemName().equals("morphine")){
this.opponentHealth = bd.getMyHealth();
pk.currentBattle.setStatus(bd.getMyPokemon() + " used morphine! Health increased by 200");
pk.currentBattle.updateBattleUI();
} else if(bd.getItemName().equals("steroids")) {
this.opponentStrength = bd.getMyStrength();
System.out.println("Opponent's new strength: " + bd.getMyStrength());
pk.currentBattle.setStatus(bd.getMyPokemon() + " used steroids! Attack increased");
}
else if(bd.getItemName().equals("epinephrine")) {
if(bd.getMyPokemon().equals(this.opponentPokemon)) {
this.opponentHealth = bd.getMyHealth();
pk.currentBattle.setStatus("Opponent fully restored " + bd.getMyPokemon());
}
}
} else if(bd.getType().equals("switch")) {
this.opponentPokemon = bd.getMyPokemon();
this.opponentHealth = bd.getMyHealth();
this.opponentStrength = bd.getMyStrength();
pk.currentBattle.setStatus("Opponent sent out " + bd.getMyPokemon() + "!");
}
this.pk.currentBattle.toggle();
}
private void processOwnMove(BattleData bd) {
Pokemon myP = this.getCurrentPokemon();
if(bd.getType().equals("attack")) {
boolean attackMissed = false;
boolean killedOpponent = false;
if(this.opponentHealth == bd.getOpponentHealth()){
attackMissed = true;
}
if(bd.getOpponentHealth() <= 0){
killedOpponent = true;
}
this.opponentHealth = bd.getOpponentHealth();
this.opponentStrength = bd.getOpponentStrength();
pk.currentBattle.updateBattleUI();
if(attackMissed){
pk.currentBattle.setStatus(bd.getOpponentPokemon() + " used " + bd.getAttackName() + " but it missed!");
}
else if(killedOpponent){
pk.currentBattle.setStatus(bd.getOpponentPokemon() + " used " + bd.getAttackName() + ". " + this.opponentPokemon + " fainted!");
}
else{
pk.currentBattle.setStatus(bd.getOpponentPokemon() + " used " + bd.getAttackName());
}
} else if(bd.getType().equals("item")) {
if(bd.getItemName().equals("morphine")){
this.updateItem(bd.getItemName(), -1);
myP.setHealthPoints(bd.getMyHealth());
pk.currentBattle.setStatus(bd.getMyPokemon() + " used morphine! Health increased by 200");
pk.currentBattle.updateBattleUI();
} else if(bd.getItemName().equals("steroids")) {
myP.setStrength(bd.getMyStrength());
System.out.println("My new strength: " + bd.getMyStrength());
pk.currentBattle.setStatus(bd.getMyPokemon() + " used steroids! Attack increased");
}
else if(bd.getItemName().equals("epinephrine")) {
Pokemon targetP = this.getPokemon(bd.getMyPokemon());
targetP.setHealthPoints(bd.getMyHealth());
targetP.setDead(false);
pk.currentBattle.setStatus(bd.getMyPokemon() + " used epinephrine! It was fully restored!");
}
} else if(bd.getType().equals("switch")) {
this.setCurrentPokemon(this.getPokemon(bd.getMyPokemon()));
pk.currentBattle.setStatus("I choose you " + bd.getMyPokemon() + "!");
}
this.pk.currentBattle.toggle();
}
public void sendMessageToServer(Object obj) {
try {
this.out.writeObject(obj);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while(true){
try{
// send a message every second
while(true){
if(mySocket == null){
pk.showCouldNotConnect();
return;
}
Object objectReceived = in.readObject();
if(objectReceived instanceof LoginAuthenticated){
System.out.println("Verifying...");
LoginAuthenticated messageReceived = (LoginAuthenticated)objectReceived;
if(messageReceived.getAuthenticated()){
System.out.println("Login verified");
this.pk.userHasLoggedIn();
}
else{
if(messageReceived.triedToAuthenticateFromSignUp()){
JOptionPane.showMessageDialog(pk,
"That username is already in use.",
"Error",
JOptionPane.ERROR_MESSAGE);
}
else{
System.out.println("Login failed");
JOptionPane.showMessageDialog(pk,
"Invalid username or password",
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
} else if(objectReceived instanceof UserUpdate) {
this.update((UserUpdate)objectReceived);
} else if(objectReceived instanceof PurchaseUpdate) {
if(((PurchaseUpdate) objectReceived).isSuccessful()) {
this.setMoney(((PurchaseUpdate) objectReceived).getMoney());
this.setItemQuantity("steroids", ((PurchaseUpdate) objectReceived).getSteroids());
this.setItemQuantity("morphine", ((PurchaseUpdate) objectReceived).getMorphine());
this.setItemQuantity("epinephrine", ((PurchaseUpdate) objectReceived).getEpinephrine());
this.pk.storePanel.update();
JOptionPane.showMessageDialog(pk,
"Successfully purchased",
"",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(pk,
"You do not have enough gold",
"Error",
JOptionPane.ERROR_MESSAGE);
}
} else if(objectReceived instanceof ChatMessage){
pk.addTextToChat((ChatMessage)objectReceived);
} else if(objectReceived instanceof BattleData) {
processBattleData((BattleData)objectReceived);
}else if(objectReceived instanceof SendOpponentId) {
SendOpponentId msg = (SendOpponentId)objectReceived;
this.setOpponentID(msg.getIDOfOpponent());
System.out.println("Opponent's ID:" + this.getOpponentID());
} else if(objectReceived instanceof GameOver) {
if( ((GameOver)objectReceived).getWinnerID() == this.getID()) {
//I LOST!
pk.showWinLossScreen(true);
} else {
//I WON!
pk.showWinLossScreen(false);
}
}
}
}
catch(IOException | ClassNotFoundException e){
}
}
}
}