-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerSender.java
More file actions
92 lines (86 loc) · 2.63 KB
/
ServerSender.java
File metadata and controls
92 lines (86 loc) · 2.63 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
import java.io.PrintStream;
// Continuously reads from message queue for a particular client,
// forwarding to the client.
public class ServerSender extends Thread {
private MessageQueue queue;
private PrintStream client;
private ClientTable table;
private ScoreboardTable scoreboardTable ;
/**
*
* @param queue - message queue where all data is stored
* @param client - the client
* @param table - the table with all users
* @param scoreboardTable - scoreboard table
*/
public ServerSender(MessageQueue queue, PrintStream client, ClientTable table, ScoreboardTable scoreboardTable) {
this.queue = queue;
this.client = client;
this.table = table;
this.scoreboardTable = scoreboardTable;
}
public void run() {
while (true) {
// take the message from the queue
Message msg = queue.take();
/*
* if message contains yes -
* split the message and send the opponent and the message itself to the client
*/
if (msg.toString().contains("Yes")) {
String opponent = msg.toString().substring(4);
client.println(msg);
client.println(opponent);
}
/*
* if message is no -
* only print the message to the client
*/
else if(msg.toString().equals("No")){
client.println(msg);
}
/*
* if the message contains GetUsers -
* split the message and send the message and the string with all users
*/
else if(msg.toString().contains("GetUsers")){
String nickname = msg.toString().substring(8);
client.println(msg);
client.println(table.getUsers(nickname));
}
/*
* if message is scoreboard -
* send the message and the string with the results of the scoreboard
*/
else if(msg.toString().equals("Scoreboard")){
client.println(msg);
client.println(scoreboardTable.getScoreboard());
}
/*
* if message is exit -
* send only the message
*/
else if(msg.toString().equals("Exit")){
client.println(msg);
}
/*
* if the message contains the question -
* send the opponent and the message itself
*/
else if (msg.toString().contains("Do you want to play?")) {
System.out.println("Question send");
String opponent = msg.toString().substring(20);
client.println(msg);
client.println(opponent);
}
/*
* if the message is not a specific one -
* send only the message
*/
else {
client.println(msg);
client.println("Already given.");
}
}
}
}