async def stolen_event(ctx): """ Empire was stolen from event. """ empire = await ctx.bot.db["empires"].find_one({"_id": ctx.author.id}) bank = await ctx.bot.db["bank"].find_one({"_id": ctx.author.id}) if bank.get("usd", 0) == 0: await loot_event(ctx) else: income = utils.net_income(empire) money_stolen = min( bank.get("usd", 0), random.randint(max(250, income // 2), max(1_000, income))) await ctx.bot.db["bank"].update_one({"_id": ctx.author.id}, {"$inc": { "usd": -money_stolen }}, upsert=True) s = f"${money_stolen:,}" if money_stolen > 0 else "nothing" await ctx.send(f"A thief broke into your empire and stole **{s}**")
async def calc_units_lost(empire): units_lost = dict() units_lost_cost = 0 hourly_income = max(0, utils.net_income(empire)) units = empire.get("units", dict()) # - Get available units avail_units = [] for u in Military.units: unit_entry = units.get(u.key, dict()) if unit_entry.get("owned", 0) > 0: avail_units.append(u) avail_units.sort(key=lambda un: un.calc_price(units.get(un.key, dict()).get("owned", 0), 1), reverse=False) for unit in avail_units: unit_entry = units.get(unit.key, dict()) owned = unit_entry.get("owned", 0) for i in range(1, owned + 1): price = unit.calc_price(owned - i, i) if (price + units_lost_cost) < hourly_income: units_lost[unit] = i units_lost_cost = sum([unit.calc_price(owned - n, n) for u, n in units_lost.items()]) return units_lost
async def on_dbl_vote(self, data): user_id = int(data["user"]) user = self.bot.get_user(user_id) empire = await self.bot.db["empires"].find_one({"_id": user_id} ) or dict() reward = int(min(50_000, max(utils.net_income(empire), 5_000) * 3)) await self.bot.db["bank"].update_one({"_id": user_id}, {"$inc": { "usd": reward }}, upsert=True) if user is not None: support_server = self.bot.get_guild(SupportServer.ID) try: await user.send( f"Thank you for voting for me! You have received **${reward:,}** :heart:" ) member = support_server.get_member(user_id) if member is None: await user.send( f"psst...you can join our support server here {SupportServer.LINK}" ) except (discord.Forbidden, discord.HTTPException): """ Failed """
def calc_pillaged_amount(empire, bank, multiplier: int = 1.0): hourly_income = max(0, utils.net_income(empire)) min_val = max(0, int(bank.get("usd", 0) * 0.025)) max_val = max(0, int(bank.get("usd", 0) * 0.050)) stolen_amount = int(min(bank.get("usd", 0), hourly_income, random.randint(min_val, max_val) * multiplier)) # - Add a bonus pillage amount if the chance of winning is less than 50% bonus_money = int((stolen_amount * 2.0) * (1.0 - win_chance) if win_chance < 0.50 else 0) return stolen_amount, bonus_money
async def loot_event(ctx): """ Empire finds loot event. """ empire = await ctx.bot.db["empires"].find_one({"_id": ctx.author.id}) income = utils.net_income(empire) name = utils.get_random_name() value = random.randint(max(500, income // 2), max(1_000, int(income * 0.75))) await ctx.bot.db["loot"].insert_one({ "user": ctx.author.id, "name": name, "value": value }) await ctx.send(f"You found **- {name} -** while out adventuring.")
async def income_loop(self): """ Background income & upkeep loop. """ empires = await self.bot.db["empires"].find({}).to_list(length=None) requests = [] for empire in empires: now = dt.datetime.utcnow() player_row = await self.bot.db["players"].find_one( {"_id": empire["_id"]}) or dict() bank_row = await self.bot.db["bank"].find_one( {"_id": empire["_id"]}) or dict() # - Set the last_income field which is used to calculate the income rate from the delta time await self.bot.db["empires"].update_one( {"_id": empire["_id"]}, {"$set": { "last_income": now }}) # - No login? No income if (last_login := player_row.get("last_login")) is None: continue last_income = empire.get("last_income", now) hours_since_login = (now - last_login).total_seconds() / 3600 # - User must have logged in the past 8 hours in order to get passive income if hours_since_login >= MAX_HOURS_OFFLINE: continue # - Total number of hours we need to credit the empire's bank hours = min(MAX_HOURS_OFFLINE, (now - last_income).total_seconds() / 3600) income = int(utils.net_income(empire) * hours) if bank_row.get("usd", 0) >= 250_000 and income > 0: income = int(income * 0.4)