예제 #1
0
        public void Login(string username, string md5String, ClientVersionSubPacket clientVersion,
                          ClientSession.ClientSession clientSession, string passwordToken, bool useApiAuth)
        {
            try
            {
                clientSession.SessionId = clientSession.Channel?.Id != null
                    ? SessionFactory.Instance.Sessions[clientSession.Channel.Id.AsLongText()].SessionId : 0;
                if (false) //TODO Maintenance
                {
                    clientSession.SendPacket(new FailcPacket
                    {
                        Type = LoginFailType.Maintenance
                    });
                    clientSession.Disconnect();
                    return;
                }

                if (((_loginConfiguration.ClientVersion != null) &&
                     (clientVersion != _loginConfiguration.ClientVersion)) ||
                    ((_loginConfiguration.Md5String != null) && (md5String != _loginConfiguration.Md5String)))
                {
                    clientSession.SendPacket(new FailcPacket
                    {
                        Type = LoginFailType.OldClient
                    });
                    clientSession.Disconnect();
                    return;
                }

                var acc = _accountDao.FirstOrDefault(s => s.Name.ToLower() == username.ToLower());

                if ((acc != null) && (acc.Name != username))
                {
                    clientSession.SendPacket(new FailcPacket
                    {
                        Type = LoginFailType.WrongCaps
                    });
                    clientSession.Disconnect();
                    return;
                }

                if ((acc == null) ||
                    (!useApiAuth && !string.Equals(acc.Password, passwordToken, StringComparison.OrdinalIgnoreCase)) ||
                    (useApiAuth &&
                     !_authHttpClient.IsAwaitingConnection(username, passwordToken, clientSession.SessionId)))
                {
                    clientSession.SendPacket(new FailcPacket
                    {
                        Type = LoginFailType.AccountOrPasswordWrong
                    });
                    clientSession.Disconnect();
                    return;
                }

                switch (acc.Authority)
                {
                case AuthorityType.Banned:
                    clientSession.SendPacket(new FailcPacket
                    {
                        Type = LoginFailType.Banned
                    });
                    break;

                case AuthorityType.Closed:
                case AuthorityType.Unconfirmed:
                    clientSession.SendPacket(new FailcPacket
                    {
                        Type = LoginFailType.CantConnect
                    });
                    break;

                default:
                    var servers = _channelHttpClient.GetChannels()
                                  ?.Where(c => c.Type == ServerType.WorldServer).ToList();
                    var alreadyConnnected = false;
                    var connectedAccount  = new Dictionary <int, List <ConnectedAccount> >();
                    var i = 1;
                    foreach (var server in servers ?? new List <ChannelInfo>())
                    {
                        var channelList = _connectedAccountHttpClient.GetConnectedAccount(
                            server);
                        connectedAccount.Add(i, channelList);
                        i++;
                        if (channelList.Any(a => a.Name == acc.Name))
                        {
                            alreadyConnnected = true;
                        }
                    }

                    if (alreadyConnnected)
                    {
                        clientSession.SendPacket(new FailcPacket
                        {
                            Type = LoginFailType.AlreadyConnected
                        });
                        clientSession.Disconnect();
                        return;
                    }

                    acc.Language = _loginConfiguration.UserLanguage;
                    _accountDao.InsertOrUpdate(ref acc);
                    if (servers.Count <= 0)
                    {
                        clientSession.SendPacket(new FailcPacket
                        {
                            Type = LoginFailType.CantConnect
                        });
                        clientSession.Disconnect();
                        return;
                    }

                    var subpacket = new List <NsTeStSubPacket>();
                    i = 1;
                    var servergroup = string.Empty;
                    var worldCount  = 1;
                    foreach (var server in servers.OrderBy(s => s.Name))
                    {
                        if (server.Name != servergroup)
                        {
                            i           = 1;
                            servergroup = server.Name;
                            worldCount++;
                        }

                        var channelcolor =
                            (int)Math.Round((double)connectedAccount[i].Count / server.ConnectedAccountLimit * 20)
                            + 1;
                        subpacket.Add(new NsTeStSubPacket
                        {
                            Host       = server.Host,
                            Port       = server.Port,
                            Color      = channelcolor,
                            WorldCount = worldCount,
                            WorldId    = i,
                            Name       = server.Name
                        });
                        i++;
                    }

                    subpacket.Add(new NsTeStSubPacket
                    {
                        Host       = "-1",
                        Port       = null,
                        Color      = null,
                        WorldCount = 10000,
                        WorldId    = 10000,
                        Name       = useApiAuth ? "4" : "1"
                    });     //useless server to end the client reception
                    clientSession.SendPacket(new NsTestPacket
                    {
                        AccountName = username,
                        SubPacket   = subpacket,
                        SessionId   = clientSession.SessionId,
                        Unknown     = useApiAuth ? 2 : (int?)null
                    });
                    return;
                }

                clientSession.Disconnect();
            }
            catch
            {
                clientSession.SendPacket(new FailcPacket
                {
                    Type = LoginFailType.UnhandledError
                });
                clientSession.Disconnect();
            }
        }
예제 #2
0
        public override void Execute(EntryPointPacket packet, ClientSession clientSession)
        {
            if (clientSession.Account == null)
            {
                var alreadyConnnected = false;
                var name = packet.Name;
                foreach (var channel in _channelHttpClient.GetChannels().Where(c => c.Type == ServerType.WorldServer))
                {
                    var accounts = _connectedAccountHttpClient.GetConnectedAccount(channel);
                    var target   = accounts.FirstOrDefault(s => s.Name == name);

                    if (target != null)
                    {
                        alreadyConnnected = true;
                        break;
                    }
                }

                if (alreadyConnnected)
                {
                    clientSession.Disconnect();
                    return;
                }

                var account = _accountDao.FirstOrDefault(s => s.Name == name);

                if (account != null)
                {
                    if (_authHttpClient.IsAwaitingConnection(name, packet.Password, clientSession.SessionId) ||
                        (account.Password.Equals(packet.Password.ToSha512(), StringComparison.OrdinalIgnoreCase) &&
                         !_authHttpClient.IsAwaitingConnection(name, "", clientSession.SessionId)))
                    {
                        var accountobject = new AccountDto
                        {
                            AccountId = account.AccountId,
                            Name      = account.Name,
                            Password  = account.Password.ToLower(),
                            Authority = account.Authority,
                            Language  = account.Language
                        };
                        SessionFactory.Instance.Sessions
                        .FirstOrDefault(s => s.Value.SessionId == clientSession.SessionId)
                        .Value.RegionType = account.Language;
                        clientSession.InitializeAccount(accountobject);
                        //Send Account Connected
                    }
                    else
                    {
                        _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_PASSWORD));
                        clientSession.Disconnect();
                        return;
                    }
                }
                else
                {
                    _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_ACCOUNT));
                    clientSession.Disconnect();
                    return;
                }
            }

            var characters = _characterDao.Where(s =>
                                                 (s.AccountId == clientSession.Account.AccountId) && (s.State == CharacterState.Active));

            _logger.Information(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.ACCOUNT_ARRIVED),
                                clientSession.Account.Name);

            // load characterlist packet for each character in Character
            clientSession.SendPacket(new ClistStartPacket {
                Type = 0
            });
            foreach (var character in characters.Select(characterDto => _adapter.Adapt <Character>(characterDto)))
            {
                var equipment = new WearableInstance[16];

                /* IEnumerable<ItemInstanceDTO> inventory = _iteminstanceDAO.Where(s => s.CharacterId == character.CharacterId && s.Type == (byte)InventoryType.Wear);
                 *
                 *
                 * foreach (ItemInstanceDTO equipmentEntry in inventory)
                 * {
                 *   // explicit load of iteminstance
                 *   WearableInstance currentInstance = equipmentEntry as WearableInstance;
                 *   equipment[(short)currentInstance.Item.EquipmentSlot] = currentInstance;
                 *
                 * }
                 */
                var petlist = new List <short?>();
                var mates   = _mateDao.Where(s => s.CharacterId == character.CharacterId)
                              .ToList();
                for (var i = 0; i < 26; i++)
                {
                    if (mates.Count > i)
                    {
                        petlist.Add(mates[i].Skin);
                        petlist.Add(mates[i].VNum);
                    }
                    else
                    {
                        petlist.Add(-1);
                    }
                }

                // 1 1 before long string of -1.-1 = act completion
                clientSession.SendPacket(new ClistPacket
                {
                    Slot       = character.Slot,
                    Name       = character.Name,
                    Unknown    = 0,
                    Gender     = character.Gender,
                    HairStyle  = character.HairStyle,
                    HairColor  = character.HairColor,
                    Unknown1   = 0,
                    Class      = character.Class,
                    Level      = character.Level,
                    HeroLevel  = character.HeroLevel,
                    Equipments = new List <short?>
                    {
                        equipment[(byte)EquipmentType.Hat]?.ItemVNum,
                        equipment[(byte)EquipmentType.Armor]?.ItemVNum,
                        equipment[(byte)EquipmentType.WeaponSkin]?.ItemVNum ??
                        equipment[(byte)EquipmentType.MainWeapon]?.ItemVNum,
                        equipment[(byte)EquipmentType.SecondaryWeapon]?.ItemVNum,
                        equipment[(byte)EquipmentType.Mask]?.ItemVNum,
                        equipment[(byte)EquipmentType.Fairy]?.ItemVNum,
                        equipment[(byte)EquipmentType.CostumeSuit]?.ItemVNum,
                        equipment[(byte)EquipmentType.CostumeHat]?.ItemVNum
                    },
                    JobLevel        = character.JobLevel,
                    QuestCompletion = 1,
                    QuestPart       = 1,
                    Pets            = petlist,
                    Design          = equipment[(byte)EquipmentType.Hat]?.Item.IsColored ?? false
                        ? equipment[(byte)EquipmentType.Hat].Design : 0,
                    Unknown3 = 0
                });
            }

            clientSession.SendPacket(new ClistEndPacket());
        }