Chat System - User Levels

Introduction: This tutorial is the seventeenth in my Java Network Programming using KryoNet series, or twelfth in creating a chat client and server system, in which we are going to be adding a user level system in to the server in preparation for the next tutorial (making requests from clients to admins). Previous: In the previous tutorial we added private message features to the client. The System: We are going to give the user a GUI to interact with the system - to send messages and see the currently connected members of the chat. When a client connects, add them to a list. Send incoming messages to everyone within the client list except the sender - or send them one back saying that it is received, as confirmation. This Tutorial: This tutorial we are going to add user levels to the server so we can have admins on the server in preparation for the next couple of tutorials. So first we want to create a new level variable in our CustomClient class on our server...
  1. private int id, level;
Then we want to create a new method to return the level for later on...
  1. public int getLevel() {
  2.         return this.level;
  3. }
Now we want to add a new command to the server commands method. This command is /admin followed by the username of the client to make an admin...
  1. else if(user.toLowerCase().startsWith("/admin")) {
  2.         String args[] = user.split(" ");
  3.         if (args.length == 2) {
  4.                 clientHandler.makeAdmin(args[1]);
  5.         }else
  6.                 System.out.println("Incorrect number of specified tokens. Please use /admin {username}...");
  7. }
So the above if selection statement checks if there is a username only supplied, if there is then it parses it to a new makeAdmin method in the ClientHandler class. Lets make this now...
  1. public void makeAdmin(String username) {
  2.         for (CustomClient c : this.clients) {
  3.                 if (c.getUsername().equalsIgnoreCase(username)) {
  4.                         c.editLevel(2);
  5.                         break;
  6.                 }
  7.         }
  8. }
Finally we want to make the editLevel method in our CustomClient class. This simply changes the level of the client...
  1. public void editLevel(int newLevel) {
  2.         this.level = newLevel;
  3. }
Now we are ready to use user levels in the next couple of tutorials...

Add new comment