Beispiel #1
0
        public async Task <SessionToken> BeginSessionAsync(
            AuthToken token,
            string clientVersion,
            string accessKey,
            bool isLocal,
            float syncTime)
        {
            var game = gameData.Client;
            var user = gameData.GetUser(token.UserId);

            if (game == null)
            {
                return(null);
            }

            if (game.AccessKey != accessKey)
            {
                //logger.LogError("Unable to start session for user: "******", client version: {clientVersion}, accessKey: {accessKey}");
                return(null);
            }

            if (clientVersion.ToLower() != game.ClientVersion.ToLower())
            {
                var expectedVersionStr = game.ClientVersion.Replace("a", "");
                var clientVersionStr   = clientVersion.Replace("a", "");
                if (!Version.TryParse(clientVersionStr, out var version) || !Version.TryParse(expectedVersionStr, out var expectedVersion) || version < expectedVersion)
                {
                    //logger.LogError("Unable to start session for user: "******", client version: {clientVersion}, accessKey: {accessKey}");
                    return(null); // new SessionToken();
                }
            }

            var userId = token.UserId;

            gameData.ClearAllCharacterSessionStates(userId);

            var activeSession = gameData.GetSessionByUserId(userId);
            // x => x.UserId == userId && x.Status == (int)SessionStatus.Active
            var oldSessionExpired = false;
            var oldSession        = activeSession;

            if (activeSession != null)
            {
                oldSessionExpired = DateTime.UtcNow - activeSession.Updated.GetValueOrDefault() >= TimeSpan.FromMinutes(30);
                if (oldSessionExpired)
                {
                    activeSession.Status  = (int)SessionStatus.Inactive;
                    activeSession.Stopped = DateTime.UtcNow;
                    activeSession         = null;
                }
            }

            var newGameSession = activeSession ?? gameData.CreateSession(userId);

            if (activeSession == null)
            {
                gameData.Add(newGameSession);
            }

            if (oldSession != null && !oldSessionExpired)
            {
                logger.LogError("BeginSessionAsync was called while an existing session is active. User: "******". Previous players will not be cleared.");
            }
            else
            {
                var activeChars = gameData.GetSessionCharacters(newGameSession);
                if (activeChars != null)
                {
                    foreach (var c in activeChars)
                    {
                        c.UserIdLock = null;
                    }
                }
                //#if DEBUG
                //                logger.LogDebug(user.UserName + " game session started. " + activeChars.Count + " characters cleared.");
                //#endif
            }

            newGameSession.Revision = 0;

            var sessionState = gameData.GetSessionState(newGameSession.Id);

            sessionState.SyncTime      = syncTime;
            sessionState.ClientVersion = clientVersion;

            SendPermissionData(newGameSession, user);
            SendVillageInfo(newGameSession);

            return(GenerateSessionToken(token, user, newGameSession, clientVersion));
        }