Exemplo n.º 1
0
    protected void btnLogIn_Click(object sender, EventArgs e)
    {
        string userName = txtUser.Text.Trim();

        // using stmt is not really necessary
        // we use it the whole time, and the object will be released at the end of the event handler
        var entities = new TheoryTrainerEntities();

        // see if the user exists in the database
        var user = (from u in entities.Users
                    where u.Name == userName
                    select u).SingleOrDefault <User>(); // returns null if not found

        if (user == null)
        {
            // if user does not exist, create a new User object and add to db
            var newUser = new User
            {
                Name           = userName,
                CreateDateTime = DateTime.Now,
                UpdateDateTime = DateTime.Now
            };
            entities.Users.Add(newUser);
            entities.SaveChanges();

            // then set UserName property of speller
            speller.UserName = newUser.Name;
        }
        else
        {
            // if user was found, retrieve user from db
            var existingUser = (from u in entities.Users
                                where u.Name == userName
                                select u).SingleOrDefault <User>();

            // set UserNAme property of speller
            speller.UserName = existingUser.Name;

            // display best percentage and datetime
            lblBestPercentage.Text = string.Format("{0} on {1}",
                                                   existingUser.BestPercentage.ToString("p1"),
                                                   existingUser.UpdateDateTime.ToShortDateString());
        }

        // Display UserName
        lblUser.Text = speller.UserName;
    }
Exemplo n.º 2
0
    protected void btnLogOut_Click(object sender, EventArgs e)
    {
        string userName = txtUser.Text.Trim(); // TODO: this could be unreliable

        var entities = new TheoryTrainerEntities();

        var currentUser = (from u in entities.Users
                           where u.Name == userName
                           select u).SingleOrDefault <User>();

        // update the session percentage, rounded to 3 decimal places
        // the if stmt prevents an exception if user logs in and immediately logs out without trying to spell a chord
        if (speller.PercentageCorrect > 0)
        {
            currentUser.SessionPercentage = Math.Round(speller.PercentageCorrect, 3);
        }
        else
        {
            currentUser.SessionPercentage = 0;
        }

        // check to see if it's a new "best" percentage
        if (speller.PercentageCorrect > currentUser.BestPercentage)
        {
            currentUser.BestPercentage = Math.Round(speller.PercentageCorrect, 3);
        }

        currentUser.UpdateDateTime = DateTime.Now;
        entities.SaveChanges();

        ResetUIChords();
        ResetUIStats();

        // Reset the "user" properties of speller to clear out the user who logged out
        speller.UserName       = null;
        speller.UserSpelling   = null;
        speller.Attempts       = 0;
        speller.CorrectAnswers = 0;
    }