RPG任务 - 查询玩家数据

时间:2017-02-20 20:07:46

标签: mysql sql database storage

给定以下表结构,其中每个包含一个存储有关RPG中任务的部分。我试图找出查询每位玩家当前数据的最佳方式。

有关任务结构的一些信息,它是一个3层次的顺序,每层有许多任务,在一个任务中,每个NPC有很多任务需要完成,但按时间顺序排列。也如图所示:

enter image description here

我目前使用MySQL设计的布局如下:

CREATE TABLE NPCs (
    ID int AUTO_INCREMENT,
    Name varchar(50),
    RoomID int,
    X int(2),
    Y int(2),
    TypeID int,
    ImageID int,
    PRIMARY KEY(ID),
    FOREIGN KEY (ImageID) REFERENCES Images(ID) 
        ON UPDATE CASCADE,
    FOREIGN KEY (TypeID) REFERENCES NPCTypes(ID) 
        ON UPDATE CASCADE,
    FOREIGN KEY (RoomID) REFERENCES Rooms(ID) 
        ON UPDATE CASCADE
);

CREATE TABLE Quests (
    ID int AUTO_INCREMENT,
    QuestNumber int,
    Name varchar(50),
    NPCID int,
    RewardXP int(3)
    RewardMoney int(3),
    LevelID int,
    Text varchar(100),
    CompletedText varchar(100),
    PRIMARY KEY(ID),
    FOREIGN KEY (LevelID) REFERENCES Levels(ID) 
        ON UPDATE CASCADE,
    FOREIGN KEY (NPCID) REFERENCES NPCs(ID) 
        ON UPDATE CASCADE
);

CREATE TABLE QuestStages (
    ID int AUTO_INCREMENT,
    UserID int,
    QuestID int,
    Accepted bool,
    Complete bool,
    PRIMARY KEY(ID),
    FOREIGN KEY (UserID) REFERENCES Users(ID) 
        ON UPDATE CASCADE,
    FOREIGN KEY (QuestID) REFERENCES Quests(ID) 
        ON UPDATE CASCADE
);

CREATE TABLE LinkedQuests (
    ID int AUTO_INCREMENT,
    QuestID int,
    PreQuestID int,
    PRIMARY KEY(ID),
    FOREIGN KEY (QuestID) REFERENCES Quests(ID) 
        ON UPDATE CASCADE,
    FOREIGN KEY (PreQuestID) REFERENCES Quests(ID) 
        ON UPDATE CASCADE    
);

每个任务都从一个种子任务开始,你按时间顺序完成这些任务,但需要一种简单的方法来查询数据。

由于玩家可以在给定时间按任何NPC启动,暂停和恢复任务,因此我试图找出以各种格式查询数据的最佳方式:

使用UserID完成任务任务的列表(Quests.ID,Quests.Accepted,Quests.Name,Quests.Text,Quests.RoomID,Quests.RewardMoney,Quests.RewardXP)

使用ID选择Quest的详细信息(Quests.ID,Quests.Name,Quest.Text / Quest.CompletedText //基于完成与否// Quests.RewardMoney,Quests.RewardXP)

使用RoomID(Quests.ID,Quests.Accepted,NPCs.ID AS NPCID,NPCs.X,NPCs.Y)在会议室和搭建的NPC中进行任务的详细信息

编辑这是一个例子

Quests 
ID | QuestNumber | Name     | NPCID  | RewardXP  | RewardMoney |  LevelID | Text       | CompletedText
1  |  1          | "Quest1" |  1     |   10      |   10        |  1       |  "do this" | "Well Done"
2  |  2          | "Quest2" |  1     |   10      |   10        |  1       |  "do this" | "Well Done"
3  |  3          | "Quest3" |  2     |   10      |   10        |  1       |  "do this" | "Well Done"
4  |  4          | "Quest4" |  2     |   10      |   10        |  1       |  "do this" | "Well Done"
5  |  5          | "Quest5" |  2     |   10      |   10        |  1       |  "do this" | "Well Done"
6  |  6          | "Quest6" |  3     |   10      |   10        |  1       |  "do this" | "Well Done"

NPCs
ID   |  RoomID 
1    |  1
2    |  1
3    |  2

QuestStages 
ID| UserID | QuestID | Accepted | Complete
1 | 1      | 2       | TRUE     | FALSE
2 | 1      | 3       | TRUE     | TRUE
3 | 1      | 4       | FALSE    | FALSE
4 | 1      | 5       | TRUE     | TRUE
5 | 1      | 6       | TRUE     | FALSE

LinkedQuests
QuestID | PreQuestID
2       |  1
5       |  4
4       |  3

显示User.ID 1 1

的任务列表
Quests.ID| Quests.Accepted| Quests.Name| Quests.Text| Quests.RoomID| Quests.RewardMoney | Quests.RewardXP
3        | TRUE           | "Quest3"   |  "do this" | 1            | ... 

// will not show Quest 2 as is completed
// will not show Quest 4 as it is dependent on quest 3 being completed

1 个答案:

答案 0 :(得分:0)

DEMO 您可以在此处查看到达最终查询的中间步骤。

-- Final Output, Join to get quest details
SELECT QS.`QuestID`, QS.`Accepted`, Q.`Name`, Q.`Text`, Q.`RewardMoney`
FROM QuestStages QS
LEFT JOIN LinkedQuests LQ
  ON QS.`QuestID` = LQ.`QuestID`
LEFT JOIN QuestStages Pre
  ON QS.`UserID` = Pre.`UserID`
 AND LQ.`PreQuestID` = Pre.`QuestID`
JOIN Quests Q
  ON QS.`QuestID` = Q.`QuestNumber`
WHERE QS.`UserID` = 1
  AND QS.`Complete` = 'FALSE'
  AND (Pre.`Complete` = 'TRUE' OR Pre.`Complete` IS NULL)  
  -- Prerequirement is complete or doesnt have Prerequirement
ORDER BY QS.`QuestID`
  ;

OUTPUT ,我为调试做了三个步骤。

enter image description here