-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpeedServerThread.java
278 lines (251 loc) · 7.94 KB
/
SpeedServerThread.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Stack;
/**
* Speed Server Thread to handle one client connection.
*
* @author Sunny Guan
* @version May 30, 2019
* @author Period: 5
* @author Assignment: Speed
*
* @author Sources: None
*/
public class SpeedServerThread extends Thread
{
private int id = -1;
private Socket socket = null;
private SpeedServerThread otherPlayer = null;
private Speed game;
private String name;
/**
* @param socket
* Client socket
* @param game
* Game controller
* @param id
* first or second player
*/
public SpeedServerThread( Socket socket, Speed game, int id )
{
super( "KKMultiServerThread" );
this.id = id;
this.socket = socket;
this.game = game;
}
/**
* Get player name
*
* @return player name
*/
public String getPlayerName()
{
return name;
}
/**
* Get instance of the opponent
*
* @return the opponent
*/
public SpeedServerThread getOtherPlayer()
{
return otherPlayer;
}
/**
* Set the other player
*
* @param otherPlayer
* the opponent
*/
public void setOtherPlayer( SpeedServerThread otherPlayer )
{
this.otherPlayer = otherPlayer;
}
/**
* Get output stream to the other player's socket
*
* @return
*/
public PrintWriter getOut()
{
return out;
}
/**
* Set output stream to the opponent's socket
*
* @param out
* output Print Writer
*/
public void setOut( PrintWriter out )
{
this.out = out;
}
private PrintWriter out;
@Override
// game logic
// handles input from clients and respond according to the protocol
public void run()
{
try
{
out = new PrintWriter( socket.getOutputStream(), true );
BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
String inputLine = in.readLine();
name = inputLine.substring( 5 );
System.out.println( name + " joined the server." );
if ( id == 1 )
{
for ( Card c : game.getHand1() )
{
out.println( "HANDADD|" + c.toString() );
}
}
else
{
for ( Card c : game.getHand2() )
{
out.println( "HANDADD|" + c.toString() );
}
}
out.println( "SETPILE|" + game.getCentralPile1().get( 0 ) + "|1" );
out.println( "SETPILE|" + game.getCentralPile2().get( 0 ) + "|2" );
while ( ( inputLine = in.readLine() ) != null )
{
if ( inputLine.equals( "STUCK" ) )
{
game.stuckCount++;
if ( game.stuckCount == 2 )
{
game.flipSideDeck();
String msg1 = "SETPILE|" + game.getCentralPile1Card().toString() + "|1|T";
String msg2 = "SETPILE|" + game.getCentralPile2Card().toString() + "|2|T";
out.println( msg1 );
out.println( msg2 );
otherPlayer.out.println( msg1 );
otherPlayer.out.println( msg2 );
game.stuckCount = 0;
System.out.println( "Both players stuck." );
}
}
else if ( inputLine.equals( "UNSTUCK" ) )
{
game.stuckCount--;
}
else if ( inputLine.startsWith( "HANDREMOVE" ) )
{
game.removeCard( id, inputLine.split( "\\|" )[1] );
ArrayList<Card> hand = getHand( id );
Stack<Card> deck = getDeck( id );
if ( hand.isEmpty() && deck.isEmpty() )
{
gameOverAction();
break;
}
}
else if ( inputLine.startsWith( "MOVETOPILE" ) )
{
Card c = new Card( inputLine.split( "\\|" )[1] );
int pileNum = Integer.parseInt( inputLine.split( "\\|" )[2] );
boolean canSet = game.set( id, c, pileNum );
if ( canSet )
{
game.removeCard( id, inputLine.split( "\\|" )[1] );
out.println( "HANDREMOVE|" + c );
String msg = "SETPILE|" + inputLine.split( "\\|" )[1] + "|" + inputLine.split( "\\|" )[2];
out.println( msg );
otherPlayer.out.println( msg );
ArrayList<Card> hand = getHand( id );
Stack<Card> deck = getDeck( id );
if ( hand.isEmpty() && deck.isEmpty() )
{
gameOverAction();
break;
}
}
}
else if ( inputLine.startsWith( "REFILL" ) )
{
ArrayList<Card> hand = getHand( id );
Stack<Card> deck = getDeck( id );
int refillIndex = Integer.parseInt( inputLine.split( "\\|" )[1] );
while ( hand.size() < 5 )
{
if ( !deck.isEmpty() )
{
Card c = deck.pop();
hand.add( c );
out.println( "HANDADD|" + c.toString() );
}
else
{
out.println( "DEACTCARD" );
otherPlayer.out.println( "DECKEMPTY2|" + refillIndex );
break;
}
}
}
else if ( inputLine.startsWith( "FGAMEOVER" ) )
{ // TODO testing only!
gameOverAction();
// break;
}
System.out.println( inputLine );
System.out.println( "Left in deck: " + game.getDeck1().size() + ", " + game.getDeck2().size() );
}
socket.close();
}
catch ( IOException e )
{
e.printStackTrace();
}
}
/**
* Helper method to get this client's hand
*
* @param id
* first or second player
* @return ArrayList of the player's hand
*/
public ArrayList<Card> getHand( int id )
{
return id == 1 ? game.getHand1() : game.getHand2();
}
/**
* Helper method to get this client's deck
*
* @param id
* first or second player
* @return stack of the player's deck
*/
public Stack<Card> getDeck( int id )
{
return id == 1 ? game.getDeck1() : game.getDeck2();
}
/**
* Game over output to console and output streams
*/
public void gameOverAction()
{
out.println( "YOUWIN" );
otherPlayer.out.println( "YOULOSE" );
System.out.println( "Game over! " + name + " wins! " + otherPlayer.getPlayerName() + " loses!" );
}
/**
* Remove a card from hand
*
* @param id
* first or second player
* @param c
* card to remove
* @return if the removal was successful
*/
public boolean remove( int id, Card c )
{
ArrayList<Card> hand = id == 1 ? game.getHand1() : game.getHand2();
return hand.remove( c );
}
}