Ejemplo n.º 1
0
  def listen(self, port, host):
    logger.info("Server listening on %s:%s.", host, port)
    self.store.lock()

    green.await_coro(
        websockets.serve(green.coroutine(self.accept),
                         host, port))
Ejemplo n.º 2
0
    def _wrapper():
      green.await_coro(self.start_event.wait())

      try:
        _f(self, *args, **kwargs)
      finally:
        self.loop.stop()
Ejemplo n.º 3
0
        def _wrapper():
            green.await_coro(self.start_event.wait())

            try:
                _f(self, *args, **kwargs)
            finally:
                self.loop.stop()
Ejemplo n.º 4
0
    def on_start(self):
        super().on_start()

        self.app = flask.Flask(__name__)
        self.app.manager = self

        self.route("/store.json", views.store_query)
        self.route("/", views.index)

        logger.info("Started manager.")

        green.await_coro(
            self.loop.create_server(
                lambda: wsgi.WSGIServerHttpProtocol(self.app),
                self.config.bind_host, self.config.bind_port))
Ejemplo n.º 5
0
  def on_start(self):
    super().on_start()

    self.app = flask.Flask(__name__)
    self.app.manager = self

    self.route("/store.json", views.store_query)
    self.route("/", views.index)

    logger.info("Started manager.")

    green.await_coro(
        self.loop.create_server(lambda: wsgi.WSGIServerHttpProtocol(self.app),
                                self.config.bind_host,
                                self.config.bind_port))
Ejemplo n.º 6
0
 def get(self, key):
     v = green.await_coro(
         self.redis.hget(self.hash_key.encode("utf-8"),
                         str(key).encode("utf-8")))
     if v is None:
         raise KeyError(key)
     return v
Ejemplo n.º 7
0
 def on_start(self):
   self.bus = bus.Bus()
   self.store = store.GameStore(green.await_coro(asyncio_redis.Pool.create(
       host=self.config.redis_host, port=self.config.redis_port,
       encoder=encoders.BytesEncoder())))
   self.statsd = statsd.StatsClient(self.config.statsd_host,
                                    self.config.statsd_port,
                                    prefix="elpizo")
Ejemplo n.º 8
0
 def on_start(self):
     self.bus = bus.Bus()
     self.store = store.GameStore(
         green.await_coro(
             asyncio_redis.Pool.create(host=self.config.redis_host,
                                       port=self.config.redis_port,
                                       encoder=encoders.BytesEncoder())))
     self.statsd = statsd.StatsClient(self.config.statsd_host,
                                      self.config.statsd_port,
                                      prefix="elpizo")
Ejemplo n.º 9
0
  def lock(self):
    # this acquires an advisory lock -- locking is not mandatory, but I don't
    # suggest you try bypassing it.
    if not green.await_coro(
        self.redis.set(self._LOCK_KEY.encode("utf-8"), b"",
                       only_if_not_exists=True)):
      raise StoreError("""\
Store is locked. This generally occurs if the server was uncleanly shut down, \
or another copy of the server is running. If you are sure another copy of the \
server is not running, please run:

    python -m elpizo.tools.repairdb
""")
    self.is_lock_acquired = True
Ejemplo n.º 10
0
    def lock(self):
        # this acquires an advisory lock -- locking is not mandatory, but I don't
        # suggest you try bypassing it.
        if not green.await_coro(
                self.redis.set(self._LOCK_KEY.encode("utf-8"),
                               b"",
                               only_if_not_exists=True)):
            raise StoreError("""\
Store is locked. This generally occurs if the server was uncleanly shut down, \
or another copy of the server is running. If you are sure another copy of the \
server is not running, please run:

    python -m elpizo.tools.repairdb
""")
        self.is_lock_acquired = True
Ejemplo n.º 11
0
    def unlock(self):
        unlocked = green.await_coro(
            self.redis.delete([self._LOCK_KEY.encode("utf-8")])) == 1

        if not self.is_lock_acquired and unlocked:
            logger.warn("Store lock was BROKEN!")

        if self.is_lock_acquired and not unlocked:
            raise StoreError("""\
Lock was acquired but could not be unlocked. This is VERY BAD! Please repair \
the database IMMEDIATELY!

    python -m elpizo.tools.repairdb
""")

        self.is_lock_acquired = False
Ejemplo n.º 12
0
  def unlock(self):
    unlocked = green.await_coro(
        self.redis.delete([self._LOCK_KEY.encode("utf-8")])) == 1

    if not self.is_lock_acquired and unlocked:
      logger.warn("Store lock was BROKEN!")

    if self.is_lock_acquired and not unlocked:
      raise StoreError("""\
Lock was acquired but could not be unlocked. This is VERY BAD! Please repair \
the database IMMEDIATELY!

    python -m elpizo.tools.repairdb
""")

    self.is_lock_acquired = False
Ejemplo n.º 13
0
 def is_locked(self):
   return green.await_coro(self.redis.exists(self._LOCK_KEY.encode("utf-8")))
Ejemplo n.º 14
0
 def is_locked(self):
     return green.await_coro(
         self.redis.exists(self._LOCK_KEY.encode("utf-8")))
Ejemplo n.º 15
0
Archivo: kvs.py Proyecto: kapace/elpizo
 def keys(self):
   for fut in green.await_coro(
       self.redis.hkeys(self.hash_key.encode("utf-8"))):
     yield green.await(fut).decode("utf-8")
Ejemplo n.º 16
0
def initdb(app):
  app.store.lock()
  try:
    input("This will DELETE ALL DATA from the database! Press ENTER to "
          "continue, or Ctrl+C to abort. ")
  except KeyboardInterrupt:
    app.store.unlock()
    return

  green.await_coro(app.store.redis.flushdb())

  app.store.lock()

  windvale = realm.Realm(name="Windvale", size=geometry.Vector2(128, 128))
  app.store.realms.create(windvale)

  for base_y in range(0, windvale.size.y, realm.Region.SIZE):
    for base_x in range(0, windvale.size.x, realm.Region.SIZE):
      tiles = []

      for offset_y in range(realm.Region.SIZE):
        for offset_x in range(realm.Region.SIZE):
          x = base_x + offset_x
          y = base_y + offset_y

          if x == 0 and y == 0:
            tile = 34
          elif x == 0 and y == windvale.size.y - 1:
            tile = 40
          elif x == windvale.size.x - 1 and y == 0:
            tile = 36
          elif x == windvale.size.x - 1 and y == windvale.size.y - 1:
            tile = 38
          elif x == 0:
            tile = 16
          elif x == windvale.size.x - 1:
            tile = 24
          elif y == 0:
            tile = 20
          elif y == windvale.size.y - 1:
            tile = 28
          else:
            tile = 0

          tiles.append(tile)

      platform_tiles = [-1] * (realm.Region.SIZE * realm.Region.SIZE)
      platform_tiles[4 + 4 * realm.Region.SIZE] = 34
      platform_tiles[5 + 4 * realm.Region.SIZE] = 20
      platform_tiles[6 + 4 * realm.Region.SIZE] = 36
      platform_tiles[4 + 5 * realm.Region.SIZE] = 16
      platform_tiles[5 + 5 * realm.Region.SIZE] = 0
      platform_tiles[6 + 5 * realm.Region.SIZE] = 24
      platform_tiles[4 + 6 * realm.Region.SIZE] = 40
      platform_tiles[5 + 6 * realm.Region.SIZE] = 28
      platform_tiles[6 + 6 * realm.Region.SIZE] = 38

      wall_tiles = [-1] * (realm.Region.SIZE * realm.Region.SIZE)
      wall_tiles[4 + 7 * realm.Region.SIZE] = 46
      wall_tiles[6 + 7 * realm.Region.SIZE] = 46

      stairs_tiles = [-1] * (realm.Region.SIZE * realm.Region.SIZE)
      stairs_tiles[5 + 7 * realm.Region.SIZE] = 47

      passabilities = [0b1111] * (realm.Region.SIZE * realm.Region.SIZE)
      passabilities[4 + 3 * realm.Region.SIZE] = 0b1110
      passabilities[5 + 3 * realm.Region.SIZE] = 0b1110
      passabilities[6 + 3 * realm.Region.SIZE] = 0b1110
      passabilities[3 + 4 * realm.Region.SIZE] = 0b1101
      passabilities[4 + 4 * realm.Region.SIZE] = 0b0011
      passabilities[5 + 4 * realm.Region.SIZE] = 0b1011
      passabilities[6 + 4 * realm.Region.SIZE] = 0b1001
      passabilities[7 + 4 * realm.Region.SIZE] = 0b0111
      passabilities[3 + 5 * realm.Region.SIZE] = 0b1101
      passabilities[4 + 5 * realm.Region.SIZE] = 0b0111
      passabilities[6 + 5 * realm.Region.SIZE] = 0b1101
      passabilities[7 + 5 * realm.Region.SIZE] = 0b0111
      passabilities[3 + 6 * realm.Region.SIZE] = 0b1101
      passabilities[4 + 6 * realm.Region.SIZE] = 0b0110
      passabilities[6 + 6 * realm.Region.SIZE] = 0b1100
      passabilities[7 + 6 * realm.Region.SIZE] = 0b0111
      passabilities[4 + 7 * realm.Region.SIZE] = 0b0000
      passabilities[6 + 7 * realm.Region.SIZE] = 0b0000

      region = realm.Region(
          realm_id=windvale.id, location=geometry.Vector2(base_x, base_y),
          passabilities=passabilities,
          layers=[realm.Layer(terrain="grassland", tiles=tiles),
                  realm.Layer(terrain="dirt", tiles=platform_tiles),
                  realm.Layer(terrain="grassland", tiles=platform_tiles),
                  realm.Layer(terrain="dirt_wall", tiles=wall_tiles),
                  realm.Layer(terrain="stairs", tiles=stairs_tiles)],
          entities=set())
      # We don't use create() here because we've already assigned an id.
      windvale.regions.save(region)

  logging.info("Created Windvale.")

  app.store.entities.create(entities.Player(
      name="Valjean",
      gender="male",
      body="light",
      hair="brown_messy_1",
      facial="brown_beard",
      direction=1,
      health=10,
      realm_id=windvale.id,
      location=geometry.Vector3(0, 0, 0),
      inventory=set(),
      torso_item=app.store.create_item(torso_items.WhiteLongsleeveShirt()),
      legs_item=app.store.create_item(legs_items.TealPants()),
      feet_item=app.store.create_item(feet_items.BrownShoes()),
      weapon=app.store.create_item(weapons.Dagger())))

  app.store.entities.create(entities.Player(
      name="Marius",
      gender="male",
      body="light",
      hair="brown_messy_1",
      direction=1,
      health=10,
      realm_id=windvale.id,
      location=geometry.Vector3(0, 16, 0),
      inventory=set(),
      legs_item=app.store.create_item(legs_items.TealPants())))

  app.store.entities.create(entities.Player(
      name="Courfeyrac",
      gender="male",
      body="light",
      hair="brown_messy_1",
      direction=1,
      health=10,
      realm_id=windvale.id,
      location=geometry.Vector3(16, 16, 0),
      inventory=set(),
      legs_item=app.store.create_item(legs_items.TealPants())))

  app.store.entities.create(entities.Player(
      name="Enjolras",
      gender="male",
      body="light",
      hair="brown_messy_1",
      direction=1,
      health=10,
      realm_id=windvale.id,
      location=geometry.Vector3(12, 16, 0),
      inventory=set(),
      legs_item=app.store.create_item(legs_items.TealPants())))

  for _ in range(25):
    app.store.entities.create(entities.NPC(
        name="Green Slime",
        gender="neuter",
        body="green_slime",
        direction=1,
        health=5,
        realm_id=windvale.id,
        location=geometry.Vector3(random.randint(0, 32), random.randint(0, 32), 0),
        inventory={app.store.create_item(restorative.Carrot())},
        behavior="wander"))

  logging.info("Created players.")
Ejemplo n.º 17
0
Archivo: net.py Proyecto: kapace/elpizo
 def close(self):
   green.await_coro(self.websocket.close())
Ejemplo n.º 18
0
    def listen(self, port, host):
        logger.info("Server listening on %s:%s.", host, port)
        self.store.lock()

        green.await_coro(
            websockets.serve(green.coroutine(self.accept), host, port))
Ejemplo n.º 19
0
  def connect(self, uri):
    logger.info("Client connecting to %s", uri)

    websocket = green.await_coro(websockets.connect(uri))
    self.handle(net.Transport(websocket))
Ejemplo n.º 20
0
Archivo: kvs.py Proyecto: kapace/elpizo
 def get(self, key):
   v = green.await_coro(self.redis.hget(self.hash_key.encode("utf-8"),
                                        str(key).encode("utf-8")))
   if v is None:
     raise KeyError(key)
   return v
Ejemplo n.º 21
0
def initdb(app):
    app.store.lock()
    try:
        input("This will DELETE ALL DATA from the database! Press ENTER to "
              "continue, or Ctrl+C to abort. ")
    except KeyboardInterrupt:
        app.store.unlock()
        return

    green.await_coro(app.store.redis.flushdb())

    app.store.lock()

    windvale = realm.Realm(name="Windvale", size=geometry.Vector2(128, 128))
    app.store.realms.create(windvale)

    for base_y in range(0, windvale.size.y, realm.Region.SIZE):
        for base_x in range(0, windvale.size.x, realm.Region.SIZE):
            tiles = []

            for offset_y in range(realm.Region.SIZE):
                for offset_x in range(realm.Region.SIZE):
                    x = base_x + offset_x
                    y = base_y + offset_y

                    if x == 0 and y == 0:
                        tile = 34
                    elif x == 0 and y == windvale.size.y - 1:
                        tile = 40
                    elif x == windvale.size.x - 1 and y == 0:
                        tile = 36
                    elif x == windvale.size.x - 1 and y == windvale.size.y - 1:
                        tile = 38
                    elif x == 0:
                        tile = 16
                    elif x == windvale.size.x - 1:
                        tile = 24
                    elif y == 0:
                        tile = 20
                    elif y == windvale.size.y - 1:
                        tile = 28
                    else:
                        tile = 0

                    tiles.append(tile)

            platform_tiles = [-1] * (realm.Region.SIZE * realm.Region.SIZE)
            platform_tiles[4 + 4 * realm.Region.SIZE] = 34
            platform_tiles[5 + 4 * realm.Region.SIZE] = 20
            platform_tiles[6 + 4 * realm.Region.SIZE] = 36
            platform_tiles[4 + 5 * realm.Region.SIZE] = 16
            platform_tiles[5 + 5 * realm.Region.SIZE] = 0
            platform_tiles[6 + 5 * realm.Region.SIZE] = 24
            platform_tiles[4 + 6 * realm.Region.SIZE] = 40
            platform_tiles[5 + 6 * realm.Region.SIZE] = 28
            platform_tiles[6 + 6 * realm.Region.SIZE] = 38

            wall_tiles = [-1] * (realm.Region.SIZE * realm.Region.SIZE)
            wall_tiles[4 + 7 * realm.Region.SIZE] = 46
            wall_tiles[6 + 7 * realm.Region.SIZE] = 46

            stairs_tiles = [-1] * (realm.Region.SIZE * realm.Region.SIZE)
            stairs_tiles[5 + 7 * realm.Region.SIZE] = 47

            passabilities = [0b1111] * (realm.Region.SIZE * realm.Region.SIZE)
            passabilities[4 + 3 * realm.Region.SIZE] = 0b1110
            passabilities[5 + 3 * realm.Region.SIZE] = 0b1110
            passabilities[6 + 3 * realm.Region.SIZE] = 0b1110
            passabilities[3 + 4 * realm.Region.SIZE] = 0b1101
            passabilities[4 + 4 * realm.Region.SIZE] = 0b0011
            passabilities[5 + 4 * realm.Region.SIZE] = 0b1011
            passabilities[6 + 4 * realm.Region.SIZE] = 0b1001
            passabilities[7 + 4 * realm.Region.SIZE] = 0b0111
            passabilities[3 + 5 * realm.Region.SIZE] = 0b1101
            passabilities[4 + 5 * realm.Region.SIZE] = 0b0111
            passabilities[6 + 5 * realm.Region.SIZE] = 0b1101
            passabilities[7 + 5 * realm.Region.SIZE] = 0b0111
            passabilities[3 + 6 * realm.Region.SIZE] = 0b1101
            passabilities[4 + 6 * realm.Region.SIZE] = 0b0110
            passabilities[6 + 6 * realm.Region.SIZE] = 0b1100
            passabilities[7 + 6 * realm.Region.SIZE] = 0b0111
            passabilities[4 + 7 * realm.Region.SIZE] = 0b0000
            passabilities[6 + 7 * realm.Region.SIZE] = 0b0000

            region = realm.Region(realm_id=windvale.id,
                                  location=geometry.Vector2(base_x, base_y),
                                  passabilities=passabilities,
                                  layers=[
                                      realm.Layer(terrain="grassland",
                                                  tiles=tiles),
                                      realm.Layer(terrain="dirt",
                                                  tiles=platform_tiles),
                                      realm.Layer(terrain="grassland",
                                                  tiles=platform_tiles),
                                      realm.Layer(terrain="dirt_wall",
                                                  tiles=wall_tiles),
                                      realm.Layer(terrain="stairs",
                                                  tiles=stairs_tiles)
                                  ],
                                  entities=set())
            # We don't use create() here because we've already assigned an id.
            windvale.regions.save(region)

    logging.info("Created Windvale.")

    app.store.entities.create(
        entities.Player(
            name="Valjean",
            gender="male",
            body="light",
            hair="brown_messy_1",
            facial="brown_beard",
            direction=1,
            health=10,
            realm_id=windvale.id,
            location=geometry.Vector3(0, 0, 0),
            inventory=set(),
            torso_item=app.store.create_item(
                torso_items.WhiteLongsleeveShirt()),
            legs_item=app.store.create_item(legs_items.TealPants()),
            feet_item=app.store.create_item(feet_items.BrownShoes()),
            weapon=app.store.create_item(weapons.Dagger())))

    app.store.entities.create(
        entities.Player(name="Marius",
                        gender="male",
                        body="light",
                        hair="brown_messy_1",
                        direction=1,
                        health=10,
                        realm_id=windvale.id,
                        location=geometry.Vector3(0, 16, 0),
                        inventory=set(),
                        legs_item=app.store.create_item(
                            legs_items.TealPants())))

    app.store.entities.create(
        entities.Player(name="Courfeyrac",
                        gender="male",
                        body="light",
                        hair="brown_messy_1",
                        direction=1,
                        health=10,
                        realm_id=windvale.id,
                        location=geometry.Vector3(16, 16, 0),
                        inventory=set(),
                        legs_item=app.store.create_item(
                            legs_items.TealPants())))

    app.store.entities.create(
        entities.Player(name="Enjolras",
                        gender="male",
                        body="light",
                        hair="brown_messy_1",
                        direction=1,
                        health=10,
                        realm_id=windvale.id,
                        location=geometry.Vector3(12, 16, 0),
                        inventory=set(),
                        legs_item=app.store.create_item(
                            legs_items.TealPants())))

    for _ in range(25):
        app.store.entities.create(
            entities.NPC(
                name="Green Slime",
                gender="neuter",
                body="green_slime",
                direction=1,
                health=5,
                realm_id=windvale.id,
                location=geometry.Vector3(random.randint(0, 32),
                                          random.randint(0, 32), 0),
                inventory={app.store.create_item(restorative.Carrot())},
                behavior="wander"))

    logging.info("Created players.")
Ejemplo n.º 22
0
Archivo: kvs.py Proyecto: kapace/elpizo
 def set(self, key, value):
   green.await_coro(self.redis.hset(self.hash_key.encode("utf-8"),
                                    str(key).encode("utf-8"), value))
Ejemplo n.º 23
0
Archivo: kvs.py Proyecto: kapace/elpizo
 def next_serial(self):
   return int(green.await_coro(self.redis.incr(self.key.encode("utf-8"))))
Ejemplo n.º 24
0
Archivo: net.py Proyecto: kapace/elpizo
 def recv(self):
   return green.await_coro(self.websocket.recv())
Ejemplo n.º 25
0
Archivo: net.py Proyecto: kapace/elpizo
 def send(self, packet):
   green.await_coro(self.websocket.send(packet))
Ejemplo n.º 26
0
 def delete(self, key):
     green.await_coro(
         self.redis.hdel(self.hash_key.encode("utf-8"),
                         [str(key).encode("utf-8")]))
Ejemplo n.º 27
0
 def sleep(self, secs):
   green.await_coro(asyncio.sleep(secs))
Ejemplo n.º 28
0
 def keys(self):
     for fut in green.await_coro(
             self.redis.hkeys(self.hash_key.encode("utf-8"))):
         yield green. await (fut).decode("utf-8")
Ejemplo n.º 29
0
 def next_serial(self):
     return int(green.await_coro(self.redis.incr(self.key.encode("utf-8"))))
Ejemplo n.º 30
0
Archivo: kvs.py Proyecto: kapace/elpizo
 def delete(self, key):
   green.await_coro(self.redis.hdel(self.hash_key.encode("utf-8"),
                                    [str(key).encode("utf-8")]))
Ejemplo n.º 31
0
 def sleep(self, secs):
     green.await_coro(asyncio.sleep(secs))
Ejemplo n.º 32
0
 def set(self, key, value):
     green.await_coro(
         self.redis.hset(self.hash_key.encode("utf-8"),
                         str(key).encode("utf-8"), value))