Beispiel #1
0
        public HttpResponseMessage PostNewMatch(MatchContract obj)
        {
            if (obj == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            try
            {
                var    result = obj;
                string token  = MD5Hash(obj.Match.Id.ToString() + obj.Match_User.UserId.ToString());
                obj.Match.Token = token.Substring(0, 5).ToUpper();
                db.Matches.Add(obj.Match);
                db.SaveChanges();
                obj.Match_User.MatchId = obj.Match.Id;
                obj.Match_User.Token   = obj.Match.Token;
                db.Match_Users.Add(obj.Match_User);
                db.SaveChanges();
                return(Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Falha ao criar nova Partida. n/" + e.Message));
            }
        }
        public IActionResult Index()
        {
            var game = new Game
            {
                Guid        = Guid.NewGuid().ToString(),
                CreatedDate = DateTime.Now,
                IsActive    = true,
                IsCompleted = false
            };

            game.Patterns.Add(new GamePattern
            {
                Level    = 0,
                CodePeg1 = SiteExtensions.RandomEnum <CodePeg>(),
                CodePeg2 = SiteExtensions.RandomEnum <CodePeg>(),
                CodePeg3 = SiteExtensions.RandomEnum <CodePeg>(),
                CodePeg4 = SiteExtensions.RandomEnum <CodePeg>(),
                KeyPeg1  = KeyPeg.None,
                KeyPeg2  = KeyPeg.None,
                KeyPeg3  = KeyPeg.None,
                KeyPeg4  = KeyPeg.None,
                Game     = game
            });
            _context.Games.Add(game);
            _context.GamePatterns.Add(game.Patterns.First());
            _context.SaveChanges();
            return(View(game));
        }
Beispiel #3
0
        public void FinishGame(int gameId, int myScore, int opponentsScore, Result myResult, Result opponentsResult)
        {
            var game  = CurrentGames[gameId];
            var other = game.GetOther(ClientCallback);
            var me    = game.GetMe(ClientCallback);

            other.Client.FinishGame();
            game.Stop();
            CurrentGames.Remove(gameId);
            me.Playing    = false;
            other.Playing = false;

            using (var db = new GameDataContext())
            {
                GamePlayer dbMe    = db.GamePlayers.Find(me.NickName);
                GamePlayer dbOther = db.GamePlayers.Find(other.NickName);

                var dbGame = new Game()
                {
                    CardCount    = game.CardTypes.Count,
                    GameDuration = game.GameDuration
                };
                db.Games.Add(dbGame);
                db.SaveChanges();

                var dbMyGameRound = new GameRound()
                {
                    GamePlayer     = dbMe,
                    Game           = dbGame,
                    GameId         = dbGame.Id,
                    MovesCount     = me.MovesCount,
                    PlayerNickName = me.NickName,
                    Result         = myResult
                };

                var dbOpponentRound = new GameRound()
                {
                    GamePlayer     = dbOther,
                    Game           = dbGame,
                    GameId         = dbGame.Id,
                    MovesCount     = other.MovesCount,
                    PlayerNickName = other.NickName,
                    Result         = opponentsResult
                };
                db.GameRounds.Add(dbMyGameRound); //add rounds
                db.GameRounds.Add(dbOpponentRound);

                dbGame.PlayedRounds.Add(dbMyGameRound); //add games reference
                dbGame.PlayedRounds.Add(dbOpponentRound);

                dbMe?.PlayerRounds.Add(dbMyGameRound); //add players reference
                dbOther?.PlayerRounds.Add(dbOpponentRound);

                db.SaveChanges(); //save
            }
        }
Beispiel #4
0
        public ActionResult <Game> Update(Game game)
        {
            using var gameDataContext = new GameDataContext();
            var gameData = gameDataContext.GameDatas.Where(g => g.Id == game.Id).SingleOrDefault();

            gameData.Date = game.Date;
            gameDataContext.SaveChanges();
            using var matchContext = new MatchContext();
            var matches = matchContext.Matches.Where(m => m.GameId == game.Id);

            matchContext.Matches.RemoveRange(matches);
            foreach (var player in game.Team1)
            {
                matchContext.Add(new Match
                {
                    GameId = game.Id,
                    Player = player,
                    Team   = 1,
                });
            }
            foreach (var player in game.Team2)
            {
                matchContext.Add(new Match
                {
                    GameId = game.Id,
                    Player = player,
                    Team   = 2,
                });
            }
            matchContext.SaveChanges();
            using var scoreContext = new ScoreContext();
            var score = scoreContext.Scores.Where(s => s.GameId == game.Id).Single();

            if (game.Score == '1')
            {
                score.Result = 1f;
            }
            else if (game.Score == '2')
            {
                score.Result = 0f;
            }
            else if (game.Score == 'D')
            {
                score.Result = 0.5f;
            }
            else if (game.Score == 'C')
            {
                score.Result = -1f;
            }
            else
            {
                score.Result = null;
            }
            scoreContext.SaveChanges();
            return(CreatedAtAction(nameof(GetById), new { id = game.Id }, game));
        }
Beispiel #5
0
        public HttpResponseMessage PostChallengeQuestion(ChallengeQuestion obj)
        {
            if (obj == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            try
            {
                db.ChallengeQuestions.Add(obj);
                db.SaveChanges();
                var result = obj;
                return(Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Falha ao incluir Pergunta-Desafio."));
            }
        }
        public JsonResult ChangeStatNote(string passednote, int note_id, int statID)
        {
            DateTime time = DateTime.Now;

            //no note exists
            if (note_id == 0)
            {
                StatNotes note_from_db = new StatNotes
                {
                    Note         = passednote,
                    StatID       = statID,
                    TimeModified = time,
                };
                _gamedatacontext.StatNotes.Add(note_from_db);
            }
            //updating a note
            else
            {
                StatNotes note_from_db = _gamedatacontext.StatNotes.Where(o => o.NoteID == note_id).Single();
                note_from_db.Note         = passednote;
                note_from_db.TimeModified = time;
                _gamedatacontext.Update(note_from_db);
            }
            _gamedatacontext.SaveChanges();

            if (note_id == 0)
            {
                note_id = _gamedatacontext.StatNotes.Where(o => o.Note == passednote).Single().NoteID;
            }

            return(Json(new
            {
                success = true,
                note = passednote,
                id = note_id,
                time = time.ToString()
            }));
        }
Beispiel #7
0
        public HttpResponseMessage PostLogin(Game.Domain.User userlogin)
        {
            string responseMessage = string.Empty;

            if (userlogin.Password == string.Empty || userlogin.Email == string.Empty)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Preencha os campos de login!"));
            }
            try
            {
                User user = new Game.Domain.User();
                user = (from u in db.Users
                        where userlogin.Email == u.Email &&
                        userlogin.Password == u.Password
                        select u)
                       .FirstOrDefault <User>();

                if (user != null)
                {
                    //VALIDA SE USUARIO JÁ POSSUI SESSAO ATIVA
                    Session session = db.Sessions.Where(x => x.UserId == user.Id && x.IsActive == true).FirstOrDefault <Session>();
                    if (session == null)
                    {
                        // SE NÃO TEM, CRIA UMA NOVA
                        db.Sessions.Add(new Session {
                            IsActive = true, UserId = user.Id
                        });
                        db.SaveChanges();
                        session = db.Sessions.Where(x => x.UserId == user.Id && x.IsActive == true).FirstOrDefault <Session>();
                    }
                    return(Request.CreateResponse(HttpStatusCode.OK, session));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Usuário não encontrado!"));
                }
            }
            catch (Exception)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Falha ao fazer login."));
            }
        }
Beispiel #8
0
 public bool RegisterPlayer(string playerNick)
 {
     using (var db = new GameDataContext())
     {
         if (db.GamePlayers.Count(player => player.NickName == playerNick) == 0)
         {
             var pl = new GamePlayer()
             {
                 NickName = playerNick
             };
             db.GamePlayers.Add(pl);
             db.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Beispiel #9
0
        public ActionResult <Game> Create(Game game)
        {
            using var gameDataContext = new GameDataContext();
            using var matchContext    = new MatchContext();
            long id = (gameDataContext.GameDatas.Max(m => (int?)m.Id) ?? 0) + 1;

            game.Id = id;
            if (game.Date == null)
            {
                game.Date = DateTime.Now.ToString("u", System.Globalization.CultureInfo.InvariantCulture);
            }
            gameDataContext.Add(new GameData {
                Id   = id,
                Date = game.Date,
            });
            gameDataContext.SaveChanges();
            var matches = new List <Match>();

            foreach (var player in game.Team1)
            {
                matches.Add(new Match
                {
                    GameId = id,
                    Player = player,
                    Team   = 1,
                });
            }
            foreach (var player in game.Team2)
            {
                matches.Add(new Match
                {
                    GameId = id,
                    Player = player,
                    Team   = 2,
                });
            }
            matchContext.AddRange(matches);
            matchContext.SaveChanges();
            using var scoreContext = new ScoreContext();
            float?result = null;

            if (game.Score == '1')
            {
                result = 1;
            }
            else if (game.Score == '2')
            {
                result = 0;
            }
            else if (game.Score == 'D')
            {
                result = 0.5f;
            }
            else if (game.Score == 'C')
            {
                result = -1;
            }
            scoreContext.Add(new Score
            {
                GameId = id,
                Result = result,
            });
            scoreContext.SaveChanges();
            return(CreatedAtAction(nameof(GetById), new { id = id }, game));
        }
Beispiel #10
0
        private static void SeedDb()
        {
            using (var context = new GameDataContext())
            {
                #region SaveFiles
                var saveFile = new SaveFile
                {
                    Id          = new Guid(),
                    LastSavedOn = DateTime.Now,
                };
                context.Add(saveFile);
                #endregion SaveFiles

                #region Heroes
                var basicAttackSkill = new Skill
                {
                    Id       = new Guid(),
                    Name     = "Basic Attack",
                    Cost     = 0,
                    Damage   = 2,
                    Accuracy = 1,
                    SaveFile = saveFile,
                };
                context.Add(basicAttackSkill);

                var fireballSkill = new Skill
                {
                    Id       = new Guid(),
                    Name     = "Fireball",
                    Cost     = 2,
                    Damage   = 4,
                    Accuracy = 1,
                    SaveFile = saveFile,
                };
                context.Add(fireballSkill);

                var waitSkill = new Skill
                {
                    Id       = new Guid(),
                    Name     = "Wait",
                    Cost     = 0,
                    Damage   = 0,
                    Accuracy = 1,
                    SaveFile = saveFile,
                };
                context.Add(waitSkill);

                var endgameSkill = new Skill
                {
                    Id       = new Guid(),
                    Name     = "Endgame",
                    Cost     = 0,
                    Damage   = 200,
                    Accuracy = 1,
                    SaveFile = saveFile,
                };
                context.Add(endgameSkill);

                var hero = new Heroes
                {
                    Id               = new Guid(),
                    SaveFile         = saveFile,
                    Name             = "Roland",
                    Health           = 10,
                    Mana             = 5,
                    Skill1Navigation = basicAttackSkill,
                    Skill2Navigation = fireballSkill,
                    Skill3Navigation = waitSkill,
                    Skill4Navigation = endgameSkill
                };
                context.Add(hero);
                #endregion Heroes

                #region Monsters
                var stab = new Skill
                {
                    Name     = "Stab",
                    Cost     = 0,
                    Damage   = 6,
                    Accuracy = .9,
                    SaveFile = saveFile,
                };
                context.Add(stab);

                var goblin = new Monsters
                {
                    Id               = new Guid(),
                    Name             = "Goblin",
                    Health           = 3,
                    Skill1Navigation = stab,
                };
                context.Add(goblin);

                var rockSlam = new Skill
                {
                    Name     = "Rock Slam",
                    Cost     = 0,
                    Damage   = 2,
                    Accuracy = .7,
                    SaveFile = saveFile,
                };
                context.Add(rockSlam);

                var golem = new Monsters
                {
                    Id               = new Guid(),
                    Name             = "Golem",
                    Health           = 9,
                    Skill1Navigation = rockSlam,
                };
                context.Add(golem);

                var slash = new Skill
                {
                    Name     = "Slash",
                    Cost     = 0,
                    Damage   = 3,
                    Accuracy = .8,
                    SaveFile = saveFile,
                };
                context.Add(slash);

                var skeleton = new Monsters
                {
                    Id               = new Guid(),
                    Name             = "Skeleton",
                    Health           = 7,
                    Skill1Navigation = slash,
                };
                context.Add(skeleton);
                #endregion Monsters

                context.SaveChanges();
            }
        }