Esempio n. 1
0
 public Models.Response messageProcessor(Models.Message message, Socket socket)
 {
     if (message.Type == "ClientMessage")
     {
         Models.ClientMessage cm = message.Value.ToObject <Models.ClientMessage>();
         return(clientCommandProcessor(cm, socket));
     }
     else if (message.Type == "GameMessage")
     {
         Models.GameMessage gm = message.Value.ToObject <Models.GameMessage>();
         return(gameCommandProcessor(gm, socket));
     }
     return(null);
 }
        // PROCESSING A CLIENTDISCONNECTED EVENT //

        private static void clientDisconnected(Socket clientSocket)
        {
            try
            {
                Console.WriteLine("Client [" + IPAddress.Parse(((IPEndPoint)clientSocket.RemoteEndPoint).Address.ToString()) + "] disconnected");
                // if the user that disconnected was logged in
                if (!ctrl.checkIfUserIsGuest(clientSocket))
                {
                    Models.Response response = new Models.Response();

                    // we notify every client to remove the user from their lobby
                    response.socketsToNotify          = ctrl.lobbyRoomData.lobbyRoomUsersToSocketList();
                    response.responseCommandToSockets = "REMOVELOBBYROOMUSER";
                    response.commandStringArgumentsToSockets.Add(ctrl.lobbyRoomData.getNickNameBySocket(clientSocket));

                    Models.ClientMessage cm = new Models.ClientMessage(
                        response.responseCommandToSockets,
                        response.commandStringArgumentsToSockets
                        );

                    response.socketsToNotify.Remove(clientSocket);
                    foreach (Socket s in response.socketsToNotify)
                    {
                        send(Models.Message.Serialize(Models.Message.FromValue(cm)), s);
                    }
                    ctrl.removeClient(clientSocket);
                }
                Storage.removeStorage(clientSocket);
                clientSocket.Close();
                clientSocket.Dispose();
            }
            catch (Exception ex)
            {
                logger.Log(ex.ToString());
            }
        }
Esempio n. 3
0
        private Models.Response clientCommandProcessor(Models.ClientMessage message, Socket sender)
        {
            Models.Response response;

            switch (message.Command)
            {
            case "LOGINREQUEST":
                response = ctrlLogInAndRegistration.processLogInRequest(message, sender);
                break;

            case "FORCELOGOUTREQUEST":
                response = ctrlLogInAndRegistration.processForceLogOutRequest(message, sender);
                break;

            case "REGISTERREQUEST":
                response = ctrlLogInAndRegistration.processRegisterRequest(message, sender);
                break;

            case "GETLOBBYROOMUSERS":
                response = ctrlLobby.processGetLobbyRoomUsers(sender);
                break;

            case "FETCHUSERDATA":
                response = ctrlLobby.processFetchUserData(sender);
                break;

            case "NEWCHATMESSAGE":
                response = ctrlLobby.processNewChatMessage(message, sender);
                break;

            case "GETCARDCOLLECTION":
                response = ctrlLobby.processGetCardCollection(sender);
                break;

            case "LEAVELOBBY":
                response = ctrlLobby.processLeaveLobby(message, sender);
                break;

            case "JOINLOBBY":
                response = ctrlLobby.processJoinLobby(sender);
                break;

            case "GETGAMEROOMS":
                response = ctrlLobby.processGetGameRooms(sender);
                break;

            case "CREATEGAMEROOM":
                response = ctrlLobby.processCreateGameRoom(sender);
                break;

            case "CLOSEROOM":
                response = ctrlLobby.processCloseGameRoom(message, sender);
                break;

            case "JOINROOM":
                response = ctrlLobby.processJoinGameRoom(message, sender);
                break;

            case "LEAVEROOM":
                response = ctrlLobby.processLeaveGameRoom(message, sender);
                break;

            case "SETREADY":
                response = ctrlLobby.processSetReady(message, sender);
                break;

            case "GETDECKS":
                response = ctrlCollection.processGetDecks(sender);
                break;

            case "DELETEDECK":
                response = ctrlCollection.processDeleteDeck(message);
                break;

            case "GETDECKLIST":
                response = ctrlPreGameRoom.processGetDeckList(sender);
                break;

            case "READYTOSTART":
                response = ctrlOnGoingGame.processReadyToStart(message, sender);
                break;

            case "FETCHDECK":
                response = ctrlOnGoingGame.processFetchDeck(message, sender);
                break;

            case "GETHAND":
                response = ctrlOnGoingGame.processGetHand(message, sender);
                break;

            case "GETINITIALGAMESTATE":
                response = ctrlOnGoingGame.processGetFirstGameState(message, sender);
                break;

            case "SETROOMSTATE":
                response = ctrlLobby.processSetRoomState(message);
                break;

            case "CREATEDECK":
                response = ctrlCollection.processCreateDeck(message, sender);
                break;

            case "UPDATEDECK":
                response = ctrlCollection.processUpdateDeck(message);
                break;

            default:
                response = null;
                break;
            }

            if (response != null)
            {
                response.Type = 1;
            }
            return(response);
        }
        // ACTIONS TAKEN AFTER RECEIVING DATA FROM CLIENTS //

        private static void processResponseMessage(Models.Response response)
        {
            try
            {
                if (response != null)
                {
                    if (response.Type == 1)
                    {
                        Models.ClientMessage cm;

                        if (response.responseCommandToSender != null)
                        {
                            cm = new Models.ClientMessage(
                                response.responseCommandToSender,
                                response.commandStringArgumentsToSender,
                                response.commandIntArgumentsToSender,
                                response.CardCollection
                                );
                            send(Models.Message.Serialize(Models.Message.FromValue(cm)), response.sender);
                        }

                        if (response.responseCommandToSockets != null)
                        {
                            cm = new Models.ClientMessage(
                                response.responseCommandToSockets,
                                response.commandIntArgumentsToSockets,
                                response.commandStringArgumentsToSockets
                                );
                            foreach (Socket s in response.socketsToNotify)
                            {
                                send(Models.Message.Serialize(Models.Message.FromValue(cm)), s);
                            }
                        }
                    }
                    else
                    {
                        Models.GameMessage gm;

                        if (response.responseCommandToSender != null)
                        {
                            gm = new Models.GameMessage(
                                response.responseCommandToSender,
                                response.commandStringArgumentsToSender,
                                response.commandIntArgumentsToSender);
                            send(Models.Message.Serialize(Models.Message.FromValue(gm)), response.sender);
                        }

                        if (response.responseCommandToSockets != null)
                        {
                            gm = new Models.GameMessage(
                                response.responseCommandToSockets,
                                response.commandStringArgumentsToSockets,
                                response.commandIntArgumentsToSockets
                                );
                            foreach (Socket s in response.socketsToNotify)
                            {
                                send(Models.Message.Serialize(Models.Message.FromValue(gm)), s);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logger.Log(e.ToString());
            }
        }