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); } } } }); } }
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 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; } }
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); }
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; } }
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(); } } }