-
Notifications
You must be signed in to change notification settings - Fork 0
/
blaseball_stat_csv.py
243 lines (228 loc) · 8.13 KB
/
blaseball_stat_csv.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import os
import argparse
import operator
from blaseball_mike.models import League, SimulationData, Player
from blaseball_mike.tables import StatType
INVERSE_STLATS = [
"tragicness",
"patheticism",
] # These stlats are better for the target the smaller they are
COLUMNS = [
"team",
"league",
"division",
"name",
"position",
"turnOrder",
"id",
"anticapitalism",
"baseThirst",
"buoyancy",
"chasiness",
"coldness",
"continuation",
"divinity",
"groundFriction",
"indulgence",
"laserlikeness",
"martyrdom",
"moxie",
"musclitude",
"bat",
"omniscience",
"overpowerment",
"patheticism",
"ruthlessness",
"shakespearianism",
"suppression",
"tenaciousness",
"thwackability",
"tragicness",
"unthwackability",
"watchfulness",
"pressurization",
"totalFingers",
"soul",
"deceased",
"peanutAllergy",
"cinnamon",
"fate",
"armor",
"ritual",
"blood",
"coffee",
"permAttr",
"seasAttr",
"weekAttr",
"gameAttr",
"battingStars",
"pitchingStars",
"baserunningStars",
"defenseStars",
]
def handle_player_adjustments(player, adjustments):
for adjustment in adjustments:
if adjustment["type"] == 1:
stlat_name = StatType(adjustment["stat"]).stat_name
# TODO remove when fixed in blaseball-mike
if stlat_name == "shakesperianism":
stlat_name = "shakespearianism"
###
if stlat_name in INVERSE_STLATS:
setattr(
player,
stlat_name,
min(
max(getattr(player, stlat_name) + adjustment["value"], 0.001),
0.999,
),
)
else:
setattr(
player,
stlat_name,
max(getattr(player, stlat_name) + adjustment["value"], 0.001),
)
return player
def adjust_stlats_for_items(player):
items = player.items
player_copy = Player(player.json())
for item in items:
if item.health:
player_copy = handle_player_adjustments(
player_copy, item.root["adjustments"]
)
for section in ("root", "pre_prefix", "post_prefix", "suffix"):
if getattr(item, section):
player_copy = handle_player_adjustments(
player_copy, getattr(item, section)["adjustments"]
)
if item.prefixes:
for prefix in item.prefixes:
player_copy = handle_player_adjustments(
player_copy, prefix["adjustments"]
)
for category in ("defense", "hitting", "pitching", "baserunning"):
setattr(
player_copy,
"_{}_rating".format(category),
max(
(getattr(player_copy, "_{}_rating".format(category)) or 0.001)
+ (getattr(item, "{}_rating".format(category)) or 0.001),
0.001,
),
)
return player_copy
def generate_file(filename, inactive, archive, include_items, unscattered):
sim = SimulationData.load()
if archive and os.path.isfile(filename):
os.rename(
filename,
filename.replace(".csv", "S{}preD{}.csv".format(sim.season, sim.day + 1)),
)
output = []
positions = (
("lineup", "rotation", "shadows") if inactive else ("lineup", "rotation")
)
league = League.load()
players = Player.load_all()
for subleague in league.subleagues.values():
for division in subleague.divisions.values():
for team in division.teams.values():
for position in positions:
for turn_order, player_id in enumerate(
getattr(team, "_{}_ids".format(position))
):
player = players[player_id]
if include_items:
player = adjust_stlats_for_items(player)
player_row = [
team.full_name,
subleague.name,
division.name,
player.name
if not unscattered or "unscatteredName" not in player.state
else player.state["unscatteredName"],
position,
turn_order + 1,
player.id,
player.anticapitalism,
player.base_thirst,
player.buoyancy,
player.chasiness,
player.coldness,
player.continuation,
player.divinity,
player.ground_friction,
player.indulgence,
player.laserlikeness,
player.martyrdom,
player.moxie,
player.musclitude,
player.bat.id or "",
player.omniscience,
player.overpowerment,
player.patheticism,
player.ruthlessness,
player.shakespearianism,
player.suppression,
player.tenaciousness,
player.thwackability,
player.tragicness,
player.unthwackability,
player.watchfulness,
player.pressurization,
player.total_fingers,
player.soul,
player.deceased,
player.peanut_allergy,
player.cinnamon,
player.fate,
player.armor.id or "",
player.ritual,
player._blood_id,
player._coffee_id,
";".join(
attr.id for attr in player.perm_attr + player.item_attr
),
";".join(attr.id for attr in player.seas_attr),
";".join(attr.id for attr in player.week_attr),
";".join(attr.id for attr in player.game_attr),
player.batting_rating * 5.0,
player.pitching_rating * 5.0,
player.baserunning_rating * 5.0,
player.defense_rating * 5.0,
]
output.append(player_row)
output.sort(key=operator.itemgetter(0, 4, 5))
with open(filename, "w") as f:
f.write("{}\n".format(",".join('"{}"'.format(col) for col in COLUMNS)))
f.write(
"\n".join(
",".join(['"{}"'.format(d) for d in datarow]) for datarow in output
)
)
def handle_args():
parser = argparse.ArgumentParser()
parser.add_argument("--output", default="output.csv", help="output filepath")
parser.add_argument(
"--inactive",
help="include inactive players(bench/bullpen)",
action="store_true",
)
parser.add_argument(
"--archive", help="backup existing file before generating", action="store_true"
)
parser.add_argument(
"--items", help="include item adjustments in stlats", action="store_true"
)
parser.add_argument("--unscattered", help="unscatter names", action="store_true")
args = parser.parse_args()
return args
def main():
args = handle_args()
generate_file(
args.output, args.inactive, args.archive, args.items, args.unscattered
)
if __name__ == "__main__":
main()