Esempio n. 1
0
 /// <summary>
 /// Saves the given user to the database in a background thread.
 /// </summary>
 /// <param name="userToSave">The user to save.</param>
 /// <returns>The task so this can be run in the background.</returns>
 private Task SaveUserAsync(IrcUser userToSave)
 {
     return(Task.Run(
                delegate()
     {
         lock (this.sqlite)
         {
             this.sqlite.InsertOrReplace(userToSave);
             this.sqlite.Commit();
         }
     }
                ));
 }
Esempio n. 2
0
 /// <summary>
 /// Saves the given user to the database in a background thread.
 /// </summary>
 /// <param name="userToSave">The user to save.</param>
 /// <returns>The task so this can be run in the background.</returns>
 private Task SaveUserAsync(IrcUser userToSave)
 {
     return(Task.Run(
                delegate()
     {
         lock (this.users)
         {
             if (this.users.Exists(u => u.UserName.Equals(userToSave.UserName)))
             {
                 this.users.Update(userToSave);
             }
             else
             {
                 this.users.Insert(userToSave);
             }
         }
     }
                ));
 }
Esempio n. 3
0
        // -------- Functions --------

        /// <summary>
        /// Increases the karma of the given user by 1.
        /// </summary>
        /// <param name="userName">The user name to increase the karma of.</param>
        /// <returns>The new number of karma the user has after incrementing.</returns>
        public async Task <int> IncreaseKarma(string userName)
        {
            userName = userName.ToLower();

            IrcUser user = null;

            if ((this.ircUserCache.ContainsKey(userName) == false) || (this.ircUserCache[userName].Id == -1))
            {
                // If our cache does not have the user, or the cache has an invalid ID for the user,
                // we need to query it so we have the latest information.
                user = await QueryUserAsync(userName);
            }
            // Otherwise, if our cache has the user, grab it.
            else if (this.ircUserCache.ContainsKey(userName))
            {
                user = this.ircUserCache[userName];
            }

            // If the user is null, it means it doesn't exist in the database yet; create a new one
            if (user == null)
            {
                user = new IrcUser
                {
                    KarmaCount = 0,
                    UserName   = userName
                };
            }

            // Decrease the karma of the user, but only if it doesn't cause an underflow, otherwise, leave it alone.
            if (user.KarmaCount != int.MaxValue)
            {
                ++user.KarmaCount;
            }

            // Save the user to the database, and by extension, the cache.
            await SaveUserAsync(user);

            this.ircUserCache[userName] = user;

            return(user.KarmaCount);
        }