Пример #1
0
    public void UpdateServerPlayerWon(int gameID, DTO_Player wonPlayer)
    {
        //find game for this player
        var query = from gp in db.GamePlays
                    where gp.GAME_ID == gameID
                    select gp;


        //get game from db and change its status
        GamePlay gpFromDB = null;

        foreach (GamePlay gamePlay in query)
        {
            gpFromDB = gamePlay;
            try
            {
                gpFromDB.Status = Application_Constants.GAME_STATUS_END;
                db.SubmitChanges();
            }
            catch (ChangeConflictException)
            {
                foreach (ObjectChangeConflict objchangeconf in db.ChangeConflicts)
                {
                    objchangeconf.Resolve(RefreshMode.OverwriteCurrentValues);
                }
            }
        }


        //notify other player that game ended and he lose
        games[gpFromDB.GAME_ID].OnPlayerWon(wonPlayer);
    }
        protected void OnExistGameClick(object sender, EventArgs e)
        {
            //rest start game message if exists
            GeneralErrorStartGame.Text = "";

            DTO_GamePlay selectedGame    = ((DTO_GamePlay[])Session[WebSiteManager.WATING_GAMES_LIST_SESSION])[gamesGV.SelectedRow.RowIndex];
            DTO_ACCOUNT  loggedInAccount = (DTO_ACCOUNT)(Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION]);
            DTO_Player   joiningPlayer   = loggedInAccount.players[PlayerList2.SelectedIndex];


            if (selectedGame.hostPlayerID == joiningPlayer.Id)
            {
                GeneralError.ForeColor = Color.Red;
                GeneralError.Text      = "You are owner of this game,You allowed to close the game";
            }
            else if (selectedGame.joinedPlayerID != joiningPlayer.Id)
            {
                GeneralError.ForeColor = Color.Red;
                GeneralError.Text      = "You are not player in this game";
            }
            else
            {
                WebSiteManager.ExistGame(selectedGame.Key);
                loadWatingGames();
                // first time page loades we color the tabel for player at index 0
                ColorGamesTableRows(PlayerList2.SelectedIndex);
                gamesGridViewPanel.Update();
            }
        }
Пример #3
0
        protected void AddPlayer_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(firstnameTB.Text) && !string.IsNullOrWhiteSpace(lastnameTB.Text))
            {
                DTO_Player p = new DTO_Player {
                    FirstName = firstnameTB.Text, LastName = lastnameTB.Text
                };

                List <DTO_Player> accountPLayers = (List <DTO_Player>)Session["accountPLayers"];
                if (accountPLayers == null)
                {
                    accountPLayers = new List <DTO_Player>();
                }
                accountPLayers.Add(p);


                Session["accountPLayers"] = accountPLayers;

                // show the scrollable div of players
                playersDiv.Style["display"] = "inline";
                // add this player to this div
                playersList.InnerHtml = playersList.InnerHtml + "<li style=\"margin:0; position: relative; left: 0;\" class=\"list-group-item\">" + firstnameTB.Text + " " + lastnameTB.Text + "</li>";
                // close the dialog
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "addPlayersDialogModel", "$('#addPlayersDialogModel').modal('toggle');", true);
                // hide error if was
                playersError.Style["display"] = "none";
            }
            else
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "addPlayersDialogModel", "$('#addPlayersDialogModel').modal();", true);
                upModal.Update();
            }
        }
        protected void OnJoinGameClick(object sender, EventArgs e)
        {
            // rest start game messages if exist
            GeneralErrorStartGame.Text = "";
            // set join game messages color
            GeneralError.ForeColor = Color.Red;

            // get joining player id
            DTO_ACCOUNT loggedInAccount = (DTO_ACCOUNT)(Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION]);
            DTO_Player  joiningPlayer   = loggedInAccount.players[PlayerList2.SelectedIndex];

            DTO_GamePlay selectedGameInTable;

            // check if there is game selected
            if (gamesGV.SelectedRow == null)
            {
                GeneralError.Text = "Please Select Game";
                return;
            }
            else
            {
                selectedGameInTable = ((DTO_GamePlay[])Session[WebSiteManager.WATING_GAMES_LIST_SESSION])[gamesGV.SelectedRow.RowIndex];
            }


            //validate that user not hosting game allready or not joined to other game
            if (isPlayerHostingOrJoinedGame(joiningPlayer.Id))
            {
                GeneralError.Text = "You allready registarted to game";
            }
            // check if game is full
            else if (selectedGameInTable.joinedPlayerID != null)
            {
                GeneralError.Text = "The Game is full";
            }
            // check that selected game host player is not joining player (we wont join to ourself game)
            else if (joiningPlayer.Id == selectedGameInTable.hostPlayerID)
            {
                GeneralError.Text = "Cannot join to own game";
            }
            // check that joinng player not playing vs own group of players
            else if (joiningPlayer.AccountEmail == selectedGameInTable.hostPlayerAccountEmail)
            {
                GeneralError.Text = "This game statred by your team members,Cannot play vs your own team";
            }
            else
            {
                // updated database games table with joined player to this game
                int gameKey = WebSiteManager.JoinGame(selectedGameInTable.hostPlayerID.ToString(), joiningPlayer.Id.ToString());

                loadWatingGames();
                // first time page loades we color the tabel for player at index 0
                ColorGamesTableRows(PlayerList2.SelectedIndex);
                gamesGridViewPanel.Update();

                GeneralError.ForeColor = Color.Green;
                GeneralError.Text      = "Successfully joined game, Key : " + gameKey;
            }
        }
Пример #5
0
    public void UnregisterClient(int gameID, DTO_Player player, bool isGameOver)
    {
        //get game for this player
        var query = from gp in db.GamePlays
                    where gp.GAME_ID == gameID
                    select gp;


        GamePlay gpFromDB = null;

        foreach (GamePlay gamePlay in query)
        {
            gpFromDB = gamePlay;
        }

        if (!games.ContainsKey(gpFromDB.GAME_ID))
        {
            return;
        }

        //in case one of the clients disconnected from game isGameOver equals false
        // means client disconnected in middle of game, we notify other client with game over
        // and remove him from the clients list
        if (!isGameOver)
        {
            games[gpFromDB.GAME_ID].onClientDisconnect(player);
            try
            {
                gpFromDB.Status = Application_Constants.GAME_STATUS_DISCONNECTED;
                db.SubmitChanges();
            }
            catch (ChangeConflictException)
            {
                foreach (ObjectChangeConflict objchangeconf in db.ChangeConflicts)
                {
                    objchangeconf.Resolve(RefreshMode.KeepCurrentValues);
                }
            }
        }
        else
        {
            try
            {
                gpFromDB.Status = Application_Constants.GAME_STATUS_END;
                db.SubmitChanges();
            }
            catch (ChangeConflictException)
            {
                foreach (ObjectChangeConflict objchangeconf in db.ChangeConflicts)
                {
                    objchangeconf.Resolve(RefreshMode.OverwriteCurrentValues);
                }
            }
        }

        // finally remove this game from games list
        games.Remove(gpFromDB.GAME_ID);
    }
Пример #6
0
    public List <DTO_Player> GetPlayersByGameID(string gmID)
    {
        int gameID = Int32.Parse(gmID);

        var gamePlay = from gp in db.GamePlays
                       where gp.GAME_ID == gameID
                       select gp;

        List <DTO_Player> players = new List <DTO_Player>();
        string            playerAccountEmail, accName;

        foreach (GamePlay gp in gamePlay)
        {
            if (gp.Player != null)
            {
                playerAccountEmail = gp.Player.AccountEmail;
                accName            = (from a in db.Accounts where a.EMAIL == playerAccountEmail select new DTO_ACCOUNT {
                    NAME = a.NAME
                }).FirstOrDefault().NAME;

                DTO_Player p1 = new DTO_Player
                {
                    FirstName    = gp.Player.FirstName,
                    LastName     = gp.Player.LastName,
                    Id           = gp.Player.Id,
                    AccountEmail = gp.Player.AccountEmail,
                    TEAM_NAME    = accName
                };

                players.Add(p1);
            }

            if (gp.Player1 != null)
            {
                playerAccountEmail = gp.Player1.AccountEmail;
                accName            = (from a in db.Accounts where a.EMAIL == playerAccountEmail select new DTO_ACCOUNT {
                    NAME = a.NAME
                }).FirstOrDefault().NAME;

                DTO_Player p2 = new DTO_Player
                {
                    FirstName    = gp.Player1.FirstName,
                    LastName     = gp.Player1.LastName,
                    Id           = gp.Player1.Id,
                    AccountEmail = gp.Player1.AccountEmail,
                    TEAM_NAME    = accName
                };

                players.Add(p2);
            }
        }


        return(players);
    }
Пример #7
0
    public DTO_RESTORED_GAME GetRestoredGame(int gameID)
    {
        // get this game movments
        DTO_MOVEMENT[] movements = (from movement in db.Movements
                                    where movement.Game_ID == gameID
                                    select new DTO_MOVEMENT {
            Id = movement.Id, Checker_ID = movement.Checker_ID, Col = movement.Col, Game_ID = movement.Game_ID, Row = movement.Row
        }).ToArray();

        //find game for this player
        var query = from gp in db.GamePlays
                    where gp.GAME_ID == gameID
                    select gp;

        GamePlay gpFromDB = null;

        foreach (GamePlay gamePlay in query)
        {
            gpFromDB = gamePlay;
        }

        //get this game players
        DTO_Player p1 = (from pl in db.Players
                         where pl.Id == gpFromDB.Player1.Id
                         select new DTO_Player {
            Id = pl.Id, FirstName = pl.FirstName, LastName = pl.LastName
        }).FirstOrDefault();


        DTO_Player p2 = (from pl in db.Players
                         where pl.Id == gpFromDB.Player.Id
                         select new DTO_Player {
            Id = pl.Id, FirstName = pl.FirstName, LastName = pl.LastName
        }).FirstOrDefault();

        //find team names for this players
        string q1 = (from ac in db.Accounts
                     where ac.EMAIL == gpFromDB.Player1.AccountEmail
                     select ac.NAME).FirstOrDefault();

        string q2 = (from ac in db.Accounts
                     where ac.EMAIL == gpFromDB.Player.AccountEmail
                     select ac.NAME).FirstOrDefault();

        p1.TEAM_NAME = q1;
        p2.TEAM_NAME = q2;



        return(new DTO_RESTORED_GAME {
            gameStatus = gpFromDB.Status, movements = movements, players = new DTO_Player[] { p1, p2 }
        });
    }
Пример #8
0
 // updates server on game won (the server updates the opponent player)
 public static void UpdateServerPlayerWon(DTO_Player player)
 {
     if (GameController.gameType != Constants.GAME_TYPE_PLAY_ONLINE)
     {
         return;
     }
     duplexServiceClient.UpdateServerPlayerWon(GameController.currentPlayingGameID, player);
     if (GameController.loggedInAccountPlayerPlaying != null)
     {
         UnregisterClientOnServer();
     }
 }
Пример #9
0
    public void NotifyReadyToPlay(int gameID, DTO_Player player)
    {
        //find game for this player
        var query = from gp in db.GamePlays
                    where gp.GAME_ID == gameID && gp.Status == Application_Constants.GAME_STATUS_WATING_FOR_BEGIN
                    select gp;

        GamePlay gpFromDB = null;

        foreach (GamePlay gamePlay in query)
        {
            gpFromDB = gamePlay;
        }

        games[gpFromDB.GAME_ID].readyClients++;


        if (games[gpFromDB.GAME_ID].readyClients == Application_Constants.nPlayersForGameStart)
        {
            //find team names for this players
            string q1 = (from ac in db.Accounts
                         where ac.EMAIL == gpFromDB.Player1.AccountEmail
                         select ac.NAME).FirstOrDefault();

            string q2 = (from ac in db.Accounts
                         where ac.EMAIL == gpFromDB.Player.AccountEmail
                         select ac.NAME).FirstOrDefault();



            games[gpFromDB.GAME_ID].notifyClientsWithStartGame(new DTO_Player {
                TEAM_NAME = q1, Id = gpFromDB.Player1.Id, FirstName = gpFromDB.Player1.FirstName, LastName = gpFromDB.Player1.LastName
            }, new DTO_Player {
                TEAM_NAME = q2, Id = gpFromDB.Player.Id, FirstName = gpFromDB.Player.FirstName, LastName = gpFromDB.Player.LastName
            });
            //change status of gameplay on db
            try {
                gpFromDB.Status = Application_Constants.GAME_STATUS_PLAYING;
                db.SubmitChanges();
            }catch (ChangeConflictException)
            {
                foreach (ObjectChangeConflict objchangeconf in db.ChangeConflicts)
                {
                    objchangeconf.Resolve(RefreshMode.OverwriteCurrentValues);
                }
            }
        }
    }
Пример #10
0
        // called from server whenever both client called NotifyReadyToPlay method
        public void UpdateClientGameStartOk(DTO_Player whiteCheckersPlayer, DTO_Player blackCheckersPlayer)
        {
            GameController.opponents[0].setOpponentText(whiteCheckersPlayer.TEAM_NAME, whiteCheckersPlayer.FirstName + " " + whiteCheckersPlayer.LastName);
            GameController.opponents[1].setOpponentText(blackCheckersPlayer.TEAM_NAME, blackCheckersPlayer.FirstName + " " + blackCheckersPlayer.LastName);
            GameController.isPlaying = true;
            GameController.showOpponnetsOnGameStart();

            if (GameController.thisPlayerType == Constants.whiteChecker)
            {
                GameController.loggedInAccountPlayerPlaying.TEAM_NAME = whiteCheckersPlayer.TEAM_NAME;
            }
            else
            {
                GameController.loggedInAccountPlayerPlaying.TEAM_NAME = blackCheckersPlayer.TEAM_NAME;
            }
        }
Пример #11
0
    public void UpdateServerWithCheckerMove(int gameID, DTO_Player player, DTO_Checker checker, string eatenCheckerID)
    {
        //get game for this player
        var query = from gp in db.GamePlays
                    where gp.GAME_ID == gameID
                    select gp;

        GamePlay gpFromDB = null;

        foreach (GamePlay gamePlay in query)
        {
            gpFromDB = gamePlay;
        }

        games[gpFromDB.GAME_ID].updateClientOnCheckerMove(player, checker, eatenCheckerID);
    }
Пример #12
0
    public void RegisterClient(int gameID, DTO_Player player)
    {
        IClientCallBack client            = OperationContext.Current.GetCallbackChannel <IClientCallBack>();
        bool            isRegisterSucceed = true;

        //find game for this player
        var query = from gp in db.GamePlays
                    where gp.GAME_ID == gameID
                    select gp;


        //get game
        GamePlay gpFromDB = null;

        foreach (GamePlay gamePlay in query)
        {
            gpFromDB = gamePlay;
        }

        //check if this game allready exist in games list, if exist its just add to it the clien
        // if not create game

        if (games.ContainsKey(gpFromDB.GAME_ID))
        {
            ////check if this client allready registared
            //if (games[gpFromDB.GAME_ID].isClientAllredyConnected(player))
            //{
            //    isRegisterSucceed= false;
            //    client.NotifyRegistarClientStatus(isRegisterSucceed);
            //    return;
            //}
            //else
            //{
            games[gpFromDB.GAME_ID].addClient(player, client);
            //  }
        }
        else
        {
            Game g = new Game(gpFromDB.GAME_ID);
            g.addClient(player, client);
            games.Add(gpFromDB.GAME_ID, g);
        }

        games[gpFromDB.GAME_ID].CheckForGameStart(client);
        client.NotifyRegistarClientStatus(isRegisterSucceed);
    }
Пример #13
0
    public DTO_Player FindGameAndReturnPlayerInfo(int gameID, string accountEmail)
    {
        // find game
        var gameFound = (from gm in db.GamePlays
                         where gm.GAME_ID == gameID && gm.Status == Application_Constants.GAME_STATUS_WATING_FOR_BEGIN
                         select new DTO_GamePlay {
            Key = gm.GAME_ID, gameName = gm.Name, createDate = gm.Start_Date, hostPlayerID = gm.Host_Player_ID, joinedPlayerID = gm.Join_Player_ID
        }).FirstOrDefault();

        if (gameFound == null)
        {
            return(null);
        }


        //check if one of players in this account participate in this game
        var query = from a in db.Accounts
                    where (a.Players.Any(p => p.Id == gameFound.hostPlayerID) || a.Players.Any(p => p.Id == gameFound.joinedPlayerID)) && a.EMAIL.Equals(accountEmail)
                    select a;

        if (!query.Any())
        {
            return(null);
        }

        // get player
        DTO_Player player = (from p in db.Players
                             where (p.Id == gameFound.hostPlayerID || p.Id == gameFound.joinedPlayerID) && p.AccountEmail.Equals(accountEmail)
                             select new DTO_Player {
            Id = p.Id, FirstName = p.FirstName, LastName = p.LastName
        }).FirstOrDefault();

        // is this player hosts(white checkers) game or joined(black checkers) to game
        if (gameFound.hostPlayerID == player.Id)
        {
            player.Type = Application_Constants.whiteChecker;
        }
        else
        {
            player.Type = Application_Constants.blackChecker;
        }

        return(player);
    }
        // when one of the players dissconneted from server (game crash,connection problem etc...)
        public static void OnOpponentDissconnected()
        {
            GameForm form1Ref = (GameForm)Application.OpenForms["GameForm"];

            if (form1Ref.loadingLabel.InvokeRequired)
            {
                OnOpponentDissconnectedCallBack cback = new OnOpponentDissconnectedCallBack(OnOpponentDissconnected);
                form1Ref.loadingLabel.Invoke(cback, new object[] { });
            }
            else
            {
                isPlaying            = false;
                isThisPlayerTurn     = false;
                opponents[0].Visible = false;
                opponents[1].Visible = false;
                form1Ref.loadingLabel.StopAnimation();
                form1Ref.loadingLabel.Visible = true;
                form1Ref.loadingLabel.setTextNoneUiThread(Constants.OPPONENT_DISCONNECTED, Color.Red);
                loggedInAccountPlayerPlaying = null;
            }
        }
 public static void updateCurrentOpponentVisibility(DTO_Player player)
 {
     if (GameController.opponents[0].InvokeRequired)
     {
         updateCurrentOpponentVisibilityCallBack cback = new updateCurrentOpponentVisibilityCallBack(updateCurrentOpponentVisibility);
         GameController.opponents[0].Invoke(cback, new object[] { player });
     }
     else
     {
         if (GameController.thisPlayerType == Constants.whiteChecker)
         {
             GameController.opponents[0].Visible = true;
             GameController.opponents[1].Visible = false;
         }
         else
         {
             GameController.opponents[1].Visible = true;
             GameController.opponents[0].Visible = false;
         }
     }
 }
        protected void OnStartGameClick(object sender, EventArgs e)
        {
            if (isPlayerHostingOrJoinedGame(((DTO_ACCOUNT)Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION]).players[PlayerList.SelectedIndex].Id))
            {
                GeneralErrorStartGame.ForeColor = Color.Red;
                GeneralErrorStartGame.Text      = "You Allready registared to game";
                return;
            }

            try
            {
                DTO_ACCOUNT  loggedInAccount = (DTO_ACCOUNT)(Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION]);
                DTO_Player   hostGamePlayer  = loggedInAccount.players[PlayerList.SelectedIndex];
                DTO_GamePlay game            = new DTO_GamePlay {
                    gameName = GameNameTB.Text, hostPlayerID = hostGamePlayer.Id, createDate = DateTime.Now.ToLocalTime()
                };
                int gameKey = WebSiteManager.AddGame(game);

                // load the wating games from db and bind to girdview table
                loadWatingGames();
                // first time page loades we color the tabel for player at index 0
                ColorGamesTableRows(PlayerList2.SelectedIndex);
                gamesGridViewPanel.Update();

                GeneralErrorStartGame.ForeColor = Color.Green;
                GeneralErrorStartGame.Text      = "Game successfully registared, Key : " + gameKey;
            }
            catch (WebException ex)
            {
                using (WebResponse response = ex.Response)
                {
                    var httpResponse = (HttpWebResponse)response;

                    using (Stream data = response.GetResponseStream())
                    {
                        StreamReader sr = new StreamReader(data);
                    }
                }
            }
        }
Пример #17
0
        // called from server whenever game over and there is winner
        public void UpdateClientWithGameWinner(DTO_Player wonPlayer)
        {
            GameForm gameForm = (GameForm)Application.OpenForms["GameForm"];

            gameForm.OnGameWon(wonPlayer);
        }