So I made some improvements to the chatroom server I wrote based on the code from the text game I'm developing.
Here's some of the changes I've made:
- Created users persist between server instances
- Connections are now stored in a set instead of a meaningless mapping from addresses
- Usernames are much easier to read
- Messages you send aren't broadcast back to you, so conversations flow properly
- The "people online" counter updates properly when users disconnect
- Server messages are broadcast to users when others leave or join the chatroom
- Server output is more comprehensive and detailed
- Better documentation and lots of code cleanup - no references to "gamesessions" or suspiciously-named "IOStreams"
Within the code, I've tried to make it very clear that each UserSession
instance is always at some point of a state machine. The callback to handle being sent line data (UserSession.lineReceived(self, line)
) does very little except checking the current state of the UserSession
and sending off the received line to a relevant function. It's practically at a point where a dictionary lookup for the relevant method would be just as easy as the if-elif blocks to implement.
The UserSession
is turning into a bit of a God Object, but it makes the state-machine model very easy to work with - so I'm gonna go out on a limb and say that it's probably more trouble than it's worth to refactor the whole thing into smaller chunks. Really, it's still pretty small.
The persistent user-registration was one of the easiest things to implement: each time the server starts, the users file is read; and each time a new user is created, it's overwritten. It uses Pickle to serialise the dictionary, and writes to/reads from a local binary file.
So now I've got some new TODOS:
- Implement a
/nick
command to change the current connection's username - Look into writing a client program to smooth out the experience and get rid of the usability problems with Telnet
- Store some statistics about users, like total number of messages sent
- Make the users file an absolute filepath
- Implement a
/help
command - Implement a
/me
command - linked to this, look into bolding and other terminal control features over Telnet. Think about compatability with a client program.