-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMindRushServer.java
More file actions
234 lines (218 loc) · 14.2 KB
/
MindRushServer.java
File metadata and controls
234 lines (218 loc) · 14.2 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
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
import java.io.*;
import java.net.*;
import java.util.*;
public class MindRushServer {
private static final int PORT = 5190;
private static Set<ClientHandler> clients = Collections.synchronizedSet(new HashSet<>());
private static HashMap<Integer, Integer>[] currentQuiz = new HashMap[5];
private static final HashMap<String, Integer> correctAnswers = new HashMap<>();
private static final HashMap<String, String[]> quizMap = new HashMap<>();
private static Map<ClientHandler, Set<String>> answeredQuestions = new HashMap<>();
private static List<Integer> questionIndexes;
private static boolean startPlay = false;
private static boolean playing = false;
private static List<String> questionKeys = new ArrayList<>();
private static String current_question;
public static void main(String[] args) throws IOException {
initializeQuizData();
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server started on port " + PORT);
while (true) {
Socket socket = serverSocket.accept();
ClientHandler handler = new ClientHandler(socket);
clients.add(handler);
handler.start();
}
}
private static void initializeQuizData() {
quizMap.put("What is the capital of France?", new String[]{"Rome", "London", "Paris", "Berlin"});
quizMap.put("What is the capital of Germany?", new String[]{"Frankfurt", "Munich", "Berlin", "Hamburg"});
quizMap.put("What is the capital of Italy?", new String[]{"Venice", "Milan", "Rome", "Florence"});
quizMap.put("What is the capital of Spain?", new String[]{"Madrid", "Barcelona", "Seville", "Valencia"});
quizMap.put("What is the capital of Canada?", new String[]{"Vancouver", "Montreal", "Toronto", "Ottawa"});
quizMap.put("Which planet is known as the Red planet?", new String[]{"Mars", "Jupiter", "Venus", "Saturn"});
quizMap.put("Which planet is known as the Blue planet?", new String[]{"Uranus", "Neptune", "Earth", "Saturn"});
quizMap.put("Which planet is known as the Morning Star planet?", new String[]{"Mars", "Mercury", "Jupiter", "Venus"});
quizMap.put("Which planet is known as the Ringed planet?", new String[]{"Jupiter", "Neptune", "Saturn", "Uranus"});
quizMap.put("Which planet is known as the Giant planet?", new String[]{"Earth", "Jupiter", "Saturn", "Neptune"});
quizMap.put("What is the largest ocean on Earth?", new String[]{"Atlantic Ocean", "Indian Ocean", "Pacific Ocean", "Arctic Ocean"});
quizMap.put("What is the tallest mountain in the world?", new String[]{"K2", "Mount Everest", "Kangchenjunga", "Lhotse"});
quizMap.put("What is the longest river in the world?", new String[]{"Amazon River", "Nile River", "Yangtze River", "Mississippi River"});
quizMap.put("What is the smallest country in the world?", new String[]{"Monaco", "San Marino", "Liechtenstein", "Vatican City"});
quizMap.put("What is the largest desert in the world?", new String[]{"Sahara Desert", "Gobi Desert", "Arabian Desert", "Antarctic Desert"});
quizMap.put("Who wrote 'Romeo and Juliet'?", new String[]{"Charles Dickens", "Mark Twain", "Jane Austen", "William Shakespeare"});
quizMap.put("Who painted the Mona Lisa?", new String[]{"Vincent van Gogh", "Pablo Picasso", "Claude Monet", "Leonardo da Vinci"});
quizMap.put("What is the chemical symbol for water?", new String[]{"H2O", "O2", "CO2", "NaCl"});
quizMap.put("What is the speed of light?", new String[]{"150,000 km/s", "450,000 km/s", "600,000 km/s", "300,000 km/s"});
quizMap.put("What is the capital of Japan?", new String[]{"Osaka", "Kyoto", "Nagoya", "Tokyo"});
quizMap.put("What is the largest mammal in the world?", new String[]{"Elephant", "Giraffe", "Hippopotamus", "Blue Whale"});
quizMap.put("What is the smallest planet in the solar system?", new String[]{"Mercury", "Mars", "Venus", "Pluto"});
quizMap.put("What is the hardest natural substance on Earth?", new String[]{"Gold", "Iron", "Diamond", "Platinum"});
quizMap.put("What is the most spoken language in the world?", new String[]{"English", "Mandarin Chinese", "Spanish", "Hindi"});
quizMap.put("What is the currency of the United Kingdom?", new String[]{"Euro", "Dollar", "Pound Sterling", "Yen"});
quizMap.put("What is the capital of Australia?", new String[]{"Sydney", "Melbourne", "Canberra", "Brisbane"});
quizMap.put("What is the largest continent?", new String[]{"Africa", "Asia", "Europe", "North America"});
quizMap.put("What is the boiling point of water?", new String[]{"90°C", "100°C", "110°C", "120°C"});
quizMap.put("What is the freezing point of water?", new String[]{"0°C", "-10°C", "5°C", "-5°C"});
quizMap.put("Who discovered gravity?", new String[]{"Albert Einstein", "Isaac Newton", "Galileo Galilei", "Nikola Tesla"});
quizMap.put("What is the capital of Russia?", new String[]{"Saint Petersburg", "Moscow", "Kazan", "Sochi"});
quizMap.put("What is the national animal of the United States?", new String[]{"Bald Eagle", "Grizzly Bear", "Buffalo", "Cougar"});
quizMap.put("What is the largest island in the world?", new String[]{"Greenland", "Australia", "Madagascar", "Borneo"});
quizMap.put("What is the capital of South Korea?", new String[]{"Busan", "Seoul", "Incheon", "Daegu"});
quizMap.put("What is the chemical symbol for gold?", new String[]{"Au", "Ag", "Fe", "Pb"});
quizMap.put("What is the capital of Brazil?", new String[]{"Rio de Janeiro", "São Paulo", "Brasília", "Salvador"});
quizMap.put("What is the national sport of Canada?", new String[]{"Hockey", "Lacrosse", "Baseball", "Soccer"});
quizMap.put("What is the capital of Egypt?", new String[]{"Cairo", "Alexandria", "Giza", "Luxor"});
quizMap.put("What is the largest organ in the human body?", new String[]{"Heart", "Liver", "Skin", "Lungs"});
quizMap.put("What is the capital of India?", new String[]{"Mumbai", "Delhi", "New Delhi", "Kolkata"});
correctAnswers.put("What is the capital of France?", 2);
correctAnswers.put("What is the capital of Germany?", 2);
correctAnswers.put("What is the capital of Italy?", 2);
correctAnswers.put("What is the capital of Spain?", 0);
correctAnswers.put("What is the capital of Canada?", 3);
correctAnswers.put("Which planet is known as the Red planet?", 0);
correctAnswers.put("Which planet is known as the Blue planet?", 1);
correctAnswers.put("Which planet is known as the Morning Star planet?", 3);
correctAnswers.put("Which planet is known as the Ringed planet?", 2);
correctAnswers.put("Which planet is known as the Giant planet?", 1);
correctAnswers.put("What is the largest ocean on Earth?", 2);
correctAnswers.put("What is the tallest mountain in the world?", 1);
correctAnswers.put("What is the longest river in the world?", 1);
correctAnswers.put("What is the smallest country in the world?", 3);
correctAnswers.put("What is the largest desert in the world?", 3);
correctAnswers.put("Who wrote 'Romeo and Juliet'?", 3);
correctAnswers.put("Who painted the Mona Lisa?", 3);
correctAnswers.put("What is the chemical symbol for water?", 0);
correctAnswers.put("What is the speed of light?", 3);
correctAnswers.put("What is the capital of Japan?", 3);
correctAnswers.put("What is the largest mammal in the world?", 3);
correctAnswers.put("What is the smallest planet in the solar system?", 0);
correctAnswers.put("What is the hardest natural substance on Earth?", 2);
correctAnswers.put("What is the most spoken language in the world?", 1);
correctAnswers.put("What is the currency of the United Kingdom?", 2);
correctAnswers.put("What is the capital of Australia?", 2);
correctAnswers.put("What is the largest continent?", 1);
correctAnswers.put("What is the boiling point of water?", 1);
correctAnswers.put("What is the freezing point of water?", 0);
correctAnswers.put("Who discovered gravity?", 1);
correctAnswers.put("What is the capital of Russia?", 1);
correctAnswers.put("What is the national animal of the United States?", 0);
correctAnswers.put("What is the largest island in the world?", 0);
correctAnswers.put("What is the capital of South Korea?", 1);
correctAnswers.put("What is the chemical symbol for gold?", 0);
correctAnswers.put("What is the capital of Brazil?", 2);
correctAnswers.put("What is the national sport of Canada?", 1);
correctAnswers.put("What is the capital of Egypt?", 0);
correctAnswers.put("What is the largest organ in the human body?", 2);
correctAnswers.put("What is the capital of India?", 2);
for (String question : quizMap.keySet()) {
questionKeys.add(question);
}
}
private static void broadcast(String message) {
synchronized (clients) {
for (ClientHandler client : clients) {
client.out.println(message);
}
}
}
private static class ClientHandler extends Thread {
private Socket socket;
private Scanner in;
private PrintStream out;
private String username;
private int correct;
public ClientHandler(Socket socket) {
this.socket = socket;
}
public void run() {
try {
in = new Scanner(socket.getInputStream());
out = new PrintStream(socket.getOutputStream(), true);
username = in.nextLine();
Random random = new Random();
while (in.hasNextLine()) {
String message = in.nextLine();
if (message.equals("START_CLICKED")) {
System.out.println("Received start from client!");
for (ClientHandler client : clients) {
client.correct = 0;
answeredQuestions.put(client, new HashSet<String>());
}
questionIndexes = new ArrayList<>();
for (int i = 0; i < 5; i++) {
questionIndexes.add(random.nextInt(40));
}
startPlay = true;
playing = true;
new Thread(() -> {
try {
broadcast("START GAME");
for (int i = 0; i < 5; i++) {
// current_question only has the question part of the quizMap hashmap
current_question = questionKeys.get(questionIndexes.get(i));
System.out.println("Broadcasting question: " + current_question);
broadcast("QUESTION:" + current_question + ";" + String.join(";", quizMap.get(current_question)));
Thread.sleep(15000);
}
for (ClientHandler client : clients) {
broadcast("SCORE:" + client.getScore());
}
broadcast("GAME FINISHED");
playing = false;
} catch (InterruptedException e) {
System.out.println("Countdown interrupted");
}
}).start();
} else if (message.equals("NEW_GAME")) {
System.out.println("Received start from client!");
for (ClientHandler client : clients) {
client.correct = 0;
}
questionIndexes = new ArrayList<>();
for (int i = 0; i < 5; i++) {
questionIndexes.add(random.nextInt(40));
}
startPlay = true;
playing = true;
new Thread(() -> {
try {
broadcast("START NEW GAME");
for (int i = 0; i < 5; i++) {
// current_question only has the question part of the quizMap hashmap
current_question = questionKeys.get(questionIndexes.get(i));
System.out.println("Broadcasting question: " + current_question);
broadcast("QUESTION:" + current_question + ";" + String.join(";", quizMap.get(current_question)));
Thread.sleep(15000);
}
for (ClientHandler client : clients) {
broadcast("SCORE:" + client.getScore());
}
broadcast("GAME FINISHED");
playing = false;
} catch (InterruptedException e) {
System.out.println("Countdown interrupted");
}
}).start();
}else if (playing) {
if (correctAnswers.get(current_question) == Integer.parseInt(message) && !answeredQuestions.get(this).contains(current_question)) {
correct++;
answeredQuestions.get(this).add(current_question);
}
}
}
} catch (IOException e) {
System.err.println("Client disconnected: " + e.getMessage());
} finally {
try {
socket.close();
} catch (IOException e) {
System.err.println("Error closing socket: " + e.getMessage());
}
}
}
public String getScore() {
return username + ":" + correct;
}
}
}