-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.py
187 lines (158 loc) · 5.6 KB
/
requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'''
TODO: rewrite ALL
'''
import database as db
import logging
import commands as cmd
import logic
from collections import namedtuple
# request functions to perform an action
requests = {}
def req(func):
requests[func.__name__.upper()] = func
return func
def create_request(**kw):
#TODO: Make it impossible to change loc_id after assignment
logic.loc_containers[kw['loc_id']].requests.append(kw)
########### SYSTEM REQUESTS: ###########
def ENTER(player, loc_id, x_y = (None,None)):
''' Places character at designated location or if not specified: any free safe location '''
x,y = x_y
if not player.joined:
raise Exception("Not joined trying to enter")
player.loc_id = loc_id
@logic.locking(loc_id = loc_id)
def _():
logging.debug("trying to enter")
with db.Handler() as h:
if x == None or y == None:
targ_cell = None
else:
raise NotImplementedError
create_request(loc_id = loc_id,
type = 'enter',
source = player.creature,
target_cell = targ_cell,
duration = 50)
logging.debug("Checking update")
player.committed = True
logic.check_update(player)
def EXIT(player):
''' removes creature from location '''
# TODO:
########### REQUESTS: ###########
@req
def MOVE(player, where):
''' Moves character in direction '''
if not player.in_game:
return
with db.Handler(True) as h:
@logic.locking("move", loc_id = player.loc_id)
def move(lock):
cre = player.creature
h.session.add(cre)
if not cre.cell:
player.send(cmd.error("You have not yet entered the game"))
return
if not where:
player.send(cmd.error("No direction given for 'move'"))
return
if not set(where.lower()) <= set('nswe') or len(where) > 2:
player.send(cmd.error("unknown direction given: "+where))
return
create_request(loc_id = player.loc_id,
type = "move",
source = player.creature.id,
where = where)
@req
def COMMIT(player):
if not player.in_game:
return
@locking("commit", toThread=False, loc_id=player.loc_id)
def upd(lock):
player.committed = True
check_update(player)
@req
def CANCEL(player, n = None):
''' cancells n actions '''
# TODO:
@req
def ATTACK(player, target):
if not player.in_game:
return
player.send(cmd.server_error("not yet implemented"))
@locking("attack", loc_id = player.loc_id)
def attack(lock):
with db.Handler(True) as h:
cre = player.creature
h.session.add(cre)
if not cre.cell:
player.send(cmd.error("You have not yet entered the game"))
return
target_c = cre.location.strid_objects[target]
create_request(loc_id = player.loc_id,
type = "attack",
source = player.creature,
target = target_c,
cost = 19)
@req
def PICK(player, target):
item = int(target)
# TODO: check if target in same location as player and is visible
if not player.in_game:
return
@locking("pick", loc_id = player.loc_id)
def pick(lock):
with db.Handler(True) as h:
l = h.get_location(player.loc_id)
player.creature = h.merge(player.creature)
items = dict(l.id_items)
items.update({i.id:i for i in player.creature.inventory})
if item not in items:
player.send(cmd.error("No item with ID:"+str(item)))
return
create_request(loc_id = player.loc_id,
type = "pick",
source = player.creature,
target = items[item],
cost = 12)
@req
def DROP(player, target):
item = int(target)
# TODO: check if target in same location as player and is visible
if not player.in_game:
return
@locking("drop", loc_id = player.loc_id)
def drop(lock):
with db.Handler() as h:
h.add(player.creature)
l = h.get_location(player.loc_id)
items = dict(l.id_items)
items.update({i.id:i for i in player.creature.inventory})
if item not in items:
player.send(cmd.error("No item with ID:"+str(item)))
return
create_request(loc_id = player.loc_id,
type = "drop",
source = player.creature,
target = items[item],
cost = 10)
@req
def EQUIP(player, target):
item = int(target)
if not player.in_game:
return
@locking("equip", loc_id = player.loc_id)
def equip(lock):
with db.Handler() as h:
h.add(player.creature)
l = h.get_location(player.loc_id)
items = dict(l.id_items)
items.update({i.id:i for i in player.creature.inventory})
if item and item not in items:
player.send(cmd.error("No item with ID:"+str(item)))
return
create_request(loc_id = player.loc_id,
type = "equip",
source = player.creature.id,
target = item)