async def farmsell(self, ctx, plant_name): """Sell all of your target crop.""" _plant = Plant.get_plant(self.bot.db_session, plant_name) if _plant is None: await ctx.send(MSG_PLANT_NOT_FOUND.format(ctx.author)) return _farm = ORMFarm.get_farm(ctx.author, self.bot.db_session) _account = EconomyAccount.get_economy_account(ctx.author, self.bot.db_session) total_amount = 0 for i in range(len(_farm.harvests))[::-1]: if _farm.harvests[i].plant.id == _plant.id: total_amount += _farm.harvests[i].amount del _farm.harvests[i] if total_amount == 0: await ctx.send(MSG_SELL_NONE.format(ctx.author, _plant.name)) return plant_sell_price = _plant.get_sell_price(raw=True) raw_credit = total_amount * plant_sell_price _account.add_credit(self.bot.db_session, raw_credit, name="S:{0.id}={1}".format(_plant, total_amount), raw=True) _plant.decrement_demand(self.bot.db_session, total_amount) _farm.decrease_storage(self.bot.db_session, total_amount) await ctx.send( MSG_SELL_SUCCESS.format(ctx.author, total_amount, _plant, raw_credit / 10000, _account.get_balance(), plant_sell_price / 10000))
async def setplantprice(self, ctx, plant_name, base_price: float): """(Owner) Set a plant's base unit price.""" _plant = Plant.get_plant(self.bot.db_session, plant_name) if _plant is None: await ctx.send(MSG_PLANT_NOT_FOUND.format(ctx.author)) return _plant.set_base_price(self.bot.db_session, base_price)
async def plantstats(self, ctx, plant_name): """Check plant price stats for the past 48 refreshes.""" _plant = Plant.get_plant(self.bot.db_session, plant_name) if _plant is None: await ctx.send(MSG_PLANT_NOT_FOUND.format(ctx.author)) return mean_sale = 0 plant_stats_24h = _plant.price_logs plant_stats_24h = plant_stats_24h[:24] for _stats in plant_stats_24h: logging.info("{0.price} / {0.refreshed_at}".format(_stats)) mean_sale += _stats.price mean_sale /= len(plant_stats_24h) plant_top = _plant.get_highest_sell_price() embed = discord.Embed( title="Statistics for {0.name} `{0.tag}`".format(_plant), color=0xffd700) embed.add_field(name="Yield", value="`{0.base_harvest}` units".format(_plant)) embed.add_field(name="Buy Price", value="💵 `{0:,.2f}` gil/unit".format( _plant.get_buy_price())) embed.add_field( name="Highest Recorded Sale Price", value="💵 `{0:,.2f}` gil/unit\nAt `{1}`".format( plant_top.price / 10000, plant_top.refreshed_at.strftime("%I:%M %p - %d %B '%y")), inline=False) embed.add_field( name="Base Sale Price", value="💵 `{0:,.2f}` gil/unit".format(_plant.base_sell_price / 10000), ) embed.add_field( name="Current Sale Price", value="💵 `{0:,.2f}` gil/unit".format(_plant.get_sell_price()), ) embed.add_field(name="Mean Sale Price (Past 24 Hours)", value="💵 `{0:,.2f}` gil/unit".format(mean_sale / 10000), inline=False) embed.add_field(name="Current Demand", value="`{0:,} / {1:,}` units".format( _plant.current_demand, _plant.base_demand), inline=False) graph_file = PLANT_PRICE_GRAPH_DIRECTORY + "{}.png".format( _plant.tag.upper()) graph = discord.File(graph_file, filename="image.png") embed.set_image(url="attachment://image.png") await ctx.send(embed=embed, file=graph)
async def setplanttag(self, ctx, plant_name, tag): """(Owner) Set a plant's shorthand tag.""" _plant = Plant.get_plant(self.bot.db_session, plant_name) if _plant is None: await ctx.send(MSG_PLANT_NOT_FOUND.format(ctx.author)) return _plant.tag = tag self.bot.db_session.add(_plant) self.bot.db_session.commit() await ctx.send("**Successfully changed plant tag.**")
async def plantstats(self, ctx, plant_name): """Check plant price stats for the past 48 refreshes.""" _plant = Plant.get_plant(self.bot.db_session, plant_name) if _plant is None: await ctx.send(MSG_PLANT_NOT_FOUND.format(ctx.author)) return mean_sale = 0 plant_stats = PriceLog.get_plant_price_logs(_plant, self.bot.db_session) top_plant_stats = sorted(plant_stats, key=lambda x: x.price) print(top_plant_stats) plant_stats = plant_stats[::-1] plant_stats = plant_stats[:24] for _stats in plant_stats: logging.info("{0.price} / {0.refreshed_at}".format(_stats)) mean_sale += _stats.price mean_sale /= len(plant_stats) plant_top = PriceLog.get_highest_price(_plant, self.bot.db_session) embed = discord.Embed( title="Statistics for {0.name} `{0.tag}`".format(_plant), color=0xffd700) embed.add_field(name="Yield", value="`{0.base_harvest}` units".format(_plant)) embed.add_field(name="Buy Price", value="💵 `{0:,.2f}` gil/unit".format( _plant.get_buy_price())) embed.add_field( name="Highest Recorded Sale Price", value="💵 `{0:,.2f}` gil/unit\nAt `{1}`".format( plant_top.price / 10000, plant_top.refreshed_at.strftime("%I:%M %p - %d %B '%y")), inline=False) embed.add_field( name="Base Sale Price", value="💵 `{0:,.2f}` gil/unit".format(_plant.base_sell_price / 10000), ) embed.add_field( name="Current Sale Price", value="💵 `{0:,.2f}` gil/unit".format(_plant.get_sell_price()), ) embed.add_field(name="Mean Sale Price (Past 24 Hours)", value="💵 `{0:,.2f}` gil/unit".format(mean_sale / 10000), inline=False) await ctx.send(embed=embed)
async def refreshplantprices(self, ctx): """(Owner) Manually refresh the global market prices.""" all_plants = Plant.get_plants(self.bot.db_session) for plant in all_plants: plant.randomize_price(self.bot.db_session) final_str = "" for plant in all_plants: bp = plant.get_buy_price() sp = plant.get_sell_price() _str = "***{0.name}*** `[B: {1:,.2f} gil | S: {2:,.2f} gil]`\n".format( plant, bp, sp) final_str += _str await ctx.send("**Prices refreshed!**\n{}".format(final_str))
def seed(session): plant_names = [i.name for i in session.query(Plant).all()] for PLANT in CURRENT_PLANTS: if PLANT["name"] in plant_names: continue # Skip if exists session.add(Plant( name = PLANT["name"], tag = PLANT["tag"], base_harvest = PLANT["base_harvest"], buy_price = PLANT["buy_price"], base_sell_price = PLANT["base_sell_price"], current_sell_price = get_selling_price(PLANT), randomness_factor = PLANT["randomness_factor"], growing_seconds = PLANT["growing_seconds"], )) session.commit()
async def farmplant(self, ctx, plant_name, plant_count=1): """Plant a crop on a number of your available plots.""" _plant = Plant.get_plant(self.bot.db_session, plant_name) if _plant is None: await ctx.send(MSG_PLANT_NOT_FOUND.format(ctx.author)) return _account = EconomyAccount.get_economy_account(ctx.author, self.bot.db_session) total_price = _plant.buy_price * plant_count if not _account.has_balance(total_price, raw=True): await ctx.send( MSG_INSUFFICIENT_FUNDS.format(ctx.author, _account.get_balance())) return _farm = ORMFarm.get_farm(ctx.author, self.bot.db_session) _plots = _farm.get_available_plots(self.bot.db_session) if len(_plots) == None: await ctx.send(MSG_PLOT_NOT_FOUND.format(ctx.author)) return if len(_plots) < plant_count: await ctx.send( MSG_PLANT_NO_PLOTS.format(ctx.author, len(_plots), plant_count)) return _account.add_debit( self.bot.db_session, total_price, name="B:{0.id}={0.buy_price}".format(_plant), raw=True, ) for _plot in _plots[:plant_count]: _plot.plant_to_plot(_plant, self.bot.db_session, commit_on_execution=False) self.bot.db_session.commit() await ctx.send( MSG_PLOT_PLANT.format(ctx.author, _plant, plant_count, total_price / 10000, _account.get_balance()))
def refresh_prices(bot): logging.info("Refreshing farm market prices...") all_plants = Plant.get_plants(bot.db_session) for plant in all_plants: plant.randomize_price(bot.db_session, commit_on_execution=False) bot.db_session.commit()
async def plantprices(self, ctx, plant_name=None): """Show the current global plant prices.""" try: page_number = int(plant_name) plant_name = None except TypeError: page_number = 1 except ValueError: _plant = Plant.get_plant(self.bot.db_session, plant_name) if plant_name is not None: if _plant is None: ctx.send(MSG_PLANT_NOT_FOUND.format(ctx.author)) return embed = discord.Embed(title="-=Current {0} Market Prices=-".format( _plant.name), color=0xffd700) bp = _plant.get_buy_price() sp = _plant.get_sell_price() embed.add_field(name="**`{0.tag}` - {0.name}**".format(_plant), value=MSG_PLANT_PRICES.format( _plant, bp, sp, get_growing_time_string( _plant.growing_seconds)), inline=False) else: all_plants = Plant.get_plants(self.bot.db_session) plant_count = len(all_plants) paginated_plants = [ all_plants[i:i + 10] for i in range(0, plant_count, 10) ] # Make sure page number is in bounds page_number = min(page_number, len(paginated_plants)) page_number = max(page_number, 1) embed = discord.Embed( title="-=Current Global Market Prices=-" + "\nPage {0} of {1}".format(page_number, len(paginated_plants)), color=0xffd700) logging.info(paginated_plants) final_str = "" for _plant in paginated_plants[page_number - 1]: bp = _plant.get_buy_price() sp = _plant.get_sell_price() embed.add_field(name="**`{0.tag}` - {0.name}**".format(_plant), value=MSG_PLANT_PRICES.format( _plant, bp, sp, get_growing_time_string( _plant.growing_seconds)), inline=False) embed.set_footer(text=MSG_PLANT_PRICES_FOOTER.format(( timedelta(hours=1) + datetime.now().replace(microsecond=0, second=0, minute=0) ).strftime("%I:%M %p UTC+08:00"))) await ctx.send(embed=embed)
async def farmharvest(self, ctx, harvest_range=None): """Harvest all your harvestable crops.""" _farm = ORMFarm.get_farm(ctx.author, self.bot.db_session) plot_count = len(_farm.plots) all_plots = False if harvest_range is not None: try: harvest_range = list(map(int, harvest_range.split('-'))) except ValueError: await ctx.send(MSG_CMD_INVALID.format(ctx.author)) return else: all_plots = True harvest_range = [1, plot_count] # Command validation: if len(harvest_range) > 2: await ctx.send(MSG_CMD_INVALID.format(ctx.author)) return if len(harvest_range) == 1: harvest_range.append(harvest_range[0]) if harvest_range[1] < harvest_range[0]: await ctx.send(MSG_CMD_INVALID.format(ctx.author)) return if not (0 <= harvest_range[0] - 1 < plot_count) or not (0 <= harvest_range[1] - 1 < plot_count): await ctx.send(MSG_DISCARD_OUT_OF_RANGE.format(ctx.author)) return storage_needed = 0 for i in range(harvest_range[0] - 1, harvest_range[1]): storage_needed += _farm.plots[i].get_harvest_amount() if not _farm.has_storage(storage_needed): await ctx.send( MSG_HARVEST_NOT_ENOUGH_CAPACITY.format( ctx.author, max(_farm.harvest_capacity - _farm.current_harvest, 0), storage_needed)) return if storage_needed == 0: await ctx.send(MSG_HARVEST_NONE.format(ctx.author)) return # Actual harvesting await ctx.send( "Harvesting **{0.name}#{0.discriminator}**'s crops...\n".format( ctx.author)) # Collect harvesting info harvest_stats = {} for i in range(harvest_range[0] - 1, harvest_range[1]): _harvest = _farm.plots[i].harvest(self.bot.db_session, commit_on_execution=False) if _harvest is None: continue if _harvest.plant.name not in harvest_stats: harvest_stats[_harvest.plant.name] = 0 harvest_stats[_harvest.plant.name] += _harvest.amount # Collate harvest info harvest_str = "" for plant_name in harvest_stats: _plant = Plant.get_plant(self.bot.db_session, plant_name) new_harvest = Harvest(plant_id=_plant.id, amount=harvest_stats[plant_name], farm_id=_farm.id) self.bot.db_session.add(new_harvest) harvest_str += "**{0}**, {1} units\n".format( plant_name, harvest_stats[plant_name]) # Commit to DB self.bot.db_session.commit() await ctx.send(MSG_HARVEST_SUCCESS.format(ctx.author, harvest_str))