Edit an experience entry by double-clicking on the tree
[crapvine.git] / trait.py
blobb9bd6b463cbe3b5e72e59554ac79f0d085ff3d20
1 ## This file is part of Crapvine.
2 ##
3 ## Copyright (C) 2007 Andrew Sayman <lorien420@myrealbox.com>
4 ##
5 ## Crapvine 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 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## Crapvine 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 this program. If not, see <http://www.gnu.org/licenses/>.
18 import copy
19 import pdb
21 # PyGtk
22 import gobject
24 # Character Support
25 from grapevine_xml import Attributed, AttributedListModel
26 from attribute import AttributeBuilder
28 class TraitList(AttributedListModel):
29 required_attrs = ['name']
30 number_as_text_attrs = ['display']
31 bool_attrs = ['abc', 'atomic', 'negative']
32 defaults = { 'atomic' : False, 'negative' : False }
34 column_attrs = [ 'name', 'val', 'note' ]
35 column_attr_types = [ gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING ]
37 text_children = []
39 def __init__(self):
40 AttributedListModel.__init__(self)
41 self.list = []
42 self.traits = self.list
44 def add_menu_item(self, menu_item):
45 t = Trait()
46 t.name = copy.copy(menu_item.name)
47 t.note = copy.copy(menu_item.note)
48 t.val = copy.copy(menu_item.cost)
49 self.add_trait(t)
51 def add_trait(self, trait):
52 if self.atomic:
53 self.traits.append(trait)
54 path = (len(self.traits) - 1, )
55 self.row_inserted(path, self.get_iter(path))
56 else:
57 if trait.name in [t.name for t in self.traits]:
58 for en in enumerate(self.traits):
59 t = en[1]
60 if trait.name == t.name:
61 idx = en[0]
62 try:
63 t.val = str(float(t.val) + float(trait.val))
64 except ValueError:
65 t.val = '2'
66 if t.note == '':
67 t.note = trait.note
68 path = (idx, )
69 self.row_changed(path, self.get_iter(path))
70 else:
71 self.traits.append(trait)
72 path = (len(self.traits) - 1, )
73 self.row_inserted(path, self.get_iter(path))
75 def increment_trait(self, trait_name):
76 if trait_name in [t.name for t in self.traits]:
77 for en in enumerate(self.traits):
78 t = en[1]
79 if trait_name == t.name:
80 idx = en[0]
81 try:
82 t.val = str(float(t.val) + 1)
83 except ValueError:
84 t.val = '1'
85 path = (idx, )
86 self.row_changed(path, self.get_iter(path))
87 else:
88 raise 'Unknown trait'
90 def decrement_trait(self, trait_name):
91 if trait_name in [t.name for t in self.traits]:
92 for en in enumerate(self.traits):
93 t = en[1]
94 if trait_name == t.name:
95 idx = en[0]
96 if t.val == '1':
97 del self.traits[idx]
98 path = (idx, )
99 self.row_deleted(path)
100 else:
101 try:
102 t.val = str(float(t.val) - 1)
103 except ValueError:
104 t.val = '1'
105 path = (idx, )
106 self.row_changed(path, self.get_iter(path))
107 else:
108 raise 'Unknown trait'
110 def get_total_value(self):
111 sum = 0
112 for t in self.traits:
113 try:
114 sum += float(t.val)
115 except ValueError:
116 sum += 1
117 except TypeError, e:
118 print t.val.__class__
119 print t.val
120 raise e
121 return sum
122 def get_num_entries(self):
123 return len(self.traits)
125 def get_display_total(self):
126 if self.atomic:
127 return self.get_num_entries()
128 else:
129 return self.get_total_value()
131 def get_xml(self, indent=''):
132 end_tag = ">\n" if len(self.traits) > 0 else "/>"
133 ret = '%s<traitlist %s%s' % (indent, self.get_attrs_xml(), end_tag)
134 local_indent = '%s ' % (indent)
135 ret += "\n".join([trait.get_xml(local_indent) for trait in self.traits])
136 if len(self.traits) > 0:
137 ret += '%s%s</traitlist>' % ("\n", indent)
138 return ret
139 def __str__(self):
140 return self.get_xml()
142 class Trait(Attributed):
143 required_attrs = ['name']
144 text_attrs = ['note', 'cumguzzle']
145 number_as_text_attrs = ['val']
146 defaults = { 'val' : '1' }
148 def get_xml(self, indent=''):
149 return '%s<trait %s/>' % (indent, self.get_attrs_xml())
150 def __str__(self):
151 return self.get_xml()
153 def __val_as_float(self):
154 try:
155 return float(self.val)
156 except ValueError:
157 return 0
158 def __show_note(self):
159 return True if self.note else False
160 def __show_val(self):
161 return True if self.val else False
162 def __tally_val(self):
163 return True if self.__val_as_float else False
165 def tally_str(self, dot="O"):
166 if not self.__tally_val():
167 return ''
168 else:
169 num_dots = int(round(self.__val_as_float()))
170 ret = ""
171 for i in range(num_dots):
172 ret += "%s" % (dot)
173 return ret
174 def display_str(self, display="1", dot="O"):
175 show_note = self.__show_note()
176 show_val = self.__show_val()
177 tally_val = self.__tally_val()
179 if display == "0":
180 return "%s" % (self.name)
181 elif display == "1":
182 vstr = (" x%s" % (self.val)) if show_val else ''
183 nstr = (" (%s)" % (self.note)) if show_note else ''
184 return "%s%s%s" % (self.name, vstr, nstr)
185 elif display == "2":
186 if show_val:
187 vstr = (" x%s" % (self.val))
188 if tally_val:
189 vstr += " %s" % (self.tally_str(dot))
190 nstr = (" (%s)" % (self.note)) if show_note else ''
191 return "%s%s%s" % (self.name, vstr, nstr)
192 elif display == "3":
193 if tally_val:
194 vstr = " %s" % (self.tally_str(dot))
195 nstr = (" (%s)" % (self.note)) if show_note else ''
196 return "%s%s%s" % (self.name, vstr, nstr)
197 elif display == "4":
198 paren_str = ""
199 if show_note and show_val:
200 paren_str = " (%s, %s)" % (self.val, self.note)
201 elif show_note and not show_val:
202 paren_str = " (%s)" % (self.note)
203 elif show_val and not show_note:
204 paren_str = " (%s)" % (self.val)
205 return "%s%s" % (self.name, paren_str)
206 elif display == "5":
207 paren_str = ""
208 if show_note:
209 paren_str = " (%s)" % (self.note)
210 return "%s%s" % (self.name, paren_str)
211 elif display == "6":
212 paren_str = ""
213 if show_val:
214 paren_str = " (%s)" % (self.val)
215 return "%s%s" % (self.name, paren_str)
216 elif display == "7":
217 paren_str = (" (%s)" % (self.note)) if show_note else ''
218 dstr = "%s%s" % (self.name, paren_str)
219 its = []
220 for i in range(int(round(self.__val_as_float()))):
221 its.append(dstr)
222 return ("%s" % (dot)).join(its)
223 elif display == "8":
224 return self.tally_str(dot)
225 elif display == "9":
226 if show_val:
227 return "%s" % (self.val)
228 else:
229 return ''
230 elif display == "10":
231 if show_note:
232 return "%s" % (self.note)
233 else:
234 return ''
235 elif display == "Default":
236 return self.display_str()