-
Notifications
You must be signed in to change notification settings - Fork 3
modify: change map cooldown behaviour #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -264,11 +264,23 @@ private void OnClientPutInServer(int slot) | |||||||||||||||||||||||||||||||||||||||||||
| private void OnMapStart(string mapName) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| ExtendCount = 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| DecrementAllMapCooldown(CurrentMap); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| CurrentMap = NextMap; | ||||||||||||||||||||||||||||||||||||||||||||
| NextMap = null; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Task.Run(async void() => | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| await DecrementAllMapCooldown(); | ||||||||||||||||||||||||||||||||||||||||||||
| await ApplyCooldownToCurrentMap(CurrentMap); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| catch (Exception e) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| Logger.LogError(e, "Exception occured while applying map/group cooldown."); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+271
to
+282
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gemini-code-assist So how do I needs to be? iirc I'm implemented a DB operation as a atomic, so I didn't care about fire-and-forget pattern. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right that individual database operations can be atomic, ensuring data integrity at the database level for each specific write. However, the concern with a "fire-and-forget" The most robust way to ensure these critical operations complete is to If If, due to external constraints (e.g.,
Suggested change
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gemini-code-assist no this is in main game loop so I use Task.Run() as an async There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for clarifying that Your current implementation, which wraps these operations in a Just to reiterate the point from my previous comment, while this approach is necessary due to the
For this specific scenario, your current implementation is a pragmatic solution given the |
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Server.NextFrame(ExecuteMapLimitConfig); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -303,15 +315,15 @@ private void ObtainCurrentMap(string mapName) | |||||||||||||||||||||||||||||||||||||||||||
| ExtendLimit = CurrentMap?.MaxExtends ?? DefaultMapExtends; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| private void DecrementAllMapCooldown(IMapConfig? previousMap) | ||||||||||||||||||||||||||||||||||||||||||||
| private async Task DecrementAllMapCooldown() | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| // To prevent unxpected cooldown reduction | ||||||||||||||||||||||||||||||||||||||||||||
| if (!IsFirstMapEnded || !IsSecondMapIsPassed) | ||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Decrement all cooldowns | ||||||||||||||||||||||||||||||||||||||||||||
| _mcsDatabaseProvider.MapInfoRepository.DecrementAllCooldownsAsync().ConfigureAwait(false); | ||||||||||||||||||||||||||||||||||||||||||||
| _mcsDatabaseProvider.GroupInfoRepository.DecrementAllCooldownsAsync().ConfigureAwait(false); | ||||||||||||||||||||||||||||||||||||||||||||
| await _mcsDatabaseProvider.MapInfoRepository.DecrementAllCooldownsAsync(); | ||||||||||||||||||||||||||||||||||||||||||||
| await _mcsDatabaseProvider.GroupInfoRepository.DecrementAllCooldownsAsync(); | ||||||||||||||||||||||||||||||||||||||||||||
| foreach (var (key, value) in _mcsInternalMapConfigProviderApi.GetMapConfigs()) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| foreach (IMapGroupSettings setting in value.GroupSettings) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -323,22 +335,24 @@ private void DecrementAllMapCooldown(IMapConfig? previousMap) | |||||||||||||||||||||||||||||||||||||||||||
| if (value.MapCooldown.CurrentCooldown > 0) | ||||||||||||||||||||||||||||||||||||||||||||
| value.MapCooldown.CurrentCooldown--; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Set previous map cooldown if defined in config | ||||||||||||||||||||||||||||||||||||||||||||
| if (previousMap != null) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| _mcsDatabaseProvider.MapInfoRepository.UpsertMapCooldownAsync(previousMap.MapName, previousMap.MapCooldown.MapConfigCooldown).ConfigureAwait(false); | ||||||||||||||||||||||||||||||||||||||||||||
| previousMap.MapCooldown.CurrentCooldown = previousMap.MapCooldown.MapConfigCooldown; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| private async Task ApplyCooldownToCurrentMap(IMapConfig? currentMap) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| if (currentMap == null) | ||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| foreach (IMapGroupSettings setting in previousMap.GroupSettings) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| _mcsDatabaseProvider.GroupInfoRepository.UpsertGroupCooldownAsync(setting.GroupName, setting.GroupCooldown.MapConfigCooldown).ConfigureAwait(false); | ||||||||||||||||||||||||||||||||||||||||||||
| setting.GroupCooldown.CurrentCooldown = setting.GroupCooldown.MapConfigCooldown; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| await _mcsDatabaseProvider.MapInfoRepository.UpsertMapCooldownAsync(currentMap.MapName, currentMap.MapCooldown.MapConfigCooldown); | ||||||||||||||||||||||||||||||||||||||||||||
| currentMap.MapCooldown.CurrentCooldown = currentMap.MapCooldown.MapConfigCooldown; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| foreach (IMapGroupSettings setting in currentMap.GroupSettings) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| await _mcsDatabaseProvider.GroupInfoRepository.UpsertGroupCooldownAsync(setting.GroupName, setting.GroupCooldown.MapConfigCooldown); | ||||||||||||||||||||||||||||||||||||||||||||
| setting.GroupCooldown.CurrentCooldown = setting.GroupCooldown.MapConfigCooldown; | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) | ||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||
| if (!_isMapStarted) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
async voidfor lambdas passed toTask.Runis generally discouraged. While it works here because of thetry-catchblock, it's a less robust pattern.async voidmethods have different error-handling semantics and are harder to test and compose.It's better to use an
async Tasklambda (async () => { ... }).Task.Runhas an overload that accepts aFunc<Task>, and this is the preferred way to run async operations on a background thread. TheTaskreturned byTask.Runwill then represent the entire lifetime of the async operation inside the lambda, which is generally what's expected and a more robust pattern.