public IActionResult UpdateLeaveGame([FromBody] Validators.Game GameLeave)
        {
            Result result = this.LeaveGame(GameLeave);

            if (result.ResultFunc == this.Ok)
            {
                PusherServer.Pusher pusher = Pusher.Pusher.Create();
                pusher.TriggerAsync(channelName: this.PRIVATE_GAME_CHANNELNAME + ((GameData)result.Data).id, eventName: "game_leave", data: result.Data, options: new TriggerOptions()
                {
                    SocketId = GameLeave.socket_id
                });
            }

            return(result.ResultFunc(result.Data));
        }
        public Result LeaveGame(Validators.Game gameLeave)
        {
            if (ModelState.IsValid == false)
            {
                return(new Result {
                    ResultFunc = this.BadRequest, Data = "Incorrect post data"
                });
            }

            User user = (User)HttpContext.Items["user"];
            Game game = this.db.Games.Where(x => x.id == gameLeave.id).Include(x => x.User1).FirstOrDefault();

            //Check if game exists.
            if (game == null)
            {
                return(new Result {
                    ResultFunc = this.BadRequest, Data = "Game does not exist."
                });
            }

            //Chekc if the user is playing in this game.
            if (game.User1 != user && game.User2 != user)
            {
                return(new Result {
                    ResultFunc = this.BadRequest, Data = "This is not your game."
                });
            }

            //check if the game has finished
            if (game.status == GameStatus.finished || game.status == GameStatus.canceled)
            {
                return(new Result {
                    ResultFunc = this.BadRequest, Data = "This game has finished."
                });
            }

            game.status      = GameStatus.canceled;
            game.finished_at = DateTime.Now;
            this.db.SaveChanges();

            return(new Result {
                ResultFunc = this.Ok, Data = this.FilterGameResults(game)
            });
        }
        public Result JoinGame(Validators.Game gameJoin)
        {
            if (ModelState.IsValid == false)
            {
                return(new Result {
                    ResultFunc = this.BadRequest, Data = "Incorrect post data"
                });
            }

            User user = (User)HttpContext.Items["user"];
            Game game = this.db.Games.Where(x => x.id == gameJoin.id).Include(x => x.User1).FirstOrDefault();

            //Check if game exists.
            if (game == null)
            {
                return(new Result {
                    ResultFunc = this.BadRequest, Data = "Game does not exist."
                });
            }

            //Check if the user is trying to join a game that he created.
            if (game.User1 == user)
            {
                return(new Result {
                    ResultFunc = this.BadRequest, Data = "You can't play against yourself."
                });
            }

            //Check if the game is waiting for an invited user.
            if (game.status == GameStatus.waiting_invite)
            {
                //Check if the user that tries to join is indeed the invited user.
                if (game.user2_id == user.id)
                {
                    game.status = GameStatus.started;
                    this.db.SaveChanges();
                    return(new Result {
                        ResultFunc = this.Ok, Data = this.FilterGameResults(game)
                    });
                }
                return(new Result {
                    ResultFunc = this.BadRequest, Data = "Can't join game, you were not invited."
                });
            }

            //Check if the game is waiting for a random user.
            if (game.status == GameStatus.waiting_join)
            {
                game.status = GameStatus.started;
                game.User2  = user;
                this.db.SaveChanges();
                return(new Result {
                    ResultFunc = this.Ok, Data = this.FilterGameResults(game)
                });
            }

            //Game status is probably started or finished.
            return(new Result {
                ResultFunc = this.BadRequest, Data = "Can't join game. Game is either finished or started."
            });
        }