简体   繁体   中英

Multiplayer game architecture in node.js - State management and synchronization with Redux

I'm implementing an online TCG game (think Hearthstone/Magic) as a pet project and I've decided to go React/Redux for the client UI. This led me to think about the possibility of using Redux on the server as well to maintain the global state for each client/game.

The way it would work, is players in a game would send events (via socket.io) to the server which would be validated according to the game state+rules, which would then trigger a change in the server state and propagate it to the respective clients.

The thing is, I feel like I'm gonna have to jump through a lot of hoops to get all the game rules/logic to conform to the more functional/immutable approach of Redux instead of modeling all my game entities as classes. Also, since each player has limited information about their opponents (you can't see the other player's hand for example), I'll have to have some custom logic for filtering the state information depending on which player is receiving the data.

On the other hand, I like the idea of having a centralized state split across different handlers for each domain, predictable data flow, and possibility to regress to prior states. Also the actions in Redux fit neatly into the command pattern which is useful for these kinds of games.

Should I just implement a custom Redux like state manager but drop the immutability part? Is there a more "tried and true" approach for these kinds of situations?

Look at event-sourcing.

Events from UI will affect different read-models - that is case you are talking about. You even can use some simple event sourcing framework for backend, since the main logic is based on events.

I always suggest reimagined/resolve framework as very simle and compact one to understand cqrs+es main concepts in examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM