Пример #1
0
        public Cuenta(int id)
        {
            this.id = id;
            EngineNetwork.Send(EngineNetwork.CreatePackage(new object[] {
                (byte)NetworkClientRequests.GetAccountData, id
            }));
            var package = EngineNetwork.ReceiveMultipleData();

            if (package.Count < 1)
            {
                return;
            }
            if (!package[0].ContainsKey("code"))
            {
                return;
            }
            else if (byte.Parse(package[0]["code"]) != (byte)NetworkServerResponses.AccountData)
            {
                return;
            }
            package.RemoveAll(x => x.ContainsKey("code"));
            var accountData = package[0];

            if (accountData.Count > 0)
            {
                id               = int.Parse(accountData["idcuenta"]);
                usuario          = accountData["usuario"];
                password         = accountData["password"];
                correo           = accountData["correo"];
                monedas          = int.Parse(accountData["monedas"]);
                fechaCreacion    = DateTime.Parse(accountData["fechaCreacion"]);
                confirmada       = (accountData["confirmado"] == "1") ? true : false;
                codigoValidacion = accountData["codigoValidacion"];
            }
        }
Пример #2
0
        public bool Register()
        {
            if (!string.IsNullOrWhiteSpace(codigoValidacion))
            {
                return(false);
            }
            var registerRequest = EngineNetwork.CreatePackage(new object[] {
                (byte)NetworkClientRequests.Register, usuario, password, correo
            });

            EngineNetwork.Send(registerRequest);
            Dictionary <string, string> packageReceived = EngineNetwork.ReceiveAsDictionary();

            if (packageReceived.Count == 0)
            {
                return(false);
            }
            else if (packageReceived.Count == 1)
            {
                if (byte.Parse(packageReceived["code"]) != (byte)NetworkServerResponses.RegisterSuccess)
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #3
0
        public void SendChatMessage(Partida game, string message)
        {
            var sendChatMessageRequest = EngineNetwork.CreatePackage(new object[] {
                (byte)NetworkClientRequests.SendChatMessage, game.Id, usuario, message
            });

            EngineNetwork.Send(sendChatMessageRequest);
        }
Пример #4
0
        public void Logout()
        {
            var logoutRequest = EngineNetwork.CreatePackage(new object[] {
                (byte)NetworkClientRequests.Logout, id
            });

            EngineNetwork.Send(logoutRequest);
        }
Пример #5
0
 public static void GetGamesList()
 {
     if (cuenta != null)
     {
         EngineNetwork.Send(EngineNetwork.CreatePackage(new object[] {
             (byte)NetworkClientRequests.GetGames
         }));
         var gamesPackage = EngineNetwork.ReceiveMultipleData();
         if (gamesPackage.Count == 0)
         {
             return;
         }
         if (!gamesPackage[0].ContainsKey("code"))
         {
             return;
         }
         else if (byte.Parse(gamesPackage[0]["code"]) != (byte)NetworkServerResponses.GamesList)
         {
             return;
         }
         gamesPackage.RemoveAll(x => x.ContainsKey("code"));
         foreach (var gamePackage in gamesPackage)
         {
             System.Windows.Application.Current.Dispatcher.Invoke(delegate {
                 if (!GamesList.ToList().Exists(x => x.Id == int.Parse(gamePackage["idpartida"])))
                 {
                     var game = new Partida(
                         int.Parse(gamePackage["idpartida"]),
                         int.Parse(gamePackage["ronda"]),
                         Convert.ToDateTime(gamePackage["fecha"])
                         );
                     game.PropertyChanged += Game_PropertyChanged;
                     GamesList.Add(game);
                 }
                 else
                 {
                     GamesList.ToList().Find(x => x.Id == int.Parse(gamePackage["idpartida"])).LoadJugadores();
                 }
             });
         }
         List <Partida> gamesToRemove = new List <Partida>();
         foreach (var game in GamesList)
         {
             var checkGame = gamesPackage.ToList().Find(x => int.Parse(x["idpartida"]) == game.Id);
             if (checkGame == null)
             {
                 gamesToRemove.Add(game);
             }
         }
         System.Windows.Application.Current.Dispatcher.Invoke(delegate {
             gamesToRemove.ForEach(x => GamesList.Remove(x));
         });
     }
 }
Пример #6
0
        public bool LeaveGame(Partida game)
        {
            EngineNetwork.Send(EngineNetwork.CreatePackage(new object[] {
                (byte)NetworkClientRequests.LeaveGame, id, game.Id
            }));
            var packageReceived = EngineNetwork.ReceiveMultipleData();

            if (packageReceived.Count == 0)
            {
                return(false);
            }
            else if (packageReceived.Count == 1)
            {
                if (byte.Parse(packageReceived[0]["code"]) != (byte)NetworkServerResponses.PlayerLeft)
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #7
0
        public bool JoinGame(Partida game)
        {
            var joinGameRequest = EngineNetwork.CreatePackage(new object[] {
                (byte)NetworkClientRequests.JoinGame, id, game.Id,
            });

            EngineNetwork.Send(joinGameRequest);
            List <string> packageReceived = EngineNetwork.Receive();

            if (packageReceived.Count == 0)
            {
                return(false);
            }
            else if (packageReceived.Count == 1)
            {
                if (byte.Parse(packageReceived[0]) != (byte)NetworkServerResponses.PlayerJoined)
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #8
0
        public bool Login(out byte codeResponse)
        {
            var loginRequest = EngineNetwork.CreatePackage(new object[] {
                (byte)NetworkClientRequests.Login, usuario, password
            });

            EngineNetwork.Send(loginRequest);
            Dictionary <string, string> packageReceived = EngineNetwork.ReceiveAsDictionary();

            codeResponse = (byte)NetworkServerResponses.LoginError;
            if (packageReceived.Count == 0)
            {
                return(false);
            }
            else if (packageReceived.Count == 1)
            {
                if (byte.Parse(packageReceived["code"]) == (byte)NetworkServerResponses.LoginError)
                {
                    return(false);
                }
                else if (byte.Parse(packageReceived["code"]) == (byte)NetworkServerResponses.AccountNotConfirmed)
                {
                    codeResponse = (byte)NetworkServerResponses.AccountNotConfirmed;
                    return(false);
                }
            }
            Session.Cuenta.id               = int.Parse(packageReceived["idcuenta"]);
            Session.Cuenta.usuario          = packageReceived["usuario"];
            Session.Cuenta.password         = packageReceived["password"];
            Session.Cuenta.correo           = packageReceived["correo"];
            Session.Cuenta.monedas          = int.Parse(packageReceived["monedas"]);
            Session.Cuenta.fechaCreacion    = DateTime.Parse(packageReceived["fechaCreacion"]);
            Session.Cuenta.confirmada       = (packageReceived["confirmado"] == "1") ? true : false;
            Session.Cuenta.codigoValidacion = packageReceived["codigoValidacion"];
            codeResponse = 1;
            return(true);
        }
Пример #9
0
        public void LoadJugadores()
        {
            jugadores.Clear();
            EngineNetwork.Send(EngineNetwork.CreatePackage(new object[] {
                (byte)NetworkClientRequests.GetGamePlayers, id
            }));
            List <Dictionary <string, string> > players = EngineNetwork.ReceiveMultipleData();

            if (players.Count == 0)
            {
                return;
            }
            if (!players[0].ContainsKey("code"))
            {
                return;
            }
            else if (byte.Parse(players[0]["code"]) != (byte)NetworkServerResponses.GamePlayersList)
            {
                return;
            }
            players.RemoveAll(x => x.ContainsKey("code"));
            foreach (var player in players)
            {
                jugadores.Add(new Jugador(
                                  int.Parse(player["idjugador"]),
                                  new Cuenta(int.Parse(player["idcuenta"])),
                                  this,
                                  int.Parse(player["puntuacion"]),
                                  (player["anfitrion"] == "1") ? true : false
                                  ));
            }
            if (numeroJugadores != jugadores.Count)
            {
                NumeroJugadores = jugadores.Count.ToString();
            }
        }
Пример #10
0
        public bool CreateGame()
        {
            if (Session.GamesList.ToList().Find(x => x.Jugadores.Find(p => p.Cuenta.id == id) != null) != null)
            {
                return(false);
            }
            EngineNetwork.Send(EngineNetwork.CreatePackage(new object[] {
                (byte)NetworkClientRequests.CreateGame, id
            }));
            var packageReceived = EngineNetwork.ReceiveMultipleData();

            if (packageReceived.Count == 0)
            {
                return(false);
            }
            else if (packageReceived.Count == 1)
            {
                if (byte.Parse(packageReceived[0]["code"]) != (byte)NetworkServerResponses.GameCreated)
                {
                    return(false);
                }
            }
            return(true);
        }