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

Clients visibility #174

Merged
merged 48 commits into from
Jan 15, 2024
Merged

Clients visibility #174

merged 48 commits into from
Jan 15, 2024

Conversation

Shatur
Copy link
Contributor

@Shatur Shatur commented Jan 13, 2024

Description

It's a prototype of my recent idea about visibility management. Please note, that it's a quick and dirty prototype, I just wanted to showcase the idea.

The idea is simple. When you configure server plugin, you set visibility policy based on your game. Then you can control visibility using ClientVisibility struct obtainable from ClientInfo. ClientVisibility is an enum with fields based on policy:

  • ClientVisibility::All: stores only single boolean to check if client just connected.
  • ClientVisibility::Whitelist / ClientVisibility::Blacklist:
    • A list of entities with a boolean value for each one if it was just added.
    • A set of removed entities from the list above.

When we collect changes, we also check if an entity visible or just became visible for specific client (can be optimized, see below).
When we collect removals, we additionally iterate over just hidden entities.

TODO:

  • Discuss the design itself.
  • Optimizations.
  • More docs.
  • Tests.

Advantages

  • No performance cost if game doesn't need visibility management at all.
  • Lower cost compared to Rooms #15 if game need one room per entity.
  • Extensible, more high level API like Rooms #15 can be implemented on top of it.

Disadvantages

  • If your game benefit from managing visibility for group of entities, the API will fill more barebones compared to the proposed rooms API.

Optimizations

  • I create a boxed iterator for despawns. Can we use different API to avoid this allocation?
  • I do an entity lookup for each component. Can I cache the lookup using an Option? Or maybe restructure the loop? Not specific to the idea itself, same would be about rooms API.
  • For blacklist option I iterate over each entity. Maybe I can use a separate storage for just added entities? But it will add an extra lookup for getting visibility (to check if an entity was just added or not).

@Shatur Shatur requested a review from UkoeHB January 13, 2024 16:15
Copy link
Collaborator

@UkoeHB UkoeHB left a comment

Choose a reason for hiding this comment

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

Efficiency and code clarity comments. I think this direction is promising, I will work on a Rooms design that can be layered on top.

src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server.rs Outdated Show resolved Hide resolved
src/server.rs Outdated Show resolved Hide resolved
src/server/clients_info.rs Outdated Show resolved Hide resolved
src/server.rs Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Jan 14, 2024

Codecov Report

Attention: 25 lines in your changes are missing coverage. Please review.

Comparison is base (316dc3c) 89.81% compared to head (6021346) 89.30%.

Files Patch % Lines
src/server/clients_info/client_visibility.rs 85.49% 19 Missing ⚠️
src/server/clients_info.rs 82.85% 6 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #174      +/-   ##
==========================================
- Coverage   89.81%   89.30%   -0.51%     
==========================================
  Files          19       20       +1     
  Lines        1247     1412     +165     
==========================================
+ Hits         1120     1261     +141     
- Misses        127      151      +24     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

}
}

/// Returns reference to connected client info.
Copy link
Contributor Author

@Shatur Shatur Jan 14, 2024

Choose a reason for hiding this comment

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

I feel like we should rename it into ClientState and the parent struct into ClientsState.
But I think it's better to do as part of a separate PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe: ClientCache for the parent struct, then ClientState for the per-client struct. ClientsState is pretty awkward.

src/server/clients_info.rs Outdated Show resolved Hide resolved
@Shatur Shatur marked this pull request as ready for review January 14, 2024 19:58
@Shatur Shatur requested a review from UkoeHB January 14, 2024 19:58
Split into simpler tests.
Copy link
Collaborator

@UkoeHB UkoeHB left a comment

Choose a reason for hiding this comment

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

It took me a while to figure out you were handling the 'lost visibility' vs 'don't have visibility' distinction separate from EntityState.

src/server.rs Outdated Show resolved Hide resolved
src/server.rs Outdated Show resolved Hide resolved
src/server/clients_info.rs Outdated Show resolved Hide resolved
src/server/clients_info.rs Show resolved Hide resolved
src/server/clients_info.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info.rs Outdated Show resolved Hide resolved
src/server.rs Outdated Show resolved Hide resolved
Shatur and others added 2 commits January 15, 2024 01:15
Co-authored-by: UkoeHB <37489173+UkoeHB@users.noreply.github.com>
Co-authored-by: UkoeHB <37489173+UkoeHB@users.noreply.github.com>
@Shatur Shatur linked an issue Jan 15, 2024 that may be closed by this pull request
Copy link
Collaborator

@UkoeHB UkoeHB left a comment

Choose a reason for hiding this comment

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

Making progress :)

On the other hand, I have not much progress on the Rooms abstraction if you want to give it a try. I think it can be done with one custom SystemParam (instead of an entity component) called RepliconRooms with this core API:

  • rooms.join(client id, room id)
  • rooms.leave(client id, room id)
  • rooms.add_entity(entity, room id)
  • rooms.remove_entity(entity, room id)

I'm not sure how to integrate it with events. Maybe a bespoke solution is required (a distinct events API that wraps bevy_replicon, like RoomWriter or some such).

src/server/clients_info.rs Outdated Show resolved Hide resolved
src/server/clients_info.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@UkoeHB UkoeHB left a comment

Choose a reason for hiding this comment

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

Getting closer :)

src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
src/server/clients_info/client_visibility.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@UkoeHB UkoeHB left a comment

Choose a reason for hiding this comment

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

Good work! I think it's ok to merge this, but let's wait to release it until a Rooms implementation is solidified.

@Shatur
Copy link
Contributor Author

Shatur commented Jan 15, 2024

Okay!

@Shatur Shatur merged commit 7bb0bde into master Jan 15, 2024
4 checks passed
@Shatur Shatur deleted the clients-visibility branch January 15, 2024 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rooms
3 participants