public override void Decode(ByteStream stream)
 {
     if (this.Success)
     {
         this.Document = CouchbaseDocument.Decode <GameDocument>(stream);
     }
 }
        public static void Init()
        {
            GameAvatarManager.m_documents = new Dictionary <long, GameAvatar>();

            int[] higherIDs = ServerGame.GameDatabase.GetCounterHigherIDs();

            for (int i = 0; i < higherIDs.Length; i++)
            {
                int highId = i;

                Parallel.For(1, higherIDs[i] + 1, lowId =>
                {
                    LogicLong id = new LogicLong(highId, lowId);

                    if (ServerManager.GetDocumentSocket(ServerCore.Type, id) == ServerCore.Socket)
                    {
                        IOperationResult <string> document = ServerGame.GameDatabase.Get(id).Result;

                        if (document.Success)
                        {
                            lock (GameAvatarManager.m_documents)
                            {
                                GameAvatar gameDocument = CouchbaseDocument.Load <GameAvatar>(document.Value);

                                GameAvatarManager.m_documents.Add(id, gameDocument);
                                GameMatchmakingManager.Enqueue(gameDocument);
                            }
                        }
                    }
                });
            }
        }
 public override void Encode(ByteStream stream)
 {
     if (this.Success)
     {
         CouchbaseDocument.Encode(stream, this.Document);
     }
 }
        private async void UnlockAccountMessageReceived(UnlockAccountMessage message)
        {
            if (this.m_connection.State == ClientConnectionState.LOGIN_FAILED)
            {
                if (ServerStatus.Status == ServerStatusType.SHUTDOWN_STARTED || ServerStatus.Status == ServerStatusType.MAINTENANCE)
                {
                    UnlockAccountFailedMessage unlockAccountFailedMessage = new UnlockAccountFailedMessage();
                    unlockAccountFailedMessage.SetErrorCode(UnlockAccountFailedMessage.ErrorCode.SERVER_MAINTENANCE);
                    this.SendMessage(unlockAccountFailedMessage);
                    return;
                }

                IOperationResult <string> getResult = await ServerProxy.AccountDatabase.Get(message.GetAccountId());

                if (!getResult.Success)
                {
                    UnlockAccountFailedMessage unlockAccountFailedMessage = new UnlockAccountFailedMessage();
                    unlockAccountFailedMessage.SetErrorCode(UnlockAccountFailedMessage.ErrorCode.UNLOCK_UNAVAILABLE);
                    this.SendMessage(unlockAccountFailedMessage);
                    return;
                }

                AccountDocument accountDocument = CouchbaseDocument.Load <AccountDocument>(getResult.Value);

                if (accountDocument.PassToken != message.GetPassToken() || accountDocument.State != AccountState.LOCKED)
                {
                    UnlockAccountFailedMessage unlockAccountFailedMessage = new UnlockAccountFailedMessage();
                    unlockAccountFailedMessage.SetErrorCode(UnlockAccountFailedMessage.ErrorCode.UNLOCK_UNAVAILABLE);
                    this.SendMessage(unlockAccountFailedMessage);
                    return;
                }

                if (accountDocument.StateArg != null && !accountDocument.StateArg.Equals(message.GetUnlockCode(), StringComparison.InvariantCultureIgnoreCase))
                {
                    UnlockAccountFailedMessage unlockAccountFailedMessage = new UnlockAccountFailedMessage();
                    unlockAccountFailedMessage.SetErrorCode(UnlockAccountFailedMessage.ErrorCode.UNLOCK_FAILED);
                    this.SendMessage(unlockAccountFailedMessage);
                    return;
                }

                accountDocument.State    = AccountState.NORMAL;
                accountDocument.StateArg = null;

                IOperationResult <string> updateResult = await ServerProxy.AccountDatabase.Update(accountDocument.Id, CouchbaseDocument.Save(accountDocument));

                if (!updateResult.Success)
                {
                    UnlockAccountFailedMessage unlockAccountFailedMessage = new UnlockAccountFailedMessage();
                    unlockAccountFailedMessage.SetErrorCode(UnlockAccountFailedMessage.ErrorCode.UNLOCK_UNAVAILABLE);
                    this.SendMessage(unlockAccountFailedMessage);
                    return;
                }

                UnlockAccountOkMessage unlockAccountOkMessage = new UnlockAccountOkMessage();
                unlockAccountOkMessage.SetAccountId(message.GetAccountId());
                unlockAccountOkMessage.SetPassToken(message.GetPassToken());
                this.SendMessage(unlockAccountOkMessage);
            }
        }
        public override void Decode(ByteStream stream)
        {
            this.CurrentSeasonDocument = CouchbaseDocument.Decode <SeasonDocument>(stream);

            if (stream.ReadBoolean())
            {
                this.LastSeasonDocument = CouchbaseDocument.Decode <SeasonDocument>(stream);
            }
        }
        public static async Task <GameDocument> GetAvatar(long id)
        {
            IOperationResult <string> result = await ServerAdmin.GameDatabase.Get(id);

            if (result.Success)
            {
                return(CouchbaseDocument.Load <GameDocument>(result.Value));
            }
            return(null);
        }
        public static void Init()
        {
            StreamManager.m_allianceStreams = new Dictionary <long, StreamDocument>();
            StreamManager.m_avatarStreams   = new Dictionary <long, StreamDocument>();
            StreamManager.m_replayStreams   = new Dictionary <long, StreamDocument>();
            StreamManager.m_counters        = ServerStream.StreamDatabase.GetDocumentHigherIDs();

            object locker = new object();

            for (int i = 0; i < EnvironmentSettings.HigherIdCounterSize; i++)
            {
                int highId   = i;
                int higherId = 0;

                Parallel.For(1, StreamManager.m_counters[i] + 1, lowId =>
                {
                    LogicLong id = new LogicLong(highId, lowId);
                    IOperationResult <string> result = ServerStream.StreamDatabase.Get(id).Result;

                    if (result.Success)
                    {
                        StreamDocument document = CouchbaseDocument.Load <StreamDocument>(result.Value);

                        if (ServerManager.GetDocumentSocket(11, document.Type == StreamType.REPLAY ? document.Id : document.OwnerId).ServerId == ServerCore.Id)
                        {
                            lock (locker)
                            {
                                switch (document.Type)
                                {
                                case StreamType.ALLIANCE:
                                    StreamManager.m_allianceStreams.Add(id, document);
                                    break;

                                case StreamType.AVATAR:
                                    StreamManager.m_avatarStreams.Add(id, document);
                                    break;

                                case StreamType.REPLAY:
                                    StreamManager.m_replayStreams.Add(id, document);
                                    break;
                                }

                                if (higherId < lowId)
                                {
                                    higherId = lowId;
                                }
                            }
                        }
                    }
                });

                StreamManager.m_counters[i] = higherId;
            }
        }
Пример #8
0
        private static ScoringSeason LoadSeason(LogicLong id)
        {
            IOperationResult <string> result = ServerScoring.SeasonDatabase.Get(id).Result;

            if (result.Success)
            {
                ScoringSeason scoringSeason = CouchbaseDocument.Load <ScoringSeason>(result.Value);
                scoringSeason.Init();
                return(scoringSeason);
            }

            return(null);
        }
        public override void Encode(ByteStream stream)
        {
            CouchbaseDocument.Encode(stream, this.CurrentSeasonDocument);

            if (this.LastSeasonDocument != null)
            {
                stream.WriteBoolean(true);
                CouchbaseDocument.Encode(stream, this.LastSeasonDocument);
            }
            else
            {
                stream.WriteBoolean(false);
            }
        }
Пример #10
0
        public async Task Update()
        {
            if (this.m_ended)
            {
                return;
            }

            Task t1 = this.UpdateAllianceRankingList(0);
            Task t2 = this.UpdateAllianceRankingList(1);
            Task t3 = this.UpdateAvatarRankingList();
            Task t4 = this.UpdateAvatarDuelRankingList();

            await t1;
            await t2;
            await t3;
            await t4;

            if (this.EndTime < DateTime.UtcNow)
            {
                this.m_ended = true;
            }

            if (this.NextCheckTime < DateTime.UtcNow)
            {
                foreach (AllianceRankingEntry rankingEntry in this.m_allianceRankingList[0].Values)
                {
                    rankingEntry.SetPreviousOrder(rankingEntry.GetOrder());
                }
                foreach (AllianceRankingEntry rankingEntry in this.m_allianceRankingList[1].Values)
                {
                    rankingEntry.SetPreviousOrder(rankingEntry.GetOrder());
                }
                foreach (AvatarRankingEntry rankingEntry in this.m_avatarRankingList.Values)
                {
                    rankingEntry.SetPreviousOrder(rankingEntry.GetOrder());
                }
                foreach (AvatarDuelRankingEntry rankingEntry in this.m_avatarDuelRankingList.Values)
                {
                    rankingEntry.SetPreviousOrder(rankingEntry.GetOrder());
                }

                this.NextCheckTime = DateTime.UtcNow.Date.AddDays(1);
            }

            await ServerScoring.SeasonDatabase.Update(this.Id, CouchbaseDocument.Save(this));
        }
        private async void UpdatePlayTimeSeconds()
        {
            IOperationResult <string> getResult = await ServerProxy.AccountDatabase.Get(this.AccountId);

            if (getResult.Success)
            {
                AccountDocument accountDocument = CouchbaseDocument.Load <AccountDocument>(getResult.Value);

                accountDocument.SessionCount    += 1;
                accountDocument.PlayTimeSeconds += (int)DateTime.UtcNow.Subtract(this.m_startSessionTime).TotalSeconds;

                IOperationResult <string> updateResult = await ServerProxy.AccountDatabase.Update(this.AccountId, CouchbaseDocument.Save(accountDocument), getResult.Cas);

                if (!updateResult.Success)
                {
                    this.UpdatePlayTimeSeconds();
                }
            }
        }
        public static void Init()
        {
            AllianceManager.m_alliances = new Dictionary <long, Alliance>();
            AllianceManager.m_counters  = ServerStream.AllianceDatabase.GetDocumentHigherIDs();

            for (int i = 0; i < EnvironmentSettings.HigherIdCounterSize; i++)
            {
                int highId   = i;
                int higherId = 0;

                Parallel.For(1, AllianceManager.m_counters[i] + 1, lowId =>
                {
                    LogicLong id = new LogicLong(highId, lowId);

                    if (ServerManager.GetDocumentSocket(ServerCore.Type, id) == ServerCore.Socket)
                    {
                        IOperationResult <string> document = ServerStream.AllianceDatabase.Get(id).Result;

                        if (document.Success)
                        {
                            lock (AllianceManager.m_alliances)
                            {
                                Alliance allianceDocument = CouchbaseDocument.Load <Alliance>(document.Value);
                                AllianceManager.m_alliances.Add(id, allianceDocument);

                                if (higherId < lowId)
                                {
                                    higherId = lowId;
                                }
                            }
                        }
                    }
                });

                AllianceManager.m_counters[i] = higherId;
            }
        }
 public static void Save(Alliance alliance)
 {
     ServerStream.AllianceDatabase.InsertOrUpdate(alliance.Id, CouchbaseDocument.Save(alliance));
 }
        public static void Save(AvatarStreamEntry entry)
        {
            StreamDocument document = StreamManager.m_avatarStreams[entry.GetId()];

            ServerStream.StreamDatabase.InsertOrUpdate(document.Id, CouchbaseDocument.Save(document));
        }
 private static void Save(StreamDocument document)
 {
     ServerStream.StreamDatabase.InsertOrUpdate(document.Id, CouchbaseDocument.Save(document));
 }
 public static async void SaveAccount(AccountDocument document)
 {
     await ServerAdmin.AccountDatabase.Update(document.Id, CouchbaseDocument.Save(document));
 }
 public static async void Save(GameAvatar document)
 {
     await ServerGame.GameDatabase.InsertOrUpdate(document.Id, CouchbaseDocument.Save(document));
 }
        private async void LoginMessageReceived(LoginMessage message)
        {
            if (this.m_connection.State == ClientConnectionState.DEFAULT &&
                this.CheckClientVersion(message.GetClientMajorVersion(), message.GetClientBuildVersion(), message.GetAppVersion(), message.GetResourceSha(), message.IsAndroidClient()) &&
                this.CheckServerCapabilities())
            {
                this.m_connection.Messaging.SetScramblerSeed(message.GetScramblerSeed());
                this.m_connection.SetState(ClientConnectionState.LOGIN);

                AccountDocument accountDocument;

                if (message.GetAccountId().IsZero() && message.GetPassToken() == null)
                {
                    IOperationResult <ulong> incrementSeedResult = await ServerProxy.AccountDatabase.IncrementSeed();

                    if (!incrementSeedResult.Success)
                    {
                        LoginFailedMessage loginFailedMessage = new LoginFailedMessage();
                        loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.SERVER_MAINTENANCE);
                        loginFailedMessage.SetReason("Internal server error");
                        this.SendMessage(loginFailedMessage);
                        return;
                    }

                    accountDocument = new AccountDocument((long)incrementSeedResult.Value);
                    accountDocument.Init();
                    accountDocument.Country = this.m_connection.Location;

                    IOperationResult <string> createAccountResult = await ServerProxy.AccountDatabase.Insert((long)incrementSeedResult.Value, CouchbaseDocument.Save(accountDocument));

                    if (!createAccountResult.Success)
                    {
                        LoginFailedMessage loginFailedMessage = new LoginFailedMessage();
                        loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.SERVER_MAINTENANCE);
                        loginFailedMessage.SetReason("Internal server error");
                        this.SendMessage(loginFailedMessage);
                        return;
                    }
                }
                else
                {
                    IOperationResult <string> getResult = await ServerProxy.AccountDatabase.Get(message.GetAccountId());

                    if (!getResult.Success)
                    {
                        if (getResult.Status == ResponseStatus.KeyNotFound)
                        {
                            LoginFailedMessage loginFailedMessage = new LoginFailedMessage();
                            loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.ACCOUNT_NOT_EXISTS);
                            this.SendMessage(loginFailedMessage);
                        }
                        else
                        {
                            LoginFailedMessage loginFailedMessage = new LoginFailedMessage();
                            loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.SERVER_MAINTENANCE);
                            loginFailedMessage.SetReason("Internal server error");
                            this.SendMessage(loginFailedMessage);
                        }

                        return;
                    }

                    accountDocument = CouchbaseDocument.Load <AccountDocument>(getResult.Value);

                    if (accountDocument.PassToken != message.GetPassToken())
                    {
                        LoginFailedMessage loginFailedMessage = new LoginFailedMessage();
                        loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.ACCOUNT_NOT_EXISTS);
                        this.SendMessage(loginFailedMessage);
                        return;
                    }
                }

                if (accountDocument.State != AccountState.NORMAL)
                {
                    switch (accountDocument.State)
                    {
                    case AccountState.BANNED:
                    {
                        LoginFailedMessage loginFailedMessage = new LoginFailedMessage();
                        loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.BANNED);
                        loginFailedMessage.SetReason(accountDocument.StateArg);
                        this.SendMessage(loginFailedMessage);
                        return;
                    }

                    case AccountState.LOCKED:
                    {
                        LoginFailedMessage loginFailedMessage = new LoginFailedMessage();
                        loginFailedMessage.SetErrorCode(LoginFailedMessage.ErrorCode.ACCOUNT_LOCKED);
                        this.SendMessage(loginFailedMessage);
                        return;
                    }
                    }
                }

                ProxySession session     = ProxySessionManager.Create(this.m_connection, accountDocument);
                RedisValue   prevSession = await ServerProxy.SessionDatabase.GetSet(accountDocument.Id, session.Id.ToString());

                if (!prevSession.IsNull)
                {
                    long prevSessionId = long.Parse(prevSession);

                    ServerMessageManager.SendMessage(new StopSessionMessage
                    {
                        Reason    = 1,
                        SessionId = prevSessionId
                    }, ServerManager.GetProxySocket(prevSessionId));
                }

                session.SetSocket(ServerCore.Socket); // Proxy

                LoginOkMessage loginOkMessage = new LoginOkMessage();

                loginOkMessage.SetAccountId(accountDocument.Id);
                loginOkMessage.SetHomeId(accountDocument.Id);
                loginOkMessage.SetPassToken(accountDocument.PassToken);
                loginOkMessage.SetFacebookId(accountDocument.FacebookId);
                loginOkMessage.SetGamecenterId(accountDocument.GamecenterId);

                loginOkMessage.SetServerMajorVersion(LogicVersion.MAJOR_VERSION);
                loginOkMessage.SetServerBuildVersion(LogicVersion.BUILD_VERSION);
                loginOkMessage.SetContentVersion(ResourceManager.GetContentVersion());
                loginOkMessage.SetServerEnvironment(EnvironmentSettings.Environment);
                loginOkMessage.SetSessionCount(accountDocument.SessionCount);
                loginOkMessage.SetPlayTimeSeconds(accountDocument.PlayTimeSeconds);
                loginOkMessage.SetAccountCreatedDate(accountDocument.CreateTime.ToString());
                loginOkMessage.SetStartupCooldownSeconds(ServerProxy.GetStartupCooldownSeconds());
                loginOkMessage.SetRegion(this.m_connection.Location);

                loginOkMessage.SetFacebookAppId(ResourceSettings.FacebookAppId);
                loginOkMessage.SetGoogleServiceId(ResourceSettings.GoogleServiceId);

                loginOkMessage.SetContentUrlList(ResourceSettings.ContentUrlList);
                loginOkMessage.SetChronosContentUrlList(ResourceSettings.ChronosContentUrlList);

                this.SendMessage(loginOkMessage);
                this.m_connection.SetSession(session);
                this.m_connection.SetState(ClientConnectionState.LOGGED);

                if (this.m_connection.State == ClientConnectionState.LOGGED)
                {
                    accountDocument.SessionCount   += 1;
                    accountDocument.LastSessionTime = TimeUtil.GetTimestamp();

                    ServerRequestManager.Create(new BindServerSocketRequestMessage
                    {
                        ServerType = 9,
                        ServerId   = ServerManager.GetDocumentSocket(9, accountDocument.Id).ServerId,
                        SessionId  = session.Id
                    }, ServerCore.Socket, 5).OnComplete = this.OnGameServerBound;

                    await ServerProxy.AccountDatabase.Update(accountDocument.Id, CouchbaseDocument.Save(accountDocument));
                }
                else
                {
                    this.m_connection.DestructSession();
                }
            }
        }
Пример #19
0
        private static ScoringSeason LoadOrCreateSeason(LogicLong id)
        {
            ScoringSeason scoringSeason = ScoringManager.LoadSeason(id);

            if (scoringSeason == null)
            {
                IOperationResult <string> result = ServerScoring.SeasonDatabase.Insert(id, CouchbaseDocument.Save(scoringSeason = new ScoringSeason(id))).Result;

                if (!result.Success)
                {
                    throw result.Exception;
                }

                scoringSeason.NextCheckTime = DateTime.UtcNow.Date.AddDays(1);
                scoringSeason.Init();
            }

            return(scoringSeason);
        }