|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/controlado/lol-autobuild/internal/app" |
| 10 | + "github.com/controlado/lol-autobuild/internal/auth" |
| 11 | + "github.com/controlado/lol-autobuild/internal/config" |
| 12 | + "github.com/controlado/lol-autobuild/internal/lcu" |
| 13 | + "github.com/controlado/lol-autobuild/internal/update" |
| 14 | +) |
| 15 | + |
| 16 | +type configSaver interface { |
| 17 | + Save(config.Config) error |
| 18 | +} |
| 19 | + |
| 20 | +type appConfigStore struct { |
| 21 | + mu sync.Mutex |
| 22 | + store configSaver |
| 23 | + cfg config.Config |
| 24 | +} |
| 25 | + |
| 26 | +func newAppConfigStore(store configSaver, cfg config.Config) *appConfigStore { |
| 27 | + return &appConfigStore{ |
| 28 | + store: store, |
| 29 | + cfg: cfg, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (s *appConfigStore) Save(newCfg app.RuntimeConfig) error { |
| 34 | + s.mu.Lock() |
| 35 | + defer s.mu.Unlock() |
| 36 | + |
| 37 | + next := configFromRuntimeConfig(s.cfg, newCfg) |
| 38 | + if err := s.store.Save(next); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + s.cfg = next |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +func (s *appConfigStore) configFor(appCfg app.RuntimeConfig) config.Config { |
| 47 | + s.mu.Lock() |
| 48 | + defer s.mu.Unlock() |
| 49 | + |
| 50 | + return configFromRuntimeConfig(s.cfg, appCfg) |
| 51 | +} |
| 52 | + |
| 53 | +func configFromRuntimeConfig(base config.Config, appCfg app.RuntimeConfig) config.Config { |
| 54 | + base.Sync = config.SyncConfig{ |
| 55 | + Patch: appCfg.Settings.Patch, |
| 56 | + PatchAdditionsMode: appCfg.Settings.PatchAdditionsMode, |
| 57 | + PatchAdditions: appCfg.Settings.PatchAdditions, |
| 58 | + LeagueTierPreset: appCfg.Settings.LeagueTierPreset, |
| 59 | + ApplyItems: appCfg.Settings.ApplyItems, |
| 60 | + ApplyRunes: appCfg.Settings.ApplyRunes, |
| 61 | + ApplySpells: appCfg.Settings.ApplySpells, |
| 62 | + KeepFlash: appCfg.Settings.KeepFlash, |
| 63 | + DryRun: appCfg.Settings.DryRun, |
| 64 | + } |
| 65 | + base.LCU.Enabled = appCfg.Settings.LCUEnabled |
| 66 | + base.Watch.DebounceMillis = int(appCfg.WatchDebounce / time.Millisecond) |
| 67 | + return base |
| 68 | +} |
| 69 | + |
| 70 | +func runtimeConfigFromConfig(cfg config.Config) app.RuntimeConfig { |
| 71 | + return app.RuntimeConfig{ |
| 72 | + Settings: app.Settings{ |
| 73 | + Patch: cfg.Sync.Patch, |
| 74 | + PatchAdditionsMode: cfg.Sync.PatchAdditionsMode, |
| 75 | + PatchAdditions: cfg.Sync.PatchAdditions, |
| 76 | + LeagueTierPreset: cfg.Sync.LeagueTierPreset, |
| 77 | + ApplyItems: cfg.Sync.ApplyItems, |
| 78 | + ApplyRunes: cfg.Sync.ApplyRunes, |
| 79 | + ApplySpells: cfg.Sync.ApplySpells, |
| 80 | + KeepFlash: cfg.Sync.KeepFlash, |
| 81 | + DryRun: cfg.Sync.DryRun, |
| 82 | + LCUEnabled: cfg.LCU.Enabled, |
| 83 | + }, |
| 84 | + WatchDebounce: time.Duration(cfg.Watch.DebounceMillis) * time.Millisecond, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func appLCUStatusFromLCU(status lcu.ConnectionStatus) app.LCUStatus { |
| 89 | + return app.LCUStatus{ |
| 90 | + State: appLCUConnectionStateFromLCU(status.State), |
| 91 | + Message: status.Message, |
| 92 | + Source: status.Source, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func appLCUConnectionStateFromLCU(state lcu.ConnectionState) app.LCUConnectionState { |
| 97 | + switch state { |
| 98 | + case lcu.ConnectionStateOff: |
| 99 | + return app.LCUConnectionStateOff |
| 100 | + case lcu.ConnectionStateConnected: |
| 101 | + return app.LCUConnectionStateConnected |
| 102 | + default: |
| 103 | + return app.LCUConnectionStateNotConnected |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +type updateSource interface { |
| 108 | + CurrentVersion() string |
| 109 | + Check(context.Context) (update.Result, error) |
| 110 | +} |
| 111 | + |
| 112 | +type appUpdateChecker struct { |
| 113 | + source updateSource |
| 114 | +} |
| 115 | + |
| 116 | +func (c appUpdateChecker) CurrentVersion() string { |
| 117 | + return c.source.CurrentVersion() |
| 118 | +} |
| 119 | + |
| 120 | +func (c appUpdateChecker) Check(ctx context.Context) (app.UpdateCheckResult, error) { |
| 121 | + result, err := c.source.Check(ctx) |
| 122 | + out := app.UpdateCheckResult{ |
| 123 | + CurrentVersion: result.CurrentVersion, |
| 124 | + LatestVersion: result.LatestVersion, |
| 125 | + DownloadURL: result.DownloadURL, |
| 126 | + Available: result.Available, |
| 127 | + } |
| 128 | + if errors.Is(err, update.ErrUnavailable) { |
| 129 | + return out, app.ErrUpdateUnavailable |
| 130 | + } |
| 131 | + |
| 132 | + return out, err |
| 133 | +} |
| 134 | + |
| 135 | +func appMessageFromErr(err error) app.UserMessage { |
| 136 | + switch { |
| 137 | + case err == nil: |
| 138 | + return app.UserMessage{} |
| 139 | + case errors.Is(err, lcu.ErrNotConfigured): |
| 140 | + return app.UserMessage{Code: app.MessageCodeLCUOff, Text: "LCU is off."} |
| 141 | + case errors.Is(err, lcu.ErrLockfileNotFound): |
| 142 | + return app.UserMessage{Code: app.MessageCodeLCULockfileNotFound, Text: "League Client is not open."} |
| 143 | + case errors.Is(err, lcu.ErrChampSelectUnavailable): |
| 144 | + return app.UserMessage{Code: app.MessageCodeLCUChampSelectUnavailable, Text: "Champ select is not ready."} |
| 145 | + case errors.Is(err, lcu.ErrChampionNotSelected): |
| 146 | + return app.UserMessage{Code: app.MessageCodeLCUChampionNotSelected, Text: "Select a champion first."} |
| 147 | + case errors.Is(err, auth.ErrAccessTokenUnavailable): |
| 148 | + return app.UserMessage{Code: app.MessageCodeCoachlessLoginMissing, Text: "Coachless login is missing."} |
| 149 | + default: |
| 150 | + return app.UserMessage{Text: err.Error()} |
| 151 | + } |
| 152 | +} |
0 commit comments