예제 #1
0
        public void ChangeUserState(UserInfoData userInfoData, UserState userState)
        {
            var oldUserState = userInfoData.UserState;

            userInfoData.UserState = userState;
            //OnUserStatChangeEvent?.Invoke(this, new UserStatChangeEventArgs(userInfoData.Id, userInfoData.UserState, oldUserState));
        }
예제 #2
0
        private string ResponseHandShake(ITcpConnection connection, int dataPacketId, string dataPacketToken, SendData sendData)
        {
            var handShake    = JsonConvert.DeserializeObject <HandShakeRequest>(sendData.MessageData);
            var requestState = RequestState.Success;

            // create token here
            int userIndex = Index;
            var salted    = (handShake.AccountName + userIndex).GetHashCode();
            var userToken = salted.ToString();
            //Console.WriteLine($"Hash: {salted} -> {token}");

            var newUserInfo = new UserInfoData
            {
                Id          = userIndex,
                Token       = userToken,
                AccountName = handShake.AccountName,
                Connection  = connection,
                UserState   = UserState.None,
                GameId      = -1,
                DeckType    = DeckType.None,
                DeckData    = string.Empty,
                PlayerState = PlayerState.None
            };

            var user = _registredUsers.Values.ToList().Find(p => p.AccountName == handShake.AccountName);

            if (user != null && user.Connection.Equals(connection))
            {
                Log.Warn($"Account {handShake.AccountName} already registred! EndPoint: {user.Connection.LocalEndPoint}, Key: {user.Connection.Key}, {user.Connection.Equals(connection)}");
                requestState = RequestState.Fail;
            }
            else if (user != null && !_registredUsers.TryUpdate(user.Token, newUserInfo, user))
            {
                Log.Warn($"Account {handShake.AccountName} couldn't be updated!");
                requestState = RequestState.Fail;
            }
            else if (!_registredUsers.TryAdd(userToken, newUserInfo))
            {
                Log.Warn($"Account {handShake.AccountName} couldn't be registred!");
                requestState = RequestState.Fail;
            }

            if (requestState == RequestState.Success)
            {
                Log.Info($"Registred account {handShake.AccountName}!");
            }

            return(DataPacketBuilder.ResponseServerHandShake(_id, _token, requestState, userIndex, userToken));
        }