Documentation Update. Part 2.
[asgard.git] / transaction.py
blobafbb67c08fd0d54d12ac82ced89c7c913c3ed4fc
1 ########################################################
2 #Copyright (c) 2006 Russ Adams, Sean Eubanks, Asgard Contributors
3 #This file is part of Asgard.
5 #Asgard is free software; you can redistribute it and/or modify
6 #it under the terms of the GNU General Public License as published by
7 #the Free Software Foundation; either version 2 of the License, or
8 #(at your option) any later version.
10 #Asgard is distributed in the hope that it will be useful,
11 #but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 #GNU General Public License for more details.
15 #You should have received a copy of the GNU General Public License
16 #along with Asgard; if not, write to the Free Software
17 #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 ########################################################
19 from status import *
21 class Transaction:
23 def __init__(self,target):
24 self.target = target
26 def execute(self):
27 pass
29 def getStatus(self):
30 pass
32 def setStatus(self,status):
33 pass
35 def getTarget(self):
36 return self.target
38 def setTarget(self,target):
39 self.target = target
41 class TransactionStatistic(Transaction):
42 def __init__(self,target,stat,change):
43 Transaction.__init__(self,target)
44 self.__stat = stat
45 self.__change = change
47 def execute(self):
48 """ execute the transaction; based on provided information """
50 # If a Stat is supposed to be modified
51 if self.__stat != None:
52 alive_before_transaction = self.target.isAlive()
54 # Retrieve that Stat from the target
55 stat_to_change = self.target.getStat(self.__stat)
57 # change the value of that stat by the amount in "change"
58 stat_to_change.setCurrent(stat_to_change.getCurrent() + self.__change)
60 if self.target.getStat("hp").getCurrent() <= 0 and alive_before_transaction:
61 deathT = TransactionStatus(self.target,"death",True)
62 deathT.execute()
63 elif self.target.getStat("hp").getCurrent() > self.target.getStat("hp").getMax():
64 self.target.getStat("hp").setCurrent(self.target.getStat("hp").getMax())
66 def getStat(self):
67 return self.__stat
69 def setStat(self,stat):
70 self.__stat = stat
72 def getChange(self):
73 return self.__change
75 def setChange(self,change):
76 self.__change = change
78 class TransactionStatus(Transaction):
79 def __init__(self,target,status,add):
80 Transaction.__init__(self,target)
81 self.__status = status
82 self.__add = add
84 def execute(self):
85 """ execute the status transaction; based on provided information """
87 tlistPerTurn = []
88 tlistPerIter = []
89 tlistExit = []
90 tlistEntrance =[]
91 tlistPerTurnOnMove = []
93 if not self.__add:
94 self.target.removeStatus(self.__status)
95 else:
96 # poison status
97 if self.__status == "poison":
98 # remove 5% of hp per turn; this is lame and should be more dynamic
99 trans = TransactionStatistic(self.target,"hp",int(-1 * self.target.getStat("hp").getMax()*.05))
100 tlistPerTurnOnMove.append(trans)
101 elif self.__status == "aglup":
102 trans = TransactionStatistic(self.target,"agl",10)
103 tlistEntrance.append(trans)
104 trans = TransactionStatistic(self.target,"agl",-10)
105 tlistExit.append(trans)
106 elif self.__status == "sleep":
107 tlistEntrance.append(TransactionStatistic(self.target,"canMove",-1))
108 tlistExit.append(TransactionStatistic(self.target,"canMove",1))
109 elif self.__status == "death":
110 tlistEntrance.append(TransactionStatistic(self.target,"canMove",-1))
111 tlistEntrance.append(TransactionStatistic(self.target,"hp",self.target.getStat("hp").getCurrent()*-1))
112 tlistExit.append(TransactionStatistic(self.target,"canMove",1))
113 tlistExit.append(TransactionStatistic(self.target,"hp",int(self.target.getStat("hp").getMax() * .25)))
114 elif self.__status == "paralysis":
115 tlistEntrance.append(TransactionStatistic(self.target,"canMove",-1))
116 tlistExit.append(TransactionStatistic(self.target,"canMove",1))
117 elif self.__status == "madness":
118 ai = self.target.getStat("ai").getCurrent()
119 if ai == 0:
120 tlistEntrance.append(TransactionStatistic(self.target,"ai",2))
121 tlistExit.append(TransactionStatistic(self.target,"ai",-2))
122 elif ai == 1:
123 tlistEntrance.append(TransactionStatistic(self.target,"ai",1))
124 tlistExit.append(TransactionStatistic(self.target,"ai",-1))
126 # create the status object and add it to the target
127 self.target.addStatus(Status(self.__status,tlistEntrance,tlistPerTurn,tlistPerIter,tlistExit,tlistPerTurnOnMove))
130 def getStatus(self):
131 return self.__status
133 def setStatus(self,status):
134 self.__status = status
136 def getAdd(self):
137 return self.__add
139 def setAdd(self, add):
140 self.__add = add