Exemplo n.º 1
0
        public async Task<long> GetPrice()
        {
            if (this._potionId == 0)
            {
                return 0;
            }

            int qty = 1;
            if (this._price == null)
            {
                this._price = await Price.FromCache(_potionId);
                Match matchQty = Regex.Match(this.Name, @"(\d+)x ");
                if (matchQty.Success)
                {
                    qty = int.Parse(matchQty.Groups[1].Value, CultureInfo.InvariantCulture);
                }
            }
            return qty * this._price.MarketPrice;
        }
Exemplo n.º 2
0
        public static async Task CoinShare(CommandContext bc)
        {
            if (bc.MessageTokens.Length < 3)
            {
                await bc.SendReply("Syntax: !CoinShare <players> <item>");
                return;
            }

            int players;
            string query = null;
            if (int.TryParse(bc.MessageTokens[1], out players))
            {
                query = bc.MessageTokens.Join(2);
            }
            else if (int.TryParse(bc.MessageTokens[bc.MessageTokens.Length - 1], out players))
            {
                query = string.Join(" ", bc.MessageTokens, 1, bc.MessageTokens.Length - 2);
            }
            if (players < 1 || players > 100)
            {
                await bc.SendReply("Error: Invalid number of players.");
                return;
            }

            Price price;

            int id;
            if (int.TryParse(query.TrimStart('#'), out id))
            {
                // !CoinShare <players> <id>
                price = new Price(id);
            }
            else
            {
                var prices = await Prices.FromRuneScapeSearchExact(query);

                switch (prices.Count)
                {
                    case 0:
                        await bc.SendReply(@"Grand Exchange doesn't have any item matching '\c07{0}\c'.", query);
                        return;
                    case 1:
                        price = prices[0];
                        break;
                    default:
                        string reply = @"Grand Exchange results: \c07{0}\c".FormatWith(prices.Count);
                        for (int i = 0; i < Math.Min(15, prices.Count); i++)
                        {
                            reply += @" | \c07#{0}\c {1}".FormatWith(prices[i].Id, prices[i].Name);
                        }
                        if (prices.Count > 15)
                        {
                            reply += " | ...";
                        }
                        await bc.SendReply(reply);
                        return;
                }
            }

            price = await Bot.Price.FromCache(price.Id);
            if (price.Name == null)
            {
                await bc.SendReply(@"Grand Exchange doesn't have the item \c07#{0}\c.", price.Id);
            }
            else
            {
                await bc.SendReply(@"Name: \c07{0}\c | Minimum price: \c07{1}\c | Players: \c07{2:N0}\c | Player share: \c07{3:N0}\c | \c12http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj={4}\c", price.Name, price.MarketPrice.ToShortString(1), players, price.MarketPrice / players, price.Id);
            }
        }
Exemplo n.º 3
0
        public static async Task<Price> FromDatabase(int id)
        {
            var price = new Price(id);

            var dr = await Database.FetchFirst("SELECT name,price,lastUpdate FROM prices WHERE id=@id", new MySqlParameter("@id", id));
            if (dr != null)
            {
                price.Name = (string) dr["name"];
                price.MarketPrice = (uint) dr["price"];
                price.LastUpdate = ((string) dr["lastUpdate"]).ToDateTime();
            }

            return price;
        }
Exemplo n.º 4
0
        public static async Task<Price> FromRuneScape(int id)
        {
            var price = new Price(id);

            var pricePage = await new WebClient().DownloadStringTaskAsync("http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj=" + id);

            var match = Regex.Match(pricePage, @"<h5>([^<]+)</h5>\s+<p>([^<]+)</p>");
            if (match.Success)
            {
                price.Name = match.Groups[1].Value.Trim();
                price.Examine = match.Groups[2].Value.Trim();

                match = Regex.Match(pricePage, @"<img src=""http://www.runescape.com/img/itemdb/(\w+)-icon-big.png");
                if (match.Success)
                {
                    price.IsMember = match.Groups[1].Value == "members";
                }

                match = Regex.Match(pricePage, @"<th scope=""row"">Today's Change:</th>\s+<td class=""\w+"">([^<]+)</td>");
                if (match.Success)
                {
                    price.ChangeToday = match.Groups[1].Value.ToInt32();
                }

                match = Regex.Match(pricePage, @"<th scope=""row"">30 Day Change:</th>\s+<td class=""\w+"">([^%]+)%</td>");
                if (match.Success)
                {
                    price.Change30days = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
                }

                match = Regex.Match(pricePage, @"<th scope=""row"">90 Day Change:</th>\s+<td class=""\w+"">([^%]+)%</td>");
                if (match.Success)
                {
                    price.Change90days = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
                }

                match = Regex.Match(pricePage, @"<th scope=""row"">180 Day Change:</th>\s+<td class=""\w+"">([^%]+)%</td>");
                if (match.Success)
                {
                    price.Change180days = double.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture);
                }

                match = Regex.Match(pricePage, @"<th scope=""row"">Current guide price:</th>\s+<td>([^<]+)</td>");
                if (match.Success)
                {
                    price.MarketPrice = match.Groups[1].Value.ToInt32();

                    await price.SaveToDB(false);
                }
            }

            return price;
        }
Exemplo n.º 5
0
        public static async Task PriceInfo(CommandContext bc)
        {
            if (bc.MessageTokens.Length == 1)
            {
                await bc.SendReply("Syntax: !PriceInfo <item>");
                return;
            }

            Price price;

            int id;
            if (int.TryParse(bc.MessageTokens[1].TrimStart('#'), out id))
            {
                // !PriceInfo <id>
                price = new Price(id);
            }
            else
            {
                string query = bc.MessageTokens.Join(1);
                var prices = await Prices.FromRuneScapeSearchExact(query);

                switch (prices.Count)
                {
                    case 0:
                        await bc.SendReply(@"\c12www.runescape.com\c doesn't have any record for '{0}'.", query);
                        return;
                    case 1:
                        price = prices[0];
                        break;
                    default:
                        string reply = @"Results: \c07{0}\c".FormatWith(prices.Count);
                        for (int i = 0; i < Math.Min(15, prices.Count); i++)
                        {
                            reply += @" | \c07#{0}\c {1}".FormatWith(prices[i].Id, prices[i].Name);
                        }
                        if (prices.Count > 15)
                        {
                            reply += " | ...";
                        }
                        await bc.SendReply(reply);
                        return;
                }
            }

            price = await Bot.Price.FromRuneScape(price.Id);

            string changeToday;
            if (price.ChangeToday < 0)
            {
                changeToday = @"\c04{0:0.#}\c".FormatWith(price.ChangeToday);
            }
            else if (price.Change30days > 0)
            {
                changeToday = @"\c03+{0:0.#}\c".FormatWith(price.ChangeToday);
            }
            else
            {
                changeToday = @"\c07{0:0.#}\c".FormatWith(price.ChangeToday);
            }

            string change30days;
            if (price.Change30days < 0)
            {
                change30days = @"\c04{0:0.#}%\c".FormatWith(price.Change30days);
            }
            else if (price.Change30days > 0)
            {
                change30days = @"\c03+{0:0.#}%\c".FormatWith(price.Change30days);
            }
            else
            {
                change30days = @"\c07{0:0.#}%\c".FormatWith(price.Change30days);
            }

            string change90days;
            if (price.Change90days < 0)
            {
                change90days = @"\c04{0:0.#}%\c".FormatWith(price.Change90days);
            }
            else if (price.Change90days > 0)
            {
                change90days = @"\c03+{0:0.#}%\c".FormatWith(price.Change90days);
            }
            else
            {
                change90days = @"\c07{0:0.#}%\c".FormatWith(price.Change90days);
            }

            string change180days;
            if (price.Change180days < 0)
            {
                change180days = @"\c04{0:0.#}%\c".FormatWith(price.Change180days);
            }
            else if (price.Change180days > 0)
            {
                change180days = @"\c03+{0:0.#}%\c".FormatWith(price.Change180days);
            }
            else
            {
                change180days = @"\c07{0:0.#}%2\c".FormatWith(price.Change180days);
            }

            await bc.SendReply(@"Name: \c07{0}\c | Price: \c07{1}\c | Today's change: {2} | Last 30 days: {3} | Last 90 days: {4} | Last 180 days: {5}", price.Name, price.MarketPrice.ToShortString(1), changeToday, change30days, change90days, change180days);
            await bc.SendReply(@"Examine: \c07{0}\c | \c12http://services.runescape.com/m=itemdb_rs/viewitem.ws?obj={1}\c", price.Examine, price.Id);
        }