|
1 | 1 | import sys
|
| 2 | +import time |
| 3 | +from collections import defaultdict |
| 4 | +from typing import Any, Union |
2 | 5 |
|
| 6 | +import attrs |
| 7 | + |
| 8 | +import fixtures |
3 | 9 | import parse_logs
|
4 | 10 |
|
5 | 11 |
|
| 12 | +def when_dropped(item: fixtures.Item) -> range: |
| 13 | + first_dropped = item.first_dropped or item.first_seen or 0 |
| 14 | + last_dropped = item.last_dropped or round((time.time() + 10000000) * 1000) |
| 15 | + return range(first_dropped, last_dropped + 1) |
| 16 | + |
| 17 | + |
| 18 | +@attrs.define |
| 19 | +class ItemDrops: |
| 20 | + explores: int = 0 |
| 21 | + lemonades: int = 0 |
| 22 | + ciders: int = 0 |
| 23 | + fishes: int = 0 |
| 24 | + nets: int = 0 |
| 25 | + drops: int = 0 |
| 26 | + |
| 27 | + |
| 28 | +@attrs.define |
| 29 | +class LocationDrops: |
| 30 | + explores: int = 0 |
| 31 | + lemonades: int = 0 |
| 32 | + ciders: int = 0 |
| 33 | + fishes: int = 0 |
| 34 | + nets: int = 0 |
| 35 | + drops: int = 0 |
| 36 | + items: dict[str, ItemDrops] = attrs.Factory(lambda: defaultdict(ItemDrops)) |
| 37 | + |
| 38 | + |
| 39 | +@attrs.define |
| 40 | +class Drops: |
| 41 | + explores: int = 0 |
| 42 | + lemonades: int = 0 |
| 43 | + ciders: int = 0 |
| 44 | + fishes: int = 0 |
| 45 | + nets: int = 0 |
| 46 | + drops: int = 0 |
| 47 | + locations: dict[str, LocationDrops] = attrs.Factory( |
| 48 | + lambda: defaultdict(LocationDrops) |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def count_sources( |
| 53 | + drops: Union[Drops, LocationDrops, ItemDrops], row: dict[str, Any] |
| 54 | +) -> None: |
| 55 | + if row["type"] == "explore": |
| 56 | + drops.explores += row["results"]["stamina"] |
| 57 | + elif row["type"] == "lemonade": |
| 58 | + drops.lemonades += 1 |
| 59 | + elif row["type"] == "cider": |
| 60 | + drops.ciders += 1 |
| 61 | + # This is kind of wrong for global and location stats since not all explores count |
| 62 | + # for all items but it's more correct than not. |
| 63 | + drops.explores += row["results"].get("explores", row["results"]["stamina"]) |
| 64 | + elif row["type"] == "fish": |
| 65 | + drops.fishes += 1 |
| 66 | + elif row["type"] == "net": |
| 67 | + drops.nets += 1 |
| 68 | + |
| 69 | + |
| 70 | +def compile_drops( |
| 71 | + explore: bool = False, |
| 72 | + lemonade: bool = False, |
| 73 | + cider: bool = False, |
| 74 | + fish: bool = False, |
| 75 | + net: bool = False, |
| 76 | +) -> Drops: |
| 77 | + when_items_dropped = { |
| 78 | + item.name: when_dropped(item) for item in fixtures.load_items() |
| 79 | + } |
| 80 | + # loc_items = {loc.name: loc.items for loc in fixtures.load_locations()} |
| 81 | + types: set[str] = set() |
| 82 | + if explore: |
| 83 | + types.add("explore") |
| 84 | + if lemonade: |
| 85 | + types.add("lemonade") |
| 86 | + if cider: |
| 87 | + types.add("cider") |
| 88 | + if fish: |
| 89 | + types.add("fish") |
| 90 | + if net: |
| 91 | + types.add("net") |
| 92 | + explores = Drops() |
| 93 | + logs = [row for row in parse_logs.parse_logs() if row["type"] in types] |
| 94 | + # First run through to count drops and work out which items are in each locations. |
| 95 | + for row in logs: |
| 96 | + location_name = row["results"].get("location") |
| 97 | + if not location_name: |
| 98 | + continue |
| 99 | + overflow_items: set[str] = { |
| 100 | + item["item"] for item in row["results"]["items"] if item["overflow"] |
| 101 | + } |
| 102 | + for item in row["results"]["items"]: |
| 103 | + if row["ts"] not in when_items_dropped[item["item"]]: |
| 104 | + # Ignore out-of-bounds drops. This allows accounting for stuff like drop |
| 105 | + # rates changing substantially by manually resetting firstDropped. |
| 106 | + continue |
| 107 | + if row["type"] == "cider" and item["item"] in overflow_items: |
| 108 | + # Cider overflow always reports 0 drops so any item that overflows during |
| 109 | + # a cider has to be ignored. |
| 110 | + continue |
| 111 | + explores.locations[location_name].items[item["item"]].drops += item.get( |
| 112 | + "quantity", 1 |
| 113 | + ) |
| 114 | + explores.locations[location_name].drops += item.get("quantity", 1) |
| 115 | + explores.drops += item.get("quantity", 1) |
| 116 | + # Second pass to get the explore counts. |
| 117 | + for row in logs: |
| 118 | + location_name = row["results"].get("location") |
| 119 | + if not location_name: |
| 120 | + continue |
| 121 | + overflow_items: set[str] = { |
| 122 | + item["item"] for item in row["results"]["items"] if item["overflow"] |
| 123 | + } |
| 124 | + for item, item_explores in explores.locations[location_name].items.items(): |
| 125 | + if row["ts"] not in when_items_dropped[item]: |
| 126 | + # Item couldn't drop, this doesn't count. |
| 127 | + continue |
| 128 | + if row["type"] == "cider" and item in overflow_items: |
| 129 | + # Cider overflow always reports 0 drops so any item that overflows during |
| 130 | + # a cider has to be ignored. |
| 131 | + continue |
| 132 | + count_sources(item_explores, row) |
| 133 | + count_sources(explores.locations[location_name], row) |
| 134 | + count_sources(explores, row) |
| 135 | + |
| 136 | + return explores |
| 137 | + |
| 138 | + |
6 | 139 | def total_drops() -> dict[str, dict[str, int]]:
|
7 | 140 | totals = {}
|
8 | 141 | for row in parse_logs.parse_logs("explore"):
|
|
0 commit comments