/// <summary> /// Tries to connect player to GameSession /// </summary> /// <param name="i_player">Player name</param> /// <returns>True if player could be connected, false if game is full or started and false with exception if game was removed (or other reasons for exception throw)</returns> public async Task <ServerResponseInfo <bool, Exception> > ConnectPlayerAsync(string i_player, byte[] i_address) { ServerResponseInfo <bool, Exception> res = new ServerResponseInfo <bool, Exception>(); try { //Get GameSession from StateManager GameSession gameSession = await this.StateManager.GetStateAsync <GameSession>("gamesession"); //Adds player to game if posible if (gameSession.AddPlayer(i_player, i_address)) { //If first player in lobby if (gameSession.playerCount == 1) { await this.UnregisterReminderAsync(GetReminder("RemoveIfEmpty")); //Registers LobyCheck reminder await this.RegisterReminderAsync("LobbyCheck", null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10)); } //Saves "gamesession" state await this.StateManager.SetStateAsync("gamesession", gameSession); ActorEventSource.Current.Message(ServiceUri.AbsoluteUri); ILoginService login = ServiceProxy.Create <ILoginService>(new Uri(ServiceUri.AbsoluteUri.Replace("GameManagerActorService", "LoginService"))); //Adds player to server (SQL Register) await login.AddPlayerAsync(Id.ToString()); //Returns true res.info = true; return(res); } //Saves "gamesession" state await this.StateManager.SetStateAsync("gamesession", gameSession); //Returns false (player couldn't be added: game full or started) res.info = false; return(res); } //Catch exception if "gamesesion" state doesn't exist catch (Exception e) { //Returns false and exception (player couldn't be added: game removed) res.info = false; res.exception = e; return(res); } }