public static void SaveQuest(Quest q) { if (q.New) { // New quest, add it to the database now q.ID = (uint)AddQuest(q.Name, q.GiverID, q.GiverMapID); } else { // Update info ExecuteQuery(string.Format("UPDATE quest_info SET giver_id={0},giver_map_id={1} WHERE quest_id={2};", q.GiverID, q.GiverMapID, q.ID)); // Update name ExecuteQuery(string.Format("UPDATE quest_names SET name=\"{0}\" WHERE quest_id={1};", q.Name, q.ID)); } // Delete all the rewards for this quest ExecuteQuery(string.Format("DELETE FROM quest_rewards WHERE quest_id={0}", q.ID)); // Finalize step order for (int i = 0; i < q.Steps.Count; i++) { QuestStep qs = q.Steps[i]; string sql; if (qs.New) { sql = string.Format("INSERT INTO quest_steps SET step={0},type={1},count={2},target_id={3},owner_id={4},quest_id={5}; SELECT LAST_INSERT_ID();", i, (byte)qs.CompType, qs.CompCount, qs.CompTargetID, qs.OwnerID, q.ID); } else { sql = string.Format("UPDATE quest_steps SET step={0},type={1},count={2},target_id={3},owner_id={4} WHERE quest_step_id={5};", i, (byte)qs.CompType, qs.CompCount, qs.CompTargetID, qs.OwnerID, qs.ID); } List <object[]> rows = ExecuteQuery(sql); if (rows.Count > 0) { ulong id = (ulong)rows[0][0]; qs.ID = (uint)id; } qs.New = false; // Do Rewards foreach (QuestReward qr in qs.Rewards) { sql = string.Format("INSERT INTO quest_rewards SET quest_id={0},step={1},type={2},context={3};", q.ID, i, (byte)qr.Type, qr.Context); ExecuteQuery(sql); } // Do Lines for (int j = 0; j < qs.Lines.Count; j++) { QuestLine ql = qs.Lines[j]; ql.Line = (byte)j; if (ql.New) { sql = string.Format("INSERT INTO quest_lines SET quest_id={0},step={1},line={2},icon={3},static_text={4},text=\"{5}\"; SELECT LAST_INSERT_ID();", q.ID, i, ql.Line, ql.Icon, ql.StaticText, ql.DynamicText); } else { sql = string.Format("UPDATE quest_lines SET step={1},line={2},icon={3},static_text={4},text=\"{5}\" WHERE quest_line_id={0};", ql.ID, i, ql.Line, ql.Icon, ql.StaticText, ql.DynamicText); } rows = ExecuteQuery(sql); if (rows.Count > 0) { ulong id = (ulong)rows[0][0]; ql.ID = (uint)id; } ql.New = false; } } // Kill any steps marked for deletion foreach (QuestStep qs in q.DeletedSteps) { if (!qs.New) { string sql = string.Format("DELETE FROM quest_steps WHERE quest_step_id={0};", qs.ID); ExecuteQuery(sql); } } // Save requirements foreach (QuestRequirement qr in q.Requirements) { string sql; if (qr.New) { sql = string.Format("INSERT INTO quest_requirements SET quest_id={0},type={1},param={2}; SELECT LAST_INSERT_ID();", q.ID, qr.TheType, qr.Context); } else { sql = string.Format("UPDATE quest_requirements SET type={0},param={1} WHERE quest_requirement_id={2};", qr.TheType, qr.Context, qr.ID); } List <object[]> rows = ExecuteQuery(sql); if (rows.Count > 0) { ulong id = (ulong)rows[0][0]; qr.ID = (uint)id; } qr.New = false; } // Kill any requirements marked for deletion foreach (QuestRequirement qr in q.DeletedReqs) { if (!qr.New) { string sql = string.Format("DELETE FROM quest_requirements WHERE quest_requirement_id={0};", qr.ID); ExecuteQuery(sql); } } // Clear dirty flag q.Dirty = false; }
static public Quest[] FetchQuests() { List <Quest> quests = new List <Quest>(); // Fetch Quests List <object[]> rows = ExecuteQuery("SELECT * FROM quest_info;"); foreach (object[] row in rows) { Quest q = new Quest((uint)row[0], (uint)row[1], (ushort)row[2]); quests.Add(q); } // Go get subquest data foreach (Quest q in quests) { // Fetch Name string sql = string.Format("SELECT * FROM quest_names WHERE quest_id={0};", q.ID); rows = ExecuteQuery(sql); if (rows.Count > 0) { q.Name = (string)rows[0][1]; } // Fetch Requirements sql = string.Format("SELECT * FROM quest_requirements WHERE quest_id={0};", q.ID); rows = ExecuteQuery(sql); foreach (object[] row in rows) { uint id = (uint)row[0]; byte type = (byte)row[2]; QuestRequirement qr = new QuestRequirement(id, (QuestRequirement.Type)type, (uint)row[3]); q.Requirements.Add(qr); } // Fetch Steps sql = string.Format("SELECT * FROM quest_steps WHERE quest_id={0};", q.ID); rows = ExecuteQuery(sql); foreach (object[] row in rows) { byte type = (byte)row[2]; QuestStep qs = new QuestStep((byte)row[1], (QuestStep.CompletionType)type, (uint)row[3], (uint)row[4], (uint)row[5], (uint)row[6]); q.Steps.Add(qs); } // Process Steps foreach (QuestStep qs in q.Steps) { // Fetch Rewards sql = string.Format("SELECT * FROM quest_rewards WHERE quest_id={0} AND step={1};", q.ID, qs.Step); rows = ExecuteQuery(sql); foreach (object[] row in rows) { // 0: quest_id int(10) unsigned // 1: step tinyint(3) unsigned // 2: type tinyint(10) unsigned // 3: context int(10) unsigned QuestReward qr = new QuestReward((byte)row[2], (uint)row[3]); qs.Rewards.Add(qr); } // Fetch Lines sql = string.Format("SELECT * FROM quest_lines WHERE quest_id={0} AND step={1};", q.ID, qs.Step); rows = ExecuteQuery(sql); foreach (object[] row in rows) { string text = null; if (row[6].GetType() != typeof(DBNull)) { text = (string)row[6]; } QuestLine ql = new QuestLine((byte)row[3], (ushort)row[4], (ushort)row[5], text, (uint)row[0]); qs.Lines.Add(ql); } // Order Lines qs.OrderLines(); } // Order Steps q.OrderSteps(); } return(quests.ToArray()); }