Esempio n. 1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["Players"] == null || Session["PlayerScores"] == null || Session["PlayerTurn"] == null)
            {
                getPlayers.Visible = true;
                playGame.Visible   = false;
            }
            else
            {
                getPlayers.Visible = false;
                playGame.Visible   = true;

                string[] players      = Session["Players"].ToString().TrimEnd('|').Split('|');
                string[] playerScores = Session["PlayerScores"].ToString().TrimEnd('|').Split('|');
                int      playerCount  = players.Length;
                int      playerTurn   = (int)Session["PlayerTurn"];
                int      gameScore    = (Session["score"] != null) ? (int)Session["score"] : 0;
                Session["score"] = gameScore;

                int gameLevel = (Session["level"] != null) ? 1 + Convert.ToInt32(Session["level"]) : 1;
                Session["level"] = gameLevel;

                if (gameLevel > 48)
                {
                    lblResult.Text      = "Congratulations...  You have reached the highest level!";
                    litAnswerValue.Text = Session["answerValue"].ToString();
                    for (int i = 0; i < playerCount; i++)
                    {
                        litPlayerNames.Text  += "<th class='playerName'>" + players[i] + "</th>";
                        litPlayerScores.Text += "<td class='playerScore'>" + playerScores[i] + "</td>";
                    }
                    Session.Abandon();
                    return;
                }

                int answerValue = answerBaseScore + (gameLevel - 1) * answerIncrement;
                Session["answerValue"] = answerValue;
                litAnswerValue.Text    = answerValue.ToString();
                for (int i = 0; i < playerCount; i++)
                {
                    string activeClass = ((i == playerTurn) ? " active" : "");
                    litPlayerNames.Text  += "<th class='playerName" + activeClass + "'>" + players[i] + "</th>";
                    litPlayerScores.Text += "<td class='playerScore'>" + playerScores[i] + "</td>";
                }

                litLifeline.Visible = false;
                Geography  geography      = new Geography();
                int        effectiveLevel = (int)Math.Ceiling((decimal)gameLevel / 2); //2 destinations at each level
                List <int> prevDestList   = (List <int>)Session["PrevDestList"];
                DataRow    drDestination  = geography.GetRandomDestination(effectiveLevel, prevDestList);
                string     strId          = drDestination["id"].ToString();
                int        intId          = Convert.ToInt32(strId);
                prevDestList.Add(intId);
                string strRank        = drDestination["rankinstate"].ToString();
                string strName        = drDestination["destination"].ToString();
                string strDescription = drDestination["description"].ToString();
                string strAbbr        = drDestination["abbr"].ToString();
                int[]  intArrLifeline = RandomIntegerArray(lifelineCount);
                string strLifelines   = string.Join("|", Array.ConvertAll <int, String>(intArrLifeline, Convert.ToString));

                Session["id"]           = strId;
                Session["PrevDestList"] = prevDestList;
                Session["rank"]         = strRank;
                Session["name"]         = strName;
                Session["description"]  = strDescription;
                Session["abbr"]         = strAbbr;
                Session["lifelines"]    = strLifelines;
                Session["llIndex"]      = 0;
                destName.Text           = gameLevel.ToString() + ". " + strName;
                destDescription.Text    = strDescription;
                destImg.Text            = "<img src=\"" + "images/destimages/" + strAbbr + "/" + strRank + "-" + strName.Replace(" ", "-").Replace(".", "") + ".jpg\" class=\"destImg\">";
            }
        }
        btnLifeline.Visible = true;
        btnNext.Visible     = false;
    }