Skip to content

Commit d3eb7a7

Browse files
committed
Word hint updating, and system for game messages
1 parent d7e0583 commit d3eb7a7

File tree

5 files changed

+125
-16
lines changed

5 files changed

+125
-16
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
import java.awt.event.MouseEvent;
1212
import java.awt.event.MouseListener;
1313
import java.awt.event.MouseMotionListener;
14+
import java.awt.font.TextAttribute;
1415
import java.awt.image.BufferedImage;
1516
import java.util.ArrayList;
17+
import java.util.HashMap;
18+
import java.util.Map;
1619

1720
import javax.swing.BorderFactory;
1821
import javax.swing.BoxLayout;
@@ -29,6 +32,8 @@ public class Board extends JPanel {
2932
private Client client;
3033
private Canvas canvas;
3134

35+
private JLabel wordLabel;
36+
3237
private boolean isDrawing;
3338

3439
public Board(Client cl, int WIDTH, int HEIGHT) {
@@ -65,24 +70,29 @@ public void actionPerformed(ActionEvent e) {
6570

6671
drawingMenu.add(clearButton);
6772

68-
canvas.setBorder(BorderFactory.createLineBorder(Color.BLACK, 10));
6973

74+
75+
canvas.setBorder(BorderFactory.createLineBorder(Color.BLACK, 10));
76+
7077
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
7178
this.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
7279
this.add(canvas);
7380
this.add(drawingMenu);
81+
wordLabel = new JLabel("WORD GOES HERE");
82+
Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
83+
attributes.put(TextAttribute.TRACKING, 0.5);
84+
wordLabel.setFont(getFont().deriveFont(20.0f).deriveFont(attributes));
85+
this.add(wordLabel);
7486
}
7587

7688
public boolean isDrawing() { return this.isDrawing; }
7789
public void setDrawing(boolean drawing) { this.isDrawing = drawing; }
7890
public Canvas getCanvas() { return this.canvas; }
7991

8092
protected void addWordPhrase(String phrase) {
81-
JLabel jl = new JLabel(phrase.substring(0, 1).toUpperCase() + phrase.substring(1, phrase.length()));
82-
jl.setFont(getFont().deriveFont(20.0f));
83-
this.add(jl);
84-
this.revalidate();
85-
this.repaint();
93+
wordLabel.setText(phrase.substring(0, 1).toUpperCase() + phrase.substring(1, phrase.length()));
94+
wordLabel.revalidate();
95+
wordLabel.repaint();
8696
}
8797

8898
private JButton getColorButton(Color c) {
@@ -266,10 +276,12 @@ public void mousePressed(MouseEvent e) {
266276
public void mouseReleased(MouseEvent e) {
267277
isMouseDown = false;
268278
e.consume();
269-
try {
270-
client.getNetworkHandler().sendBoard(canvas.getDrawing());
271-
} catch(Exception er) {
272-
System.out.println(er);
279+
if(isDrawing) {
280+
try {
281+
client.getNetworkHandler().sendBoard(canvas.getDrawing());
282+
} catch(Exception er) {
283+
System.out.println(er);
284+
}
273285
}
274286
}
275287

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

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,11 +591,12 @@ protected void startDrawing() {
591591
System.out.println("Error fetching words: " + e);
592592
}
593593
}
594-
String chosenWord = this.wordList.get((int) (this.wordList.size() * (Math.random())));
594+
this.chosenWord = this.wordList.get((int) (this.wordList.size() * (Math.random())));
595595
while(this.wordsGuessed.contains(chosenWord)) {
596596
chosenWord = this.wordList.get((int) (this.wordList.size() * (Math.random())));
597597
}
598598
this.board.addWordPhrase(this.chosenWord);
599+
try { new Thread(new GameTimer()).start(); } catch(Exception e) { }
599600
}
600601
}
601602

@@ -851,4 +852,50 @@ private void fatalError(String message, Exception e) {
851852
protected NetworkHandler getNetworkHandler() { return this.networkHandler; }
852853
protected Board getBoard() { return this.board; }
853854
protected String getUsername() { return this.username; }
855+
856+
protected class GameTimer implements Runnable {
857+
public void run() {
858+
long startime = java.time.Instant.now().toEpochMilli();
859+
860+
StringBuilder sb1 = new StringBuilder();
861+
StringBuilder sb2 = new StringBuilder();
862+
int in = -1;
863+
do {
864+
in = (int) (Math.random() * chosenWord.length());
865+
} while(in < 0 && chosenWord.charAt(in) == ' ');
866+
867+
for(int i = 0; i < chosenWord.length(); i++) {
868+
if(chosenWord.charAt(i) == ' ') {
869+
sb1.append(" ");
870+
sb2.append(" ");
871+
} else {
872+
sb1.append("_");
873+
if(i == in) sb2.append(chosenWord.charAt(i));
874+
else sb2.append("_");
875+
}
876+
}
877+
878+
try {
879+
System.out.println("SENT sb1 " + sb1.toString());
880+
networkHandler.sendWordUpdate(sb1.toString());
881+
} catch(Exception e) {}
882+
883+
boolean sent = false;
884+
885+
while(java.time.Instant.now().toEpochMilli() < startime + 30000) {
886+
887+
try {
888+
if(!sent && java.time.Instant.now().toEpochMilli() > startime + 15000) {
889+
System.out.println("SENT sb2 " + sb2.toString());
890+
networkHandler.sendWordUpdate(sb2.toString());
891+
sent = true;
892+
}
893+
Thread.sleep(1000);
894+
} catch(Exception e) {}
895+
}
896+
897+
System.out.println("Finished gametimer");
898+
899+
}
900+
}
854901
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,14 @@ private void handlePacket(ReceivedPacket packet) throws JSONException {
224224

225225
private void handleRawGameDataPack(ReceivedPacket packet) {
226226
if(packet.getData().has("message") && !packet.getData().getString("message").isBlank()) {
227-
client.updateMessages(packet.getData().getString("message"));
227+
if(!packet.getData().has("type")) {
228+
client.updateMessages(packet.getData().getString("message"));
229+
} else {
230+
231+
if(packet.getData().getString("type").equalsIgnoreCase("word")) {
232+
this.client.getBoard().addWordPhrase(packet.getData().getString("message"));
233+
}
234+
}
228235
} else /* if(packet.getData().has("image")) */ {
229236
if(client.getBoard().getCanvas() != null) {
230237
GameDataPack gdp = GameDataPack.fromJSON(packet.getData());
@@ -471,6 +478,23 @@ protected synchronized void sendMessage(String msg) throws IOException, JSONExce
471478
this.setWriting(false);
472479
}
473480

481+
protected synchronized void sendWordUpdate(String msg) throws IOException, JSONException, InterruptedException, ExecutionException, TimeoutException {
482+
this.setWriting(true);
483+
484+
485+
GameDataPack gdp = new GameDataPack(msg, "word");
486+
487+
if (!client.inPrivate) {
488+
sendDataPackage(gdp, MessageType.GAME_DATA);
489+
System.out.println("Sent word update");
490+
}
491+
else {
492+
sendToClientsFriendsPriv(gdp);
493+
}
494+
495+
this.setWriting(false);
496+
}
497+
474498
protected synchronized void sendBoard(BufferedImage image) throws IOException, JSONException, InterruptedException, ExecutionException, TimeoutException {
475499
this.setWriting(true);
476500

server/src/main/java/dev/abhay7/skribbl/server/Server.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ private void disconnectAndTerminateUser() {
305305

306306
private void handleDataPackage(DataPackage dataPackage) {
307307

308-
System.out.println("Received Data Package: " + dataPackage);
308+
if(!(dataPackage instanceof KeepAlivePack)) System.out.println("Received Data Package: " + dataPackage);
309309

310310
if (this.verified && dataPackage != null && dataPackage.isServerRequest()) {
311311

@@ -399,7 +399,12 @@ private void handleDataPackage(DataPackage dataPackage) {
399399
try {
400400
GameDataPack gdp = ((GameDataPack) dataPackage);
401401
if(!gdp.getMessage().isBlank()) {
402-
this.getCurrentLobby().notifyAllExceptSender(new GameDataPack(false, this.getUsername() + ": " + gdp.getMessage()), this, MessageType.GAME_DATA);
402+
if(gdp.getType().isBlank()) {
403+
this.getCurrentLobby().notifyAllExceptSender(new GameDataPack(false, this.getUsername() + ": " + gdp.getMessage()), this, MessageType.GAME_DATA);
404+
} else {
405+
System.out.println("Sending " + gdp.getType());
406+
this.getCurrentLobby().notifyAllExceptSender(new GameDataPack(gdp.getMessage(), gdp.getType()), this, MessageType.GAME_DATA);
407+
}
403408
} else if(gdp.getImage() != null) {
404409
this.getCurrentLobby().notifyAllExceptSender(new GameDataPack(false, gdp.getImage()), this, MessageType.GAME_DATA);
405410
}

server/src/main/java/dev/abhay7/skribbl/server/datapacks/GameDataPack.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313

1414
public class GameDataPack extends DataPackage {
1515

16+
private String type = "";
1617
private String message = "";
1718
private BufferedImage image;
1819

1920
public GameDataPack(String message) {
2021
this(true, message);
2122
}
2223

24+
public GameDataPack(String message, String type) {
25+
this(true, message, type);
26+
}
27+
2328
public GameDataPack(BufferedImage image) {
2429
this(true, image);
2530
}
@@ -34,6 +39,12 @@ public GameDataPack(boolean isServerRequest, BufferedImage image) {
3439
this.image = image;
3540
}
3641

42+
public GameDataPack(boolean isServerRequest, String message, String type) {
43+
super(isServerRequest);
44+
this.message = message;
45+
this.type = type;
46+
}
47+
3748
public static GameDataPack fromJSON(String json) throws JSONException {
3849
return fromJSON(new JSONObject(json));
3950
}
@@ -49,8 +60,13 @@ public static GameDataPack fromJSON(JSONObject jo) throws JSONException {
4960
System.out.println(ioe);
5061
return null;
5162
}
52-
} else if(jo.has("message") && !jo.getString("message").isBlank()) {
53-
return new GameDataPack(isServerRequest, jo.getString("message"));
63+
}
64+
if(jo.has("message") && !jo.getString("message").isBlank()) {
65+
if(jo.has("type")) {
66+
return new GameDataPack(jo.getString("message"), jo.getString("type"));
67+
} else {
68+
return new GameDataPack(isServerRequest, jo.getString("message"));
69+
}
5470
}
5571

5672
return null;
@@ -76,10 +92,15 @@ public JSONObject toJSON() throws JSONException {
7692
}
7793
}
7894

95+
if(!this.type.isBlank()) {
96+
jo.put("type", this.type);
97+
}
98+
7999
return jo;
80100
}
81101

82102
public String getMessage() { return this.message; }
103+
public String getType() { return this.type; }
83104
public BufferedImage getImage() { return this.image; }
84105

85106
}

0 commit comments

Comments
 (0)