Ejemplo n.º 1
0
 def test_add(self):
     unitmgr = UnitMgr(None, Menu(None))
     unitmgr.add(Unit('type1', UnitInfo(1, 1, 1, Coord())))
     unitmgr.add(Unit('type2', UnitInfo(2, 1, 1, Coord())))
     self.assertEqual(len(unitmgr), 2)
     self.assertTrue(1 in unitmgr)
     self.assertTrue(2 in unitmgr)
Ejemplo n.º 2
0
 def test_eq(self):
     # Only the unit id is checked
     a = Unit('type1', UnitInfo(1, 2, 3, Coord(1, 2, -3)))
     b = Unit('type1', UnitInfo(2, 2, 3, Coord(1, 2, -3)))
     self.assertTrue(a == a)
     self.assertFalse(a == b)
     self.assertFalse(b == a)
     self.assertTrue(b == b)
Ejemplo n.º 3
0
    def test_get_by_id(self):
        unitmgr = UnitMgr(None, Menu(None))
        unitmgr.add(Unit('type1', UnitInfo(1, 1, 1, Coord())))
        unitmgr.add(Unit('type2', UnitInfo(2, 1, 1, Coord())))

        missing = unitmgr.get_by_id('missing')
        self.assertIsNone(missing)

        unit1 = unitmgr.get_by_id(1)
        self.assertIsNotNone(unit1)
        self.assertEqual(unit1.unit_type, 'type1')
        self.assertEqual(unit1.unit_info.unit_id, 1)
Ejemplo n.º 4
0
    def test_response_nonempty_write_read(self):
        outstream = io.BytesIO()
        a = Sun(UnitInfo(10, 1, 1, Coord(1, 2, -3)))
        b = Planet(UnitInfo(11, 1, 1, Coord(1, -2, 1)))
        c = ReconDrone(UnitInfo(12, 1, 1, Coord(2, -3, 1)))
        UnitResponse([a, b, c]).write(outstream)

        instream = io.BytesIO(outstream.getvalue())
        response = UnitResponse.read(instream)

        self.assertIsNotNone(response.units)
        self.assertEqual(len(response.units), 3)
        self.assertEqual(response.units[0], a)
        self.assertEqual(response.units[1], b)
        self.assertEqual(response.units[2], c)
Ejemplo n.º 5
0
 def test_init(self):
     unit = Unit('type', UnitInfo(10, 11, 12, Coord(1, 2, -3)))
     self.assertEqual(unit.unit_type, 'type')
     self.assertEqual(unit.unit_info.unit_id, 10)
     self.assertEqual(unit.unit_info.user_id, 11)
     self.assertEqual(unit.unit_info.game_id, 12)
     self.assertEqual(unit.unit_info.coord, Coord(1, 2, -3))
Ejemplo n.º 6
0
    def test_get_selected_default(self):
        unitmgr = UnitMgr(None, Menu(None))
        a = Unit('type1', UnitInfo(1, 1, 1, Coord()))
        b = Unit('type2', UnitInfo(2, 1, 1, Coord()))
        unitmgr.add(a)
        unitmgr.add(b)

        empty = unitmgr.get_selected()
        self.assertIsNotNone(empty)
        self.assertEqual(len(empty), 0)

        a.selected = True

        sel = unitmgr.get_selected()
        self.assertIsNotNone(sel)
        self.assertEqual(len(sel), 1)
        self.assertEqual(sel[0].unit_info.unit_id, a.unit_info.unit_id)
Ejemplo n.º 7
0
    def test_remove_by_id(self):
        unitmgr = UnitMgr(None, Menu(None))
        unitmgr.add(Unit('type1', UnitInfo(1, 1, 1, Coord())))
        unitmgr.add(Unit('type2', UnitInfo(2, 1, 1, Coord())))
        self.assertEqual(len(unitmgr), 2)
        self.assertTrue(1 in unitmgr)
        self.assertTrue(2 in unitmgr)

        unitmgr.remove_by_id(3)
        self.assertEqual(len(unitmgr), 2)
        self.assertTrue(1 in unitmgr)
        self.assertTrue(2 in unitmgr)

        unitmgr.remove_by_id(1)
        self.assertEqual(len(unitmgr), 1)
        self.assertFalse(1 in unitmgr)
        self.assertTrue(2 in unitmgr)
Ejemplo n.º 8
0
    def test_write_read(self):
        a = Sun(UnitInfo(1, 1, 1, Coord(1, 2, -3)))
        b = Planet(UnitInfo(2, 2, 2, Coord(1, 2, -3)))
        c = ReconDrone(UnitInfo(3, 3, 3, Coord(1, 2, -3)))

        for unit in [a, b, c]:
            outstream = io.BytesIO()
            UnitFactory.write(outstream, unit)

            instream = io.BytesIO(outstream.getvalue())
            read = UnitFactory.read(instream)

            self.assertEqual(read.unit_type, unit.unit_type)
            self.assertEqual(read.unit_info.unit_id, unit.unit_info.unit_id)
            self.assertEqual(read.unit_info.user_id, unit.unit_info.user_id)
            self.assertEqual(read.unit_info.game_id, unit.unit_info.game_id)
            self.assertEqual(read.unit_info.coord, unit.unit_info.coord)
Ejemplo n.º 9
0
 def read(iostream):
     version = int.from_bytes(iostream.read(1),
                              byteorder=BYTE_ORDER,
                              signed=False)
     if version == 1:
         unit_info = UnitInfo.read(iostream)
         return ReconDrone(unit_info)
     else:
         raise Exception(f'Unsupported serialization version: {version}')
Ejemplo n.º 10
0
    def test_get_selected_false(self):
        unitmgr = UnitMgr(None, Menu(None))
        a = Unit('type1', UnitInfo(1, 1, 1, Coord()))
        b = Unit('type2', UnitInfo(2, 1, 1, Coord()))
        unitmgr.add(a)
        unitmgr.add(b)

        both = unitmgr.get_selected(False)
        self.assertIsNotNone(both)
        self.assertEqual(len(both), 2)
        self.assertEqual(both[0].unit_info.unit_id, a.unit_info.unit_id)
        self.assertEqual(both[1].unit_info.unit_id, b.unit_info.unit_id)

        a.selected = True

        unsel = unitmgr.get_selected(False)
        self.assertIsNotNone(unsel)
        self.assertEqual(len(unsel), 1)
        self.assertEqual(unsel[0].unit_info.unit_id, b.unit_info.unit_id)
Ejemplo n.º 11
0
    def test_get_by_type(self):
        unitmgr = UnitMgr(None, Menu(None))
        unitmgr.add(Unit('type1', UnitInfo(1, 1, 1, Coord())))
        unitmgr.add(Unit('type2', UnitInfo(2, 1, 1, Coord())))
        unitmgr.add(Unit('type1', UnitInfo(3, 1, 1, Coord())))

        empty = unitmgr.get_by_type('missing')
        self.assertIsNotNone(empty)
        self.assertEqual(len(empty), 0)

        type1 = unitmgr.get_by_type('type1')
        self.assertIsNotNone(type1)
        self.assertEqual(len(type1), 2)
        self.assertEqual(1, type1[0].unit_info.unit_id)
        self.assertEqual(3, type1[1].unit_info.unit_id)

        type2 = unitmgr.get_by_type('type2')
        self.assertIsNotNone(type2)
        self.assertEqual(len(type2), 1)
        self.assertEqual(2, type2[0].unit_info.unit_id)
Ejemplo n.º 12
0
    def get_for_game(self, game_id):
        log = logging.getLogger(__name__)
        log.info(f'Fetching units for game: {game_id}')

        units = []
        with self.engine.connect() as conn:
            select = sqlalchemy.select(
                [DbUnits.TABLE]).where(DbUnits.TABLE.c.game_id == game_id)
            result = conn.execute(select)
            for row in result:
                coord = Coord(row['coord_x'], -row['coord_x'] - row['coord_z'],
                              row['coord_z'])
                unit_info = UnitInfo(row['id'], row['user_id'], row['game_id'],
                                     coord)
                unit = UnitFactory.get_unit(row['type'])
                units.append(unit(unit_info))
            result.close()
        return units
Ejemplo n.º 13
0
 def test_str(self):
     unit = Unit('type', UnitInfo(10, 11, 12, Coord(1, 2, -3)))
     self.assertEqual(
         str(unit),
         'Unit[unit_type=type, unit_info=UnitInfo[unit_id=10, user_id=11, game_id=12,'
         + ' coord=Coord[x=1, y=2, z=-3]]]')