|
| 1 | +package idlerpg |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/IamMrCupp/annoybots/internal/state" |
| 11 | +) |
| 12 | + |
| 13 | +// Weather is ambient and per-biome — the coast turns stormy, the peaks snowbound, |
| 14 | +// the marsh foggy — rotating every so often. It's lighter than a realm-wide world |
| 15 | +// event: a mild thumb on the scale wherever you happen to be standing. |
| 16 | +// |
| 17 | +// fog — harder to land blows there (-2 attack) |
| 18 | +// storm — the foes there strike more surely (+2 to their attack) |
| 19 | +// rain — the going is slow (travel step reduced) |
| 20 | +// snow — slower still |
| 21 | +// clear — nothing at all |
| 22 | +const ( |
| 23 | + weatherRotate = 1200 // seconds between rotations |
| 24 | + fogAtkPenalty = 2 |
| 25 | + stormAtkBonus = 2 |
| 26 | + rainSlow = 4 // travel step reduction |
| 27 | + snowSlow = 7 |
| 28 | +) |
| 29 | + |
| 30 | +func weatherKey() string { return "rpg:weather" } |
| 31 | + |
| 32 | +// weatherState is the realm's current sky, per biome. |
| 33 | +type weatherState struct { |
| 34 | + Biomes map[string]string `json:"biomes"` // biome -> weather kind |
| 35 | + Deadline int64 `json:"deadline"` // unix seconds until it rotates |
| 36 | +} |
| 37 | + |
| 38 | +// biomeWeather constrains what each biome can get — snow in the peaks, storms at sea. |
| 39 | +var biomeWeather = map[string][]string{ |
| 40 | + "coast": {"clear", "rain", "storm", "fog"}, |
| 41 | + "mountain": {"clear", "snow", "storm", "fog"}, |
| 42 | + "forest": {"clear", "rain", "fog"}, |
| 43 | + "swamp": {"clear", "rain", "fog", "storm"}, |
| 44 | + "plains": {"clear", "rain", "storm"}, |
| 45 | +} |
| 46 | + |
| 47 | +// WeatherView is the dashboard's read-only view of the sky. |
| 48 | +type WeatherView struct { |
| 49 | + Biome string |
| 50 | + Kind string |
| 51 | +} |
| 52 | + |
| 53 | +// ReadWeather returns the current per-biome weather, sorted by biome. |
| 54 | +func ReadWeather(ctx context.Context, store state.Store) ([]WeatherView, error) { |
| 55 | + blob, err := store.GetStr(ctx, weatherKey()) |
| 56 | + if err != nil || blob == "" { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + var w weatherState |
| 60 | + if json.Unmarshal([]byte(blob), &w) != nil { |
| 61 | + return nil, nil |
| 62 | + } |
| 63 | + out := make([]WeatherView, 0, len(w.Biomes)) |
| 64 | + for b, k := range w.Biomes { |
| 65 | + out = append(out, WeatherView{Biome: b, Kind: k}) |
| 66 | + } |
| 67 | + sort.Slice(out, func(i, j int) bool { return out[i].Biome < out[j].Biome }) |
| 68 | + return out, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (m *Manager) loadWeather(ctx context.Context) { |
| 72 | + blob, err := m.store.GetStr(ctx, weatherKey()) |
| 73 | + if err != nil || blob == "" { |
| 74 | + return |
| 75 | + } |
| 76 | + var w weatherState |
| 77 | + if json.Unmarshal([]byte(blob), &w) != nil { |
| 78 | + return |
| 79 | + } |
| 80 | + m.wmu.Lock() |
| 81 | + m.weather = &w |
| 82 | + m.wmu.Unlock() |
| 83 | +} |
| 84 | + |
| 85 | +func (m *Manager) saveWeather(ctx context.Context, w *weatherState) { |
| 86 | + if blob, err := json.Marshal(w); err == nil { |
| 87 | + _ = m.store.SetStr(ctx, weatherKey(), string(blob)) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// weatherAt reports the current weather in a biome ("clear" when unknown). |
| 92 | +func (m *Manager) weatherAt(biome string) string { |
| 93 | + m.wmu.Lock() |
| 94 | + defer m.wmu.Unlock() |
| 95 | + if m.weather == nil { |
| 96 | + return "clear" |
| 97 | + } |
| 98 | + if k, ok := m.weather.Biomes[biome]; ok { |
| 99 | + return k |
| 100 | + } |
| 101 | + return "clear" |
| 102 | +} |
| 103 | + |
| 104 | +// weatherTick rolls a fresh sky for every biome when the current one has run its |
| 105 | +// course (and seeds the very first one). |
| 106 | +func (m *Manager) weatherTick(ctx context.Context) { |
| 107 | + m.wmu.Lock() |
| 108 | + w := m.weather |
| 109 | + m.wmu.Unlock() |
| 110 | + if w != nil && m.now().Unix() < w.Deadline { |
| 111 | + return |
| 112 | + } |
| 113 | + next := &weatherState{Biomes: map[string]string{}, Deadline: m.now().Unix() + weatherRotate} |
| 114 | + for biome, kinds := range biomeWeather { |
| 115 | + next.Biomes[biome] = kinds[m.roll(len(kinds))] |
| 116 | + } |
| 117 | + m.wmu.Lock() |
| 118 | + m.weather = next |
| 119 | + m.wmu.Unlock() |
| 120 | + m.saveWeather(ctx, next) |
| 121 | +} |
| 122 | + |
| 123 | +// travelSlow returns how much a biome's weather shortens a travel step. |
| 124 | +func travelSlow(kind string) int { |
| 125 | + switch kind { |
| 126 | + case "rain": |
| 127 | + return rainSlow |
| 128 | + case "snow": |
| 129 | + return snowSlow |
| 130 | + } |
| 131 | + return 0 |
| 132 | +} |
| 133 | + |
| 134 | +// weatherStatus answers !rpg weather — the sky over every biome. |
| 135 | +func (m *Manager) weatherStatus() string { |
| 136 | + m.wmu.Lock() |
| 137 | + w := m.weather |
| 138 | + m.wmu.Unlock() |
| 139 | + if w == nil || len(w.Biomes) == 0 { |
| 140 | + return "the sky is clear everywhere." |
| 141 | + } |
| 142 | + biomes := make([]string, 0, len(w.Biomes)) |
| 143 | + for b := range w.Biomes { |
| 144 | + biomes = append(biomes, b) |
| 145 | + } |
| 146 | + sort.Strings(biomes) |
| 147 | + parts := make([]string, len(biomes)) |
| 148 | + for i, b := range biomes { |
| 149 | + parts[i] = fmt.Sprintf("%s %s: %s", weatherIcon(w.Biomes[b]), b, w.Biomes[b]) |
| 150 | + } |
| 151 | + return "🌤 the sky — " + strings.Join(parts, " · ") |
| 152 | +} |
| 153 | + |
| 154 | +func weatherIcon(kind string) string { |
| 155 | + switch kind { |
| 156 | + case "rain": |
| 157 | + return "🌧" |
| 158 | + case "storm": |
| 159 | + return "⛈" |
| 160 | + case "fog": |
| 161 | + return "🌫" |
| 162 | + case "snow": |
| 163 | + return "❄️" |
| 164 | + } |
| 165 | + return "☀️" |
| 166 | +} |
0 commit comments