|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + |
| 6 | + "lunar-tear/server/internal/model" |
| 7 | + "lunar-tear/server/internal/questflow" |
| 8 | + "lunar-tear/server/internal/store" |
| 9 | +) |
| 10 | + |
| 11 | +func startAutoOrbit(user *store.UserState, questType model.QuestType, chapterId, questId, maxCount int32, nowMillis int64) { |
| 12 | + if maxCount <= 0 { |
| 13 | + if user.QuestAutoOrbit.MaxAutoOrbitCount > 0 { |
| 14 | + log.Printf("[autoOrbit] clear (start without max): prev questType=%d chapter=%d quest=%d cleared=%d/%d", |
| 15 | + user.QuestAutoOrbit.QuestType, user.QuestAutoOrbit.ChapterId, user.QuestAutoOrbit.QuestId, |
| 16 | + user.QuestAutoOrbit.ClearedAutoOrbitCount, user.QuestAutoOrbit.MaxAutoOrbitCount) |
| 17 | + } |
| 18 | + user.QuestAutoOrbit = store.QuestAutoOrbitState{} |
| 19 | + return |
| 20 | + } |
| 21 | + s := user.QuestAutoOrbit |
| 22 | + if s.MaxAutoOrbitCount > 0 && |
| 23 | + s.QuestType == int32(questType) && s.ChapterId == chapterId && |
| 24 | + s.QuestId == questId && s.MaxAutoOrbitCount == maxCount { |
| 25 | + s.LatestVersion = nowMillis |
| 26 | + user.QuestAutoOrbit = s |
| 27 | + log.Printf("[autoOrbit] continue cleared=%d/%d", s.ClearedAutoOrbitCount, s.MaxAutoOrbitCount) |
| 28 | + return |
| 29 | + } |
| 30 | + log.Printf("[autoOrbit] start questType=%d chapter=%d quest=%d max=%d", questType, chapterId, questId, maxCount) |
| 31 | + user.QuestAutoOrbit = store.QuestAutoOrbitState{ |
| 32 | + QuestType: int32(questType), |
| 33 | + ChapterId: chapterId, |
| 34 | + QuestId: questId, |
| 35 | + MaxAutoOrbitCount: maxCount, |
| 36 | + LatestVersion: nowMillis, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func finishAutoOrbit(user *store.UserState, isAutoOrbit, isRetired, isAnnihilated bool, questType model.QuestType, chapterId, questId int32, nowMillis int64, drops []questflow.RewardGrant) (endedDrops []store.AutoOrbitDropEntry, loopEnded bool) { |
| 41 | + s := user.QuestAutoOrbit |
| 42 | + if s.MaxAutoOrbitCount <= 0 { |
| 43 | + return nil, false |
| 44 | + } |
| 45 | + if s.QuestType != int32(questType) || s.ChapterId != chapterId || s.QuestId != questId { |
| 46 | + log.Printf("[autoOrbit] finish for other quest, ignored: tracked={qt=%d ch=%d q=%d} got={qt=%d ch=%d q=%d}", |
| 47 | + s.QuestType, s.ChapterId, s.QuestId, int32(questType), chapterId, questId) |
| 48 | + return nil, false |
| 49 | + } |
| 50 | + if !isRetired && !isAnnihilated { |
| 51 | + added := 0 |
| 52 | + for _, d := range drops { |
| 53 | + s.AccumulatedDrops = append(s.AccumulatedDrops, store.AutoOrbitDropEntry{ |
| 54 | + PossessionType: int32(d.PossessionType), |
| 55 | + PossessionId: d.PossessionId, |
| 56 | + Count: d.Count, |
| 57 | + IsAutoSale: d.IsAutoSale, |
| 58 | + }) |
| 59 | + added++ |
| 60 | + } |
| 61 | + s.ClearedAutoOrbitCount++ |
| 62 | + log.Printf("[autoOrbit] iter cleared=%d/%d +%d drops (total=%d)", |
| 63 | + s.ClearedAutoOrbitCount, s.MaxAutoOrbitCount, added, len(s.AccumulatedDrops)) |
| 64 | + } |
| 65 | + s.LastClearDatetime = nowMillis |
| 66 | + s.LatestVersion = nowMillis |
| 67 | + if !isAutoOrbit || isRetired || isAnnihilated || s.ClearedAutoOrbitCount >= s.MaxAutoOrbitCount { |
| 68 | + log.Printf("[autoOrbit] loop end: cleared=%d/%d total drops=%d (returned in response, accumulator kept)", |
| 69 | + s.ClearedAutoOrbitCount, s.MaxAutoOrbitCount, len(s.AccumulatedDrops)) |
| 70 | + user.QuestAutoOrbit = store.QuestAutoOrbitState{AccumulatedDrops: s.AccumulatedDrops} |
| 71 | + return s.AccumulatedDrops, true |
| 72 | + } |
| 73 | + user.QuestAutoOrbit = s |
| 74 | + return nil, false |
| 75 | +} |
| 76 | + |
| 77 | +func consumeAutoOrbitRewards(user *store.UserState) []store.AutoOrbitDropEntry { |
| 78 | + drops := user.QuestAutoOrbit.AccumulatedDrops |
| 79 | + log.Printf("[autoOrbit] consume on FinishAutoOrbit: returning %d drops (loop status max=%d cleared=%d)", |
| 80 | + len(drops), user.QuestAutoOrbit.MaxAutoOrbitCount, user.QuestAutoOrbit.ClearedAutoOrbitCount) |
| 81 | + user.QuestAutoOrbit = store.QuestAutoOrbitState{} |
| 82 | + return drops |
| 83 | +} |
0 commit comments