class ServerProtocol(Protocol): def connection_made(self, addr): log.info('client connected %s', addr) self.addr = addr hello = factory(1) hello.name = self.name self.transport.send(hello) self.state = SS.NEW_CLIENT self.world = None def connection_lost(self): if self.world: self.world.ticker().stop() log.info("client disconnected %s", self.addr) # TODO: not sure if this is the right way to detect disconnection def msg_received(self, msg): # TODO: this will require reimplementing messages in some kind of # FSM DSL, ideally programming lang agnostic if msg.id == 1: if not self.state is SS.NEW_CLIENT: raise RuntimeError self.name = msg.name self.transport.send(factory(2)) self.state = SS.GAME_CHALLENGE elif self.state is SS.GAME_CHALLENGE: if msg.id == 4: self.sock.shutdown(2) elif msg.id == 5: self.world = World(20, 20) self.player = self.world.spawn(Player, owner=self) Greenlet.spawn(self.world.ticker()) msg = factory(6) msg.shape = self.world.shape msg.size = self.world.size self.transport.send(msg) self.state = SS.GAME_IN_PROGRESS else: raise RuntimeError elif self.state is SS.GAME_IN_PROGRESS: if msg.id == 8: args = [int(x) for x in msg.coord.split(" ")] self.player.move(Coord(*args)) else: raise RuntimeError def new_state(self, s): """ @param s int """ if s: self.state = s
class TestWorldObject(TestCase): def setUp(self): self.w = World(20, 21) self.o = self.w.spawn(WorldObject, owner=OwnerMock()) def test_move(self): o, w = self.o, self.w o.move(Coord(2, 3)) assert (o.coord == Coord(0,0)).all(), "precondition fail" assert (o.coord_delta == Coord(2,3)).all(), "precondition fail" o.tick() self.assertTrue((o.coord == Coord(1,1)).all(), "object not on the expected position") self.assertTrue((o.coord_delta == Coord(1,2)).all(), "objects delta for next tick is incorrect") o.ticked(False) o.tick() self.assertTrue((o.coord == Coord(2,2)).all(), "object not on the expected position") self.assertTrue((o.coord_delta == Coord(0,1)).all(), "objects delta for next tick is incorrect") self.assertFalse(w.position(Coord(0,0)).object, "old position is not" "freed") self.assertEqual(w.position(o.coord).object, o) def test_remove(self): self.w.remove(self.o) self.assertFalse(self.o in self.w.objects) def test_move_outside(self): self.o.move(Coord(-1, 0)) self.o.tick() self.assertEqual(self.o.coord, None) def test_move_zerodelta(self): o, w = self.o, self.w o.move(Coord(0,0)) o.tick() # test shouldnt raise
def msg_received(self, msg): # TODO: this will require reimplementing messages in some kind of # FSM DSL, ideally programming lang agnostic if msg.id == 1: if not self.state is SS.NEW_CLIENT: raise RuntimeError self.name = msg.name self.transport.send(factory(2)) self.state = SS.GAME_CHALLENGE elif self.state is SS.GAME_CHALLENGE: if msg.id == 4: self.sock.shutdown(2) elif msg.id == 5: self.world = World(20, 20) self.player = self.world.spawn(Player, owner=self) Greenlet.spawn(self.world.ticker()) msg = factory(6) msg.shape = self.world.shape msg.size = self.world.size self.transport.send(msg) self.state = SS.GAME_IN_PROGRESS else: raise RuntimeError elif self.state is SS.GAME_IN_PROGRESS: if msg.id == 8: args = [int(x) for x in msg.coord.split(" ")] self.player.move(Coord(*args)) else: raise RuntimeError
def setUp(self): self.w = World(20, 21) self.o = self.w.spawn(WorldObject, owner=OwnerMock())