-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
94 lines (84 loc) · 2.31 KB
/
Server.java
File metadata and controls
94 lines (84 loc) · 2.31 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
import java.util.*;
/*
model in mvc
stores all users and conversations
listens for sends
updates appropriate windows on send
those windows are listening for update command
*/
public class Server {
List<User> userList = new ArrayList<>();
HashMap<ParticipantGroup, Chat> chatList = new HashMap<>();
WindowManager wm = new WindowManager(this);
Server(){
wm.openAdminWindow();
}
public void hostChat(ParticipantGroup p, User initiator) {
Chat c;
ParticipantGroup checkedPG = checkIfUniquePG(p);
if (checkedPG == p) {
c = new Chat(this, p);
chatList.put(p, c);
}
else {
p = checkedPG;
c = chatList.get(p);
}
wm.openChatWindow(c, initiator);
}
private ParticipantGroup checkIfUniquePG(ParticipantGroup pgToCheck) {
for(ParticipantGroup hostedGroup : chatList.keySet()) {
if(hostedGroup.participants.equals(pgToCheck.participants)) {
return hostedGroup;
}
}
return pgToCheck;
}
public void sendMessage(User author, String content, Chat chat) {
Message newMessage = new Message(author,content);
chat.addMessage(newMessage);
wm.redrawWindowsInvolvingUser(author);
}
public String[] ListUsernames(){
ArrayList<String> usernames = new ArrayList<>();
for (User u : userList) {
usernames.add(u.getName());
}
return usernames.toArray(new String[0]);
}
public String[] ListUsernames(User loo){ // leaves one out
ArrayList<String> unames = new ArrayList<>();
for (User u : userList) {
if(u != loo) {
unames.add(u.getName());
}
}
return unames.toArray(new String[0]);
}
public void addUser(User u) {
userList.add(u);
u.client = new Client(u, this);
}
public void deleteUser(User u) {
deleteUser(u.getName());
}
public void deleteUser(String s) {
for(User u : userList) {
if(u.getName().equals(s)) {
updateDeletedUserChats(u);
userList.remove(u);
}
}
}
public void updateDeletedUserChats(User u){
// does nothing right now
}
public User getUser(String s) {
for (User u : userList) {
if (u.getName().equals(s)) {
return u;
}
}
return new User(s,this); // this should never happen
}
}