-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.py
123 lines (123 loc) · 3.9 KB
/
ops.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
from database import *
import datetime
__all__ = ['TagException','CheckInException','CureException','add_kill','do_checkin','do_cure','EXC_NOTHUMAN','EXC_NOTZOMBIE','EXC_KITHUMAN','EXC_KITZOMBIE','EXC_NOTSTATION','EXC_NOSUCHZOMBIE','EXC_NOSUCHHUMAN','EXC_CHEATER','CHECKIN_TOOSOON','CURE_DISQUALIFIED','CURE_ALREADYUSED']
EXC_NOTHUMAN = 'player is not a human'
EXC_NOTZOMBIE= 'player is not a zombie'
EXC_KITHUMAN = 'human has no kit'
EXC_KITZOMBIE= 'zombie has no kit'
EXC_NOTSTATION='non-station attempting to perform station operations'
EXC_NOSUCHZOMBIE='no such zombie exists'
EXC_NOSUCHHUMAN='no such human exists'
EXC_CHEATER='duplicate uid, we may have a cheater'
CHECKIN_TOOSOON = 'player checked in too soon'
CURE_DISQUALIFIED = 'cure card is disqualified'
CURE_ALREADYUSED = 'cure card already used'
CURE_EXPIRED = 'cure card is expired'
class TagException(Exception):
pass
class CheckInException(Exception):
pass
class CureException(Exception):
pass
def add_kill(tagger, taggee, uid, override=False):
"""
Parameters:
tagger: Player inst, game_id, email, student num, twitter, or cell number
taggee: game_id
uid: uniquely identifying string. can be the Message-ID from email, tweet id, uuid, etc.
Returns:
Tag instance
Throws:
TagException
"""
v = uid
if isinstance(tagger, str) and '@' in tagger:
try:
i = int(tagger[:tagger.find('@')])
if len(str(i)) >= 10:
v = 'cell_' + uid
except:
pass
# print 'passed: ' + str(taggee)
tagger = Player.get_player(tagger)
if not override:
taggee = Player.from_game_id(taggee)
# print 'found: ' + str(taggee)
if override:
if not isinstance(taggee, Player):
taggee = Player.get_player(taggee)
# print taggee
if not tagger:
raise TagException(EXC_NOSUCHZOMBIE)
if not taggee:
raise TagException(EXC_NOSUCHHUMAN)
if Tag.has_uid(uid):
raise TagException(EXC_CHEATER)
if not tagger.is_zombie():
tagger.kill()
if not taggee.is_human() and not override:
raise TagException(EXC_NOTHUMAN)
if not tagger.signedin and not override:
raise TagException(EXC_KITZOMBIE)
if not taggee.signedin and not override:
raise TagException(EXC_KITHUMAN)
taggee.kill()
t = Tag(tagger=tagger,taggee=taggee,uid=v)
# update scorecard
Score.get_scorecard(tagger).kills = tagger.kills.count()
return t
def do_checkin(player, station):
"""
Parameters:
player: Player inst, game_id, email, student num, twitter, or cell number
station: Station inst
Returns:
Checkin instance
Throws:
CheckinException
"""
player = Player.get_player(player)
if not player:
raise CheckInException(EXC_NOSUCHHUMAN)
if not (isinstance(station, Station) or isinstance(station, Admin)):
raise CheckInException(EXC_NOTSTATION)
if player.last_checkin_time:
if datetime.datetime.now() < player.last_checkin_time + datetime.timedelta(0,0,0,0,0,Game.hours_between_checkins):
raise CheckInException(CHECKIN_TOOSOON)
if not player.is_human():
raise CheckInException(EXC_NOTHUMAN)
if not player.signedin:
raise CheckInException(EXC_KITHUMAN)
return Checkin(location=station.location,player=player)
def do_cure(player, station, cure):
"""
Parameters:
player: Player inst, game_id, email, student num, twitter, or cell number
station: station inst
cure: Cure inst or id
Returns:
Whether the cure succeeded
Throws:
CureException
"""
player = Player.get_player(player)
if not player:
raise CheckInException(EXC_NOSUCHZOMBIE)
if not (isinstance(station, Station) or isinstance(station, Admin)):
raise CureException(EXC_NOTSTATION)
if not player.is_zombie():
raise CureException(EXC_NOTZOMBIE)
if not player.signedin:
raise CureException(EXC_KITZOMBIE)
cure = Cure.get_cure(cure)
if cure.disqualified:
raise CureException(CURE_DISQUALIFIED)
if cure.used:
raise CureException(CURE_ALREADYUSED)
if cure.expiry < datetime.datetime.now():
raise CureException(CURE_EXPIRED)
cure.time = datetime.datetime.now()
cure.used = True
cure.player = player
player.cure()
return True