Esempio n. 1
0
    async def _priority(self, *args):
        if len(args) == 0:
            return await plane.send(self.ctx, "no city input")
        try:
            city = pn.find_city(args[0])
        except KeyError:
            raise commands.CommandError(self.invalid_city(args[0]))

        pri = sorted([g for g in pn.cities.values()], key=lambda x: -pn.priority(city, x))
        if len(args) != 1:
            try:
                to = pn.find_city(args[1])
            except KeyError:
                raise commands.CommandError(self.invalid_city(args[1]))

            name_pri = [g.name for g in pri]
            rank = name_pri.index(to.name) + 1
            return await plane.send(
                self.ctx, f"{round(pn.priority(city, to), 2)} - {[round(g, 2) for g in pn.priority(city, to, True)]}",
                d=f"Rank {rank}"
            )

        return await plane.send(
            self.ctx, f"Highest priority destinations from {city.name}",
            d=", ".join([self.oc(j) for j in pri[:15]])
        )
Esempio n. 2
0
 def pred2(m: discord.Message):
     if m.author == self.au and m.channel == self.ctx.channel:
         try:
             pn.find_city(m.content.lower())
         except KeyError:
             return False
         else:
             return True
Esempio n. 3
0
    async def _launch(self, *args):
        if len(args) == 0:
            raise commands.CommandError("What plane?")
        if len(args) == 1:
            raise commands.CommandError("To where?")
        if args[0].lower() not in self.user.planes:
            raise commands.CommandError("That's not a plane you own.")

        craft, args = self.user.planes[args[0].lower()], args[1:]
        if len(craft.path) != 0:
            raise commands.CommandError("That plane is currently in the air.")
        for arg in args:
            try:
                city = pn.find_city(arg)
            except KeyError:
                raise commands.CommandError(self.invalid_city(arg))
            if city.name not in self.user.cities:
                raise commands.CommandError("You don't have the license to {}.".format(city.name))

        args = [pn.find_city(g) for g in args]
        path = [craft.path[0], *args]
        fuel_cost = 0

        for i in range(len(path) - 1):
            if path[i].dist(path[i + 1]) > craft.range:
                raise commands.CommandError(f"{craft.name} can't reach {path[i + 1].name} from {path[i].name}.")

            fuel_cost += round(craft.lpk * path[i].dist(path[i + 1]) * self.fuel_price, 2)

        if round(fuel_cost) > self.user.credits:
            return await plane.send(self.ctx, "You don't have enough money for fuel.")

        self.user.credits -= round(fuel_cost)
        craft.launch(*args)
        url = pn.cd_url.format(
            self.form_dt(datetime.datetime.fromtimestamp(craft.arrival)),
            f"{craft.name}+to+{craft.path[-1].name}"
        )
        zeph.loop.create_task(self.arrival_timer(craft))
        return await plane.send(
            self.ctx, "ETA: {}".format(pn.hrmin(craft.arrival - time.time())),
            d=f"Fuel cost: Ȼ{round(fuel_cost)}", url=url
        )
Esempio n. 4
0
 async def _profile(self, *args):  # also needs to be able to take args
     val = sum([pn.find_city(g).value for g in self.user.cities] +
               [self.plane_value(g) for g in self.user.planes.values()])
     return await plane.send(
         self.ctx, "{}'s Profile".format(self.au.display_name), same_line=True,
         fs={"Licenses": NewLine(" ".join([f":flag_{pn.planemojis[g]}:" for g in self.user.countries])),
             "Credits": f"Ȼ{pn.addcomm(self.user.credits)}",
             "Airports": len(self.user.cities),
             "Airline Value": f"Ȼ{pn.addcomm(val)}"}
     )
Esempio n. 5
0
    async def _jobs(self, *args):
        if len(args) == 0:
            return await plane.send(self.ctx, "no city input")
        try:
            city = pn.find_city(args[0])
        except KeyError:
            raise commands.CommandError(self.invalid_city(args[0]))

        tm = time.time()
        if tm - city.job_reset >= 900 or len(city.jobs) == 0:
            print(f"generating jobs for {city.name}")
            city.rpj()  # only rpj city when called

        fil = lambda j: j.destination.name in self.user.cities
        fil_str = "J"
        if len(args) > 1 and args[1].lower() == "all":
            fil = lambda j: True
            fil_str = "All j"
        elif len(args) > 1 and args[1].lower() == "to":
            if len(args) == 2:
                raise commands.CommandError("to where?")

            try:
                fil = lambda j: j.destination.country == pn.find_country(args[2].lower()).name
                fil_str = f"{pn.find_country(args[2].lower()).name} j"
            except KeyError:
                try:
                    fil = lambda j: j.destination.name == pn.find_city(args[2].lower()).name
                    fil_str = f"{pn.find_city(args[2].lower()).name} j"
                except KeyError:
                    raise commands.CommandError("invalid location")

        reset = tm // 900 * 900
        if tm - city.job_reset >= 900:
            city.job_reset = reset

        await JobNavigator(
            self, city, fil, fil_str,
            footer=f"new jobs in {self.form_et(((tm // 900 + 1) * 900 - tm) // 60)} min"
        ).run(self.ctx)
Esempio n. 6
0
    async def _airport(self, *args):
        if len(args) == 0:
            raise commands.CommandError("Please input a function or airport.")
        if args[0].lower() in ["buy", "sell"]:
            if len(args) == 1:
                raise commands.CommandError("no airport input")
            args = args[1], args[0], *args[2:]

        try:
            city = pn.find_city(args[0])
        except KeyError:
            raise commands.CommandError(self.invalid_city(args[0]))

        assert isinstance(city, pn.City)
        if len(args) > 1 and args[1].lower() == "buy":
            if city.country not in self.user.countries:
                raise commands.CommandError("You don't have the {} license.".format(city.country))
            if city.name in self.user.cities:
                raise commands.CommandError("airport already owned")
            price = round(city.value)
            if price > self.user.credits:
                raise commands.CommandError("not enough credits")

            if await confirm(f"You're buying {city.name} Airport for Ȼ{pn.addcomm(price)}.", self.ctx, self.au):
                self.user.credits -= price
                self.user.cities.append(city.name)
                return await succ.send(self.ctx, "Airport purchased!")
            else:
                return await plane.send(self.ctx, "Purchase cancelled.")

        if len(args) > 1 and args[1].lower() == "sell":
            if city.name not in self.user.cities:
                raise commands.CommandError("airport not owned")

            resale = int(city.value / 4)
            if await confirm(f"You're selling {city.name} Airport for Ȼ{pn.addcomm(resale)}.", self.ctx, self.au):
                self.user.credits += resale
                self.user.cities.remove(city.name)
                return await succ.send(self.ctx, "Airport sold!")

            return await plane.send(self.ctx, "Sale cancelled.")

        return await plane.send(
            self.ctx, city.name + " Airport",
            fs={**city.dict, "Owned": ["No", "Yes"][city.name in self.user.cities]}, same_line=True
        )
Esempio n. 7
0
    async def _dist(self, *args):
        if len(args) == 0:
            return await plane.send(self.ctx, "no city input")
        if len(args) == 1:
            return await plane.send(self.ctx, "no destination input")

        try:
            pn.find_city(args[0])
        except KeyError:
            raise commands.CommandError(self.invalid_city(args[0]))

        st = 0
        for i in range(1, len(args)):
            try:
                pn.find_city(args[i])
            except KeyError:
                raise commands.CommandError(self.invalid_city(args[i]))

            st += pn.find_city(args[i - 1]).dist(pn.find_city(args[i]))

        return await plane.send(self.ctx, f"{round(st, 2)} km")
Esempio n. 8
0
    async def _market(self, *args):
        prices = self.model_prices

        if len(args) > 0:
            if args[0].lower() != "buy":
                raise commands.CommandError(f"invalid function ``{args[0]}``")
            if len(args) == 1:
                raise commands.CommandError("no purchase input")
            if args[1].lower() not in pn.craft:
                raise commands.CommandError("invalid model")

            model = args[1].lower()
            if prices[model] > zeph.planeUsers[self.au.id].credits:
                return await plane.send(self.ctx, "You don't have enough credits.")

            if await confirm(f"You're buying a {pn.craft[model].name} for Ȼ{pn.addcomm(prices[model])}.",
                             self.ctx, self.au):
                await succ.send(self.ctx, "Aircraft purchased! What would you like to name your new craft?")

                def pred1(m: discord.Message):
                    return m.author == self.au and m.channel == self.ctx.channel

                def pred2(m: discord.Message):
                    if m.author == self.au and m.channel == self.ctx.channel:
                        try:
                            pn.find_city(m.content.lower())
                        except KeyError:
                            return False
                        else:
                            return True

                while True:
                    try:
                        mess = await zeph.wait_for("message", timeout=300, check=pred1)
                    except asyncio.TimeoutError:
                        return await plane.send(self.ctx, "Purchase timed out and cancelled.")

                    else:
                        if [g in pn.permit for g in mess.content].count(False) != 0:
                            await plane.send(self.ctx,
                                             "Plane names can only contain alphanumerics, dashes, and underscores.",
                                             d=f"What would you like to name your new {pn.craft[model].name}?")
                        elif mess.content.lower() in zeph.planeUsers[self.au.id].planes:
                            await plane.send(self.ctx, "You already own a plane by that name.",
                                             d=f"What would you like to name your new {pn.craft[model].name}?")
                        elif mess.content.lower() == "sell":
                            await plane.send(self.ctx, "You can't name a plane that.",
                                             d=f"What would you like to name your new {pn.craft[model].name}?")
                        else:
                            await succ.send(self.ctx, f"{pn.craft[model].name} named {mess.content}.")
                            new = pn.Plane.new(model)
                            new.name = mess.content
                            break

                await plane.send(self.ctx, f"What city do you want to deploy {new.name} in?")
                while True:
                    try:
                        mess = await zeph.wait_for("message", timeout=300, check=pred2)
                    except asyncio.TimeoutError:
                        return await plane.send(self.ctx, "Purchase timed out and cancelled.")

                    else:
                        if pn.find_city(mess.content).name not in zeph.planeUsers[self.au.id].cities:
                            await plane.send(self.ctx, f"You don't own {pn.find_city(mess.content).name}.",
                                             d=f"What city do you want to deploy {new.name} in?")
                        else:
                            new.path = pn.Path(0, pn.find_city(mess.content))
                            self.user.credits -= prices[model]
                            self.user.planes[new.name.lower()] = new
                            return await succ.send(self.ctx, f"{new.name} ready for flight!")

            else:
                return await plane.send(self.ctx, "Purchase cancelled.")

        else:
            prices = {pn.craft[g].name: f"Ȼ{pn.addcomm(prices[g])}" for g in prices}
            return await FieldNavigator(plane, prices, 6, "The Market [{page}/{pgs}]", same_line=True).run(self.ctx)