laneya

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

commit
3e2360ea1ee3878f94723c0a51bcce54921d02f7
parent
2e13577427a58b7b3718598b31009a938ed3285e
Author
radow <masteroftheriddles@googlemail.com>
Date
2014-10-05 19:46
Merge branch 'feature-user'

Diffstat

M laneya/actions.py 4 ++++
M laneya/client.py 2 ++
M laneya/server.py 52 ++++++++++++++++++++++++++++++++--------------------

3 files changed, 38 insertions, 20 deletions


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

@@ -36,3 +36,7 @@ def position(x=None, y=None, entity=None):
   36    36     """Set an entities position."""
   37    37     assert isinstance(x, int)
   38    38     assert isinstance(y, int)
   -1    39 
   -1    40 
   -1    41 def logout():
   -1    42     """Delete the requesting user."""

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

@@ -36,6 +36,8 @@ def connected(protocol):  # TODO
   36    36     reactor.callLater(6, lambda: protocol.move('east'))
   37    37     reactor.callLater(8, lambda: protocol.move('stop'))
   38    38 
   -1    39     reactor.callLater(10, lambda: protocol.sendRequest('logout'))
   -1    40 
   39    41 
   40    42 def main():
   41    43     log.startLogging(sys.stdout)

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

@@ -8,12 +8,27 @@ from twisted.internet import task
    8     8 import protocol
    9     9 
   10    10 
   -1    11 class User(object):
   -1    12     def __init__(self, position_x=0, position_y=0, direction='stop'):
   -1    13         self.position_x = position_x
   -1    14         self.position_y = position_y
   -1    15         self.direction = direction
   -1    16 
   -1    17 
   11    18 class ServerProtocol(protocol.ServerProtocol):
   12    19     def requestReceived(self, user, action, **kwargs):  # TODO
   -1    20         if user not in self.factory.users:
   -1    21             self.factory.users[user] = User()
   -1    22             print("login %s" % user)
   -1    23 
   13    24         if action == 'echo':
   14    25             return kwargs
   15    26         elif action == 'move':
   16    -1             self.factory.direction = kwargs['direction']
   -1    27             self.factory.users[user].direction = kwargs['direction']
   -1    28             return {}
   -1    29         elif action == 'logout':
   -1    30             del self.factory.users[user]
   -1    31             print("logout %s" % user)
   17    32             return {}
   18    33         else:
   19    34             self.broadcastUpdate(action, **kwargs)
@@ -24,27 +39,24 @@ class ServerProtocol(protocol.ServerProtocol):
   24    39 class Server(protocol.ServerProtocolFactory):
   25    40     def __init__(self):
   26    41         protocol.ServerProtocolFactory.__init__(self, ServerProtocol)
   27    -1 
   28    -1         # TODO: should be set per user
   29    -1         self.direction = 'stop'
   30    -1         self.position_x = 0
   31    -1         self.position_y = 0
   -1    42         self.users = {}
   32    43 
   33    44     def mainloop(self):
   34    -1         if self.direction == 'north':
   35    -1             self.position_y -= 1
   36    -1         elif self.direction == 'east':
   37    -1             self.position_x += 1
   38    -1         elif self.direction == 'south':
   39    -1             self.position_y += 1
   40    -1         elif self.direction == 'west':
   41    -1             self.position_x -= 1
   42    -1         if self.direction != 'stop':
   43    -1             self.broadcastUpdate(
   44    -1                 'position',
   45    -1                 x=self.position_x,
   46    -1                 y=self.position_y,
   47    -1                 entity='example')
   -1    45         for key, user in self.users.iteritems():
   -1    46             if user.direction == 'north':
   -1    47                 user.position_y -= 1
   -1    48             elif user.direction == 'east':
   -1    49                 user.position_x += 1
   -1    50             elif user.direction == 'south':
   -1    51                 user.position_y += 1
   -1    52             elif user.direction == 'west':
   -1    53                 user.position_x -= 1
   -1    54             if user.direction != 'stop':
   -1    55                 self.broadcastUpdate(
   -1    56                     'position',
   -1    57                     x=user.position_x,
   -1    58                     y=user.position_y,
   -1    59                     entity=key)
   48    60 
   49    61 
   50    62 def main():