python - 如何为多人游戏的赢/输报告组织 MySQL 表

标签 python mysql

我使用 Python 解析来自游戏服务器的纯文本游戏报告,现在我想将输赢信息存储在 MySQL 中,但我已经阅读了几个小时,但找不到好的方法。

纯文本报告如下所示:

name="9" id=#000094
clienttag=W2BN type="melee" option="normal"
created="Tue Jul 21 23:22:26 MSK" started="Tue Jul 21 23:23:02 MSK" ended="Tue Jul 21 23:24:32 MSK"
mapfile="Garden of War.pud" mapauth="Blizzard" mapsize=256x256 tileset="Unknown"
joins=3 maxplayers=8


sh4de            DRAW    
D.StyLez         DRAW    
Player           DRAW    


On map "Garden of War.pud":


sh4de was Orc and played for 1 minute

  Overall Score 1
             1 for Units
             0 for Structures
             0 for Resources

  Units Score 1
             1 Units Produced
             0 Units Killed
             0 Units Lost

  Structures Score 0
             0 Structures Constructed
             0 Structures Razed
             0 Structures Lost

  Resources Score 0
             0 Gold Mined
             0 Lumber Harvested
             0 Oil Harvested
             0 Total Spent
,
On map "Garden of War.pud":

D.StyLez was Orc and played for 1 minute

  Overall Score 2
             1 for Units
             1 for Structures
             0 for Resources

  Units Score 1
             1 Units Produced
             0 Units Killed
             0 Units Lost

  Structures Score 1
             1 Structures Constructed
             0 Structures Razed
             0 Structures Lost

  Resources Score 0
             0 Gold Mined
             0 Lumber Harvested
             0 Oil Harvested
             0 Total Spent
,
On map "Garden of War.pud":

Player was Human and played for 1 minute

  Overall Score 1
             1 for Units
             0 for Structures
             0 for Resources

  Units Score 1
             1 Units Produced
             0 Units Killed
             0 Units Lost

  Structures Score 0
             0 Structures Constructed
             0 Structures Razed
             0 Structures Lost

  Resources Score 0
             0 Gold Mined
             0 Lumber Harvested
             0 Oil Harvested
             0 Total Spent
,

我已经解析了整个事情,我有关于谁赢了谁输了的变量,每个人在哪些类别中得到了多少分,以及报告顶部的所有游戏信息。

但不知道如何将其放入 MySQL

目标是能够进行详细的查询,例如

“选择sh4de是orc的所有游戏并打败d.sytelz”

这是一款 8 人游戏。我唯一能想到的就是能够查询谁赢了或输了我能做的游戏:

“从 game_reports 中选择 * where winner_1 or winner_2 or winner_3 or winner_4 or winner_5 or ....... = sh4de and loser_1 or loser_2 or loser_3 ... = D.stylez”

但我无法知道哪个“winner_#”sh4de 是,所以我无法确定他是兽人还是人类...

此外,如果我这样做(winner_1、winner_2 winner_3),每一行都会很大 - winner 1 必须有 winner_1_overall_score、winner_1_unit_score 等 x8​​ 个获胜者、x8 个失败者、x8 个 DRAW、x8 个 Disc

必须有一个“正确”的方法来做到这一点,我很难过

最佳答案

好吧,这实际上对任何人来说都很难回答,因为我们对您正在做的事情的背景信息知之甚少。我假设您具有 MySQL 的基本知识,所以我将在这里做一个粗略的概述。我只是粗略地勾勒出表格,希望这能给你一个起点。


格式:

Table
-Column [Type] [Relation] [Notes]

您首先可能需要一个玩家表来跟踪所有玩家。如果您不关心在游戏之间跟踪玩家,则可以跳过此步骤。

Player
-ID Int PK AutoIncrement
-Name Varchar(50)
-(Whatever other metadata you want to store about the player)

接下来您需要一个表格来跟踪每场比赛:

Game
-ID Int PK AutoIncrement
-Map Varchar(255)
-Time DateTime

现在您需要在游戏和玩家之间建立多对多关系。为了节省空间,我将把分数包括在这个表格中,但你可能想把它分开。

GamePlayerScore
-GameID FK(Game::ID) PK
-PlayerID FK(Player::ID) PK
-Score Int
-Class varchar(50)

显然,这是一个非常典型的例子,您会想要将事情拆分得更多,但您应该了解基本概念。我不建议存储每场比赛的获胜者,而是通过提取分数在代码中确定获胜者。由于您也有不同的分数类型,您可能需要一个表来保存每个带有“类型”列的分数。

设置表后,您可以使用联接正确聚合数据并执行您正在谈论的那种查询。


编辑:Archer 询问了如何处理玩家得分较高但仍然失败的情况。在不知道如何确定获胜的情况下,我建议将我之前的回答修改为如下内容:

GamePlayerScore
-ID PK AutoIncrement
-PlayerID FK(Player::ID)
-GameID FK(Game::ID)
-Place Int *Number representing how well the player placed*
-PlayerClass

Score
-ID PK AutoIncrement
-GamePlayerScoreID FK(GamePlayerScore::ID)
-Type varchar(50) *Overall, units, structures, etc.*
-Value Int

ScoreStat
-ScoreID FK(Score::ID) PK
-Name varchar(50) *gold mined, lumber harvested, etc.*
-Value Int

这非常快,而且可能会做得更好,但是您可以在深入查看表格时看到事情如何变得更加具体。此外,您没有为每件事单独的表格,而是将所有事物分成相似的类别并使用类型将所有事物分类。

此外,由于您是新加入的,我会推荐 this编写它们时的图像可帮助您确定实际选择的数据。如果您有任何其他问题,我们很乐意提供帮助。

关于python - 如何为多人游戏的赢/输报告组织 MySQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34686454/

相关文章:

php - 如何更新表的特定数据?

Mysql 将现有列添加到另一个表

php - 数据库迁移在播种两个时间戳列 laravel 5 时抛出错误

mysql - 我希望我在 Netbeans 上制作的 Web 应用程序能够上线?

Python `for` 语法 : block code vs single line generator expressions

python - 简单的字符串匹配

python - % 在 Ipython 中的库前签名

python - 如何制作散点图动画

mysql - 如何将两个表内连接到连接表

python - Python 中对象的相等性