| 1 | = Advanced Database Reports (SQL Queries and Stored Procedures) = |
| 2 | |
| 3 | This page includes advanced SQL queries and stored procedures for generating reports from the *Legends of the Balkans* game database. The queries are based on the following tables: |
| 4 | |
| 5 | == 1. Player Progress Report == |
| 6 | This query provides a summary of each player's progress—including their character details and any associated faction reputation. |
| 7 | |
| 8 | {{{ |
| 9 | SELECT p.PlayerID, |
| 10 | p.Username, |
| 11 | c.Name AS CharacterName, |
| 12 | c.Health, |
| 13 | c.EXP, |
| 14 | c.Gold, |
| 15 | f.Name AS FactionName, |
| 16 | r.ReputationPoints, |
| 17 | r.Rank |
| 18 | FROM Player p |
| 19 | JOIN Character c ON p.PlayerID = c.PlayerID |
| 20 | LEFT JOIN Reputation r ON c.CharacterID = r.CharacterID |
| 21 | LEFT JOIN Faction f ON r.FactionID = f.FactionID; |
| 22 | }}} |
| 23 | |
| 24 | == 2. Faction Reputation Update After Quest Completion == |
| 25 | The following stored procedure updates a character’s reputation for a given faction based on the EXP reward from a completed quest. |
| 26 | |
| 27 | {{{ |
| 28 | CREATE OR REPLACE FUNCTION UpdateFactionReputation( |
| 29 | char_id INT, |
| 30 | quest_id INT, |
| 31 | faction_id INT |
| 32 | ) |
| 33 | RETURNS VOID AS |
| 34 | $$ |
| 35 | DECLARE |
| 36 | reputation_change INT; |
| 37 | BEGIN |
| 38 | -- Get the EXP reward from the quest |
| 39 | SELECT EXP_Reward INTO reputation_change |
| 40 | FROM Quest |
| 41 | WHERE QuestID = quest_id; |
| 42 | |
| 43 | -- Update the Reputation record for the character and faction |
| 44 | UPDATE Reputation |
| 45 | SET ReputationPoints = ReputationPoints + reputation_change |
| 46 | WHERE CharacterID = char_id AND FactionID = faction_id; |
| 47 | END; |
| 48 | $$ LANGUAGE plpgsql; |
| 49 | }}} |
| 50 | |
| 51 | == 3. Completed Quests Report for a Specific Player == |
| 52 | This query retrieves the list of quests completed by a specific player. (Note: The join table *Character_Quest* contains only the CharacterID and QuestID.) |
| 53 | |
| 54 | {{{ |
| 55 | SELECT p.Username, |
| 56 | q.Name AS QuestName |
| 57 | FROM Player p |
| 58 | JOIN Character c ON p.PlayerID = c.PlayerID |
| 59 | JOIN Character_Quest cq ON c.CharacterID = cq.CharacterID |
| 60 | JOIN Quest q ON cq.QuestID = q.QuestID |
| 61 | WHERE p.PlayerID = ?; -- Replace ? with the specific PlayerID |
| 62 | }}} |
| 63 | |
| 64 | == 4. List of Players in a Specific Faction == |
| 65 | This query lists all players whose characters have a reputation record with a specific faction. Replace the faction name as needed. |
| 66 | |
| 67 | {{{ |
| 68 | SELECT DISTINCT p.PlayerID, |
| 69 | p.Username, |
| 70 | c.Name AS CharacterName |
| 71 | FROM Player p |
| 72 | JOIN Character c ON p.PlayerID = c.PlayerID |
| 73 | JOIN Reputation r ON c.CharacterID = r.CharacterID |
| 74 | JOIN Faction f ON r.FactionID = f.FactionID |
| 75 | WHERE f.Name = 'Kingdom of Zeta'; -- Replace with the desired faction name |
| 76 | }}} |
| 77 | |
| 78 | == 5. Market Listings for a Specific Player == |
| 79 | This query retrieves all items listed on the market by a specific player. |
| 80 | |
| 81 | {{{ |
| 82 | SELECT m.MarketID, |
| 83 | i.Name AS ItemName, |
| 84 | m.Price |
| 85 | FROM Market m |
| 86 | JOIN Item i ON m.ItemID = i.ItemID |
| 87 | WHERE m.SellerID = ?; -- Replace ? with the specific Seller's PlayerID |
| 88 | }}} |
| 89 | |
| 90 | == 6. Top Players by Total Experience and Completed Quests == |
| 91 | This query provides a list of top players, ordered by the total EXP of all their characters and the number of quests they have completed. |
| 92 | |
| 93 | {{{ |
| 94 | SELECT p.PlayerID, |
| 95 | p.Username, |
| 96 | SUM(c.EXP) AS TotalEXP, |
| 97 | COUNT(cq.QuestID) AS CompletedQuests |
| 98 | FROM Player p |
| 99 | JOIN Character c ON p.PlayerID = c.PlayerID |
| 100 | LEFT JOIN Character_Quest cq ON c.CharacterID = cq.CharacterID |
| 101 | GROUP BY p.PlayerID, p.Username |
| 102 | ORDER BY TotalEXP DESC, CompletedQuests DESC; |
| 103 | }}} |
| 104 | |
| 105 | == 7. Items Owned by a Specific Player == |
| 106 | This query lists the items owned by a specific player's characters. |
| 107 | |
| 108 | {{{ |
| 109 | SELECT p.Username, |
| 110 | i.Name AS ItemName |
| 111 | FROM Player p |
| 112 | JOIN Character c ON p.PlayerID = c.PlayerID |
| 113 | JOIN Character_Item ci ON c.CharacterID = ci.CharacterID |
| 114 | JOIN Item i ON ci.ItemID = i.ItemID |
| 115 | WHERE p.PlayerID = ?; -- Replace ? with the specific PlayerID |
| 116 | }}} |
| 117 | |
| 118 | == 8. Items Listed in the Market == |
| 119 | This query retrieves all items currently listed on the market along with seller information. |
| 120 | |
| 121 | {{{ |
| 122 | SELECT m.MarketID, |
| 123 | i.Name AS ItemName, |
| 124 | m.Price, |
| 125 | p.Username AS Seller |
| 126 | FROM Market m |
| 127 | JOIN Item i ON m.ItemID = i.ItemID |
| 128 | JOIN Player p ON m.SellerID = p.PlayerID; |
| 129 | }}} |
| 130 | |
| 131 | == 9. List of Guilds and Their Leaders == |
| 132 | This query lists all guilds along with the username of their leader. |
| 133 | |
| 134 | {{{ |
| 135 | SELECT g.GuildID, |
| 136 | g.Name AS GuildName, |
| 137 | p.Username AS LeaderName |
| 138 | FROM Guild g |
| 139 | JOIN Player p ON g.LeaderID = p.PlayerID; |
| 140 | }}} |
| 141 | |
| 142 | == 10. Characters in Guilds == |
| 143 | This query retrieves characters that belong to guilds, along with their guild name. |
| 144 | |
| 145 | {{{ |
| 146 | SELECT p.Username, |
| 147 | c.Name AS CharacterName, |
| 148 | g.Name AS GuildName |
| 149 | FROM Character_Guild cg |
| 150 | JOIN Character c ON cg.CharacterID = c.CharacterID |
| 151 | JOIN Player p ON c.PlayerID = p.PlayerID |
| 152 | JOIN Guild g ON cg.GuildID = g.GuildID; |
| 153 | }}} |