Ejemplo n.º 1
0
 public void TimesOut(Connection connection, Request request)
 {
     try
     {
         gameLogic.TimesOut(request.LastPlayerWantsToLeave());
         connection.SendMessage(BuildResponse(ResponseCode.Ok));
     }
     catch (TimesOutException e)
     {
         connection.SendMessage(BuildResponse(ResponseCode.GameFinished, e.Message));
         connection.Close();
     }catch (LastPlayerAbandonedGameException e)
     {
         connection.SendMessage(BuildResponse(ResponseCode.BadRequest, e.Message));
         connection.Close();
     }
     catch (RecordNotFoundException e)
     {
         connection.SendMessage(BuildResponse(ResponseCode.NotFound, e.Message));
     }
     catch (ClientNotConnectedException e)
     {
         connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
     }
 }
Ejemplo n.º 2
0
        public void DoAction(Connection connection, Request request)
        {
            try
            {
                Client loggedUser   = CurrentClient(request);
                string usernameFrom = loggedUser.Username;

                string action = request.Action();

                List <string> answer = new List <string>();

                answer = answer.Concat(gameLogic.DoAction(usernameFrom, action)).ToList();
                answer.Insert(0, GetPlayerPosition(loggedUser.Username));

                connection.SendMessage(BuildResponse(ResponseCode.Ok, answer.ToArray()));
            }
            catch (RecordNotFoundException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.NotFound, e.Message));
            }
            catch (ClientNotConnectedException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
            }
            catch (ActionException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.InvalidAction, e.Message));
            }
            catch (BusinessException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.BadRequest, e.Message));
            }
        }
Ejemplo n.º 3
0
        public void Handle(Connection conn)
        {
            while (conn.IsConnected())
            {
                try
                {
                    string[] message = conn.ReadMessage();
                    var      request = new Request(message);

                    switch (request.Command)
                    {
                    case Command.Login:
                        serverController.ConnectClient(conn, request);
                        break;

                    case Command.ListAllSubjects:
                        serverController.ListAllSubjects(conn, request);
                        break;

                    case Command.DisconnectClient:
                        serverController.DisconnectClient(conn, request);
                        break;

                    default:
                        serverController.InvalidCommand(conn);
                        break;
                    }
                }
                catch (Exception e)
                {
                    conn.SendMessage(new string[] { ResponseCode.InternalServerError.ToString(), "There was a problem with the server" });
                    break;
                }
            }
        }
Ejemplo n.º 4
0
        public void ReadyToSendPicture(Connection connection, Request request)
        {
            picture          = string.Empty;
            picturesUsername = request.Username();
            extension        = request.PictureExtension();

            connection.SendMessage(BuildResponse(ResponseCode.Ok));
        }
Ejemplo n.º 5
0
        public void CheckIfGameHasFinished(Connection connection, Request request)
        {
            try
            {
                string response = gameLogic.GetGameResult();

                connection.SendMessage(BuildResponse(ResponseCode.Ok, response));
            }
            catch (RecordNotFoundException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.NotFound, e.Message));
            }
            catch (ClientNotConnectedException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
            }
        }
Ejemplo n.º 6
0
        public void GetResultByTimesOut(Connection connection, Request request)
        {
            try
            {
                List <string> timesOut = gameLogic.GetGameResultByTimeOut();

                connection.SendMessage(BuildResponse(ResponseCode.Ok, timesOut.ToArray()));
            }
            catch (RecordNotFoundException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.NotFound, e.Message));
            }
            catch (ClientNotConnectedException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
            }
        }
Ejemplo n.º 7
0
 public void DisconnectClient(Connection connection, Request request)
 {
     try
     {
         gameLogic.DisconnectClient(request.UserToken());
         connection.SendMessage(BuildResponse(ResponseCode.Ok, "Client disconnected"));
         connection.Close();
     }
     catch (RecordNotFoundException e)
     {
         connection.SendMessage(BuildResponse(ResponseCode.NotFound, e.Message));
     }
     catch (ClientNotConnectedException e)
     {
         connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
     }
 }
Ejemplo n.º 8
0
        public void ConnectClient(Connection connection, Request request)
        {
            try
            {
                var    client = new Client(request.Username(), request.Password());
                string token  = gameLogic.Login(client);

                object[] response = string.IsNullOrEmpty(token)
                    ? BuildResponse(ResponseCode.NotFound, "Client not found. Wrong username or password.")
                    : BuildResponse(ResponseCode.Ok, token);
                connection.SendMessage(response);
            }
            catch (ClientAlreadyConnectedException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.Forbidden, e.Message));
            }
        }
Ejemplo n.º 9
0
        public void SendLastPicturePart(Connection connection, Request request)
        {
            picture += request.Bytes();

            try
            {
                string pathTest = Path.Combine(Environment.CurrentDirectory, picturesUsername + extension);

                var img = Image.FromStream(new MemoryStream(Convert.FromBase64String(picture)));
                img.Save(pathTest);
                connection.SendMessage(BuildResponse(ResponseCode.Ok));
            }
            catch (Exception ex)
            {
                connection.SendMessage(BuildResponse(ResponseCode.BadRequest, ex.Message));
            }
        }
Ejemplo n.º 10
0
        public void RemovePlayerFromGame(Connection connection, Request request)
        {
            try
            {
                Client loggedUser   = CurrentClient(request);
                string usernameFrom = loggedUser.Username;

                List <string> response = gameLogic.RemovePlayerFromGame(usernameFrom);

                connection.SendMessage(BuildResponse(ResponseCode.Ok, response.ToArray()));
            }
            catch (RecordNotFoundException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.NotFound, e.Message));
            }
            catch (ClientNotConnectedException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
            }
        }
Ejemplo n.º 11
0
        public void SelectRole(Connection connection, Request request)
        {
            try
            {
                Client loggedClient = CurrentClient(request);

                string role = request.Role();

                gameLogic.SelectRole(loggedClient, role);

                connection.SendMessage(BuildResponse(ResponseCode.Ok));
            }
            catch (ClientNotConnectedException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
            }
            catch (NoMonstersInGameException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.BadRequest, e.Message));
            }
        }
Ejemplo n.º 12
0
        public void JoinGame(Connection connection, Request request)
        {
            try
            {
                Client loggedUser = CurrentClient(request);

                gameLogic.JoinGame(loggedUser.Username);

                List <string> response = new List <string>();
                response.Add(GetPlayerPosition(loggedUser.Username));

                connection.SendMessage(BuildResponse(ResponseCode.Ok, response.ToArray()));
            }
            catch (ClientNotConnectedException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
            }
            catch (FullGameException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.BadRequest, e.Message));
            }
        }
Ejemplo n.º 13
0
        public void ListConnectedClients(Connection connection, Request request)
        {
            try
            {
                Client        loggedUser = CurrentClient(request);
                List <Client> clients    = gameLogic.GetLoggedClients();

                string[] clientsUsernames =
                    clients.Select(c => c.Username)
                    .ToArray();

                connection.SendMessage(BuildResponse(ResponseCode.Ok, clientsUsernames));
            }
            catch (RecordNotFoundException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.NotFound, e.Message));
            }
            catch (ClientNotConnectedException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
            }
        }
Ejemplo n.º 14
0
        public void ListPlayersInGame(Connection connection, Request request)
        {
            try
            {
                Client        loggedUser     = CurrentClient(request);
                List <Player> connectedUsers = gameLogic.GetLoggedPlayers();

                string[] connectedUsernames =
                    connectedUsers.Where(player => !player.Client.Equals(loggedUser)).Select(c => c.Client.Username)
                    .ToArray();

                connection.SendMessage(BuildResponse(ResponseCode.Ok, connectedUsernames));
            }
            catch (RecordNotFoundException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.NotFound, e.Message));
            }
            catch (ClientNotConnectedException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
            }
        }
Ejemplo n.º 15
0
        public void ListAllSubjects(Connection connection, Request request)
        {
            try
            {
                List <Subject> subjects = logic.GetSubjects();

                string[] subjectsNames = subjects.Select(c => c.Name).ToArray();

                string[] hola = new string[1];
                hola[0] = "hola";

                connection.SendMessage(BuildResponse(ResponseCode.Ok, hola));
            }

            /*  catch (RecordNotFoundException e)
             * {
             *     connection.SendMessage(BuildResponse(ResponseCode.NotFound, e.Message));
             * } */
            catch (ClientNotConnectedException e)
            {
                connection.SendMessage(BuildResponse(ResponseCode.Unauthorized, e.Message));
            }
        }
Ejemplo n.º 16
0
        public void Handle(Connection conn)
        {
            while (conn.IsConnected())
            {
                try
                {
                    string[] message = conn.ReadMessage();
                    var      request = new Request(message);

                    switch (request.Command)
                    {
                    case Command.Login:
                        serverController.ConnectClient(conn, request);
                        break;

                    case Command.DoAction:
                        serverController.DoAction(conn, request);
                        break;

                    case Command.ListPlayersInGame:
                        serverController.ListPlayersInGame(conn, request);
                        break;

                    case Command.ListAllClients:
                        serverController.ListAllClients(conn, request);
                        break;

                    case Command.ListConnectedClients:
                        serverController.ListConnectedClients(conn, request);
                        break;

                    case Command.DisconnectClient:
                        serverController.DisconnectClient(conn, request);
                        break;

                    case Command.JoinGame:
                        serverController.JoinGame(conn, request);
                        break;

                    case Command.SelectRole:
                        serverController.SelectRole(conn, request);
                        break;

                    case Command.TimesOut:
                        serverController.TimesOut(conn, request);
                        break;

                    case Command.RemovePlayerFromGame:
                        serverController.RemovePlayerFromGame(conn, request);
                        break;

                    case Command.CheckIfGameHasFinished:
                        serverController.CheckIfGameHasFinished(conn, request);
                        break;

                    case Command.GetResultByTimesOut:
                        serverController.GetResultByTimesOut(conn, request);
                        break;

                    case Command.ReadyToSendPicture:
                        serverController.ReadyToSendPicture(conn, request);
                        break;

                    case Command.SendPicturePart:
                        serverController.SendPicturePart(conn, request);
                        break;

                    case Command.SendLastPicturePart:
                        serverController.SendLastPicturePart(conn, request);
                        break;

                    default:
                        serverController.InvalidCommand(conn);
                        break;
                    }
                }
                catch (Exception e)
                {
                    conn.SendMessage(new string[] { ResponseCode.InternalServerError.ToString(), "There was a problem with the server" });
                    break;
                }
            }
        }
Ejemplo n.º 17
0
 public void InvalidCommand(Connection connection)
 {
     object[] response = BuildResponse(ResponseCode.BadRequest, "Unrecognizable command");
     connection.SendMessage(response);
 }
Ejemplo n.º 18
0
 public void SendPicturePart(Connection connection, Request request)
 {
     picture += request.Bytes();
     connection.SendMessage(BuildResponse(ResponseCode.Ok));
 }