1010import edu .sdccd .cisc191 .Common .Request ;
1111import javafx .application .Application ;
1212import javafx .stage .Stage ;
13+ import lombok .Getter ;
1314import org .json .simple .JSONArray ;
1415import org .json .simple .JSONObject ;
1516import org .json .simple .parser .JSONParser ;
2829 * It handles game, user, and size requests, as well as modifications to user data.
2930 */
3031public class Client {
31- /**
32- * A static user representing the client user. Initialized with name "Chase" and money 1000000.
33- */
34-
35- /**
36- * The socket used to connect to the server.
37- */
38- private static Socket clientSocket ;
39-
40- private static ObjectOutputStream out ; // The output stream for sending requests to the server.
41- private static ObjectInputStream in ; // The input stream for receiving responses from the server.
42-
43- // --- Socket and Request Methods ---
44- /**
45- * Establishes a connection to the server using the provided IP address and port.
46- *
47- * @param ip the IP address of the server.
48- * @param port the port number on the server.
49- * @throws IOException if an I/O error occurs when opening the connection.
50- */
51- public static void startConnection (String ip , int port ) throws IOException {
52- clientSocket = new Socket (ip , port );
5332
54- out = new ObjectOutputStream ( clientSocket . getOutputStream ());
55- in = new ObjectInputStream ( clientSocket . getInputStream () );
33+ protected HttpClient httpClient () {
34+ return HttpClient . newHttpClient ( );
5635 }
5736
58-
5937 /**
60- * Sends a request to the server and returns a response of the expected type.
38+ * Grabs odds from the Sports API via the Database Server
6139 *
62- * Never call this method directly, call one of its wrappers for safe usage.
63- *
64- * @param <T> the type parameter corresponding to the expected response type.
65- * @return the response from the server cast to the specified type.
66- * @throws Exception if an error occurs during the request.
67- */
68- static <T > T sendRequest (Request request , Class <T > responseType ) throws Exception {
69- // Write request
70- out .writeObject (request );
71- out .flush ();
72-
73- // read back whatever the server sent
74- Object raw = in . readObject ();
75- System .out .println ("Raw: " + raw );
76- System .out .println ("Raw type: " + raw .getClass ());
77- System .out .println ("Response Type: " + responseType );
78-
79- // cast into the expected type
80-
81- try {
82- return responseType .cast (raw );
83- }
84- catch (ClassCastException e ) {
85- System .out .println ("ClassCastException, could not cast " + raw .getClass () + " to " + responseType );
86- }
87-
88- return null ;
89-
90- }
91-
92- // Update stopConnection to check for null before closing resources:
93- public void stopConnection () throws IOException {
94- if (in != null ) {
95- in .close ();
96- }
97- if (out != null ) {
98- out .close ();
99- }
100- if (clientSocket != null ) {
101- clientSocket .close ();
102- }
103- }
104-
105-
106- public User userGetRequest (int id ) throws IOException {
107- Client client = new Client ();
108-
109- try {
110- client .startConnection ("localhost" , 4445 );
111-
112- // build a request object
113- Request req = new Request ("User" , id );
114-
115- return client .sendRequest (req , User .class );
116- } catch (Exception e ) {
117- throw new RuntimeException (e );
118- } finally {
119- try {
120- client .stopConnection ();
121- } catch (Exception e ) {
122- e .printStackTrace ();
123- }
124- }
125-
126- }
127- /**
128- * Modifies a user on the server with the provided attributes and returns the updated user.
129- *
130- * @param id the identifier of the user to modify.
131- * @param modifiedAttributes a map containing the fields and their new values.
132- * Valid Fields: ("Name", "Money", "addBet", "removeBet")
133- * @return the updated User object if modification is successful; null otherwise.
134- * @throws IOException if an I/O error occurs during the request.
135- */
136- public User userModifyRequest (int id , Map <String , Object > modifiedAttributes ) throws IOException {
137- Client client = new Client ();
138- try {
139- client .startConnection ("localhost" , 4444 );
140- System .out .println ("Sending userModifyRequest with ID: " + id );
141- return client .sendRequest (new Request ("ModifyUser" , id , modifiedAttributes ), User .class );
142- } catch (Exception e ) {
143- e .printStackTrace ();
144- }
145- stopConnection ();
146- return null ;
147- }
148-
40+ * @param gameId the game ID (from the API) to grab the odds for.
41+ * @param sport the sport to grab the odds for.
42+ * @param homeOrAway 0 for home, 1 for away.
43+ * */
14944 public static double getOdds (int gameId , String sport , int homeOrAway ) throws IOException , InterruptedException {
150- HttpClient client = HttpClient . newHttpClient ();
45+ HttpClient client = new Client (). httpClient ();
15146
15247 HttpRequest request = HttpRequest .newBuilder ()
15348 .uri (URI .create ("http://localhost:9090/games/odds/" + sport + "/" + gameId ))
@@ -199,7 +94,7 @@ public static double getOdds(int gameId, String sport, int homeOrAway) throws IO
19994 * @throws IOException if an I/O error occurs during retrieval.
20095 */
20196 public static ArrayList <Game > getGames () throws Exception {
202- HttpClient client = HttpClient . newHttpClient ();
97+ HttpClient client = new Client (). httpClient ();
20398
20499 HttpRequest request = HttpRequest .newBuilder ()
205100 .uri (URI .create ("http://localhost:9090/games" ))
@@ -228,8 +123,11 @@ public static ArrayList<Game> getGames() throws Exception {
228123 return allGames ;
229124 }
230125
126+ /**
127+ * Retrieves the primary user, user of ID 1, from the server and initializes it to a User object
128+ * */
231129 public static User getMainUser () throws Exception {
232- HttpClient client = HttpClient . newHttpClient ();
130+ HttpClient client = new Client (). httpClient ();
233131
234132 HttpRequest request = HttpRequest .newBuilder ()
235133 .uri (URI .create ("http://localhost:9090/users/1" ))
@@ -262,8 +160,12 @@ public static User getMainUser() throws Exception {
262160
263161 }
264162
163+ /**
164+ * Uses a PUT request to update the main user's money amount.
165+ * @param money the new amount of money to set the main user's money to.
166+ * */
265167 private static void updateMainUserMoney (long money ) throws Exception {
266- HttpClient client = HttpClient . newHttpClient ();
168+ HttpClient client = new Client (). httpClient ();
267169
268170 String jsonBody = "{\" id\" : 1, \" name\" : \" Chase\" , \" money\" : " + money + "}" ;
269171
@@ -280,8 +182,11 @@ private static void updateMainUserMoney(long money) throws Exception {
280182
281183 }
282184
185+ /**
186+ * Calls a GET method on the Database server to fulfill expired user bets
187+ * */
283188 public static void updateBets () throws Exception {
284- HttpClient client = HttpClient . newHttpClient ();
189+ HttpClient client = new Client (). httpClient ();
285190
286191 HttpRequest request = HttpRequest .newBuilder ()
287192 .uri (URI .create ("http://localhost:9090/updateAllBets" ))
@@ -293,6 +198,14 @@ public static void updateBets() throws Exception {
293198 System .out .println ("Status Code (update bets on app open): " + response .statusCode ());
294199 }
295200
201+ /**
202+ * Calls a PATCH method on the Database server to add a bet to a user's bets.
203+ * @param userId the ID of the user to add the bet to.
204+ * @param gameId the ID (from the API) of the game to add the bet for.
205+ * @param betTeam the team the user bets on.
206+ * @param betAmt the amount the user bets.
207+ * @param winAmt the amount the user wins.
208+ * */
296209 public static void patchAddBetToMainUser (Long userId , Long gameId , String betTeam , int betAmt , int winAmt ) throws Exception {
297210 ObjectMapper mapper = new ObjectMapper ();
298211 // build only the DTO fields
@@ -306,7 +219,7 @@ public static void patchAddBetToMainUser(Long userId, Long gameId, String betTea
306219
307220 System .out .println ("PATCH body: " + jsonBody );
308221
309- HttpClient client = HttpClient . newHttpClient ();
222+ HttpClient client = new Client (). httpClient ();
310223 HttpRequest request = HttpRequest .newBuilder ()
311224 .uri (URI .create ("http://localhost:9090/" + userId + "/bets" ))
312225 .header ("Content-Type" , "application/json" )
@@ -320,14 +233,15 @@ public static void patchAddBetToMainUser(Long userId, Long gameId, String betTea
320233 System .out .println ("Response Body: " + response .body ());
321234 }
322235
236+ @ Getter
323237 private static ArrayList <BotBase > bots = new ArrayList <>();
324238
325- public static ArrayList < BotBase > getBots () {
326- return bots ;
327- }
328-
239+ /**
240+ * Creates an array of BotBase objects from the server.
241+ * @throws Exception if an error occurs during retrieval.
242+ * */
329243 public static void createBotArray () throws Exception {
330- HttpClient client = HttpClient . newHttpClient ();
244+ HttpClient client = new Client (). httpClient ();
331245
332246 HttpRequest request = HttpRequest .newBuilder ()
333247 .uri (URI .create ("http://localhost:9090/users" ))
@@ -353,15 +267,22 @@ public static void createBotArray() throws Exception {
353267 }
354268 }
355269
270+ /**
271+ * Starts all the bots in the array.
272+ * @throws Exception if an error occurs during retrieval.
273+ */
356274 public static void launchBots () throws Exception {
357275 createBotArray ();
358276 for (BotBase bot : bots ) {
359277 bot .startBot ();
360278 }
361279 }
362280
363-
364-
281+ /**
282+ * Launches bots and the JavaFX UI
283+ * @param args the command line arguments.
284+ * @throws Exception if an error occurs during retrieval.
285+ * */
365286 public static void main (String [] args ) throws Exception {
366287 launchBots ();
367288 Application .launch (UI .class , args );
0 commit comments