home · contact · privacy
Refactor.
[plomrogue2-experiments] / server_ / tasks.py
1 from server_.game_error import GameError
2
3
4
5 class TaskManager:
6
7     def __init__(self, task_classes):
8         self.task_classes = task_classes
9
10     def get_task_class(self, task_name):
11         for task_class in self.task_classes:
12             if task_class.__name__ == 'Task_' + task_name:
13                 return task_class
14         return None
15
16
17 class Task:
18     argtypes = ''
19
20     def __init__(self, thing, args=()):
21         self.thing = thing
22         self.args = args
23         self.todo = 3
24
25     @property
26     def name(self):
27         prefix = 'Task_'
28         class_name = self.__class__.__name__
29         return class_name[len(prefix):]
30
31     def check(self):
32         pass
33
34     def get_args_string(self):
35         stringed_args = []
36         for arg in self.args:
37             if type(arg) == str:
38                 stringed_args += [server_.io.quote(arg)]
39             else:
40                 raise GameError('stringifying arg type not implemented')
41         return ' '.join(stringed_args)
42
43
44
45 class Task_WAIT(Task):
46
47     def do(self):
48         return 'success'
49
50
51
52 class Task_MOVE(Task):
53     argtypes = 'string:direction'
54
55     def check(self):
56         test_pos = self.thing.world.map_.move(self.thing.position, self.args[0])
57         if self.thing.world.map_[test_pos] != '.':
58             raise GameError('%s would move into illegal terrain' % self.thing.id_)
59         for t in self.thing.world.things:
60             if t.position == test_pos:
61                 raise GameError('%s would move into other thing' % self.thing.id_)
62
63     def do(self):
64         self.thing.position = self.thing.world.map_.move(self.thing.position,
65                                                          self.args[0])
66
67
68
69 task_manager = TaskManager((Task_WAIT, Task_MOVE))