Ejemplo n.º 1
0
        public static Bitbar GetBitbar(User user)
        {
            using (DatabaseConnection conn = new DatabaseConnection()) {
                MySqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT id, value, max_value, image, target_color, fill_color "
                                  + "FROM gadget_bitbar WHERE user_id = @userid";
                cmd.Parameters.AddWithValue("@userid", user.UserId);
                MySqlDataReader reader = cmd.ExecuteReader();

                // Create bitbar for user if one does not already exist.
                if (!reader.HasRows)
                {
                    return(CreateBitbar(user));
                }

                reader.Read();

                Bitbar bitbar = new Bitbar(reader.GetString("id"), user);
                bitbar.Value       = reader.GetInt32("value");
                bitbar.MaxValue    = reader.GetInt32("max_value");
                bitbar.Image       = reader.GetBytes("image");
                bitbar.TargetColor = reader.GetString("target_color");
                bitbar.FillColor   = reader.GetString("fill_color");

                return(bitbar);
            }
        }
Ejemplo n.º 2
0
        public static Bitbar CreateBitbar(User user)
        {
            using (DatabaseConnection conn = new DatabaseConnection()) {
                MySqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "INSERT INTO gadget_bitbar "
                                  + "(id, user_id, value, max_value, image, target_color, fill_color) "
                                  + "VALUES (@id, @userid, @val, @maxval, @img, @tcolor, @fcolor)";

                string id = TokenGenerator.Generate();
                while (BitbarExists(id))
                {
                    id = TokenGenerator.Generate();
                }

                Bitbar bitbar = new Bitbar(id, user);
                cmd.Parameters.AddWithValue("@id", id);
                cmd.Parameters.AddWithValue("@userid", user.UserId);
                cmd.Parameters.AddWithValue("@val", bitbar.Value);
                cmd.Parameters.AddWithValue("@maxval", bitbar.MaxValue);
                cmd.Parameters.AddWithValue("@img", bitbar.Image);
                cmd.Parameters.AddWithValue("@tcolor", bitbar.TargetColor);
                cmd.Parameters.AddWithValue("@fcolor", bitbar.FillColor);

                cmd.ExecuteNonQuery();
                return(bitbar);
            }
        }
Ejemplo n.º 3
0
        public static Bitbar GetBitbar(string id)
        {
            using (DatabaseConnection conn = new DatabaseConnection()) {
                MySqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT user_id, value, max_value, image, target_color, fill_color "
                                  + "FROM gadget_bitbar WHERE id = @id";


                cmd.Parameters.AddWithValue("@id", id);
                MySqlDataReader reader = cmd.ExecuteReader();

                if (!reader.HasRows)
                {
                    throw new NotFoundException("Bitbar " + id + " does not exist.");
                }

                reader.Read();

                User user = UserManager.GetUser(reader.GetString("user_id"));

                Bitbar bitbar = new Bitbar(id, user);
                bitbar.Value       = reader.GetInt32("value");
                bitbar.MaxValue    = reader.GetInt32("max_value");
                bitbar.Image       = reader.GetBytes("image");
                bitbar.TargetColor = reader.GetString("target_color");
                bitbar.FillColor   = reader.GetString("fill_color");

                return(bitbar);
            }
        }
Ejemplo n.º 4
0
        public static void AddBits(User user, int count)
        {
            using (DatabaseConnection conn = new DatabaseConnection()) {
                MySqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "UPDATE gadget_bitbar "
                                  + "SET value = value + @count "
                                  + "WHERE user_id = @userid";
                cmd.Parameters.AddWithValue("@count", count);
                cmd.Parameters.AddWithValue("@userid", user.UserId);
                cmd.ExecuteNonQuery();

                Logger.Log("Added " + count + " to bitbar of " + user.UserId);
            }

            Bitbar bitbar = BitbarManager.GetBitbar(user);

            BitbarHub.UpdateSource(bitbar);
        }
Ejemplo n.º 5
0
        public static void UpdateBitbar(Bitbar bitbar)
        {
            using (DatabaseConnection conn = new DatabaseConnection()) {
                MySqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "UPDATE gadget_bitbar "
                                  + "SET value = @val, max_value = @maxval, image = @img, "
                                  + "target_color = @tcolor, fill_color = @fcolor "
                                  + "WHERE id = @id";
                cmd.Parameters.AddWithValue("@val", bitbar.Value);
                cmd.Parameters.AddWithValue("@maxval", bitbar.MaxValue);
                cmd.Parameters.AddWithValue("@img", bitbar.Image);
                cmd.Parameters.AddWithValue("@tcolor", bitbar.TargetColor);
                cmd.Parameters.AddWithValue("@fcolor", bitbar.FillColor);
                cmd.Parameters.AddWithValue("@id", bitbar.Id);
                cmd.ExecuteNonQuery();
            }

            BitbarHub.UpdateSource(bitbar);
        }