Skip to content
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

BankSystem: cached balances #1909

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

whatston3
Copy link
Contributor

About the PR

Adds caching to the server's BankAccountComponent handler for ComponentGetState.

Logic should largely follow previous behaviour - only the user controlling a given entity can update its value in the database/cache. Adds a dictionary in the BankSystem (yes, likely poor form, may need a refactor) to prevent unnecessary DB lookups when the last requested value matches the most recently written one (even if the value hasn't gone through into the DB)

Why / Balance

Updating BankAccounts was a pain, and the ComponentGetState function seems to get called not only on writing values but on queries as well.

Should clear up any issues for #1399 - will behave a lot better with many clients.

How to test

  1. Deposit money with a character, attach a breakpoint on Content.Server/_NF/Bank/BankSystem.cs:78 or so to ensure that only one DB hit results from the deposit.
  2. Disconnect, close the server, reopen it, check your character's money, should be consistent with the deposited value.

Media

  • I have added screenshots/videos to this PR showcasing its changes ingame, or this PR does not require an ingame showcase

Breaking changes

Changelog

Largely an internal feature, no changelog needed.

@github-actions github-actions bot added the C# label Aug 25, 2024
@whatston3 whatston3 mentioned this pull request Aug 25, 2024
@github-actions github-actions bot added the Status: Needs Review This PR is awaiting reviews label Aug 25, 2024
Content.Server/_NF/Bank/BankSystem.cs Outdated Show resolved Hide resolved
Content.Server/_NF/Bank/BankSystem.cs Outdated Show resolved Hide resolved
Content.Server/_NF/Bank/BankSystem.cs Outdated Show resolved Hide resolved
@GreaseMonk GreaseMonk added Status: Awaiting Changes This PR has changes that need to be made before merging and removed Status: Needs Review This PR is awaiting reviews labels Aug 26, 2024
@whatston3
Copy link
Contributor Author

Still needs work - might write to cache/DB on component write instead of component write, dirty, component read and cache/DB write.

@whatston3 whatston3 marked this pull request as draft August 27, 2024 18:37
@whatston3
Copy link
Contributor Author

Rewrote the bank account system. What exists now should work much more simply than it did before.

Balance in the BankAccountComponent is now only available server-side (though this could change, the value itself is unimportant w.r.t. DB state).
TryBankDeposit and TryBankWithdraw now query the cached character preferences and update them directly, absolutely no reliance on component values. BankAccountComponentState was removed entirely, and the ComponentGetState handler was removed.
The BankAccountComponent's value is updated when players are attached or detached from an entity, or when a deposit/withdrawal is made. One upside of this is that cloning or moving to a new character only means adding a BankAccount to the character.

With a small set of tests, this seems alright. Might have implications with aghosting into/out of urists. Will invalidate prior comments.

@whatston3 whatston3 dismissed GreaseMonk’s stale review August 28, 2024 14:53

Branch was changed, BankAccountManager largely irrelevant, current implementation is simpler.

@github-actions github-actions bot added Status: Needs Review This PR is awaiting reviews and removed Status: Awaiting Changes This PR has changes that need to be made before merging labels Aug 28, 2024
@whatston3 whatston3 marked this pull request as ready for review August 28, 2024 14:56
@dvir001
Copy link
Contributor

dvir001 commented Sep 7, 2024

Loadout wont charge, pending fix.

@whatston3
Copy link
Contributor Author

Issues should be fixed. I've added bank functions without entities for use in the loadout selection. The new API might not be great, and requires some care under use, but it does seem to work under modest testing. The functions should be commented as such.

I've added session to the PlayerSpawningEvent and code that uses it, since we need to know this to get the bank account. A bankless character will spawn without issue, but will receive a default or fallback loadout, no freebies, shouldn't have much potential for abuse if there are very expensive loadout options.

@github-actions github-actions bot added the Merge Conflict This PR has conflicts that prevent merging label Sep 7, 2024
Copy link
Contributor

github-actions bot commented Sep 7, 2024

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the Merge Conflict This PR has conflicts that prevent merging label Sep 7, 2024
Copy link
Contributor

@GreaseMonk GreaseMonk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disconnecting and reconnecting the client to localhost resets your (displayed) bank account to 0$

@GreaseMonk
Copy link
Contributor

This PR need 3 approvals before merging. Bugs like these can potentially reset banks to 0$ and that would be really really bad

@GreaseMonk GreaseMonk added Status: Awaiting Changes This PR has changes that need to be made before merging and removed Status: Needs Review This PR is awaiting reviews labels Sep 8, 2024
@GreaseMonk
Copy link
Contributor

Please edge case test
spawn as diona
die and split into mini dionas
after reform, check bank

Just as a precaution because this is where bank stuff is also transferred onto new entities.

@whatston3
Copy link
Contributor Author

Please edge case test spawn as diona die and split into mini dionas after reform, check bank

Just as a precaution because this is where bank stuff is also transferred onto new entities.

I have added two new calls for updates - a new event when the DB's preferences load finishes (this was the issue with reconnects), and a ComponentInit (this handles late-added components - if you remove someone's BankAccountComponent and add it, that works fine now, their balance will be up-to-date with their last cached state).

In terms of bank account safety, this is better than before - the server's BankSystem does not trust any Client's component state ever. With this, Client systems for vending machines, etc. could just attempt to withdraw funds without checking the component's state.

I think this is good to go, so I'll request another look from @GreaseMonk. Cloning's fine, nymph splitting is fine. All cases where a BankAccountComponent has been set up have been altered before his last review - none of these can set the balance now, it is all driven through the BankSystem, which is good.

@github-actions github-actions bot added Status: Needs Review This PR is awaiting reviews and removed Status: Awaiting Changes This PR has changes that need to be made before merging labels Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C# Status: Needs Review This PR is awaiting reviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants