public async Task MatchMakeGame( TokenViewModel Token ) { try { var User = await this.VerifyToken( Token ); if ( User != null ) { //See if Any Games Are Available before making a new one var games = db.Games.Where( c => c.PrivateMatch == false && c.CanAddPlayer == true ); Game game; if(games.Count() == 0) { //There are no available games. //Create New Game game = new Game(); //Add Current User to Game game.AddPlayer( new Player( User.Profile ) ); //Save Game db.Games.Add( game ); await db.SaveChangesAsync(); Clients.Caller.newGame(game); } else { //Add Current Player to oldest game. game = games.First(); game.AddPlayer(new Player(User.Profile)); //Save Changes await db.SaveChangesAsync(); //Get a list of all connections for the Game ICollection<String> connections = new Collection<String>(); //For Every Player: foreach( var player in game.Players) { //For Every Connection: foreach( var connection in player.Value.Profile.User.Connections) { //Add to ID Connection List connections.Add(connection.Id.ToString()); } } //Notify All clients about new Player Joining. Clients.Clients(connections as IList<String>).playerJoined(game); } } else { Clients.Caller.error( "Unauthorized Access." ); } } catch ( Exception error ) { Clients.Caller.error( "Exception Occurred: " + error.Message ); } }
/// <summary> /// Verifies the Token matches the Email Address given. All Connections must /// </summary> /// <param name="Token">User's Email and Token</param> /// <returns>The User that the User and Token apply to. If a User can not be identified properly, null is returned.</returns> public async Task<MyUser> VerifyToken( TokenViewModel Token ) { //Check Model State if ( !ModelState.IsValid ) { //If Model State is not Valid, call error on Caller. Clients.Caller.error( "Invalid Model: " + ModelState ); return null; } else { //Else, attempt to match the Email with the Token. try { //Try to find user with specified Email Address var user = await db.Users.FirstOrDefaultAsync( c => c.Email == Token.Email ); //Check to see if query was succesful if ( user != null ) { //User properly found. Attempt to verify the token. if ( user.Id != Token.Token ) { //Token does not match our system Clients.Caller.error( "Incorrect Token." ); return null; } else { //Token is authenitcated Clients.Caller.tokenVerified( "Token Verified" ); return user; } } else { //Else, the Email does not exist in our system. Clients.Caller.error( "Email Not Found" ); return null; } } //Catch all Exceptions to avoid silent failure catch ( Exception error ) { //Return all error messages to error method on Caller. Clients.Caller.error( "Exception Occurred: " + error.Message ); return null; } } }
public async Task GetAllGames( TokenViewModel Token ) { try { var User = await this.VerifyToken( Token ); if ( User != null ) { //todo Return All Games to Client. } else { Clients.Caller.error( "Unauthorized Access." ); } } catch(Exception error) { Clients.Caller.error("Exception Occurred: " + error.Message); } }
public async Task GetLeaderboards( TokenViewModel Token ) { try { var User = await this.VerifyToken( Token ); if ( User != null ) { //todo Return Leaderboard Stats or something. This can be added later. } else { Clients.Caller.error( "Unauthorized Access." ); } } catch ( Exception error ) { Clients.Caller.error( "Exception Occurred: " + error.Message ); } }
public async Task GetProfile( TokenViewModel Token ) { try { var User = await this.VerifyToken( Token ); if ( User != null ) { //Return Profile that belongs to User var profile = new ProfileViewModel(User.Profile); Clients.Caller.Profile(profile); } else { Clients.Caller.error( "Unauthorized Access." ); } } catch ( Exception error ) { Clients.Caller.error( "Exception Occurred: " + error.Message ); } }
public async Task CreateNewCustomGame( TokenViewModel Token, GameViewModel Game ) { try { var User = await this.VerifyToken( Token ); if ( User != null ) { //Create New Game Game game = new Game(Game); //Add Current User to Game game.AddPlayer(new Player(User.Profile)); //Save Game db.Games.Add(game); await db.SaveChangesAsync(); //Send Game back to client. Clients.Caller.newGame(game); } else { Clients.Caller.error( "Unauthorized Access." ); } } catch ( Exception error ) { Clients.Caller.error( "Exception Occurred: " + error.Message ); } }
public async Task PlayTurn( TokenViewModel Token, GameViewModel Game, TurnViewModel Turn ) { try { var User = await this.VerifyToken( Token ); if ( User != null ) { //todo Create a New Game } else { Clients.Caller.error( "Unauthorized Access." ); } } catch ( Exception error ) { Clients.Caller.error( "Exception Occurred: " + error.Message ); } }