WAReborn / WorldsAdriftReborn

64 stars 14 forks source link

Save characters in db & fix save character #47

Closed geoffev closed 1 year ago

geoffev commented 1 year ago

Handles character saving into a db, currently only uses SQLite but can easily be extended to use a config file with connection params to a MySQL, PgSQL,... db; After character is saved local PlayerPrefs are updated and you return to the character selection screen to enter world. The game calls the character list endpoint every time you go to the character selection screen and sets the PlayerPrefs so characters by default are kept in sync from the server to the client.

Can easily be extended to keep character inventory data.

Users do not need to run any SQL scripts, this is handled by Entity Framework. Migrations only run if they need to run.

I know this probably isn't high prio, but it also fixes https://github.com/sp00ktober/WorldsAdriftReborn/issues/7

For Developers If you haven't worked with entity framework yet and plan to extend upon the database: https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=dotnet-core-cli

Known Issues:

TheSpookyCat commented 1 year ago

so characters by default are kept in sync from the server to the client.

How would this work with multiple servers where each has their own http server expecting this player data?

sp00ktober commented 1 year ago

so characters by default are kept in sync from the server to the client.

How would this work with multiple servers where each has their own http server expecting this player data?

I guess that would need to be handled by some server to server syncing code, which is not clear yet how it will be done and is probably out of scope for this pr.

TheSpookyCat commented 1 year ago

I don't think "some server to server syncing code" will help us... its likely to introduce more problems than it solves, no reason why any two independently hosted servers should ever have to communicate with one another.

The game calls the character list endpoint every time you go to the character selection screen and sets the PlayerPrefs so characters by default are kept in sync from the server to the client.

This will definitely come back to haunt ¯_(ツ)_/¯

geoffev commented 1 year ago

so characters by default are kept in sync from the server to the client.

How would this work with multiple servers where each has their own http server expecting this player data?

Do you require each server to have a http server running the rest api? (I know each person that hosts a game server will at least have 1 rest api) The character data includes the server identifier. If /deploymentStatus is more functional in the future you can have one rest api support multiple servers. Also depending on how far in the future you are gonna take server discoverability you are either going to have a to have a public API with only needed endpoint /deploymentStatus that keeps track of all community run servers and store & return their corresponding rest api url and then set it using the mod, or have the user set the rest server url in configs themselves which would in turn be a rest api hosted by someone that has a community server(s) and you'll only see those.

Either way if you are going to check for multiple server afaik a working /deploymentStatus endpoint that actually checks the server status either public or private hosted is needed. With SQLite there is a bit more oversight (unless they can share the same db file) if you run multiple REST APIs but with a SQL Server it'll be less of a headache.

But this seems some distant future stuff.

geoffev commented 1 year ago

I don't think "some server to server syncing code" will help us... its likely to introduce more problems than it solves, no reason why any two independently hosted servers should ever have to communicate with one another.

The game calls the character list endpoint every time you go to the character selection screen and sets the PlayerPrefs so characters by default are kept in sync from the server to the client.

This will definitely come back to haunt ¯(ツ)

I should specify the game overwrites (not update/add) PlayerPrefs when it fetches the characterlist from the REST API upon clicking play, At the moment if you for example played on host X's server (and set the proper rest api url in the mod config) and then change the config variables to match host Y's rest url/server then your character list will only include the characters you have on host Y.

TheSpookyCat commented 1 year ago

✂️ I should specify the game overwrites (not update/add) PlayerPrefs when it fetches the characterlist from the REST API upon clicking play, At the moment if you for example played on host X's server (and set the proper rest api url in the mod config) and then change the config variables to match host Y's rest url/server then your character list will only include the characters you have on host Y.

Good to know, thanks for clarifying

or have the user set the rest server url in configs themselves which would in turn be a rest api hosted by someone that has a community server(s) and you'll only see those.

Imo this would be the best solution to start from and provide a solid backbone for whatever might come after... I'm sure someone at some point will create a public api for discovery if the project gets to that point

But this seems some distant future stuff.

Very distant but I find it's always better to at least consider the end goal while going along

geoffev commented 1 year ago

Very distant but I find it's always better to at least consider the end goal while going along

Agreed best to raise such concerns beforehand😄

geoffev commented 1 year ago

Small update to the PR, Fixed the characterlist endpoint so you can actually write a custom name for a new character, and added the endpoint POST /player/reserveName which is called when saving a character to check if that name is already taken or not.

Most of the character selection screen seems to be fixed with this except Delete Character, need to check what exactly it does in the game as it doesn't seem to call the rest api.

TheSpookyCat commented 1 year ago

If you're still making changes could you set this to draft and @ me when you're done?

Would love to do a review whenever you're ready

geoffev commented 1 year ago

If you're still making changes could you set this to draft and @ me when you're done?

Would love to do a review whenever you're ready

@TheSpookyCat Yes sorry, forgot to set to draft before previous changes. Should be all done now.