// don't change order of shapes here! private static void SeedGameShapes(RoshamboContext context) { context.Set <GameShape>().AddOrUpdate( new[] { new GameShape() { ShapeName = ShapeNames.Rock }, new GameShape() { ShapeName = ShapeNames.Paper }, new GameShape() { ShapeName = ShapeNames.Scissors }, new GameShape() { ShapeName = ShapeNames.Lizard }, new GameShape() { ShapeName = ShapeNames.Spock }, new GameShape() { ShapeName = ShapeNames.Undefined } }); }
private static void SeedLobbyStates(RoshamboContext context) { context.Set <LobbyState>().AddOrUpdate( new[] { new LobbyState() { Name = LobbyStateNames.AwaitingToPlayers }, new LobbyState() { Name = LobbyStateNames.ReadyToStart }, new LobbyState() { Name = LobbyStateNames.Playing }, new LobbyState() { Name = LobbyStateNames.Summarizing }, new LobbyState() { Name = LobbyStateNames.Blocked } }); }
private int?GetIdRoundWinner(string userName, RoshamboContext context) { if (string.IsNullOrEmpty(userName)) { return(null); } var user = context.Users.Single(u => u.UserName.Equals(userName)); return(user.Id); }
//add opponent to Players table private void SetOpponentToLobby(RoshamboContext context, string lobbyName, string opponentName) { var opponent = context.Users.FirstOrDefault(u => u.UserName.Equals(opponentName)); var lobby = context.Lobbies.FirstOrDefault(l => l.LobbyName.Equals(lobbyName)); if (lobby != null && opponent != null) { opponent.ConnectionId = Context.ConnectionId; var competitor = new Competitor { User = opponent, Lobby = lobby }; context.Set <Competitor>().Add(competitor); context.SaveChanges(); } }
private int SetLobbyState(RoshamboContext context, string lobbyStateName, string lobbyName) { try { var lobbyId = -1; var lobby = context.Lobbies.FirstOrDefault(l => l.LobbyName.Equals(lobbyName)); var lobbyState = context.Set <LobbyState>().First(x => x.Name.Equals(lobbyStateName)); if (lobby != null) { lobbyId = lobby.LobbyId; lobby.IdLobbyState = lobbyState.LobbyStateId; context.SaveChanges(); Clients.All.lobbyStateChanged(new { lobby.LobbyId, LobbyName = lobbyName, LobbyState = lobbyState.Name }); } return(lobbyId); } catch (Exception e) { return(-1); } }
//todo: refactor public static async Task <TipModel> GenerateTip(RoshamboContext context, int gameId, int roundNumber, string userName) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (string.IsNullOrEmpty(userName)) { throw new ArgumentNullException(nameof(userName)); } var game = context.Games.Find(gameId); if (game == null) { throw new InvalidOperationException("There is no game with such id."); } var opName = game.OpponentName; var ownerName = game.LobbyOwner.UserName; var tip = new TipModel() { GameId = gameId, RoundNumber = roundNumber }; var round = game.Rounds.Single(r => r.RoundNumber.Equals(roundNumber)); // user is lobby owner if (userName.Equals(ownerName)) { var opponentShape = round.OpponentShapeName; if (string.IsNullOrEmpty(opponentShape)) { tip.TipSucceeded = false; return(await Task.FromResult(tip)); } tip.TipSucceeded = true; var trueShape = round.OpponentShapeName; var fakeShape = FakeShapeGenerator.GenerateFakeShape(trueShape); // mess data due to be unpredictable if (FakeShapeGenerator.GenerateRandomIndex() == 0) { tip.ShapeOne = trueShape; tip.ShapeTwo = fakeShape; } else { tip.ShapeTwo = trueShape; tip.ShapeOne = fakeShape; } return(await Task.FromResult(tip)); } // user is opponent else if (userName.Equals(opName)) { // just dublicating logic here var trueShape = round.LobbyOwnerShapeName; if (string.IsNullOrEmpty(trueShape)) { tip.TipSucceeded = false; return(await Task.FromResult(tip)); } tip.TipSucceeded = true; var fakeShape = FakeShapeGenerator.GenerateFakeShape(trueShape); if (FakeShapeGenerator.GenerateRandomIndex() == 0) { tip.ShapeOne = trueShape; tip.ShapeTwo = fakeShape; } else { tip.ShapeTwo = trueShape; tip.ShapeOne = fakeShape; } return(await Task.FromResult(tip)); } throw new ArgumentException(nameof(userName)); }
private async Task ActualizeConnectionInternal(string userName, string connectionId, RoshamboContext context) { var user = context.Users.Single(x => x.UserName.Equals(userName)); user.ConnectionId = connectionId; // restore groups user participates in if (user.OwnLobby != null) { await Groups.Add(connectionId, user.OwnLobby.LobbyName); } if (user.Competitor != null) { await Groups.Add(connectionId, user.Competitor.Lobby.LobbyName); } await context.SaveChangesAsync(); }
public static void SeedContext(RoshamboContext context) { SeedLobbyStates(context); SeedGameShapes(context); }