通过网络使用观察者模式进行棋盘游戏
我希望用 C++ 制作一款基于 Risk 的网络棋盘游戏。我的想法是拥有一个中央服务器,其中托管一个游戏大厅,用户可以在其中连接并制作/加入游戏。在这种情况下,观察者模式似乎很有吸引力,因为我可以在服务器上托管所有游戏模型/逻辑,而客户端只是观察者并使用视图显示当前游戏状态。
我的第一个问题:这种方法可行吗?我所听到/想到的大部分内容是客户有自己的游戏模型。然而,我认为对于计算量不是很大的游戏,由服务器托管的单一模型将具有优势(没有不同步问题,防止作弊等)。
我的第二个问题:我将如何通过网络实现观察者模式?由于我无法通过网络进行直接方法调用,因此我需要某种简单的方法来使用数据来模拟这种情况。使用“拉”(观察者请求更新游戏数据)或“推”(服务器向所有客户端推送新的更新数据)方法是否会有更多优势?
I am looking to make a networked board game based on Risk in C++. My idea was to have a central server which hosts a game lobby where users can connect and make/join games. The Observer pattern seems attractive in this case, since I could host all the game model/logic on the server, and the clients would just be observers to this and display the current game state using a view.
My first question: Is this approach possible? Most of what I've heard/thought is that the clients have their own game models. However I'm thinking for a game that's not computationally intensive, a single model hosted by a server would have advantages (no out of sync issues, prevents cheating, etc.).
My second question: How would I go about implementing the Observer pattern over a network? Since I can't make a direct method call over the network, I would need some kind of easy way to simulate this using data. Would there be more advantages using a "pull" (observer requests updates to game data) or "push" (server pushes out new updated data to all clients) approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从您的问题陈述来看,您似乎需要实现分布式观察者模式或分布式发布/订阅。 PubSub 是一种消息传递范式,可以使用 MOM(面向消息的中间件)轻松实现 - (请参阅 RabbitMQ、ActiveMQ、OpenMQ),其中 MOM 承担繁重的工作。
XMPP 也可以满足您的目的(请参阅 XEP-0060)。您所需要的只是一个 Jabber 服务器和一个 C++ XMPP 库(gloox 是一个很好的库,并且支持 XEP-0600)。
您可能还对 pubsubhubbub 感兴趣。
From your problem statement, it seem that you need to implement Distributed Observer Pattern or Distributed Publish / Subscribe. PubSub is a messaging paradigm and can be easily implemented with a MOM (Message Oriented Middleware) - ( see RabbitMQ, ActiveMQ, OpenMQ) where the MOM does the heavy lifting.
XMPP can also serve your purpose right(see XEP-0060). All you need is a Jabber server and a C++ XMPP library (gloox is a good one and supports XEP-0600).
You might also be interested in pubsubhubbub.
如果您确实想这样做,请让您的具体观察者也实现代理模式。代理处理发送/接收数据,基本上将本地方法调用转换为远程方法调用。
由于您正在处理异步数据,因此您可能需要查找称为“发布/订阅”的观察者变体。其中的观察是由具体观察者监听事件,然后在需要通信时引发事件。例如,接收数据可能会引发事件。
您还可以研究远程处理,这正是您在这里尝试做的事情。尽管对于您想要做的事情来说,它可能有点太重了。
If you really want to do it this way, make your concrete observers also implement the proxy pattern. The proxy deals with sending/receiving data, basically translating a local method call into a remote method call.
Since you're dealing with asynchronous data, you might want to look up a variation of observer called "publish/subscribe". The observations in that are made by the concrete observers listening for events, and then raising events when they need to communicate. Receipt of data, for example, could raise an event.
You could also look into remoting, which is sort of what you're trying to do here. Though it may be a bit too heavy weight for what you're trying to do.
您似乎将观察者模式用于双重目的。您的游戏客户端将“观察”服务器,但这还不是全部。他们还将玩家信息传输回服务器。所以他们所做的不仅仅是观察。也许只使用基本的客户端/服务器范例,而不用担心“设计模式”。或者,如果您想要一种设计模式方法,可以看看中介者模式,其中服务器是中介者。就我个人而言,我会坚持使用客户端/服务器方法。
抱歉,我知道这并不能回答您的具体问题,但仅供思考。
It seems that you're using the Observer pattern for a dual purpose. You're game clients will be "observing" the server, but that's not all. they'll also be transmitting player information back to the server. So they're really doing more than observing. Maybe just use the basic client/server paradigm and don't worry about the "design pattern". Or, if you want a design patterned approach maybe look at the mediator pattern where the server is the mediator. Personally, I'd stick with client/server approach.
Sorry, I know this doesn't answer you're specific questions, but just take it as food for thought.
对于你的第一个问题:是的,这是每个客户端-服务器游戏都使用的模型。
对于你的第二个问题:“推”方法在定时同步和带宽方面更好。使用RPC库(远程过程调用)来模拟函数调用。如果你使用 C++,我推荐 Raknet。如果您使用 Java,我推荐 Jnag 或 proto-buffers。
For your first question: Yes, it is a model used by every client-server games.
For your second question: The "push" approach is better in terms of timed synchronization and bandwitch. Use a RPC library (Remote Procedure Call) to simulate the function calls. If you are using C++, I recommend Raknet. If you are using Java, I recommend Jnag or the proto-buffers.