laneya

multiplayer roguelike game
git clone https://git.ce9e.org/laneya.git

commit
090989371c74a485d91a8401b831aca454db76d2
parent
cc83b1aa5bd4406519d934e90abdebd71b17c389
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2018-08-03 10:53
Gardening

Diffstat

M laneya/map.py 47 +++++++++++++++++++++++++++--------------------
M laneya/protocol.py 9 ++++++---
M laneya/server.py 13 ++++++++-----

3 files changed, 41 insertions, 28 deletions


diff --git a/laneya/map.py b/laneya/map.py

@@ -3,6 +3,24 @@ import json
    3     3 import random
    4     4 
    5     5 
   -1     6 def collision_free(room, other):
   -1     7     return (
   -1     8         room['x_min'] > other['x_max'] + 1 or
   -1     9         room['x_max'] < other['x_min'] - 1 or
   -1    10         room['y_min'] > other['y_max'] + 1 or
   -1    11         room['y_max'] < other['y_min'] - 1
   -1    12     )
   -1    13 
   -1    14 
   -1    15 def in_room(x, y, room):
   -1    16     return (
   -1    17         x >= room['x_min'] and
   -1    18         x <= room['x_max'] and
   -1    19         y >= room['y_min'] and
   -1    20         y <= room['y_max']
   -1    21     )
   -1    22 
   -1    23 
    6    24 class MapManager(object):
    7    25     """Manager that takes care of generating and storing all maps.
    8    26 
@@ -17,10 +35,7 @@ class MapManager(object):
   17    35         self.persist = persist
   18    36         self.store = {}
   19    37 
   20    -1     def generate(self, X, Y, Z):
   21    -1         """Generate a new map."""
   22    -1 
   23    -1         _map = Map(self.server, self.width, self.height)
   -1    38     def generate_rooms(self):
   24    39         rooms = []
   25    40 
   26    41         # make sure user and ghost are inside of a room
@@ -44,26 +59,18 @@ class MapManager(object):
   44    59                 'y_max': max(y1, y2),
   45    60             }
   46    61 
   47    -1             def collision_free(other):
   48    -1                 return (
   49    -1                     room['x_min'] > other['x_max'] + 1 or
   50    -1                     room['x_max'] < other['x_min'] - 1 or
   51    -1                     room['y_min'] > other['y_max'] + 1 or
   52    -1                     room['y_max'] < other['y_min'] - 1
   53    -1                 )
   54    -1 
   55    62             if (room['x_max'] - room['x_min'] > 2 and
   56    63                     room['y_max'] - room['y_min'] > 2):
   57    -1                 if all((collision_free(other) for other in rooms)):
   -1    64                 if all(collision_free(room, other) for other in rooms):
   58    65                     rooms.append(room)
   59    66 
   60    -1         def in_room(x, y, room):
   61    -1             return (
   62    -1                 x >= room['x_min'] and
   63    -1                 x <= room['x_max'] and
   64    -1                 y >= room['y_min'] and
   65    -1                 y <= room['y_max']
   66    -1             )
   -1    67         return rooms
   -1    68 
   -1    69     def generate(self, X, Y, Z):
   -1    70         """Generate a new map."""
   -1    71 
   -1    72         _map = Map(self.server, self.width, self.height)
   -1    73         rooms = self.generate_rooms()
   67    74 
   68    75         # carve rooms
   69    76         for x in range(self.width):

diff --git a/laneya/protocol.py b/laneya/protocol.py

@@ -65,7 +65,10 @@ Update
   65    65 import json
   66    66 import logging
   67    67 
   68    -1 import trollius as asyncio
   -1    68 try:
   -1    69     import asyncio
   -1    70 except ImportError:
   -1    71     import trollius as asyncio
   69    72 
   70    73 from . import promise as q
   71    74 from . import actions
@@ -121,7 +124,7 @@ class LoopingCall(object):
  121   124 
  122   125 
  123   126 class NetstringReceiver(asyncio.Protocol):
  124    -1     """ protocol that sends and receives netstrings.
   -1   127     """Protocol that sends and receives netstrings.
  125   128 
  126   129     See http://cr.yp.to/proto/netstrings.txt for the specification of
  127   130     netstrings.
@@ -181,7 +184,7 @@ class BaseProtocol(JSONProtocol):
  181   184         try:
  182   185             fn = getattr(actions, action)
  183   186             fn(**data)
  184    -1         except:
   -1   187         except Exception:
  185   188             logger.error('Invalid action: %s %s' % (action, data))
  186   189             raise InvalidError
  187   190 

diff --git a/laneya/server.py b/laneya/server.py

@@ -1,4 +1,7 @@
    1    -1 import trollius as asyncio
   -1     1 try:
   -1     2     import asyncio
   -1     3 except ImportError:
   -1     4     import trollius as asyncio
    2     5 
    3     6 from . import protocol
    4     7 from .map import MapManager, User
@@ -6,7 +9,7 @@ from .map import MapManager, User
    6     9 
    7    10 class Server(protocol.ServerProtocolFactory):
    8    11     def __init__(self):
    9    -1         protocol.ServerProtocolFactory.__init__(self)
   -1    12         super(Server, self).__init__()
   10    13         self.users = {}
   11    14         self.map_manager = MapManager(self, 60, 40)
   12    15 
@@ -14,14 +17,14 @@ class Server(protocol.ServerProtocolFactory):
   14    17         if user not in self.users:
   15    18             initial_map = self.map_manager.get(0, 0, 0)
   16    19             self.users[user] = User(user, initial_map, 10, 10)
   17    -1             print("login %s" % user)
   -1    20             print('login %s' % user)
   18    21 
   19    22         if action == 'move':
   20    23             self.users[user].direction = kwargs['direction']
   21    24         elif action == 'logout':
   22    25             self.users[user].kill()
   23    26             del self.users[user]
   24    -1             print("logout %s" % user)
   -1    27             print('logout %s' % user)
   25    28         elif action == 'get_map':
   26    29             return self.users[user].map.encode()
   27    30         else:
@@ -33,7 +36,7 @@ class Server(protocol.ServerProtocolFactory):
   33    36             _map.step()
   34    37 
   35    38     def get_active_maps(self):
   36    -1         return set([user.map for user in self.users.values()])
   -1    39         return set(user.map for user in self.users.values())
   37    40 
   38    41 
   39    42 def main():