- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 python 实现一个游戏服务器。 GameServer 类包含多个游戏实例,每个实例包含多个玩家。我正在尝试找出最适合使用的数据结构。有一个函数接收所有传入数据,它需要在游戏中查找玩家并更新信息。
目前,GameServer有一组GameInstance,而GameInstance有一组玩家。这需要我迭代每个游戏和玩家以找到正确的游戏,而且我认为这不是最好的方法,因为它必须每秒运行数百次。
传入数据具有连接(从中接收数据)和消息。这意味着我为类(class)中的每个玩家存储一个连接,以便我可以将消息发送回特定玩家。我无法保存每个玩家连接的字典,因为它们必须按游戏实例分组。请帮助我了解构建此结构的最有效方法。
最佳答案
Currently, GameServer has a set of GameInstances, and GameInstance has a set of players. This requires me to iterate through every game and player to find the correct game, and I don't think this is the best way to do it, because it will have to be run hundreds of times per second.
你说得对!虽然我会回答你的具体问题,但你应该做的是阅读有关数据结构的内容。每个工作程序员至少对最常见的数据结构及其性能特征有基本的了解是至关重要的。
根据您对问题的描述,您需要在标识每个游戏的键与描述它的对象之间维护一个映射(可能使用哈希表)。
关于python - 如何用 Python 构建游戏实例数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23598452/
我是一名优秀的程序员,十分优秀!