- commit
- def6f9dc207311e93ef924e3c19ea0e98deb8737
- parent
- 44a58353f3c44f2f7fafc755c9b112ba9f58ce06
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2014-10-05 10:17
Merge branch 'feature-move'
Diffstat
| M | laneya/actions.py | 22 | ++++++++++++++++++++++ |
| M | laneya/client.py | 9 | +++++++++ |
| M | laneya/server.py | 37 | ++++++++++++++++++++++++++++++++++++- |
3 files changed, 67 insertions, 1 deletions
diff --git a/laneya/actions.py b/laneya/actions.py
@@ -14,3 +14,25 @@ def echo(foo=''): 14 14 15 15 def other(foo=''): 16 16 """Example client action.""" -1 17 -1 18 -1 19 def move(direction=None): -1 20 """Start moving in the defined direction. -1 21 -1 22 This message is primarily useful for clients that want to move their users -1 23 on the map. They can easily convert key press events to ``move`` -1 24 messages. -1 25 -1 26 I might be worth to also broadcast these messages so clients can -1 27 interpolate a user's movement. ``move`` messages are however not fit to -1 28 handle the actual movement of entities because clients may run with -1 29 different speeds. The ``position`` message should be used instead. -1 30 -1 31 """ -1 32 assert direction in ['north', 'east', 'south', 'west', 'stop'] -1 33 -1 34 -1 35 def position(x=None, y=None, entity=None): -1 36 """Set an entities position.""" -1 37 assert isinstance(x, int) -1 38 assert isinstance(y, int)
diff --git a/laneya/client.py b/laneya/client.py
@@ -17,6 +17,9 @@ class ClientProtocol(protocol.ClientProtocol): 17 17 def updateReceived(self, action, **kwargs): # TODO 18 18 print(action, kwargs) 19 19 -1 20 def move(self, direction): -1 21 return self.sendRequest('move', direction=direction) -1 22 20 23 21 24 def connected(protocol): # TODO 22 25 protocol.setup('testuser') @@ -27,6 +30,12 @@ def connected(protocol): # TODO 27 30 protocol.sendRequest('other', foo='bar')\ 28 31 .then(_print, log.err) 29 32 -1 33 protocol.move('south') -1 34 reactor.callLater(2, lambda: protocol.move('west')) -1 35 reactor.callLater(4, lambda: protocol.move('north')) -1 36 reactor.callLater(6, lambda: protocol.move('east')) -1 37 reactor.callLater(8, lambda: protocol.move('stop')) -1 38 30 39 31 40 def main(): 32 41 log.startLogging(sys.stdout)
diff --git a/laneya/server.py b/laneya/server.py
@@ -3,6 +3,7 @@ import sys 3 3 from twisted.python import log 4 4 from twisted.internet.endpoints import TCP4ServerEndpoint 5 5 from twisted.internet import reactor -1 6 from twisted.internet import task 6 7 7 8 import protocol 8 9 @@ -11,16 +12,50 @@ class ServerProtocol(protocol.ServerProtocol): 11 12 def requestReceived(self, user, action, **kwargs): # TODO 12 13 if action == 'echo': 13 14 return kwargs -1 15 elif action == 'move': -1 16 self.factory.direction = kwargs['direction'] -1 17 return {} 14 18 else: 15 19 self.broadcastUpdate(action, **kwargs) 16 20 reactor.callLater(5, self.broadcastUpdate, action, **kwargs) 17 21 return {} 18 22 19 23 -1 24 class Server(protocol.ServerProtocolFactory): -1 25 def __init__(self): -1 26 protocol.ServerProtocolFactory.__init__(self, ServerProtocol) -1 27 -1 28 # TODO: should be set per user -1 29 self.direction = 'stop' -1 30 self.position_x = 0 -1 31 self.position_y = 0 -1 32 -1 33 def mainloop(self): -1 34 if self.direction == 'north': -1 35 self.position_y -= 1 -1 36 elif self.direction == 'east': -1 37 self.position_x += 1 -1 38 elif self.direction == 'south': -1 39 self.position_y += 1 -1 40 elif self.direction == 'west': -1 41 self.position_x -= 1 -1 42 if self.direction != 'stop': -1 43 self.broadcastUpdate( -1 44 'position', -1 45 x=self.position_x, -1 46 y=self.position_y, -1 47 entity='example') -1 48 -1 49 20 50 def main(): 21 51 log.startLogging(sys.stdout) -1 52 server = Server() 22 53 endpoint = TCP4ServerEndpoint(reactor, 5001)23 -1 endpoint.listen(protocol.ServerProtocolFactory(ServerProtocol))-1 54 endpoint.listen(server) -1 55 -1 56 mainloop = task.LoopingCall(server.mainloop) -1 57 mainloop.start(0.1) -1 58 24 59 reactor.run() 25 60 26 61