public void ColorGamesTableRows(int selectedIndexPlayerForJoinID)
        {
            DTO_ACCOUNT loggedInAccount         = (DTO_ACCOUNT)Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION];
            int         selectedPlayerForJoinID = loggedInAccount.players[selectedIndexPlayerForJoinID].Id;

            foreach (GridViewRow r in gamesGV.Rows)
            {
                if (r.RowType == DataControlRowType.DataRow)
                {
                    // IDS From row in table
                    int            hostingGamePlayerID = ((DTO_GamePlay[])Session[WebSiteManager.WATING_GAMES_LIST_SESSION])[r.RowIndex].hostPlayerID;
                    Nullable <int> joinedGamePlayerID  = ((DTO_GamePlay[])Session[WebSiteManager.WATING_GAMES_LIST_SESSION])[r.RowIndex].joinedPlayerID;

                    // avoid comparing int to null (selectedPlayerForJoinID == joinedGamePlayerID)
                    if (joinedGamePlayerID == null)
                    {
                        joinedGamePlayerID = -1;
                    }

                    if (hostingGamePlayerID != -1 && joinedGamePlayerID != -1)
                    {
                        // this case means the game started
                        r.BackColor = ColorTranslator.FromHtml(WebSiteManager.GAME_STATUS_PLAYING_COLOR);
                    }
                    else if (selectedPlayerForJoinID == hostingGamePlayerID || selectedPlayerForJoinID == joinedGamePlayerID)
                    {
                        r.BackColor = ColorTranslator.FromHtml(WebSiteManager.GAME_STATUS_REGISTARED_COLOR);
                    }
                    else
                    {
                        r.BackColor = ColorTranslator.FromHtml(WebSiteManager.GAME_STATUS_WATING_COLOR);
                    }
                }
            }
        }
        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();
            }
        }
        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;
            }
        }
Пример #4
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            List <DTO_Player> accountPLayers = (List <DTO_Player>)Session["accountPLayers"];

            // check if players added if no show error message
            if (accountPLayers.Count == 0)
            {
                playersError.Style["display"] = "inline";
                return;
            }
            else if (WebSiteManager.IsEmailExist(Email.Text))
            {
                GeneralError.Text = "Email allready in exists.";
                return;
            }
            else
            {
                // create new user and add to db , in case faild validation errors will be on page.
                try
                {
                    if (IsValid)
                    {
                        // register account to DB
                        // create Hash from user password
                        string hashedPassword = WebSiteManager.CreateHash(Password.Text, WebSiteManager.PASSWORD_HASH_SALT);
                        // create account entitiy
                        DTO_ACCOUNT a = new DTO_ACCOUNT {
                            EMAIL = Email.Text, PASSWORD = hashedPassword, NAME = nickname.Text
                        };
                        // this players not include there ids from db
                        a.players = accountPLayers.ToArray();
                        // add to db
                        WebSiteManager.RegisterAccount(a);
                        // getaccount players with there ids from db
                        a.players = WebSiteManager.GetPlayers(a.EMAIL);
                        // store logged in account and transfer to homepage
                        Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION] = new DTO_ACCOUNT {
                            EMAIL = a.EMAIL, PASSWORD = a.PASSWORD, NAME = a.NAME, players = a.players
                        };


                        Server.Transfer("../Default.aspx", true);
                    }
                }
                catch (WebException ex)
                {
                    using (WebResponse response = ex.Response)
                    {
                        var httpResponse = (HttpWebResponse)response;

                        using (Stream data = response.GetResponseStream())
                        {
                            StreamReader sr = new StreamReader(data);
                            GeneralError.Text = sr.ReadToEnd();
                        }
                    }
                }
            }
        }
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DTO_ACCOUNT loggedInAccount = (DTO_ACCOUNT)Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION];
                Email.Text    = loggedInAccount.EMAIL;
                nickname.Text = loggedInAccount.NAME;
            }

            OldPassword.Text = "";
            NewPassword.Text = "";
        }
Пример #6
0
        // updates account information on edit profile,games,players
        public static void UpdateAccount(DTO_ACCOUNT a)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DTO_ACCOUNT));
            MemoryStream stream            = new MemoryStream();

            ser.WriteObject(stream, a);
            string    data      = Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length);
            WebClient webClient = new WebClient();

            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadString(SERVICE_URL + "/UpdateAccount", "POST", data);
        }
Пример #7
0
    // methods documentation is found in Service interfaces class (Iservice.cs file)

    public void UpdateAccount(DTO_ACCOUNT account)
    {
        var accountForUpdate = from acc in db.Accounts
                               where acc.EMAIL == account.EMAIL
                               select acc;

        foreach (Account acc in accountForUpdate)
        {
            acc.EMAIL    = account.EMAIL;
            acc.PASSWORD = account.PASSWORD;
            acc.NAME     = account.NAME;
        }

        db.SubmitChanges();
    }
Пример #8
0
        public static DTO_ACCOUNT Login(string email, string password)
        {
            string      passwordHashed = CreateHash(password, Constants.PASSWORD_HASH_SALT);
            DTO_ACCOUNT account        = duplexServiceClient.ClientLogin(email, passwordHashed);

            if (account == null)
            {
                return(null);
            }
            else
            {
                account.players = duplexServiceClient.ClientGetPlayers(account.EMAIL);
                return(account);
            }
        }
Пример #9
0
    public void RegisterAccount(DTO_ACCOUNT account)
    {
        var           playersEntitiySet = new EntitySet <Player>();
        List <Player> playersList       = new List <Player>();

        foreach (DTO_Player dtoplayer in account.players)
        {
            playersList.Add(new Player {
                Id = dtoplayer.Id, FirstName = dtoplayer.FirstName, LastName = dtoplayer.LastName
            });
        }

        playersEntitiySet.AddRange(playersList);
        db.Accounts.InsertOnSubmit(new Account {
            EMAIL = account.EMAIL, NAME = account.NAME, PASSWORD = account.PASSWORD, Players = playersEntitiySet
        });
        db.SubmitChanges();
    }
        // get current logged in account player names for and load them into drop drop-down-list
        private void loadLoggedInUserPlayers()
        {
            DTO_ACCOUNT loggedInAccount = (DTO_ACCOUNT)(Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION]);

            DTO_Player[] loggedInAccountPlayers = loggedInAccount.players;

            List <ListItem> loggedInAccountPlayersNames = new List <ListItem>();

            for (int i = 0; i < loggedInAccountPlayers.Length; i++)
            {
                loggedInAccountPlayersNames.Add(new ListItem(loggedInAccountPlayers[i].FirstName + " " + loggedInAccountPlayers[i].LastName));
            }

            PlayerList.DataSource = loggedInAccountPlayersNames;
            PlayerList.DataBind();

            PlayerList2.DataSource = loggedInAccountPlayersNames;
            PlayerList2.DataBind();
        }
        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);
                    }
                }
            }
        }
        protected void OnRemovePlayer(object sender, EventArgs e)
        {
            List <DTO_Player> players        = (List <DTO_Player>)Session["AllAccountPlayers"];
            List <int>        players4delete = (List <int>)Session[WebSiteManager.PLAYERS_4_DELETE_SESSION];

            if (PlayersLV.SelectedIndex < 0)
            {
                return;
            }
            players4delete.Add(players[PlayersLV.SelectedIndex].Id);

            DTO_ACCOUNT loggedInAccount = (DTO_ACCOUNT)(Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION]);

            // find logged in account player and delete it from session memory
            loggedInAccount.players = loggedInAccount.players.Where(p => p.Id != players[PlayersLV.SelectedIndex].Id).ToArray();
            Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION] = loggedInAccount;


            players.RemoveAt(PlayersLV.SelectedIndex);
            loadAccountPlayersLB(players);
            Session[WebSiteManager.PLAYERS_4_DELETE_SESSION] = players4delete;
        }
        protected void OnSaveClick(object sender, EventArgs e)
        {
            DTO_ACCOUNT       loggedInAccount = (DTO_ACCOUNT)Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION];
            List <DTO_Player> players         = (List <DTO_Player>)Session["AllAccountPlayers"];

            if (players.Count() == 0)
            {
                ErrorMessage.Text = "You Must leav at least one player,please refresh the page and start over";
                return;
            }

            string oldPassword = WebSiteManager.CreateHash(OldPassword.Text, WebSiteManager.PASSWORD_HASH_SALT);

            if (IsValid && oldPassword.Equals(loggedInAccount.PASSWORD))
            {
                loggedInAccount.EMAIL    = Email.Text;
                loggedInAccount.PASSWORD = WebSiteManager.CreateHash(NewPassword.Text, WebSiteManager.PASSWORD_HASH_SALT);
                loggedInAccount.NAME     = nickname.Text;
                Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION] = loggedInAccount;
                WebSiteManager.UpdateAccount(new DTO_ACCOUNT {
                    EMAIL = loggedInAccount.EMAIL, NAME = loggedInAccount.NAME, PASSWORD = loggedInAccount.PASSWORD
                });

                // Update cockie with new password if changed
                // Create the cookie and set its value to the username and a hash of the logged in account
                HttpCookie cookie = new HttpCookie(WebSiteManager.REMEMBER_LOGIN_COOCKIE);
                cookie.Value = loggedInAccount.EMAIL + "," + loggedInAccount.PASSWORD;
                Response.Cookies.Add(cookie);


                WebSiteManager.RemovePlayers(((List <int>)Session[WebSiteManager.PLAYERS_4_DELETE_SESSION]));
                WebSiteManager.RemoveGames(((List <int>)Session[WebSiteManager.GAMES_4_DELETE_SESSION]));
            }
            else
            {
                ErrorMessage.Text = "Old Password not match";
            }
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            // check if remmber me cockie is set, if is set get logged in account
            if (Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION] == null && Request.Cookies[WebSiteManager.REMEMBER_LOGIN_COOCKIE] != null && Request.Cookies[WebSiteManager.REMEMBER_LOGIN_COOCKIE].Value.Length > 0)
            {
                var accountLoginInfo = Request.Cookies[WebSiteManager.REMEMBER_LOGIN_COOCKIE].Value;

                string[] values = accountLoginInfo.Split(',');
                // Retrieve the username and hash from the split values.
                string email    = values[0];
                string password = values[1];
                // get logged in account from db
                DTO_ACCOUNT loggedInAccount = WebSiteManager.Login(email, password);
                // get logged in account players from db
                DTO_Player[] players = WebSiteManager.GetPlayers(email);
                loggedInAccount.players = players;
                // save logged in account to current session
                Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION] = loggedInAccount;
            }


            SetLoggedInBar();
        }
        protected void LogIn(object sender, EventArgs e)
        {
            //     if (IsPostBack && Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION] != null) return;

            if (IsValid)
            {
                try
                {
                    // first of all check if email exist
                    if (!WebSiteManager.IsEmailExist(Email.Text))
                    {
                        FailureText.Text     = "Email not exist";
                        ErrorMessage.Visible = true;
                        return;
                    }
                    else
                    {
                        // generate hash from password for comparing password in db

                        string      passwordhashed  = WebSiteManager.CreateHash(Password.Text, WebSiteManager.PASSWORD_HASH_SALT);
                        DTO_ACCOUNT loggedInAccount = WebSiteManager.Login(Email.Text, passwordhashed);


                        if (loggedInAccount != null)
                        {
                            DTO_Player[] players = WebSiteManager.GetPlayers(Email.Text);
                            loggedInAccount.players = players;

                            // is remmber me
                            if (RememberMe.Checked)
                            {
                                // Create the cookie and set its value to the username and a hash of the logged in account
                                HttpCookie cookie = new HttpCookie(WebSiteManager.REMEMBER_LOGIN_COOCKIE);
                                cookie.Value = loggedInAccount.EMAIL + "," + loggedInAccount.PASSWORD;
                                Response.Cookies.Add(cookie);
                            }

                            Session[WebSiteManager.LOGGEDIN_ACCOUNT_SESSION] = loggedInAccount;
                            Server.Transfer("../Default.aspx", true);
                        }
                        else
                        {
                            FailureText.Text     = "Wrong Login Information";
                            ErrorMessage.Visible = true;
                        }
                    }
                }
                catch (WebException ex)
                {
                    using (WebResponse response = ex.Response)
                    {
                        var httpResponse = (HttpWebResponse)response;

                        using (Stream data = response.GetResponseStream())
                        {
                            StreamReader sr = new StreamReader(data);
                            FailureText.Text     = sr.ReadToEnd();
                            ErrorMessage.Visible = true;
                        }
                    }
                }
            }
        }