Ejemplo n.º 1
0
        public void Login(GameLauncher gameLauncher, string nickname, string password)
        {
            try
            {
                //Preparing player variable
                Player player = new Player()
                {
                    Nickname = nickname,
                    Password = password
                };

                bool waiting = false;

                ClientServiceProvider csp = new ClientServiceProvider(
                    NetworkObjectParameters.LoginServerInformation.ServerPublicAddress,
                    NetworkObjectParameters.LoginServerInformation.ServerPort,
                    NetworkObjectParameters.LoginServerBufferSize,
                    (serviceProvider, message) =>
                {
                    if (message.Length == 2)
                    {
                        player = ObjectWrapper.DeserializeRequest <Player>(message[1]);

                        if (player == null)
                        {
                            waiting = true;
                            return;
                        }
                    }
                    else
                    {
                        List <int> idList = ObjectWrapper.DeserializeRequest <List <int> >(message[2]);

                        if (idList == null)
                        {
                            waiting = true;
                            return;
                        }

                        foreach (int i in idList)
                        {
                            player.PlayerAvatarMetadataList.Add(new PlayerAvatarMetadata()
                            {
                                Player         = player,
                                AvatarMetadata = new AvatarMetadata()
                                {
                                    ID             = i,
                                    AvatarCategory = (AvatarCategory)int.Parse(message[1])
                                },
                            });
                        }
                    }
                });

                csp.OnFailToEstabilishConnection += () =>
                {
                    Feedback.CreateWarningMessageBox(Language.FailToEstabilishConnection);
                    OnFailToEstabilishConnection(gameLauncher);
                };
                csp.StartOperation();
                csp.RequestQueue.Enqueue(NetworkObjectParameters.LoginServerLoginAttemptRequest, player);

                while (!waiting)
                {
                    Thread.Sleep(100);
                }

                csp.StopOperation();

                if (player == null || player.ID == 0)
                {
                    //Error
                    Feedback.CreateWarningMessageBox(Language.PlayerNotFound);
                    OnFailToEstabilishConnection(gameLauncher);

                    return;
                }

                player.LoadOwnedAvatarDictionary();

                //Success
                gameLauncher.TickAction += () => { gameLauncher.Close(); };
                Parameter.Player         = player;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Ex: {ex.Message}");
            }
        }