Esempio n. 1
0
 //observers..
 public void addObserver(GameClient c)
 {
     if(!observers.ContainsKey(c.getUserName())){
         this.observers.Add(c.getUserName(), c);
     }
     if (curGame != null) {
          c.sendMessage(Message.BOARD_STATE + " " +this.tid.ToString()+" "+ curGame.getBoardState());
     }
 }
Esempio n. 2
0
 public GameTable(int tableId)
 {
     gameHistroy = new Dictionary<int, CheckersGame>();
     observers = new Dictionary<string, GameClient>();
     tid = tableId;
     seatsOpen = 2;
     black = null;
     red = null;
     curGame = null;
 }
Esempio n. 3
0
 /// <summary>
 /// Broadcasts to everyone that c joined the lobby.
 /// </summary>
 /// <param name="c"></param>
 public void alertJoinLobby(GameClient c)
 {
     //we have to be locked from an above method.
     foreach (object o in clients.Values) {
         GameClient gc = (GameClient)o;
         if (!gc.Equals(c)) {
             if (gc.isConnected()) {
                 gc.sendMessage(Message.NOW_IN_LOBBY + " " + c.getUserName());
             } else dcList.Add(gc);
         }
     }
     clearClosedClients();
 }
Esempio n. 4
0
 private void stopObservingAll(GameClient c)
 {
     List<int> observing = c.getObserving();
     if (observing.Count > 0) {
         foreach (int tid in observing)
             this.stopObserving(c, tid.ToString(), true);
     }
 }
Esempio n. 5
0
 private void stopObserving(GameClient c, string tableId, bool disconnecting)
 {
     int tid;
     try {
         tid = int.Parse(tableId);
     } catch (Exception) {
         c.sendMessage(Message.BAD_MESSAGE);
         return;
     }
     if (tables.ContainsKey(tid)) {
         GameTable t = tables[tid];
         t.removeObserver(c);
         c.stopObserving(tid);
         //c.joinLobby();
         //if (!disconnecting)
         //    alertJoinLobby(c);
     } else c.sendMessage(Message.TBL_NOT_EXIST);
 }
Esempio n. 6
0
 /// <summary>
 /// send rdy alert to checkers game
 /// </summary>
 /// <param name="c"></param>
 private void sendReady(GameClient c)
 {
     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()))) {
             tbl.clientReady(c);
             return;
         }
     }
 }
Esempio n. 7
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.");
 }
Esempio n. 8
0
 public void sendMove(GameClient c, string from, string to)
 {
     curGame.tsMakeMove(c, from, to);
 }
Esempio n. 9
0
 /// <summary>
 /// Removes a player from the table
 /// </summary>
 /// <param name="c">the player instance to remove</param>
 public void nowLeaving(GameClient c)
 {
     if (black != null && black.Equals(c)) {
         black = null;
         if(red != null)
             red.sendMessage(Message.OPP_LEFT_TABLE.ToString());
         seatsOpen++;
     }
     else if (red != null && red.Equals(c)) {
         red = null;
         if(black != null)
             black.sendMessage(Message.OPP_LEFT_TABLE.ToString());
         seatsOpen++;
     }
     if (curGame != null) {
         //if a game was instanciated, tell the game a player quit.
         //This will make the game close properly.
         curGame.tsPlayerLeft(c);
     }
 }
Esempio n. 10
0
        /**
         * All clients will be constantly updated about the status of the lobby, regardless of whether they are in
         * the game or not.
         */
        ///<summary> ejects the game client passed from its table.</summary>
        private void leaveTable(GameClient c)
        {
            //find the table they are in and tell them the client is leaving.
            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, false);
                    return;
                }
            }
            //else cant find the player in any table.
            c.sendMessage(Message.TBL_NOT_EXIST);
        }
Esempio n. 11
0
 //verify the client can join the table - see if tid has open seat
 private void joinTable(GameClient c, int tid)
 {
     if (c.isLoggedIn()) {
         if (tables.Count > 0) {
             foreach (int i in tables.Keys) {
                 if (i == tid) {
                     GameTable t = tables[i];
                     if (t.joinTable(c)) {
                         putOnTable(c, tid);
                         if (!t.hasOpenSeat()) {
                             t.createGame();
                         }
                     }
                     else {
                         //we couldnt join the table. It must be full.
                         c.sendMessage(Message.TBL_FULL.ToString());
                     }
                     return;
                 }
             }
             //else we iterated over all the tables, and couldnt find the id.
             c.sendMessage(Message.TBL_NOT_EXIST.ToString());
         }
         else
             c.sendMessage(Message.TBL_NOT_EXIST.ToString());
     }
     else c.sendMessage(Message.LOGIN_FAIL);
 }
Esempio n. 12
0
 private void createTable(GameClient client)
 {
     if (client.isLoggedIn()) {
         Random r = new Random();
         while (true) {
             int tid = r.Next(1001, 5000);
             bool exists = false;
             if (tables.Count == 0) {
                 tables.Add(tid, new GameTable(tid));
                 alertNewTable(tid);
                 joinTable(client, tid);
                 return;
             }
             else {
                 foreach (int i in tables.Keys) {
                     if (i == tid) {
                         exists = true;
                         break;
                     }
                 }
                 if (!exists) {
                     tables.Add(tid, new GameTable(tid));
                     alertNewTable(tid);
                     joinTable(client, tid);
                     return;
                 }
             }
         }
     }
     else client.sendMessage(Message.LOGIN_FAIL);
 }
Esempio n. 13
0
 private void addObserver(GameClient c, string tableId)
 {
     //if (c.isLoggedIn()) {
         int tid;
         try {
             tid = int.Parse(tableId);
         }
         catch (Exception) {
             c.sendMessage(Message.BAD_MESSAGE);
             return;
         }
         if (tables.ContainsKey(tid)) {
             GameTable t = tables[tid];
             //c.leaveLobby();
             //alertLeaveLobby(c);
             c.observeTable(tid);
             //give the player the latest update about the table its about to observe.
             sayWhoOnTbl(c, tid);
             t.addObserver(c);
         }
         else c.sendMessage(Message.TBL_NOT_EXIST);
     //} c.sendMessage(Message.LOGIN_FAIL);
 }
Esempio n. 14
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();
 }
Esempio n. 15
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);
                }
            }
        }
Esempio n. 16
0
 public void clearClosedClient(GameClient client)
 {
     FileLogger.writeToExceptionLog("clearClosedClient removing client " + client.getUserName());
     this.alertPlayerDisconnected(client);
 }
Esempio n. 17
0
 public bool joinTable(GameClient user)
 {
     bool joinOK = false;
     lock(this){
         if (seatsOpen == 2) {
             if (coin.Next(1, 3) == 1)
                 black = user;
             else red = user;
             --seatsOpen;
             joinOK = true;
         }
         else if (seatsOpen == 1) {
             if (black == null)
                 black = user;
             else red = user;
             --seatsOpen;
             joinOK = true;
         }
         else {
             joinOK = false;
         }
         return joinOK;
     }
 }
Esempio n. 18
0
 private void putOffTable(GameClient c, GameTable t, bool disconnecting)
 {
     t.nowLeaving(c);
     c.leaveTable(t.getTid());
     alertTableUpdate(t.getTid());
     c.joinLobby();
     if(!disconnecting)
         alertJoinLobby(c);
 }
Esempio n. 19
0
 public void removeObserver(GameClient c)
 {
     if (observers.ContainsKey(c.getUserName()))
         observers.Remove(c.getUserName());
 }
Esempio n. 20
0
 private void putOnTable(GameClient c, int tid)
 {
     c.leaveLobby();
     alertLeaveLobby(c);
     c.joinTable(tid);
     alertTableUpdate(tid);
 }
Esempio n. 21
0
 private void sayWhoOnTbl(GameClient client, int tid )
 {
     GameTable tbl;
         try {
             tbl = tables[tid];
         }
         catch (Exception) {
             client.sendMessage(Message.TBL_NOT_EXIST);
             return;
         }
         if (tbl != null) {
             //the clients <2> <3> are on table with tid <1>. <2> is black. <3> is red. If either is -1, the seat is open.
             GameClient[] players = tbl.getPlayers();
             //red = 0, black = 1.
             string rp = players[0] == null ? "-1" : players[0].getUserName();
             string bp = players[1] == null ? "-1" : players[1].getUserName();
             client.sendMessage(Message.WHO_ON_TBL.ToString() + " " + tbl.getTid() + " " + bp + " " + rp);
         }
 }
Esempio n. 22
0
 //a game client sent a move. We need to send this move to the chceckers game instance.
 private void sendMove(GameClient c, string from, string to)
 {
     try {
         int x = int.Parse(from.Substring(0, 1));
         x = int.Parse(from.Substring(2, 1));
         x = int.Parse(to.Substring(0, 1));
         x = int.Parse(to.Substring(2, 1));
     }
     catch (Exception) {
         c.sendMessage(Message.BAD_MESSAGE);
         return;
     }
     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())))
         {
             tbl.sendMove(c, from, to);
             return;
         }
     }
 }
Esempio n. 23
0
 public void clientReady(GameClient c)
 {
     if (curGame != null) {
         curGame.tsAlertReady(c);
     } else c.sendMessage(Message.GAME_NOT_CREATED);
 }
Esempio n. 24
0
 /// <summary>
 /// Overload of the Equals method. GC's are equal if they have the same user name only.
 /// </summary>        
 public bool Equals(GameClient c)
 {
     return this.getUserName().Equals(c.getUserName());
 }