laneya

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

commit
991491d52d094a8e12d5b43480cb2a6afe8635d8
parent
bd244f98e446e9d42d6265e3339b68dde7060c4a
Author
Tobias Bengfort <tobias.bengfort@gmx.net>
Date
2014-10-04 15:13
add user information in protocol

Diffstat

M laneya/client.py 9 +++++++++
M laneya/protocol.py 13 ++++++++-----
M laneya/server.py 2 +-

3 files changed, 18 insertions, 6 deletions


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

@@ -13,11 +13,20 @@ def _print(s):
   13    13 
   14    14 
   15    15 class ClientProtocol(protocol.Protocol):
   -1    16     def setup(self, user):
   -1    17         self.user = user
   -1    18 
   -1    19     def sendRequest(self, action, **kwargs):
   -1    20         return protocol.Protocol.sendRequest(
   -1    21             self, self.user, action, **kwargs)
   -1    22 
   16    23     def updateReceived(self, action, **kwargs):  # TODO
   17    24         print(action, kwargs)
   18    25 
   19    26 
   20    27 def connected(protocol):  # TODO
   -1    28     protocol.setup('testuser')
   -1    29 
   21    30     protocol.sendRequest('echo', foo='bar')\
   22    31         .then(_print)
   23    32 

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

@@ -109,7 +109,7 @@ class Protocol(JSONProtocol):
  109   109     def __init__(self):
  110   110         self._responseDeferreds = {}
  111   111 
  112    -1     def requestReceived(self, action, **kwargs):
   -1   112     def requestReceived(self, user, action, **kwargs):
  113   113         """Overwrite this on the server implementation."""
  114   114         raise NotImplementedError
  115   115 
@@ -117,9 +117,9 @@ class Protocol(JSONProtocol):
  117   117         """Overwrite this on the client implementation."""
  118   118         raise NotImplementedError
  119   119 
  120    -1     def _requestReceived(self, key, action, **data):
   -1   120     def _requestReceived(self, key, user, action, **data):
  121   121         try:
  122    -1             response = self.requestReceived(action, **data)
   -1   122             response = self.requestReceived(user, action, **data)
  123   123         except InvalidError as err:
  124   124             return self._sendResponse(key, 'invalid', message=str(err))
  125   125         except IllegalError as err:
@@ -145,10 +145,12 @@ class Protocol(JSONProtocol):
  145   145 
  146   146     def jsonReceived(self, message):
  147   147         if message['type'] == 'request':
  148    -1             self.validate_message(message, ['action', 'data', 'key', 'type'])
   -1   148             self.validate_message(
   -1   149                 message, ['action', 'data', 'key', 'type', 'user'])
  149   150             self.validate_action(message['action'], message['data'])
  150   151             self._requestReceived(
  151   152                 message['key'],
   -1   153                 message['user'],
  152   154                 message['action'],
  153   155                 **message['data'])
  154   156 
@@ -174,11 +176,12 @@ class Protocol(JSONProtocol):
  174   176         else:
  175   177             log.err('Message type not known: %s' % message['type'])
  176   178 
  177    -1     def sendRequest(self, action, **kwargs):
   -1   179     def sendRequest(self, user, action, **kwargs):
  178   180         """Send a request and get a promise yielding the response."""
  179   181         data = {
  180   182             'type': 'request',
  181   183             'key': generate_key(),
   -1   184             'user': user,
  182   185             'action': action,
  183   186             'data': kwargs,
  184   187         }

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

@@ -24,7 +24,7 @@ class ServerProtocol(protocol.Protocol):
   24    24         for connection in self.factory.connections:
   25    25             connection.sendUpdate(action, **kwargs)
   26    26 
   27    -1     def requestReceived(self, action, **kwargs):  # TODO
   -1    27     def requestReceived(self, user, action, **kwargs):  # TODO
   28    28         if action == 'echo':
   29    29             return kwargs
   30    30         else: