Commit d89c1622 authored by Jeremy Pallats's avatar Jeremy Pallats 💬
Browse files

FIX #129: Near addition

parent 6b1d4316
Loading
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -1301,6 +1301,25 @@ class Near(Action):
            suffix="[L] Large pads.\n[M] M pads only."
        )[0]

    async def prison(self, eddb_session):
        """
        Find nearest prison megaship.
        """
        sys_name = ' '.join(self.args.system)
        stations = await self.bot.loop.run_in_executor(
            None,
            functools.partial(
                cogdb.eddb.get_closest_station_by_government, eddb_session,
                sys_name, "Prison", limit=10
            )
        )

        lines = [['Station', 'System', 'Distance']]
        lines += [[f"{station.name}", f"{system.name}", f"{dist:.2f}"] for station, system, dist in stations]
        return f"__Closest 10 Prison Megaships__\nCentred on: {sys_name}\n\n" + \
            cog.tbl.format_table(lines, header=True)[0]


    async def execute(self):
        msg = 'Invalid near sub command.'
        with cogdb.session_scope(cogdb.EDDBSession) as eddb_session:
@@ -1308,6 +1327,8 @@ class Near(Action):
                msg = await self.control(eddb_session)
            elif self.args.subcmd == 'if':
                msg = await self.ifactors(eddb_session)
            elif self.args.subcmd == 'prison':
                msg = await self.prison(eddb_session)

        await self.bot.send_message(self.msg.channel, msg)

+4 −1
Original line number Diff line number Diff line
@@ -452,10 +452,13 @@ def subs_near(subs, prefix):
    subcmd.add_argument('power', help='A unique substring of power name.')
    subcmd.add_argument('system', nargs='+', help='The system to lookup.')

    subcmd = subcmds.add_parser('if', help='Find interstellar factorsr.')
    subcmd = subcmds.add_parser('if', help='Find nearest interstellar factors.')
    subcmd.add_argument('system', nargs='+', help='The system to lookup.')
    subcmd.add_argument('-m', '--medium', action='store_true', default=False, help='The include mpad only stations.')

    subcmd = subcmds.add_parser('prison', help='Find nearest prison megaship.')
    subcmd.add_argument('system', nargs='+', help='The system to centre on.')


@register_parser
def subs_ocr(subs, prefix):
+30 −0
Original line number Diff line number Diff line
@@ -2004,6 +2004,36 @@ def get_system_closest_to_HQ(session, systems, *, power='hudson'):
        one()


def get_closest_station_by_government(session, system, gov_type, *, limit=10):
    """Find the closest pirson megaship to designated system.

    Results will be a list of tuples of [(Station, System), ...] ordered by distance to system starting at.

    Args:
        session: A session onto the db.
        system: The system to find prison megaships near.
        limit: The number of matching stations to return.

    Raises:
        InvalidCommandArgs: Could not find the system.
    """
    try:
        found = session.query(System).\
            filter(System.name == system).\
            one()
    except sqla_orm.exc.NoResultFound as exc:
        raise cog.exc.InvalidCommandArgs(f"Could not find system: {system}\n\nPlease check for typos.") from exc

    return session.query(Station, System, System.dist_to(found)).\
        join(System, Station.system_id == System.id).\
        join(Faction, Station.controlling_minor_faction_id == Faction.id).\
        join(Government, Faction.government_id == Government.id).\
        filter(Government.text == gov_type).\
        order_by(System.dist_to(found)).\
        limit(limit).\
        all()


def populate_system_controls(session):
    """
    Compute all pairs of control and exploited systems
+12 −0
Original line number Diff line number Diff line
@@ -1700,6 +1700,18 @@ async def test_cmd_near_if(f_bot):
    assert "Lacaille 9352" in actual


@pytest.mark.asyncio
async def test_cmd_near_prison(f_bot):
    msg = fake_msg_gears("!near prison Rana")

    await action_map(msg, f_bot).execute()

    actual = str(f_bot.send_message.call_args).replace("\\n", "\n")
    assert "The Pillar of Fortitude" in actual
    assert "SPF-LF 1" in actual
    assert "29.34" in actual


@pytest.mark.asyncio
async def test_cmd_vote_prep(f_bot, f_dusers, f_global_testbed, f_vote_testbed):
    msg = fake_msg_gears("!vote prep 1")
+11 −0
Original line number Diff line number Diff line
@@ -327,3 +327,14 @@ def test_find_route_from_hq(eddb_session):

    dist, sorted_systems = cogdb.eddb.find_route_closest_hq(eddb_session, systems)
    assert [x.name for x in sorted_systems] == expected


def test_get_closest_station_by_government(eddb_session):
    results = cogdb.eddb.get_closest_station_by_government(eddb_session, 'Rana', 'Prison')
    expect = 'The Pillar of Fortitude'
    assert results[0][0].name == expect


def test_get_closest_station_by_government_bad_system(eddb_session):
    with pytest.raises(cog.exc.InvalidCommandArgs):
        cogdb.eddb.get_closest_station_by_government(eddb_session, 'zxzxzx', 'Prison')