Skip to content

Commit ae0ca8a

Browse files
committed
scoring system
1 parent d3eb7a7 commit ae0ca8a

File tree

3 files changed

+158
-91
lines changed

3 files changed

+158
-91
lines changed

client/src/main/java/dev/abhay7/skribbl/client/Client.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,21 @@ public class Client {
5555
private String inGameTitle;
5656

5757

58-
private Map<String, Integer> currentPlayersMap;
58+
public Map<String, Integer> currentPlayersMap;
5959
private ArrayList<String> currentPlayersList;
6060
private ArrayList<String> playersWhoHavePlayed;
6161
private ArrayList<String> wordsGuessed;
6262

63-
private String chosenWord = "";
63+
public String chosenWord = "";
64+
public volatile String guessedWord = "";
6465

6566
private ArrayList<String> wordList;
6667

6768
private JPanel currentlyDisplayedPanel;
68-
private JPanel usersPanel;
69+
public JPanel usersPanel;
70+
public JScrollPane usersScrollPane;
6971
private JTextArea chatPanel;
72+
public JTextField messageArea;
7073
private Board board;
7174

7275
private Socket socket;
@@ -79,6 +82,7 @@ public class Client {
7982
private byte openDialogs;
8083

8184
public volatile boolean inPrivate;
85+
public volatile long startime = java.time.Instant.now().toEpochMilli();
8286

8387
public Map<String, PWDSignalSession> privateSessions;
8488

@@ -463,7 +467,7 @@ private JPanel getGamePanel(String lobName, Map<String, Integer> players, boolea
463467
usersPanel.add(new JLabel((p.equals(this.username) ? p + " (You)" : p) + " - " + players.get(p) + " Points"));
464468
}
465469

466-
JScrollPane usersScrollPane = new JScrollPane(usersPanel);
470+
usersScrollPane = new JScrollPane(usersPanel);
467471
usersScrollPane.setPreferredSize(new Dimension(WIDTH / 5, HEIGHT * 7 / 10));
468472
usersScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
469473
usersScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
@@ -491,7 +495,7 @@ private JPanel getGamePanel(String lobName, Map<String, Integer> players, boolea
491495
JPanel sendMessagePanel = new JPanel();
492496
sendMessagePanel.setLayout(new FlowLayout());
493497

494-
JTextField messageArea = new JTextField();
498+
messageArea = new JTextField();
495499
messageArea.setPreferredSize(new Dimension(WIDTH / 6, HEIGHT * 1 / 20));
496500
messageArea.addActionListener((_) -> {
497501
sendMessage(messageArea, chatPanel);
@@ -558,6 +562,7 @@ private void sendMessage(JTextField messageArea, JTextArea chatPanel) {
558562

559563
@Override
560564
protected Void doInBackground() throws Exception {
565+
guessedWord = messageArea.getText();
561566
networkHandler.sendMessage(messageArea.getText());
562567
return null;
563568
}
@@ -855,7 +860,7 @@ private void fatalError(String message, Exception e) {
855860

856861
protected class GameTimer implements Runnable {
857862
public void run() {
858-
long startime = java.time.Instant.now().toEpochMilli();
863+
startime = java.time.Instant.now().toEpochMilli();
859864

860865
StringBuilder sb1 = new StringBuilder();
861866
StringBuilder sb2 = new StringBuilder();

client/src/main/java/dev/abhay7/skribbl/client/Main.java

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.abhay7.skribbl.client;
22

3-
import java.nio.charset.StandardCharsets;
3+
// import java.nio.charset.StandardCharsets;
44

55
import javax.swing.Box;
66
import javax.swing.BoxLayout;
@@ -12,7 +12,7 @@
1212

1313
import com.formdev.flatlaf.FlatIntelliJLaf;
1414

15-
import dev.abhay7.skribbl.client.jameskwong.pwdsignal.PWDSignalSession;
15+
// import dev.abhay7.skribbl.client.jameskwong.pwdsignal.PWDSignalSession;
1616

1717
public class Main {
1818

@@ -28,122 +28,122 @@ public static void main(String[] args) {
2828
return;
2929
}
3030

31-
try {
32-
System.out.println("TEST CASE 1 -- NORMAL OPERATIONS");
33-
PWDSignalSession alice = new PWDSignalSession("Test123", true);
31+
// try {
32+
// System.out.println("TEST CASE 1 -- NORMAL OPERATIONS");
33+
// PWDSignalSession alice = new PWDSignalSession("Test123", true);
3434

35-
PWDSignalSession bob = new PWDSignalSession("Test123", false);
35+
// PWDSignalSession bob = new PWDSignalSession("Test123", false);
3636

37-
byte[] alicePayload1 = alice.createPayload1();
38-
byte[] bobPayload1 = bob.createPayload1();
37+
// byte[] alicePayload1 = alice.createPayload1();
38+
// byte[] bobPayload1 = bob.createPayload1();
3939

40-
alice.acceptPayload1(bobPayload1, 0);
41-
bob.acceptPayload1(alicePayload1, 0);
40+
// alice.acceptPayload1(bobPayload1, 0);
41+
// bob.acceptPayload1(alicePayload1, 0);
4242

43-
// payload 1 done
43+
// // payload 1 done
4444

45-
byte[] alicePayload2 = alice.createPayload2();
46-
byte[] bobPayload2 = bob.createPayload2();
45+
// byte[] alicePayload2 = alice.createPayload2();
46+
// byte[] bobPayload2 = bob.createPayload2();
4747

48-
alice.acceptPayload2(bobPayload2, 0);
49-
bob.acceptPayload2(alicePayload2, 0);
48+
// alice.acceptPayload2(bobPayload2, 0);
49+
// bob.acceptPayload2(alicePayload2, 0);
5050

51-
// Payload 2 done
51+
// // Payload 2 done
5252

53-
byte[] alicePayload3 = alice.createPayload3();
54-
byte[] bobPayload3 = bob.createPayload3();
53+
// byte[] alicePayload3 = alice.createPayload3();
54+
// byte[] bobPayload3 = bob.createPayload3();
5555

56-
alice.acceptPayload3(bobPayload3, 0);
57-
bob.acceptPayload3(alicePayload3, 0);
56+
// alice.acceptPayload3(bobPayload3, 0);
57+
// bob.acceptPayload3(alicePayload3, 0);
5858

59-
System.out.println("Alice state: " + alice.getState());
60-
System.out.println("Bob state: " + bob.getState());
59+
// System.out.println("Alice state: " + alice.getState());
60+
// System.out.println("Bob state: " + bob.getState());
6161

62-
System.out.println();
62+
// System.out.println();
6363

64-
String message = "Hi bob! I'm alice";
65-
byte[] messageBytes = message.getBytes();
66-
byte[] messageBytesEncrypted = alice.encryptSendPacket(messageBytes, 0, messageBytes.length);
64+
// String message = "Hi bob! I'm alice";
65+
// byte[] messageBytes = message.getBytes();
66+
// byte[] messageBytesEncrypted = alice.encryptSendPacket(messageBytes, 0, messageBytes.length);
6767

68-
System.out.println("Original message: " + new String(messageBytes, StandardCharsets.UTF_8));
69-
System.out.println();
70-
System.out.println("Encrypted message: " + new String(messageBytesEncrypted, StandardCharsets.UTF_8));
71-
System.out.println();
68+
// System.out.println("Original message: " + new String(messageBytes, StandardCharsets.UTF_8));
69+
// System.out.println();
70+
// System.out.println("Encrypted message: " + new String(messageBytesEncrypted, StandardCharsets.UTF_8));
71+
// System.out.println();
7272

73-
byte[] messageBytesDecrypted = bob.decryptReceivePacket(messageBytesEncrypted, 0, messageBytesEncrypted.length);
74-
System.out.println("Bob's POV: " + new String(messageBytesDecrypted, StandardCharsets.UTF_8));
75-
System.out.println();
73+
// byte[] messageBytesDecrypted = bob.decryptReceivePacket(messageBytesEncrypted, 0, messageBytesEncrypted.length);
74+
// System.out.println("Bob's POV: " + new String(messageBytesDecrypted, StandardCharsets.UTF_8));
75+
// System.out.println();
7676

77-
message = "Yo whats good Alice?";
78-
messageBytes = message.getBytes();
79-
messageBytesEncrypted = bob.encryptSendPacket(messageBytes, 0, messageBytes.length);
77+
// message = "Yo whats good Alice?";
78+
// messageBytes = message.getBytes();
79+
// messageBytesEncrypted = bob.encryptSendPacket(messageBytes, 0, messageBytes.length);
8080

81-
System.out.println("Original message: " + new String(messageBytes, StandardCharsets.UTF_8));
82-
System.out.println();
83-
System.out.println("Encrypted message: " + new String(messageBytesEncrypted, StandardCharsets.UTF_8));
84-
System.out.println();
81+
// System.out.println("Original message: " + new String(messageBytes, StandardCharsets.UTF_8));
82+
// System.out.println();
83+
// System.out.println("Encrypted message: " + new String(messageBytesEncrypted, StandardCharsets.UTF_8));
84+
// System.out.println();
8585

86-
messageBytesDecrypted = alice.decryptReceivePacket(messageBytesEncrypted, 0, messageBytesEncrypted.length);
87-
System.out.println("Alice's POV: " + new String(messageBytesDecrypted, StandardCharsets.UTF_8));
88-
System.out.println();
86+
// messageBytesDecrypted = alice.decryptReceivePacket(messageBytesEncrypted, 0, messageBytesEncrypted.length);
87+
// System.out.println("Alice's POV: " + new String(messageBytesDecrypted, StandardCharsets.UTF_8));
88+
// System.out.println();
8989

90-
System.out.println("\n\nTEST CASE 2 -- REFLECTION ATTACK");
90+
// System.out.println("\n\nTEST CASE 2 -- REFLECTION ATTACK");
9191

92-
// attacker replays Bob's message back to him
92+
// // attacker replays Bob's message back to him
9393

94-
try {
95-
byte[] reflectionAttack = bob.decryptReceivePacket(messageBytesEncrypted, 0, messageBytesEncrypted.length);
96-
}
97-
catch (Exception e) {
98-
System.out.println("Error ocurred during Reflection attack occurred: " + e.getMessage());
99-
}
94+
// try {
95+
// byte[] reflectionAttack = bob.decryptReceivePacket(messageBytesEncrypted, 0, messageBytesEncrypted.length);
96+
// }
97+
// catch (Exception e) {
98+
// System.out.println("Error ocurred during Reflection attack occurred: " + e.getMessage());
99+
// }
100100

101-
System.out.println("\n\nTEST CASE 3 -- REPLAY ATTACK");
101+
// System.out.println("\n\nTEST CASE 3 -- REPLAY ATTACK");
102102

103103

104-
try {
105-
byte[] replayAttack = alice.decryptReceivePacket(messageBytesEncrypted, 0, messageBytesEncrypted.length);
106-
}
107-
catch (Exception e) {
108-
System.out.println("Error ocurred during replay attack occurred: " + e.getMessage());
109-
}
104+
// try {
105+
// byte[] replayAttack = alice.decryptReceivePacket(messageBytesEncrypted, 0, messageBytesEncrypted.length);
106+
// }
107+
// catch (Exception e) {
108+
// System.out.println("Error ocurred during replay attack occurred: " + e.getMessage());
109+
// }
110110

111-
System.out.println("\n\nTEST CASE 4 -- WRONG PASSWORD");
111+
// System.out.println("\n\nTEST CASE 4 -- WRONG PASSWORD");
112112

113-
alice = new PWDSignalSession("Test123", true);
113+
// alice = new PWDSignalSession("Test123", true);
114114

115-
bob = new PWDSignalSession("Bruh", false);
115+
// bob = new PWDSignalSession("Bruh", false);
116116

117-
alicePayload1 = alice.createPayload1();
118-
bobPayload1 = bob.createPayload1();
117+
// alicePayload1 = alice.createPayload1();
118+
// bobPayload1 = bob.createPayload1();
119119

120-
alice.acceptPayload1(bobPayload1, 0);
121-
bob.acceptPayload1(alicePayload1, 0);
120+
// alice.acceptPayload1(bobPayload1, 0);
121+
// bob.acceptPayload1(alicePayload1, 0);
122122

123-
// payload 1 done
123+
// // payload 1 done
124124

125-
alicePayload2 = alice.createPayload2();
126-
bobPayload2 = bob.createPayload2();
125+
// alicePayload2 = alice.createPayload2();
126+
// bobPayload2 = bob.createPayload2();
127127

128-
alice.acceptPayload2(bobPayload2, 0);
129-
bob.acceptPayload2(alicePayload2, 0);
128+
// alice.acceptPayload2(bobPayload2, 0);
129+
// bob.acceptPayload2(alicePayload2, 0);
130130

131-
// Payload 2 done
131+
// // Payload 2 done
132132

133-
alicePayload3 = alice.createPayload3();
134-
bobPayload3 = bob.createPayload3();
133+
// alicePayload3 = alice.createPayload3();
134+
// bobPayload3 = bob.createPayload3();
135135

136-
alice.acceptPayload3(bobPayload3, 0);
137-
bob.acceptPayload3(alicePayload3, 0);
136+
// alice.acceptPayload3(bobPayload3, 0);
137+
// bob.acceptPayload3(alicePayload3, 0);
138138

139-
System.out.println("Alice state: " + alice.getState());
140-
System.out.println("Bob state: " + bob.getState());
139+
// System.out.println("Alice state: " + alice.getState());
140+
// System.out.println("Bob state: " + bob.getState());
141141

142-
System.out.println();
143-
}
144-
catch (Exception e) {
145-
System.out.println("Error occurred during wrong password attack: " + e.getMessage());
146-
}
142+
// System.out.println();
143+
// }
144+
// catch (Exception e) {
145+
// System.out.println("Error occurred during wrong password attack: " + e.getMessage());
146+
// }
147147
}
148148

149149
private static String[] promptForStartupData() {
@@ -170,14 +170,14 @@ private static String[] promptForStartupData() {
170170
String username = usernameField.getText().trim();
171171
String serverInput = serverField.getText().trim();
172172

173-
if (!username.isEmpty() && serverInput.matches("^.+:\\d+$") && username.length() < 15) {
173+
if (!username.isEmpty() && serverInput.matches("^.+:\\d+$") && username.length() < 15 && username.indexOf(":") < 0) {
174174
String[] parts = serverInput.split(":");
175175

176176
toRet[0] = username;
177177
toRet[1] = parts[0];
178178
toRet[2] = Integer.parseInt(parts[1]) + "";
179179
} else {
180-
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a username (less than 15 characters) and server in IP:PORT format.", "Input Error", JOptionPane.ERROR_MESSAGE);
180+
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a username (less than 15 characters) without a : and server in IP:PORT format.", "Input Error", JOptionPane.ERROR_MESSAGE);
181181
}
182182
} else {
183183
JOptionPane.showMessageDialog(null, "User did not proceed with connection.");

client/src/main/java/dev/abhay7/skribbl/client/NetworkHandler.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.util.concurrent.TimeUnit;
1818
import java.util.concurrent.TimeoutException;
1919

20+
import javax.swing.JLabel;
21+
2022
import org.json.JSONException;
2123
import org.json.JSONObject;
2224

@@ -225,11 +227,58 @@ private void handlePacket(ReceivedPacket packet) throws JSONException {
225227
private void handleRawGameDataPack(ReceivedPacket packet) {
226228
if(packet.getData().has("message") && !packet.getData().getString("message").isBlank()) {
227229
if(!packet.getData().has("type")) {
230+
if(client.getBoard().isDrawing()) {
231+
String guess = packet.getData().getString("message").substring(packet.getData().getString("message").indexOf(":") + 2);
232+
if(client.chosenWord.equalsIgnoreCase(guess)) {
233+
try {
234+
new Thread(() -> {
235+
try {
236+
sendGameDataPack(new GameDataPack("SUCCESS_GUESS:" + guess + ":" + (int) ((30000 - (java.time.Instant.now().toEpochMilli() - this.client.startime)) / 100), "guess"));
237+
} catch(Exception e) { System.err.println(e); }
238+
}).start();
239+
} catch(Exception e) { System.err.println(e); }
240+
}
241+
}
228242
client.updateMessages(packet.getData().getString("message"));
229243
} else {
230-
231244
if(packet.getData().getString("type").equalsIgnoreCase("word")) {
232245
this.client.getBoard().addWordPhrase(packet.getData().getString("message"));
246+
} else if(packet.getData().getString("type").equalsIgnoreCase("guess")) {
247+
if(packet.getData().getString("message").split(":")[0].equals("SUCCESS_GUESS") && packet.getData().getString("message").split(":")[1].equals(client.guessedWord)) {
248+
this.client.updateMessages("GUESS IS CORRECT");
249+
try {
250+
this.client.currentPlayersMap.put(client.username, this.client.currentPlayersMap.get(client.username) + Integer.parseInt(packet.getData().getString("message").split(":")[2]));
251+
this.client.usersPanel.removeAll();
252+
for(String p: this.client.currentPlayersMap.keySet()) {
253+
this.client.usersPanel.add(new JLabel((p.equals(this.client.getUsername()) ? p + " (You)" : p) + " - " + this.client.currentPlayersMap.get(p) + " Points"));
254+
}
255+
this.client.usersPanel.revalidate();
256+
this.client.usersPanel.repaint();
257+
try {
258+
new Thread(() -> {
259+
try {
260+
sendGameDataPack(new GameDataPack(this.client.getUsername() + ":" + this.client.currentPlayersMap.get(this.client.username), "scoreupdate"));
261+
} catch(Exception e) { System.err.println(e); }
262+
}).start();
263+
} catch(Exception e) { System.err.println(e); }
264+
} catch(Exception e) {
265+
System.out.println(e);
266+
}
267+
}
268+
} else if(packet.getData().getString("type").equalsIgnoreCase("scoreupdate")) {
269+
try {
270+
String username = packet.getData().getString("message").split(":")[0];
271+
int newScore = Integer.parseInt(packet.getData().getString("message").split(":")[1]);
272+
this.client.currentPlayersMap.put(username, newScore);
273+
this.client.usersPanel.removeAll();
274+
for(String p: this.client.currentPlayersMap.keySet()) {
275+
this.client.usersPanel.add(new JLabel((p.equals(this.client.getUsername()) ? p + " (You)" : p) + " - " + this.client.currentPlayersMap.get(p) + " Points"));
276+
}
277+
this.client.usersPanel.revalidate();
278+
this.client.usersPanel.repaint();
279+
} catch(Exception e) {
280+
System.err.println(e);
281+
}
233282
}
234283
}
235284
} else /* if(packet.getData().has("image")) */ {
@@ -461,6 +510,19 @@ public synchronized WordsFetchPack getWords() throws IOException, JSONException,
461510
return wfp;
462511
}
463512

513+
protected synchronized void sendGameDataPack(GameDataPack gdp) throws IOException, JSONException, InterruptedException, ExecutionException, TimeoutException {
514+
this.setWriting(true);
515+
516+
if (!client.inPrivate) {
517+
sendDataPackage(gdp, MessageType.GAME_DATA);
518+
}
519+
else {
520+
sendToClientsFriendsPriv(gdp);
521+
}
522+
523+
this.setWriting(false);
524+
}
525+
464526
// Send a message
465527
protected synchronized void sendMessage(String msg) throws IOException, JSONException, InterruptedException, ExecutionException, TimeoutException {
466528
this.setWriting(true);

0 commit comments

Comments
 (0)