-
Notifications
You must be signed in to change notification settings - Fork 9
/
data.py
1146 lines (1031 loc) · 34.6 KB
/
data.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import collections
from dataclasses import dataclass, field
import os
import json
import requests
from dataclasses_json import DataClassJsonMixin, config
from typing import Any, List, Dict, Iterable, Mapping, Optional, Set, Union, ClassVar
from datetime import datetime, timedelta
from enum import Enum, IntEnum, auto, unique
from sin_values import SIN_PHASES
EXCLUDE_FROM_CACHE = {
"team": {"runs", "wins", "eDensity"},
"player": {"consecutiveHits", "eDensity"},
"stadium": {"hype"},
"item": {"health"},
}
stat_indices = [
"tragicness",
"buoyancy",
"thwackability",
"moxie",
"divinity",
"musclitude",
"patheticism",
"martyrdom",
"cinnamon",
"baseThirst",
"laserlikeness",
"continuation",
"indulgence",
"groundFriction",
"shakespearianism",
"suppression",
"unthwackability",
"coldness",
"overpowerment",
"ruthlessness",
"pressurization",
"omniscience",
"tenaciousness",
"watchfulness",
"anticapitalism",
"chasiness",
]
def offset_timestamp(timestamp: str, delta_secs: float) -> str:
timestamp = timestamp.replace("Z", "+00:00")
dt = datetime.fromisoformat(timestamp)
dt = dt + timedelta(seconds=delta_secs)
timestamp = dt.isoformat()
return timestamp.replace("+00:00", "Z")
def get_cached(key, url):
key = key.replace(":", "_")
path = os.path.join("cache", key + ".json")
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
if os.path.exists(path):
with open(path, "r", encoding="utf-8") as f:
try:
data = json.load(f)
return data
except json.JSONDecodeError:
pass
data = requests.get(url).json()
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f)
return data
@unique
class Mod(Enum):
"""
Modifications which are being used within Resim.
Not an exhaustive list! Add here when necessary.
"""
def _generate_next_value_(name, start, count, last_values):
"""Ensure Mod values match names"""
return name
ZERO = "0"
AA = auto()
AAA = auto()
ACIDIC = auto()
AFFINITY_FOR_CROWS = auto()
AFTER_PARTY = auto()
AIR_BALLOONS = auto()
ATTRACTOR = auto()
BASE_INSTINCTS = auto()
BIG_BUCKET = auto()
BIRD_SEED = auto()
CAREFUL = auto()
CHUNKY = auto()
COFFEE_PERIL = auto()
DEBT_THREE = auto()
ECHO = auto()
ECHO_CHAMBER = auto()
EGO1 = auto()
EGO2 = auto()
EGO3 = auto()
EGO4 = auto()
ELECTRIC = auto()
ELSEWHERE = auto()
EXTRA_BASE = auto()
FIERY = auto()
FIRE_EATER = auto()
FIREPROOF = auto()
FLINCH = auto()
FLOOD_PUMPS = auto()
FRIEND_OF_CROWS = auto()
FORCE = auto()
GAUDY = auto()
GRAVITY = auto()
GRIND_RAIL = auto()
GROWTH = auto()
GUARDED = auto()
H20 = auto()
HAUNTED = auto()
HIGH_PRESSURE = auto()
HONEY_ROASTED = auto()
HOOPS = auto()
HOTEL_MOTEL = auto()
INHABITING = auto()
LATE_TO_PARTY = auto()
LEGENDARY = auto()
LOVE = auto()
MARKED = auto()
MAXIMALIST = auto()
MINIMALIST = auto()
NEGATIVE = auto()
O_NO = auto()
ON_FIRE = auto()
OUTDOORSY = auto()
OVERPERFORMING = auto()
PARASITE = auto()
PARTY_TIME = auto()
PEANUT_MISTER = auto()
PERK = auto()
PSYCHIC = auto()
PSYCHOACOUSTICS = auto()
REDACTED = auto()
REVERBERATING = auto()
SALMON_CANNONS = auto()
SCATTERED = auto()
SECRET_BASE = auto()
SEEKER = auto()
SHELLED = auto()
SINKING_SHIP = auto()
SIPHON = auto()
SLOW_BUILD = auto()
SMITHY = auto()
SMOOTH = auto()
SWEETENER = auto()
SWIM_BLADDER = auto()
TRAVELING = auto()
TRIPLE_THREAT = auto()
UNDERPERFORMING = auto()
UNDERTAKER = auto()
WILD = auto()
@classmethod
def coerce(cls, value):
return cls(value) if value in cls.__members__ else None
def __str__(self):
return self.value
def get_feed_between(start, end):
key = f"feed_range_{start}_{end}"
resp = get_cached(
key, f"https://api.sibr.dev/eventually/v2/events?after={start}&before={end}&sortorder=asc&limit=200000"
)
return resp
def get_game_feed(game_id):
key = f"feed_game_{game_id}"
resp = get_cached(key, f"https://api.sibr.dev/eventually/v2/events?gameTags={game_id}&sortorder=asc&limit=1000")
return resp
def cacheable(data: Dict[str, Any], object_type: str) -> Dict[str, Any]:
return {k: v for k, v in data.items() if k not in EXCLUDE_FROM_CACHE[object_type]}
@unique
class EventType(IntEnum):
# Can't use -1 because the Feed uses it for
# "Undefined type; used in the Library for Leaders
# and Postseason series matchups (plus currently-redacted messages)"
NOT_YET_HANDLED_IN_ENUM = -99999
# Pulled from https://www.blaseball.wiki/w/SIBR:Feed#Event_types
# and https://github.com/beiju/blarser/blob/main/blarser/src/api/eventually_schema.rs#L143
UNDEFINED_TYPE = -1
LETS_GO = 0
PLAY_BALL = 1
HALF_INNING = 2
PITCHER_CHANGE = 3
STOLEN_BASE = 4
WALK = 5
STRIKEOUT = 6
FLY_OUT = 7
GROUND_OUT = 8
HOME_RUN = 9
HIT = 10
GAME_END = 11
BATTER_UP = 12
STRIKE = 13
BALL = 14
FOUL_BALL = 15
SHAMING_RUN = 20
HOME_FIELD_ADVANTAGE = 21
HIT_BY_PITCH = 22
BATTER_SKIPPED = 23
PARTY = 24
STRIKE_ZAPPED = 25
WEATHER_CHANGE = 26
MILD_PITCH = 27
INNING_END = 28
BIG_DEAL = 29
BLACK_HOLE = 30
SUN2 = 31
BIRDS_CIRCLE = 33
FRIEND_OF_CROWS = 34
BIRDS_UNSHELL = 35
BECOME_TRIPLE_THREAT = 36
GAIN_FREE_REFILL = 37
COFFEE_BEAN = 39
FEEDBACK_BLOCKED = 40
FEEDBACK_SWAP = 41
SUPERALLERGIC_REACTION = 45
ALLERGIC_REACTION = 47
REVERB_BESTOWS_REVERBERATING = 48
REVERB_ROSTER_SHUFFLE = 49
BLOODDRAIN = 51
BLOODDRAIN_SIPHON = 52
BLOODDRAIN_BLOCKED = 53
INCINERATION = 54
INCINERATION_BLOCKED = 55
FLAG_PLANTED = 56
RENOVATION_BUILT = 57
LIGHT_SWITCH_TOGGLED = 58
DECREE_PASSED = 59
BLESSING_OR_GIFT_WON = 60
WILL_RECIEVED = 61
FLOODING_SWEPT = 62
SALMON_SWIM = 63
POLARITY_SHIFT = 64
ENTER_SECRET_BASE = 65
EXIT_SECRET_BASE = 66
CONSUMERS_ATTACK = 67
ECHO_CHAMBER = 69
GRIND_RAIL = 70
TUNNELS_USED = 71
PEANUT_MISTER = 72
PEANUT_FLAVOR_TEXT = 73
TASTE_THE_INFINITE = 74
EVENT_HORIZON_ACTIVATION = 76
EVENT_HORIZON_AWAITS = 77
SOLAR_PANELS_AWAIT = 78
SOLAR_PANELS_ACTIVATION = 79
TAROT_READING = 81
EMERGENCY_ALERT = 82
RETURN_FROM_ELSEWHERE = 84
OVER_UNDER = 85
UNDER_OVER = 86
UNDERSEA = 88
HOMEBODY = 91
SUPERYUMMY = 92
PERK = 93
EARLBIRD = 96
LATE_TO_THE_PARTY = 97
SHAME_DONOR = 99
ADDED_MOD = 106
REMOVED_MOD = 107
MOD_EXPIRES = 108
PLAYER_ADDED_TO_TEAM = 109
PLAYER_REPLACED_BY_NECROMANCY = 110
PLAYER_REPLACES_RETURNED = 111
PLAYER_REMOVED_FROM_TEAM = 112
PLAYER_TRADED = 113
PLAYER_SWAP = 114
PLAYER_MOVE = 115
PLAYER_BORN_FROM_INCINERATION = 116
PLAYER_STAT_INCREASE = 117
PLAYER_STAT_DECREASE = 118
PLAYER_STAT_REROLL = 119
PLAYER_STAT_DECREASE_FROM_SUPERALLERGIC = 122
PLAYER_MOVE_FAILED_FORCE = 124
ENTER_HALL_OF_FLAME = 125
EXIT_HALL_OF_FLAME = 126
PLAYER_GAINED_ITEM = 127
PLAYER_LOST_ITEM = 128
REVERB_FULL_SHUFFLE = 130
REVERB_LINEUP_SHUFFLE = 131
REVERB_ROTATION_SHUFFLE = 132
TEAM_JOINED_LEAGUE = 135
EXISTING_PLAYER_ADDED_TO_ILB = 136
PLAYER_HATCHED = 137
WON_INTERNET_SERIES = 141
POSTSEASON_SPOT = 142
FINAL_STANDINGS = 143
MODIFICATION_CHANGE = 144
ADDED_MOD_FROM_OTHER_MOD = 146
REMOVED_MODIFICATION = 147
CHANGED_MODIFIER = 148
TEAM_WAS_SHAMED = 154
TEAM_DID_SHAME = 155
SUN_2_OUTCOME = 156
BLACK_HOLE_OUTCOME = 157
ELIMINATED_FROM_POSTSEASON = 158
POSTSEASON_ADVANCE = 159
HIGH_PRESSURE_ON_OFF = 165
LOVERS_LINEUP_OPTIMIZED = 166
ECHO_MESSAGE = 169
ECHO_INTO_STATIC = 170
REMOVED_MULTIPLE_MODIFICATIONS_ECHO = 171
ADDED_MULTIPLE_MODIFICATIONS_ECHO = 172
PSYCHO_ACOUSTICS = 173
RECEIVER_BECOMES_ECHO = 174
INVESTIGATION_PROGRESS = 175
GLITTER_CRATE_DROP = 177
MIDDLING = 178
PLAYER_HIDDEN_STAT_INCREASE = 179
PLAYER_HIDDEN_STAT_DECREASE = 180
ENTERING_CRIMESCENE = 181
AMBITIOUS = 182
UNAMBITIOUS = 183
COASTING = 184
ITEM_BREAKS = 185
ITEM_DAMAGE = 186
BROKEN_ITEM_REPAIRED = 187
DAMAGED_ITEM_REPAIRED = 188
COMMUNITY_CHEST_GAME_EVENT = 189
FAX_MACHINE_ACTIVATION = 191
HOLIDAY_INNING = 192
PRIZE_MATCH = 193
TEAM_RECEIVED_GIFT = 194
SMITHY_ACTIVATION = 195
A_BLOOD_TYPE = 198
PLAYER_SOUL_INCREASED = 199
BALLPARK_MOD_RATIFIED = 203
HYPE_BUILT = 206
PRACTICING_MODERATION = 208
RUNS_SCORED = 209
BALLOONS_INFLATED = 213
WIN_COLLECTED_REGULAR = 214
WIN_COLLECTED_POSTSEASON = 215
GAME_OVER = 216
SUN_SUN_PRESSURE = 217
TUNNEL_FOUND_NOTHING = 218
TUNNEL_FLED_ELSEWHERE = 219
TUNNEL_STOLE_ITEM = 220
WEATHER_EVENT = 223
STORM_WARNING = 263
SNOWFLAKES = 264
# Ensure that not-yet-handled values warn us,
# then default to a safe value
@classmethod
def _missing_(cls, value):
print(f"!!! unknown type: {value}")
return cls.NOT_YET_HANDLED_IN_ENUM
@unique
class Weather(IntEnum):
VOID = 0
SUN_2 = 1
OVERCAST = 2
RAINY = 3
SANDSTORM = 4
SNOWY = 5
ACIDIC = 6
ECLIPSE = 7
GLITTER = 8
BLOODDRAIN = 9
PEANUTS = 10
BIRDS = 11
FEEDBACK = 12
REVERB = 13
BLACK_HOLE = 14
COFFEE = 15
COFFEE_2 = 16
COFFEE_3S = 17
FLOODING = 18
SALMON = 19
POLARITY_PLUS = 20
POLARITY_MINUS = 21
SUN_90 = 23
SUN_POINT_1 = 24
SUM_SUN = 25
SUPERNOVA_ECLIPSE = 26
BLACK_HOLE_BLACK_HOLE = 27
JAZZ = 28
NIGHT = 29
def is_coffee(self):
return self.value in [self.COFFEE, self.COFFEE_2, self.COFFEE_3S]
def is_polarity(self):
return self.value in [self.POLARITY_PLUS, self.POLARITY_MINUS]
def can_echo(self):
return self.value in [self.FEEDBACK, self.REVERB]
@unique
class Base(IntEnum):
"""Yes, these are zero-indexed."""
FIRST = 0
SECOND = 1
THIRD = 2
FOURTH = 3
FIFTH = 4
@unique
class Blood(IntEnum):
A = 0
AA = 1
AAA = 2
ACIDIC = 3
BASIC = 4
O = 5 # noqa: E741
O_NO = 6
H2O = 7
ELECTRIC = 8
LOVE = 9
FIRE = 10
PSYCHIC = 11
GRASS = 12
BALL = 13
STRIKE = 14
@unique
class ModType(IntEnum):
PERMANENT = 0
SEASON = 1
WEEK = 2
GAME = 3
ITEM = 4
def mods_by_type_decoder(raw: Dict[str, List[str]]):
return {ModType(int(k)): v for k, v in raw.items()}
@dataclass
class TeamOrPlayerMods(DataClassJsonMixin):
mods: Set[str]
# Used internally only
_mods_by_type: Dict[ModType, Set[str]] = field(metadata=config(decoder=mods_by_type_decoder))
@classmethod
def mods_init_args(cls, data: Dict[str, Any]) -> Dict[str, Any]:
MOD_KEYS = {
ModType.PERMANENT: "permAttr",
ModType.SEASON: "seasAttr",
ModType.WEEK: "weekAttr",
ModType.GAME: "gameAttr",
ModType.ITEM: "itemAttr",
}
mods_by_type = {}
for mod_type, key in MOD_KEYS.items():
mods_by_type[mod_type] = set(data.get(key, []))
return dict(_mods_by_type=mods_by_type, mods=cls._concatenate_mods(mods_by_type))
def add_mod(self, mod: Union[Mod, str], mod_type: ModType):
mod = str(mod)
if mod in self._mods_by_type[mod_type]:
return
self._mods_by_type[mod_type].add(mod)
self._update_mods()
def remove_mod(self, mod: Union[Mod, str], mod_type: ModType):
mod = str(mod)
if mod not in self._mods_by_type[mod_type]:
return
self._mods_by_type[mod_type].remove(mod)
self._update_mods()
def has_mod(self, mod: Union[Mod, str], mod_type: Optional[ModType] = None) -> bool:
mod = str(mod)
if mod_type is None:
return mod in self.mods
return mod in self._mods_by_type[mod_type]
def has_any(self, *mods: Mod) -> bool:
return any(self.has_mod(mod) for mod in mods)
def print_mods(self, mod_type: Optional[ModType] = None) -> str:
return str(list(self._mods_by_type.get(mod_type) or self.mods))
def _update_mods(self):
self.mods = self._concatenate_mods(self._mods_by_type)
@staticmethod
def _concatenate_mods(mods_by_type: Dict[ModType, Set[str]]) -> Set[str]:
return set().union(*mods_by_type.values())
@dataclass
class TeamData(TeamOrPlayerMods):
object_type: ClassVar[str] = "team"
null: ClassVar["TeamData"]
id: Optional[str]
last_update_time: str
lineup: List[str]
rotation: List[str]
shadows: List[str]
eDensity: float = 0
level: int = 0
nickname: str = ""
rotation_slot: int = 0
@classmethod
def from_chron(cls, data: Dict[str, Any], last_update_time: str, prev_team_data: Optional["TeamData"]):
team_data = TeamData(
id=data["id"],
last_update_time=last_update_time,
lineup=data["lineup"],
rotation=data["rotation"],
shadows=data.get("shadows", []) + data.get("bullpen", []) + data.get("bench", []),
level=data.get("level") or 0,
eDensity=data.get("eDensity") or 0,
nickname=data.get("nickname") or "",
rotation_slot=data.get("rotationSlot") or 0,
**cls.mods_init_args(data),
)
if prev_team_data is not None:
if prev_team_data.is_cache_equivalent(team_data):
team_data.last_update_time = prev_team_data.last_update_time
return team_data
def is_cache_equivalent(self, other: "TeamData") -> bool:
return (
self.id == other.id
and
# Excluding last update time
self.lineup == other.lineup
and self.rotation == other.rotation
and self.shadows == other.shadows
and
# Excluding eDensity
self.level == other.level
and self.nickname == other.nickname
# Excluding rotation slot
)
@staticmethod
def make_null():
return TeamData.from_chron(
{
"id": None,
"nickname": "Null Team",
# Using [None] rather than [] means things like fielder selection
# won't get an index error
"lineup": [None],
"rotation": [None],
},
"1970-01-01T00:00:00.000Z",
None,
)
TeamData.null = TeamData.make_null()
@dataclass
class StadiumData(DataClassJsonMixin):
object_type: ClassVar[str] = "stadium"
null: ClassVar["StadiumData"]
id: Optional[str]
last_update_time: str
mods: Set[str]
name: str
nickname: str
mysticism: float
viscosity: float
elongation: float
filthiness: float
obtuseness: float
forwardness: float
grandiosity: float
ominousness: float
fortification: float
inconvenience: float
hype: float
def has_mod(self, mod: Mod) -> bool:
return mod.value in self.mods
def print_mods(self) -> str:
return list(set(self.mods))
@classmethod
def from_chron(cls, data, last_update_time: str, prev_stadium_data: Optional["StadiumData"]):
stadium_data = StadiumData(
id=data["id"],
last_update_time=last_update_time,
mods=set(data["mods"]),
name=data["name"],
nickname=data["nickname"],
mysticism=data["mysticism"],
viscosity=data["viscosity"],
elongation=data["elongation"],
filthiness=data["filthiness"],
obtuseness=data["obtuseness"],
forwardness=data["forwardness"],
grandiosity=data["grandiosity"],
ominousness=data["ominousness"],
fortification=data["fortification"],
inconvenience=data["inconvenience"],
hype=data["hype"],
)
if prev_stadium_data is not None:
if prev_stadium_data.is_cache_equivalent(stadium_data):
stadium_data.last_update_time = prev_stadium_data.last_update_time
return stadium_data
def is_cache_equivalent(self, other: "StadiumData") -> bool:
return (
self.id == other.id
and
# Excluding last update time
self.mods == other.mods
and self.name == other.name
and self.nickname == other.nickname
and self.mysticism == other.mysticism
and self.viscosity == other.viscosity
and self.elongation == other.elongation
and
# Excluding filthiness
self.obtuseness == other.obtuseness
and self.forwardness == other.forwardness
and self.grandiosity == other.grandiosity
and self.ominousness == other.ominousness
and self.fortification == other.fortification
and self.inconvenience == other.inconvenience
# Excluding hype
)
@staticmethod
def make_null():
return StadiumData(
last_update_time="1970-01-01T00:00:00.000Z",
id=None,
mods=set(),
name="Null Stadium",
nickname="Null Stadium",
mysticism=0.5,
viscosity=0.5,
elongation=0.5,
filthiness=0,
obtuseness=0.5,
forwardness=0.5,
grandiosity=0.5,
ominousness=0.5,
fortification=0.5,
inconvenience=0.5,
hype=0,
)
StadiumData.null = StadiumData.make_null()
@dataclass
class ItemData:
id: Optional[str]
name: str
health: int
durability: int
# Some Items have None ratings (e.g. Lucky Air Bat)
defense_rating: Optional[float]
hitting_rating: Optional[float]
pitching_rating: Optional[float]
baserunning_rating: Optional[float]
stats: dict
@staticmethod
def from_dict(data):
stats = {}
components = [data["root"], data["suffix"], data["prePrefix"], data["postPrefix"]] + (data["prefixes"] or [])
for component in components:
if not component:
continue
for adjustment in component["adjustments"]:
if adjustment["type"] == 1:
stat_name = stat_indices[adjustment["stat"]]
value = adjustment["value"]
stats[stat_name] = stats.get(stat_name, 0) + value
return ItemData(
id=data["id"],
name=data["name"],
health=data["health"],
durability=data["durability"],
defense_rating=data["defenseRating"],
hitting_rating=data["hittingRating"],
pitching_rating=data["pitchingRating"],
baserunning_rating=data["baserunningRating"],
stats=stats,
)
@staticmethod
def null():
return ItemData(
id=None,
name="Null Item",
health=0,
durability=0,
defense_rating=0,
hitting_rating=0,
pitching_rating=0,
baserunning_rating=0,
stats={},
)
@dataclass
class PlayerData(TeamOrPlayerMods):
object_type: ClassVar[str] = "player"
null: ClassVar["PlayerData"]
id: Optional[str]
last_update_time: str
raw_name: str
unscattered_name: Optional[str]
data: Dict[str, Any]
# Player attributes
buoyancy: float
divinity: float
martyrdom: float
moxie: float
musclitude: float
patheticism: float
thwackability: float
tragicness: float
ruthlessness: float
overpowerment: float
unthwackability: float
shakespearianism: float
suppression: float
coldness: float
baseThirst: float
continuation: float
ground_friction: float
indulgence: float
laserlikeness: float
anticapitalism: float
chasiness: float
omniscience: float
tenaciousness: float
watchfulness: float
pressurization: float
cinnamon: float
blood: Optional[Blood]
consecutive_hits: int
bat: Optional[str]
soul: int
eDensity: float
items: List[ItemData]
season_mod_sources: Dict[str, List[str]]
permanent_mod_sources: Dict[str, List[str]]
# Old incinerations don't have a peanut allergy field
peanut_allergy: Optional[bool]
@classmethod
def from_chron(cls, data: Dict[str, Any], last_update_time: str, prev_player_data: Optional["PlayerData"]):
data_state = data.get("state", {})
items = [ItemData.from_dict(item) for item in data.get("items") or []]
player_data = PlayerData(
id=data["id"],
last_update_time=last_update_time,
raw_name=data["name"],
unscattered_name=data_state.get("unscatteredName"),
data=data,
items=items,
blood=data.get("blood") or None,
consecutive_hits=data.get("consecutiveHits") or 0,
bat=data.get("bat") or None,
soul=data.get("soul") or 0,
eDensity=data.get("eDensity") or 0,
season_mod_sources=data_state.get("seasModSources", {}),
permanent_mod_sources=data_state.get("permModSources", {}),
peanut_allergy=data.get("peanutAllergy"),
**cls.mods_init_args(data),
**cls.stats_init_args(data, items),
)
if prev_player_data is not None:
if prev_player_data.is_cache_equivalent(player_data):
player_data.last_update_time = prev_player_data.last_update_time
return player_data
@property
def name(self):
return self.unscattered_name or self.raw_name
def vibes(self, day) -> float:
if self.has_mod(Mod.SCATTERED):
return 0
return self.raw_vibes(day)
def raw_vibes(self, day) -> float:
# must use pre-item buoyancy
# todo: does this apply to pressurization and cinnamon too?
frequency = 6 + round(10 * self.data["buoyancy"])
# Pull from pre-computed sin values
sin_phase = SIN_PHASES[frequency][day]
# Original formula:
# sin_phase = math.sin(math.pi * ((2 / frequency) * day + 0.5))
pressurization = self.pressurization
cinnamon = self.cinnamon or 0
return 0.5 * ((sin_phase - 1) * pressurization + (sin_phase + 1) * cinnamon)
def stats_with_items(self) -> Dict[str, float]:
return self._get_stats_with_items(self.data, self.items)
def multiplied(self, stat: str, multiplier: float) -> float:
# we can do this nicer i think but whatevs
raw_stat = self.data[stat.replace("ground_friction", "groundFriction")]
full_stat = getattr(self, stat)
item_stat = full_stat - raw_stat
return raw_stat * multiplier + item_stat
def is_cache_equivalent(self, other: "PlayerData") -> bool:
return (
self.id == other.id
and
# Excluding last update time
self.raw_name == other.raw_name
and self.unscattered_name == other.unscattered_name
and
# Excluding data
self.buoyancy == other.buoyancy
and self.divinity == other.divinity
and self.martyrdom == other.martyrdom
and self.moxie == other.moxie
and self.musclitude == other.musclitude
and self.patheticism == other.patheticism
and self.thwackability == other.thwackability
and self.tragicness == other.tragicness
and self.ruthlessness == other.ruthlessness
and self.overpowerment == other.overpowerment
and self.unthwackability == other.unthwackability
and self.shakespearianism == other.shakespearianism
and self.suppression == other.suppression
and self.coldness == other.coldness
and self.baseThirst == other.baseThirst
and self.continuation == other.continuation
and self.ground_friction == other.ground_friction
and self.indulgence == other.indulgence
and self.laserlikeness == other.laserlikeness
and self.anticapitalism == other.anticapitalism
and self.chasiness == other.chasiness
and self.omniscience == other.omniscience
and self.tenaciousness == other.tenaciousness
and self.watchfulness == other.watchfulness
and self.pressurization == other.pressurization
and self.cinnamon == other.cinnamon
and self.blood == other.blood
and
# Excluding consecutive hits
self.bat == other.bat
and self.soul == other.soul
and
# Excluding eDensity
self.items == other.items
and self.season_mod_sources == other.season_mod_sources
and self.peanut_allergy == other.peanut_allergy
)
@staticmethod
def _get_stats_with_items(data: Dict[str, Any], items: List[ItemData]) -> Dict[str, float]:
stats = {stat: data[stat] for stat in stat_indices}
for item in items:
# if item.health != 0:
for stat, value in item.stats.items():
if stat in ["patheticism", "tragicness", "buoyancy"]:
# path increases from items seem to actually *decrease* path in the formulas (and the other way
# around for path decreases)... even though the star calculations on the site ding you for
# having an item that increases path! at least right now, through season 19.
# tragicness: also backwards
stats[stat] -= value
elif stat not in ["buoyancy", "cinnamon", "pressurization"]:
stats[stat] += value
# else:
# for stat, value in item.stats.items():
# # well aren't you special
# if stat == "thwackability":
# stats[stat] += value
return stats
def update_stats(self):
stats = self.stats_with_items()
self.buoyancy = stats["buoyancy"]
self.divinity = stats["divinity"]
self.martyrdom = stats["martyrdom"]
self.moxie = stats["moxie"]
self.musclitude = stats["musclitude"]
self.patheticism = stats["patheticism"]
self.thwackability = stats["thwackability"]
self.tragicness = stats["tragicness"]
self.ruthlessness = stats["ruthlessness"]
self.overpowerment = stats["overpowerment"]
self.unthwackability = stats["unthwackability"]
self.shakespearianism = stats["shakespearianism"]
self.suppression = stats["suppression"]
self.coldness = stats["coldness"]
self.baseThirst = stats["baseThirst"]
self.continuation = stats["continuation"]
self.ground_friction = stats["groundFriction"]
self.indulgence = stats["indulgence"]
self.laserlikeness = stats["laserlikeness"]
self.anticapitalism = stats["anticapitalism"]
self.chasiness = stats["chasiness"]
self.omniscience = stats["omniscience"]
self.tenaciousness = stats["tenaciousness"]
self.watchfulness = stats["watchfulness"]
self.pressurization = stats["pressurization"]
self.cinnamon = stats.get("cinnamon") or 0
@classmethod
def stats_init_args(cls, data: Dict[str, Any], items: List[ItemData]) -> Dict[str, float]:
stats = cls._get_stats_with_items(data, items)
return dict(
buoyancy=stats["buoyancy"],
divinity=stats["divinity"],
martyrdom=stats["martyrdom"],
moxie=stats["moxie"],
musclitude=stats["musclitude"],
patheticism=stats["patheticism"],
thwackability=stats["thwackability"],
tragicness=stats["tragicness"],
ruthlessness=stats["ruthlessness"],
overpowerment=stats["overpowerment"],
unthwackability=stats["unthwackability"],
shakespearianism=stats["shakespearianism"],
suppression=stats["suppression"],
coldness=stats["coldness"],
baseThirst=stats["baseThirst"],
continuation=stats["continuation"],
ground_friction=stats["groundFriction"],
indulgence=stats["indulgence"],
laserlikeness=stats["laserlikeness"],
anticapitalism=stats["anticapitalism"],
chasiness=stats["chasiness"],
omniscience=stats["omniscience"],
tenaciousness=stats["tenaciousness"],
watchfulness=stats["watchfulness"],
pressurization=stats["pressurization"],
cinnamon=stats.get("cinnamon") or 0,
)
@staticmethod
def make_null():
return PlayerData.from_chron(
{
"id": None,
"name": "Null Player",
"buoyancy": 0.5,
"divinity": 0.5,
"martyrdom": 0.5,
"moxie": 0.5,
"musclitude": 0.5,
"patheticism": 0.5,
"thwackability": 0.5,
"tragicness": 0.5,
"ruthlessness": 0.5,
"overpowerment": 0.5,
"unthwackability": 0.5,
"shakespearianism": 0.5,
"suppression": 0.5,
"coldness": 0.5,
"baseThirst": 0.5,
"continuation": 0.5,
"groundFriction": 0.5,