home
·
contact
·
privacy
projects
/
plomrogue2-experiments
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Lots of refactoring.
[plomrogue2-experiments]
/
server.py
diff --git
a/server.py
b/server.py
index 215317dd6714cfdd97c59b3f980781414e5b269e..14df1f37b5b02be228ad82056813c717720df2e1 100755
(executable)
--- a/
server.py
+++ b/
server.py
@@
-90,7
+90,8
@@
class Task:
class Thing:
class Thing:
- def __init__(self, position):
+ def __init__(self, type_, position):
+ self.type = type_
self.position = position
self.task = Task('wait')
self.position = position
self.task = Task('wait')
@@
-102,9
+103,18
@@
class Thing:
self.position[0] -= 1
elif direction == 'DOWN':
self.position[0] += 1
self.position[0] -= 1
elif direction == 'DOWN':
self.position[0] += 1
+ elif direction == 'RIGHT':
+ self.position[1] += 1
+ elif direction == 'LEFT':
+ self.position[1] -= 1
def decide_task(self):
def decide_task(self):
- self.set_task('wait')
+ if self.position[1] > 1:
+ self.set_task('move', 'LEFT')
+ elif self.position[1] < 3:
+ self.set_task('move', 'RIGHT')
+ else:
+ self.set_task('wait')
def set_task(self, task, *args, **kwargs):
self.task = Task(task, args, kwargs)
def set_task(self, task, *args, **kwargs):
self.task = Task(task, args, kwargs)
@@
-118,7
+128,7
@@
class Thing:
"""
self.task.todo -= 1
if self.task.todo <= 0:
"""
self.task.todo -= 1
if self.task.todo <= 0:
- task= getattr(self, 'task_' + self.task.name)
+ task
= getattr(self, 'task_' + self.task.name)
task(*self.task.args, **self.task.kwargs)
self.task = None
if is_AI and self.task is None:
task(*self.task.args, **self.task.kwargs)
self.task = None
if is_AI and self.task is None:
@@
-130,12
+140,12
@@
class World:
def __init__(self):
self.turn = 0
self.map_size = (5, 5)
def __init__(self):
self.turn = 0
self.map_size = (5, 5)
- self.map_ = 'xxxxx\n'+\
- 'x...x\n'+\
- 'x.X.x\n'+\
- 'x...x\n'+\
+ self.map_ = 'xxxxx\n'
+\
+ 'x...x\n'
+\
+ 'x.X.x\n'
+\
+ 'x...x\n'
+\
'xxxxx'
'xxxxx'
- self.things = [Thing(
position=[3, 3]), Thing(
[1, 1])]
+ self.things = [Thing(
'human', [3, 3]), Thing('monster',
[1, 1])]
self.player_i = 0
self.player = self.things[self.player_i]
self.player_i = 0
self.player = self.things[self.player_i]
@@
-176,6
+186,17
@@
class CommandHandler:
"""Transform tuple (y,x) into string 'Y:'+str(y)+',X:'+str(x)."""
return 'Y:' + str(tuple_[0]) + ',X:' + str(tuple_[1])
"""Transform tuple (y,x) into string 'Y:'+str(y)+',X:'+str(x)."""
return 'Y:' + str(tuple_[0]) + ',X:' + str(tuple_[1])
+ def quoted(self, string):
+ """Quote and escape string so client interprets it as single token."""
+ quoted = []
+ quoted += ['"']
+ for c in string:
+ if c in {'"', '\\'}:
+ quoted += ['\\']
+ quoted += [c]
+ quoted += ['"']
+ return ''.join(quoted)
+
def proceed_to_next_player_turn(self, connection_id):
"""Run game world turns until player can decide their next step.
def proceed_to_next_player_turn(self, connection_id):
"""Run game world turns until player can decide their next step.
@@
-193,15
+214,17
@@
class CommandHandler:
for thing in self.world.things[self.world.player_i+1:]:
thing.proceed()
self.world.turn += 1
for thing in self.world.things[self.world.player_i+1:]:
thing.proceed()
self.world.turn += 1
- for thing
in self.world.things[:self.world.player_i]:
+ for thing in self.world.things[:self.world.player_i]:
thing.proceed()
self.world.player.proceed(is_AI=False)
if self.world.player.task is None:
break
self.send_all('NEW_TURN ' + str(self.world.turn))
self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size))
thing.proceed()
self.world.player.proceed(is_AI=False)
if self.world.player.task is None:
break
self.send_all('NEW_TURN ' + str(self.world.turn))
self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size))
- self.send_all('TERRAIN\n' + self.world.map_)
- self.send_all('POSITION ' + self.stringify_yx(self.world.player.position))
+ self.send_all('TERRAIN\n' + self.quoted(self.world.map_))
+ for thing in self.world.things:
+ self.send_all('THING TYPE:' + thing.type + ' '
+ + self.stringify_yx(thing.position))
def cmd_fib(self, tokens, connection_id):
"""Reply with n-th Fibonacci numbers, n taken from tokens[1:].
def cmd_fib(self, tokens, connection_id):
"""Reply with n-th Fibonacci numbers, n taken from tokens[1:].
@@
-243,8
+266,10
@@
class CommandHandler:
self.world.turn += 1
self.send_all('NEW_TURN ' + str(self.world.turn))
self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size))
self.world.turn += 1
self.send_all('NEW_TURN ' + str(self.world.turn))
self.send_all('MAP_SIZE ' + self.stringify_yx(self.world.map_size))
- self.send_all('TERRAIN\n' + self.world.map_)
- self.send_all('POSITION ' + self.stringify_yx(self.world.player.position))
+ self.send_all('TERRAIN\n' + self.quoted(self.world.map_))
+ for thing in self.world.things:
+ self.send_all('THING TYPE:' + thing.type + ' '
+ + self.stringify_yx(thing.position))
self.pool_result = self.pool.map_async(fib, (35, 35))
def cmd_get_turn(self, connection_id):
self.pool_result = self.pool.map_async(fib, (35, 35))
def cmd_get_turn(self, connection_id):
@@
-253,8
+278,9
@@
class CommandHandler:
def cmd_move(self, direction, connection_id):
"""Set player task to 'move' with direction arg, finish player turn."""
def cmd_move(self, direction, connection_id):
"""Set player task to 'move' with direction arg, finish player turn."""
- if not direction in {'UP', 'DOWN'}:
- raise ArgumentError('MOVE ARGUMENT MUST BE "UP" or "DOWN"')
+ if direction not in {'UP', 'DOWN', 'RIGHT', 'LEFT'}:
+ raise ArgumentError('MOVE ARGUMENT MUST BE ONE OF: '
+ 'UP, DOWN, RIGHT, LEFT')
self.world.player.set_task('move', direction=direction)
self.proceed_to_next_player_turn(connection_id)
self.world.player.set_task('move', direction=direction)
self.proceed_to_next_player_turn(connection_id)