Beispiel #1
0
        /// <summary>
        /// Send setup messages to the newly connected client.
        /// Tell the client they are in the lobby,
        /// who is in the lobby,
        /// and what tables are up.
        /// Finally, start the listening thread for the client.
        /// </summary>
        /// <param name="client"></param>
        public void sendConnectionMsg(GameClient client)
        {
            /** We should be locked from start listening */
            //if the client is not in the client data structure yet, add it.
            if (!clients.Contains(client.getUserName()))
            {
                clients.Add(client.getUserName(), client);
            }
            //first join the lobby.
            client.joinLobby();
            //get everyone else in the lobby.
            string lobbyList = "";

            foreach (object o in clients.Values)
            {
                GameClient gc = (GameClient)o;
                if (gc.inLobby())
                {
                    lobbyList += gc.getUserName() + " ";
                }
            }
            client.sendMessage(Message.WHO_IN_LOBBY + " " + lobbyList);
            //get all the tables.
            string tids = "";

            foreach (int i in tables.Keys)
            {
                tids += i.ToString() + " ";
            }
            client.sendMessage(Message.TBL_LIST + " " + tids.Trim());
            //tell everyone this client joined the lobby.
            this.alertJoinLobby(client);
            //finally, start the listening thread for the game client to hear incoming requests.
            client.Start();
        }
Beispiel #2
0
 private void broadcastMsg(string fromUser, string msg)
 {
     foreach (object o in clients.Values)
     {
         GameClient gc = (GameClient)o;
         if (gc.inLobby())
         {
             if (gc.isConnected())
             {
                 gc.sendMessage(Message.MSG + " " + fromUser + " 0 " + msg);
             }
             else
             {
                 dcList.Add(gc);
             }
         }
     }
     clearClosedClients();
 }
Beispiel #3
0
        public void alertPlayerDisconnected(GameClient c)
        {
            //we have to locked from an above method
            string name = c.getUserName();

            //first check if the user is observing a table. If so, remove the user from observer list.
            if (c.isObserving())
            {
                this.stopObservingAll(c);
            }
            //next check if the user is on a table. If so, tell the table the user is leaving.
            if (tables.Count > 0)
            {
                foreach (GameTable tbl in tables.Values)
                {
                    GameClient[] players = tbl.getPlayers();
                    if (
                        (players[0] != null &&
                         c.getUserName().Equals(players[0].getUserName()))
                        ||
                        (players[1] != null &&
                         c.getUserName().Equals(players[1].getUserName()))
                        )
                    {
                        putOffTable(c, tbl, true);
                        break;
                    }
                }
            }
            //now see if the user is in the lobby. If so, tell everyone he is leaving.
            if (c.inLobby())
            {
                alertLeaveLobby(c);
            }
            //now free up the username and remove the client instance from memory.
            if (clients.Contains(name))
            {
                clients.Remove(name);
                // c.setConnected(false);
            }
            this.gui.output("User " + name + " has left the server.");
        }
Beispiel #4
0
        /// <summary>
        /// Process a message receieved from a game client.
        /// </summary>
        /// <param name="client">The GameClient instance that sent the message</param>
        /// <param name="msg">The TCP message code</param>
        /// <param name="args">A list of the arguments for the message</param>
        public void processMessage(GameClient client, int msg, List <string> args)
        {
            string text = "";

            lock (this) {
                try {
                    switch (msg)
                    {
                    case Message.MSG_ALL:            //client sends message <1> to everyone in lobby
                        if (client.inLobby())
                        {
                            //when we broadcast a message, each word is in the args.
                            foreach (string s in args)
                            {
                                text += s + " ";
                            }
                        }
                        broadcastMsg(client.getUserName(), text);
                        break;

                    case Message.MSG_C:             //client sends message <3> to client <1>.
                        for (int i = 1; i < args.Count; i++)
                        {
                            text += args[i] + " ";
                        }
                        sendMsg(client.getUserName(), args[0], text);
                        break;

                    case Message.MAKE_TBL:          //client wants to make and sit at a table
                        if (client.inLobby())
                        {
                            createTable(client);
                        }
                        else
                        {
                            client.sendMessage(Message.NOT_IN_LOBBY.ToString());
                        }
                        break;

                    case Message.JOIN_TBL:          //client wants to join table id <1>
                        if (!client.inLobby())
                        {
                            client.sendMessage(Message.NOT_IN_LOBBY.ToString());
                        }
                        else
                        {
                            int p1 = -1;
                            try {
                                p1 = int.Parse(args[0]);
                            }
                            catch (Exception) {
                                client.sendMessage(Message.BAD_MESSAGE.ToString());
                                return;
                            }
                            joinTable(client, p1);
                        }
                        break;

                    case Message.READY:             //client is ready for game to start
                        sendReady(client);
                        break;

                    case Message.MOVE:              //client moves from <1> to <2>
                        sendMove(client,
                                 args[0].Substring(0, 1) + "," + args[0].Substring(2, 1),
                                 args[1].Substring(0, 1) + "," + args[1].Substring(2, 1));
                        break;

                    case Message.LEAVE_TBL:         //client leaves the table
                        if (!client.inLobby())
                        {
                            leaveTable(client);
                        }
                        else
                        {
                            client.sendMessage(Message.ERR_IN_LOBBY.ToString());
                        }
                        break;

                    case Message.ASK_TBL_STATUS:        //client is asking for the status of table <1>
                        int tid = -1;
                        try {
                            tid = int.Parse(args[0]);
                        }
                        catch (Exception) {
                            client.sendMessage(Message.BAD_MESSAGE.ToString());
                            return;
                        }
                        sayWhoOnTbl(client, tid);
                        break;

                    /** part 4 new messages **/
                    case Message.OBSERVE_TBL:
                        // if (client.inLobby())
                        addObserver(client, args[0]);
                        //else client.sendMessage(Message.NOT_IN_LOBBY);
                        break;

                    case Message.STOP_OBSERVING:
                        if (client.isObserving())
                        {
                            stopObserving(client, args[0], false);
                        }
                        else
                        {
                            client.sendMessage(Message.NOT_OBSERVING);
                        }
                        break;

                    case Message.REGISTER:
                        registerClient(client, args[0]);
                        break;

                    case Message.LOGIN:
                        loginClient(client, args[0]);
                        break;

                    case Message.UPDATE_PROFILE:
                        text = "";
                        foreach (string s in args)
                        {
                            text += s + " ";
                        }
                        updateProfile(client, text);
                        break;

                    case Message.GET_PROFILE:
                        getProfile(client, args[0]);
                        break;

                    case Message.QUIT:              //client leaves the server
                        alertPlayerDisconnected(client);
                        break;
                    }
                }
                catch (Exception) {
                    //the message, or some parameter was bad.
                    client.sendMessage(Message.BAD_MESSAGE);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Process a message receieved from a game client.
        /// </summary>
        /// <param name="client">The GameClient instance that sent the message</param>
        /// <param name="msg">The TCP message code</param>
        /// <param name="args">A list of the arguments for the message</param>
        public void processMessage(GameClient client, int msg, List<string> args)
        {
            string text = "";
            lock (this) {
                try {
                    switch (msg) {
                        case Message.MSG_ALL:        //client sends message <1> to everyone in lobby
                            if (client.inLobby())
                                //when we broadcast a message, each word is in the args.
                                foreach (string s in args)
                                    text += s + " ";
                            broadcastMsg(client.getUserName(), text);
                            break;
                        case Message.MSG_C:         //client sends message <3> to client <1>.
                            for (int i = 1; i < args.Count; i++)
                                text += args[i] + " ";
                            sendMsg(client.getUserName(), args[0], text);
                            break;
                        case Message.MAKE_TBL:      //client wants to make and sit at a table
                            if (client.inLobby())
                                createTable(client);
                            else client.sendMessage(Message.NOT_IN_LOBBY.ToString());
                            break;
                        case Message.JOIN_TBL:      //client wants to join table id <1>
                            if (!client.inLobby())
                                client.sendMessage(Message.NOT_IN_LOBBY.ToString());
                            else {
                                int p1 = -1;
                                try {
                                    p1 = int.Parse(args[0]);
                                }
                                catch (Exception) {
                                    client.sendMessage(Message.BAD_MESSAGE.ToString());
                                    return;
                                }
                                joinTable(client, p1);
                            }
                            break;
                        case Message.READY:         //client is ready for game to start
                            sendReady(client);
                            break;
                        case Message.MOVE:          //client moves from <1> to <2>
                            sendMove(client,
                                args[0].Substring(0, 1) + "," + args[0].Substring(2, 1),
                                args[1].Substring(0,1) + "," + args[1].Substring(2, 1));
                            break;
                        case Message.LEAVE_TBL:     //client leaves the table
                            if (!client.inLobby())
                                leaveTable(client);
                            else client.sendMessage(Message.ERR_IN_LOBBY.ToString());
                            break;
                        case Message.ASK_TBL_STATUS:    //client is asking for the status of table <1>
                             int tid = -1;
                                try {
                                    tid = int.Parse(args[0]);
                                }
                                catch (Exception) {
                                    client.sendMessage(Message.BAD_MESSAGE.ToString());
                                    return;
                                }
                                sayWhoOnTbl(client, tid);
                            break;
                        /** part 4 new messages **/
                        case Message.OBSERVE_TBL:
                           // if (client.inLobby())
                                addObserver(client, args[0]);
                            //else client.sendMessage(Message.NOT_IN_LOBBY);
                            break;
                        case Message.STOP_OBSERVING:
                            if(client.isObserving())
                                stopObserving(client, args[0], false);
                            else client.sendMessage(Message.NOT_OBSERVING);
                            break;
                      /* case Message.REGISTER:
                            registerClient(client, args[0]);
                            break;
                        case Message.LOGIN:
                            loginClient(client, args[0]);
                            break;
                        case Message.UPDATE_PROFILE:
                            text = "";
                            foreach (string s in args)
                                text += s + " ";
                            updateProfile(client, text);
                            break;
                        case Message.GET_PROFILE:
                            getProfile(client, args[0]);
                            break;*/

                        case Message.QUIT:          //client leaves the server
                            alertPlayerDisconnected(client);
                            break;
                    }
                }
                catch (Exception) {
                    //the message, or some parameter was bad.
                    client.sendMessage(Message.BAD_MESSAGE);
                }
            }
        }
Beispiel #6
0
 public void alertPlayerDisconnected(GameClient c)
 {
     //we have to locked from an above method
     string name = c.getUserName();
     //first check if the user is observing a table. If so, remove the user from observer list.
     if (c.isObserving()) {
         this.stopObservingAll(c);
     }
     //next check if the user is on a table. If so, tell the table the user is leaving.
     if (tables.Count > 0) {
         foreach (GameTable tbl in tables.Values) {
             GameClient[] players = tbl.getPlayers();
             if (
                 (players[0] != null &&
                 c.getUserName().Equals(players[0].getUserName()))
                                         ||
                 (players[1] != null &&
                 c.getUserName().Equals(players[1].getUserName()))
             ) {
                 putOffTable(c, tbl, true);
                 break;
             }
         }
     }
     //now see if the user is in the lobby. If so, tell everyone he is leaving.
     if (c.inLobby())
         alertLeaveLobby(c);
     //now free up the username and remove the client instance from memory.
     if (clients.Contains(name)) {
        clients.Remove(name);
        // c.setConnected(false);
     }
     this.gui.output("User " + name + " has left the server.");
 }