I am bogged down by this for a while and I can't figure out a proper way to do it. In a multiplayer game, a player should be able to see other players' visible states on the same screen/ same zone. For example, my player should see other players' state change when they change size or color or collide with some entities if these players are in my player's viewport. The same applies to other players, so they can see what my player is doing, seamlessly.
I have designed a architecture that if each player has some collection called "nearbyPlayers", each player can iterate through them one by one and get the state changes and then send them to the current player. But on the server because these players are at the same zone some state changes will be iterated many times. Assume A, B, C and D are on the same viewport, to get the environmental screen state changes for A, I need to iterate through collection = [A, B, C, D]. And for B, C and D I need to iterate through the same collection. I think this way my structure has repeatedly computed some states many times, which wasted a lot of CPU cycles. Maybe there is a term to describe the problem but I don't know.
I have also tried to design a pub-sub pattern, so that if players are on the same screen/same zone, each of them are subscribed to each other and if one player changes state the others should know the change. But still, the player need to tell all the subscribers the change. And in a simple implementation of this pattern I didn't find much performance improved.
In both ways I find it easy to make mistakes such as detecting collisions with the same entity multiple times. Is there a generic or standard way to solve this problem?