Debian Codegraph @

python/dialog/pre/age.py

raw
1 # -*- coding: utf-8 -*-
2 #age.py
3 # This is one of the files that can be called by an npc_dialog,
4 # The following code runs when a dialog has a pre rule of 'age'
5 # The syntax is
6 # ["age", "agetoken", "years", "months", "days", "hours", "minutes"]
7 # To deliver a True verdict, the agetoken must correspond to a date
8 # that is was at least as long ago as the duration specified in the options.
9 ## DIALOGCHECK
10 ## MINARGS 6
11 ## MAXARGS 6
12 ## .*
13 ## \d
14 ## \d
15 ## \d
16 ## \d
17 ## \d
18 ## ENDDIALOGCHECK
19
20
21 class checkfailed(Exception):
22 pass
23
24 # maximum months, days, hours, as defined by the server.
25 # minutes per hour is hardcoded to approximately 60
26
27 try:
28 MAXTIMES = [Crossfire.Time.MONTHS_PER_YEAR, Crossfire.Time.WEEKS_PER_MONTH*Crossfire.Time.DAYS_PER_WEEK,
29 Crossfire.Time.HOURS_PER_DAY, 60]
30 # we have three times to consider, the old time, the current time, and the desired time difference.
31 if len(args) != 6:
32 raise checkfailed()
33 markername = args[0]
34 oldtime = self.getStatus(markername).split("-")
35 oldtime = map(int, oldtime)
36 if len(oldtime) !=5:
37 # The marker hasn't been set yet
38 raise checkfailed()
39
40 desireddiff = map(int, args[1:])
41 currenttime = (Crossfire.GetTime())[:5]
42 actualdiff = []
43
44 for i in range(5):
45 actualdiff.append(currenttime[i]-oldtime[i])
46
47 for i in range(4,0,-1):
48 # let's tidy up desireddiff first
49 if desireddiff[i] > MAXTIMES[i-1]:
50 desireddiff[i-1] += desireddiff[i] // MAXTIMES[i-1]
51 desireddiff[i] %= MAXTIMES[i-1]
52 # Then actualdiff
53 if actualdiff[i] < 0:
54 actualdiff[i] += MAXTIMES[i-1]
55 actualdiff[i-1] -=1
56 Crossfire.Log(Crossfire.LogDebug, "CFDialog: tidied up desired difference: %s actual difference %s" %(desireddiff, actualdiff))
57 for i in range(5):
58 if actualdiff[i] < desireddiff[i]:
59 raise checkfailed()
60 except checkfailed:
61 verdict = False