-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriviaClient.java
More file actions
130 lines (88 loc) · 4.23 KB
/
TriviaClient.java
File metadata and controls
130 lines (88 loc) · 4.23 KB
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
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class TriviaClient {
public static void main(String[] args) {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter username:");
String username = stdIn.readLine();
while (true) {
System.out.println("Enter command (start, quest, leaderboard, answer [A/B/C/D]):");
String command = stdIn.readLine();
if ("start".equalsIgnoreCase(command.trim())) {
startGame(username);
} else if ("quest".equalsIgnoreCase(command.trim())) {
fetchQuestion(username);
} else {
// For simplicity, keep other commands as is
handleOtherCommands(command);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void startGame(String username) throws IOException {
String url = "http://127.0.0.1:8000/api/start";
String urlParameters = "username=" + URLEncoder.encode(username, StandardCharsets.UTF_8.toString());
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
URL startUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) startUrl.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
conn.setUseCaches(false);
try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
wr.write(postData);
}
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("Server says: " + response.toString());
}
private static void fetchQuestion(String username) throws IOException {
String url = "http://127.0.0.1:8000/api/quest";
String urlParameters = "username=" + URLEncoder.encode(username, StandardCharsets.UTF_8.toString());
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
int postDataLength = postData.length;
URL questUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) questUrl.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
conn.setUseCaches(false);
try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
wr.write(postData);
}
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("Server says: " + response.toString());
}
private static void handleOtherCommands(String command) {
// Handle other commands as needed
System.out.println("Command: " + command);
}
}