-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatUser.java
More file actions
142 lines (130 loc) · 6.17 KB
/
ChatUser.java
File metadata and controls
142 lines (130 loc) · 6.17 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
import java.io.*;
import java.net.*;
public class ChatUser{
//socket used to connect to user
public Socket connectionSocket;
//input and output streams from the socket
public DataInputStream userInput;
public DataOutputStream outputToUser;
//nickname and userID
public String nickname;
public String userID = "temp";
//room
public String room = "lobby";
//variables counting time since last ping
private long lastPing = System.currentTimeMillis();
private int pingTimer = 0;
//equals override
@Override
public boolean equals(Object user){
//i wanted this to check based on a chatuser or a string so i can use it for direct messages
// and checking duplicate nicknames with ArrayList's contains() method
//turns out it doesnt work like that for like anything
//keeping the string part of that around just in case it becomes useful later
//anyway we start by checking if the object compared is a chatuser
if(user instanceof ChatUser){
//create variable compared equal to user if it was a chat user
ChatUser compared = (ChatUser) user;
//check if the user's nickname is equal, if it is, return true, otherwise return false
if(compared.nickname.equals(this.nickname)){
return true;
} else {
return false;
}
} else { // end chat user check
//start string check
if (user instanceof String) {
//second verse, same as the first
//make a variable to return the user argument as a string
String compared = (String) user;
//if it equals the nickname, it's the same, otherwise, it isnt
if(compared.equals(this.nickname)){
return true;
} else {
return false;
}
} else { //end string check
//return false if it isn't a chat user or a string
return false;
} //end if
}// end else
}// end equals
//so there are like, timer classes and stuff in java.util, but they all use a lot of threading
//and im already using a *lot* of threading, to the point im a bit worried about memory usage
//so instead of using that, i'm gonna pull a trick from gamedev and just make a delta variable
//turns out because all the other threads are running halting expressions that still requires
// that i make a thread, but its still probably better to have
// one thread than 20
//tl;dr: this is used to keep track of how long it's been since a ping
public synchronized void timerUpdate(){
//set a variable to the current time so it stays consistent
long currTime = System.currentTimeMillis();
//increase the ping timer to however long it's been since the last ping
pingTimer = pingTimer + (int) (currTime - this.lastPing);
//update the timer since the last ping
lastPing = currTime;
//return total time since last ping
return;
}
//usually this trick is used to keep track of how much time has passed between frames so that you can
// make sure your processes aren't determined on how quickly the frames are rendering
//not actually relevant i just thought it was cool
//while i'm adding useless trivia in the comments i may as well add that i coded the first iteration of
// this on a whim while i was supposed to be programming the first draft
// of the expanded message parsing system to handle metadata and stuff
//which meant that since i wanted to set up my git commits in a manner approaching reasonable i couldnt
// actually test it until i had a more basic version of the switch case
// trees done
//what they dont tell you about adhd is that it gives a flat debuff to your ability to implement best
// practice while coding
//properly synchronize timer reset
public synchronized void timerReset(){
pingTimer = 0;
lastPing = System.currentTimeMillis();
}
public synchronized int pingValue(){
return pingTimer;
}
//synchronized setters and getters for the nickname
public synchronized String nickname(){
return nickname;
}
public synchronized void name(String name){
this.nickname = name;
}
//synchronized setters and getters for the room
public synchronized String room(){
return room;
}
public synchronized void newRoom(String newRoom){
this.room=newRoom;
}
//constructor method
//i should probably say something about this but for the life of me i can't figure out what
//it takes strings and a connection and then it makes the variables equal to those
//youre a smart cookie you can figure it out
public ChatUser(Socket connection, String name, String room) throws IOException{
this.connectionSocket = connection;
this.userInput = new DataInputStream(this.connectionSocket.getInputStream());
this.outputToUser = new DataOutputStream(this.connectionSocket.getOutputStream());
this.nickname = name;
this.room = room;
}
//overloaded constructor to very quickly get around the fact that contains doesn't work like i thought it did
public ChatUser(String name){
this.connectionSocket = null;
this.userInput = null;
this.outputToUser = null;
this.nickname = name;
this.room = null;
}
public void reUseUser(Socket connection, String name, String room) throws IOException{
this.connectionSocket = connection;
this.userInput = new DataInputStream(this.connectionSocket.getInputStream());
this.outputToUser = new DataOutputStream(this.connectionSocket.getOutputStream());
this.nickname = name;
this.room = room;
this.lastPing=System.currentTimeMillis();
this.pingTimer=0;
}
}