-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomizer.py
180 lines (153 loc) · 5.18 KB
/
randomizer.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
import csv
import io
import random
from views.main_view import MainView
from model.background_traits import BackgroundTraits
from utils.data_file_loader import DataFileLoader as DFL
class Randomizer(object):
def __init__(self):
self.classes = []
self.races = []
self.bg_traits = None
self.sources = {}
self.specializations = {}
self.cur_race = None
self.cur_race_src = None
self.cur_race_src_short = None
self.cur_class = None
self.cur_spec_name = None
self.cur_spec = None
self.cur_class_src = None
self.cur_class_src_short = None
self.cur_bg = None
self.cur_traits = ''
self.cur_bg_src_short = None
self.load_data()
def load_data(self):
self.classes = DFL().load_csv('classes')
self.races = DFL().load_csv('races')
self.bg_traits = BackgroundTraits('background_traits')
temp_sources = DFL().load_csv('sources')
temp_specs = DFL().load_csv('class_specializations')
for tsrc in temp_sources:
self.sources[tsrc[0]] = tsrc[1]
for tspec in temp_specs:
self.specializations[tspec[0]] = tspec[1:]
def _get_filtered_list(self, full_list, options):
if options is None:
return full_list
filtered_set = []
for item in full_list:
for opt in options:
if opt in item[-1]:
filtered_set += [item]
break
return filtered_set
def _get_short_source_text(self, srctext):
src_texts = []
sources = srctext.split('/')
for rsrc in sources:
additional = False
if rsrc.endswith('+'):
rsrc = rsrc[:-1]
additional = True
if additional:
rsrc = "%s [addl. vars.]" % rsrc
src_texts += [rsrc]
return ', '.join(src_texts)
def _get_source_text(self, srctext):
src_texts = []
sources = srctext.split('/')
for rsrc in sources:
additional = False
if rsrc.endswith('+'):
rsrc = rsrc[:-1]
additional = True
if rsrc.startswith('UA'):
rsrc = 'UA'
full_src = self.sources[rsrc]
if additional:
full_src = "%s [additional variations]" % full_src
src_texts += [full_src]
return '(Source: %s)' % ', '.join(src_texts)
def _shuffle_race(self, even=False, skip_race=False, sources=None):
rs = self._get_filtered_list(self.races, sources)
if skip_race or rs == []:
return
if even:
races = []
for r in rs:
if r[0] not in races:
races += [r[0]]
race = random.choice(races)
single_race = []
for r in rs:
if r[0] == race:
single_race += [r]
_, variation, identity, srcs = random.choice(single_race)
else:
race, variation, identity, srcs = random.choice(rs)
race_text = race
if variation != '':
race_text = "%s %s" % (variation, race_text)
if identity != '':
race_text = "%s (%s)" % (race_text, identity)
self.cur_race = race_text
self.cur_race_src = self._get_source_text(srcs)
self.cur_race_src_short = self._get_short_source_text(srcs)
def _shuffle_class(self, even=False, skip_class=False, skip_spec=False, sources=None):
cs = self._get_filtered_list(self.classes, sources)
if (skip_class and skip_spec) or cs == []:
return
if even or skip_class:
if skip_class:
class_name = self.cur_class
else:
classes = []
for c in cs:
if c[0] not in classes:
classes += [c[0]]
class_name = random.choice(classes)
single_class = []
for c in cs:
if c[0] == class_name:
single_class += [c]
_, specification, exception, srcs = random.choice(single_class)
else:
class_name, specification, exception, srcs = random.choice(cs)
spec_name, formatting = self.specializations[class_name]
spec_text = ''
if exception != '':
spec_text = exception
else:
spec_text = str.format(formatting, specification)
self.cur_class = class_name
self.cur_spec = spec_text
self.cur_spec_name = spec_name
self.cur_class_src = self._get_source_text(srcs)
self.cur_class_src_short = self._get_short_source_text(srcs)
def _shuffle_bg(self, skip_bgs=False, sources=None):
bg = self.bg_traits.get_random_bg(sources)
if skip_bgs or bg is None:
return
self.cur_bg = bg.name
self.cur_bg_src_short = bg.source
self.cur_traits = bg.get_traits_as_str()
def pick(self, even=False, skip_race=False, skip_class=False, skip_spec=False, skip_bg=False, sources=None):
self._shuffle_race(even, skip_race, sources)
self._shuffle_class(even, skip_class, skip_spec, sources)
self._shuffle_bg(skip_bg, sources)
# self.print_race_class_with_short_sources()
def print_race_class(self):
print(self.cur_race)
print(self.cur_class)
def print_race_class_with_sources(self):
print('%s | %s' % (self.cur_race, self.cur_race_src))
print('%s | %s' % (self.cur_class, self.cur_class_src))
def print_race_class_with_short_sources(self):
print('%s | %s' % (self.cur_race, self.cur_race_src_short))
print('%s | %s' % (self.cur_class, self.cur_class_src_short))
if __name__ == '__main__':
rnd = Randomizer()
v = MainView.load_view(rnd)
v.present(style='fullscreen')