Exemplo n.º 1
0
        internal static Game SaveGame(string sub, DateTime endGame, double latitude, double longitude, string[] clues, int[] user_ids, string photo)
        {
            using (var context = new Database.postgresContext())
            {
                var user = context.User.FirstOrDefault(u => u.Sub == sub);

                if (user == null)
                {
                    throw new Database.UserNotFoundException();
                }

                var game = new Database.Game()
                {
                    CreatorId     = user.Id,
                    Ended         = false,
                    Latitude      = latitude,
                    Longitude     = longitude,
                    Photo         = photo,
                    StartDatetime = DateTime.Now,
                    EndDatetime   = endGame,
                    WinCode       = GenerateRandomCode()
                };

                context.Game.Add(game);

                context.SaveChanges();

                foreach (string clue in clues)
                {
                    Database.Clue c = new Database.Clue();
                    c.GameId = game.Id;
                    c.Text   = clue;
                    context.Clue.Add(c);
                }

                foreach (int user_id in user_ids)
                {
                    var u = context.User.Find(user_id);
                    Database.GameUser gu = new Database.GameUser();
                    gu.GameId = game.Id;
                    gu.UserId = user_id;
                    context.GameUser.Add(gu);
                    CloudMessagingService.GameInvitation(user.Sub, game.Id);
                }

                context.SaveChanges();

                return(ParseGame(game));
            }
        }
Exemplo n.º 2
0
        internal static Game SaveGame(string sub, Game gameNew)
        {
            using (var context = new Database.postgresContext())
            {
                var user = context.User.First(u => u.Sub == sub);

                var game = new Database.Game()
                {
                    CreatorId     = user.Id,
                    Ended         = false,
                    Latitude      = gameNew.Latitude,
                    Longitude     = gameNew.Longitude,
                    Photo         = gameNew.Photo,
                    StartDatetime = DateTime.Now,
                    EndDatetime   = gameNew.EndDatetime,
                    WinCode       = GenerateRandomCode()
                };

                context.Game.Add(game);

                context.SaveChanges();

                foreach (string clue in gameNew.Clues)
                {
                    Database.Clue c = new Database.Clue();
                    c.GameId = game.Id;
                    c.Text   = clue;
                    context.Clue.Add(c);
                }

                foreach (int user_id in gameNew.UserIds)
                {
                    var u = context.User.Find(user_id);
                    Database.GameUser gu = new Database.GameUser();
                    gu.GameId = game.Id;
                    gu.UserId = user_id;
                    context.GameUser.Add(gu);
                    CloudMessagingService.GameInvitation(user.Sub, game.Id);
                }

                context.SaveChanges();



                return(ParseGame(game));
            }
        }
Exemplo n.º 3
0
        public static Game WinGame(int game_id, string sub, string win_code)
        {
            using (var context = new Database.postgresContext())
            {
                var user = context.User.FirstOrDefault(u => u.Sub == sub);

                if (user == null)
                {
                    throw new Database.UserNotFoundException();
                }

                using (var dbContextTransaction = context.Database.BeginTransaction())
                {
                    var game = context.Game.FirstOrDefault(g => g.Id == game_id);

                    if (game == null)
                    {
                        throw new Database.GameNotFoundException();
                    }

                    if (!game.Ended)
                    {
                        if (game.WinCode == win_code)
                        {
                            game.WinId        = user.Id;
                            game.WinTimestamp = DateTime.Now;
                            game.Ended        = true;

                            context.SaveChanges();

                            CloudMessagingService.GameEnded(game_id, user);
                        }
                    }

                    dbContextTransaction.Commit();

                    return(ParseGame(game));
                }
            }
        }