Skip to content

Commit

Permalink
Merge pull request #629 from RyanYappert/fix/kill_transaction
Browse files Browse the repository at this point in the history
Fix: InstanceEnemyKillHandler transaction and packet queueing
  • Loading branch information
RyanYappert authored Nov 9, 2024
2 parents ff40009 + af0b76b commit 7f665da
Show file tree
Hide file tree
Showing 26 changed files with 707 additions and 646 deletions.
53 changes: 23 additions & 30 deletions Arrowgene.Ddon.Database/IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ CDataPawnSearchParameter searchParams

// CharacterJobData
bool ReplaceCharacterJobData(uint commonId, CDataCharacterJobData replacedCharacterJobData, DbConnection? connectionIn = null);
bool UpdateCharacterJobData(uint commonId, CDataCharacterJobData updatedCharacterJobData);
bool UpdateCharacterJobData(uint commonId, CDataCharacterJobData updatedCharacterJobData, DbConnection? connectionIn = null);

// Wallet Points
bool InsertWalletPoint(uint characterId, CDataWalletPoint walletPoint);
Expand Down Expand Up @@ -414,37 +414,39 @@ uint excludedCharacterId
);

// Rewards
bool InsertBoxRewardItems(uint commonId, QuestBoxRewards rewards);
bool DeleteBoxRewardItem(uint commonId, uint uniqId);
List<QuestBoxRewards> SelectBoxRewardItems(uint commonId);
bool InsertBoxRewardItems(uint commonId, QuestBoxRewards rewards, DbConnection? connectionIn = null);
bool DeleteBoxRewardItem(uint commonId, uint uniqId, DbConnection? connectionIn = null);
List<QuestBoxRewards> SelectBoxRewardItems(uint commonId, DbConnection? connectionIn = null);

// Completed Quests
List<CompletedQuest> GetCompletedQuestsByType(uint characterCommonId, QuestType questType);
CompletedQuest GetCompletedQuestsById(uint characterCommonId, QuestId questId);
List<CompletedQuest> GetCompletedQuestsByType(uint characterCommonId, QuestType questType, DbConnection? connectionIn = null);
CompletedQuest GetCompletedQuestsById(uint characterCommonId, QuestId questId, DbConnection? connectionIn = null);
bool InsertIfNotExistCompletedQuest(
uint characterCommonId,
QuestId questId,
QuestType questType
QuestType questType,
DbConnection? connectionIn = null
);

bool ReplaceCompletedQuest(
uint characterCommonId,
QuestId questId,
QuestType questType,
uint count = 1
uint count = 1,
DbConnection? connectionIn = null
);

// Quest Progress
bool InsertQuestProgress(uint characterCommonId, uint questScheduleId, QuestType questType, uint step);
bool UpdateQuestProgress(uint characterCommonId, uint questScheduleId, QuestType questType, uint step);
bool RemoveQuestProgress(uint characterCommonId, uint questScheduleId, QuestType questType);
List<QuestProgress> GetQuestProgressByType(uint characterCommonId, QuestType questType);
QuestProgress GetQuestProgressByScheduleId(uint characterCommonId, uint questScheduleId);
bool InsertQuestProgress(uint characterCommonId, uint questScheduleId, QuestType questType, uint step, DbConnection? connectionIn = null);
bool UpdateQuestProgress(uint characterCommonId, uint questScheduleId, QuestType questType, uint step, DbConnection? connectionIn = null);
bool RemoveQuestProgress(uint characterCommonId, uint questScheduleId, QuestType questType, DbConnection? connectionIn = null);
List<QuestProgress> GetQuestProgressByType(uint characterCommonId, QuestType questType, DbConnection? connectionIn = null);
QuestProgress GetQuestProgressByScheduleId(uint characterCommonId, uint questScheduleId, DbConnection? connectionIn = null);

// Quest Priority
bool InsertPriorityQuest(uint characterCommonId, uint questScheduleId);
List<uint> GetPriorityQuestScheduleIds(uint characterCommonId);
bool DeletePriorityQuest(uint characterCommonId, uint questScheduleId);
bool InsertPriorityQuest(uint characterCommonId, uint questScheduleId, DbConnection? connectionIn = null);
List<uint> GetPriorityQuestScheduleIds(uint characterCommonId, DbConnection? connectionIn = null);
bool DeletePriorityQuest(uint characterCommonId, uint questScheduleId, DbConnection? connectionIn = null);

// System mail
long InsertSystemMailAttachment(SystemMailAttachment attachment);
Expand Down Expand Up @@ -495,9 +497,10 @@ ushort addStat2
// Play points
bool ReplaceCharacterPlayPointData(
uint id,
CDataJobPlayPoint updatedCharacterPlayPointData
CDataJobPlayPoint updatedCharacterPlayPointData,
DbConnection? connectionIn = null
);
bool UpdateCharacterPlayPointData(uint id, CDataJobPlayPoint updatedCharacterPlayPointData);
bool UpdateCharacterPlayPointData(uint id, CDataJobPlayPoint updatedCharacterPlayPointData, DbConnection? connectionIn = null);

// Stamps
public bool InsertCharacterStampData(uint id, CharacterStampBonus stampData);
Expand Down Expand Up @@ -535,23 +538,13 @@ bool InsertBBMProgress(
bool killedDeath,
ulong lastTicketTime
);
bool UpdateBBMProgress(
uint characterId,
ulong startTime,
uint contentId,
BattleContentMode contentMode,
uint tier,
bool killedDeath,
ulong lastTicketTime
);
bool UpdateBBMProgress(uint characterId, BitterblackMazeProgress progress);
bool UpdateBBMProgress(uint characterId, BitterblackMazeProgress progress, DbConnection? connectionIn = null);
BitterblackMazeProgress SelectBBMProgress(uint characterId);
bool RemoveBBMProgress(uint characterId);

// Bitterblack Maze Rewards
bool InsertBBMRewards(uint characterId, uint goldMarks, uint silverMarks, uint redMarks);
bool UpdateBBMRewards(uint characterId, uint goldMarks, uint silverMarks, uint redMarks);
bool UpdateBBMRewards(uint characterId, BitterblackMazeRewards rewards);
bool UpdateBBMRewards(uint characterId, BitterblackMazeRewards rewards, DbConnection? connectionIn = null);
bool RemoveBBMRewards(uint characterId);
BitterblackMazeRewards SelectBBMRewards(uint characterId);

Expand Down
95 changes: 37 additions & 58 deletions Arrowgene.Ddon.Database/Sql/Core/DdonSqlCompletedQuests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,12 @@ public abstract partial class DdonSqlDb<TCon, TCom, TReader> : SqlDb<TCon, TCom,
private readonly string SqlSelectCompletedQuestById = $"SELECT {BuildQueryField(CompletedQuestsFields)} FROM \"ddon_completed_quests\" WHERE \"character_common_id\" = @character_common_id AND \"quest_id\" = @quest_id;";
private readonly string SqlUpdateCompletedQuestId = $"UPDATE \"ddon_completed_quests\" SET \"clear_count\" = @clear_count WHERE \"character_common_id\" = @character_common_id AND \"quest_id\" = @quest_id;";

public List<CompletedQuest> GetCompletedQuestsByType(uint characterCommonId, QuestType questType)
public List<CompletedQuest> GetCompletedQuestsByType(uint characterCommonId, QuestType questType, DbConnection? connectionIn = null)
{
using TCon connection = OpenNewConnection();
return GetCompletedQuestsByType(connection, characterCommonId, questType);
}

public List<CompletedQuest> GetCompletedQuestsByType(TCon connection, uint characterCommonId, QuestType questType)
{
List<CompletedQuest> results = new List<CompletedQuest>();

ExecuteInTransaction(conn =>
return ExecuteQuerySafe<List<CompletedQuest>>(connectionIn, (connection) =>
{
ExecuteReader(conn, SqlSelectCompletedQuestByType,
List<CompletedQuest> results = new List<CompletedQuest>();
ExecuteReader(connection, SqlSelectCompletedQuestByType,
command => {
AddParameter(command, "@character_common_id", characterCommonId);
AddParameter(command, "@quest_type", (uint)questType);
Expand All @@ -45,90 +38,76 @@ public List<CompletedQuest> GetCompletedQuestsByType(TCon connection, uint chara

results.Add(new CompletedQuest()
{
QuestId = (QuestId) GetUInt32(reader, "quest_id"),
QuestId = (QuestId)GetUInt32(reader, "quest_id"),
QuestType = questType,
ClearCount = GetUInt32(reader, "clear_count")
});
}
});
return results;
});

return results;
}

public CompletedQuest GetCompletedQuestsById(uint characterCommonId, QuestId questId)
public CompletedQuest GetCompletedQuestsById(uint characterCommonId, QuestId questId, DbConnection? connectionIn = null)
{
using TCon connection = OpenNewConnection();
return GetCompletedQuestsById(connection, characterCommonId, questId);
}

public CompletedQuest GetCompletedQuestsById(TCon connection, uint characterCommonId, QuestId questId)
{
CompletedQuest result = null;

ExecuteInTransaction(conn =>
return ExecuteQuerySafe<CompletedQuest>(connectionIn, (connection) =>
{
ExecuteReader(conn, SqlSelectCompletedQuestById,
CompletedQuest result = null;
ExecuteReader(connection, SqlSelectCompletedQuestById,
command => {
AddParameter(command, "@character_common_id", characterCommonId);
AddParameter(command, "@quest_id", (uint) questId);
AddParameter(command, "@quest_id", (uint)questId);
}, reader => {
if (reader.Read())
{
result = new CompletedQuest()
{
QuestId = questId,
QuestType = (QuestType) GetUInt32(reader, "quest_type"),
QuestType = (QuestType)GetUInt32(reader, "quest_type"),
ClearCount = GetUInt32(reader, "clear_count")
};
}
});
return result;
});

return result;
}

public bool InsertIfNotExistCompletedQuest(uint characterCommonId, QuestId questId, QuestType questType)
{
using TCon connection = OpenNewConnection();
return InsertIfNotExistCompletedQuest(connection, characterCommonId, questId, questType);
}

public bool InsertIfNotExistCompletedQuest(TCon connection, uint characterCommonId, QuestId questId, QuestType questType)
public bool InsertIfNotExistCompletedQuest(uint characterCommonId, QuestId questId, QuestType questType, DbConnection? connectionIn = null)
{
return ExecuteNonQuery(connection, SqlInsertIfNotExistCompletedQuestId, command =>
return ExecuteQuerySafe<bool>(connectionIn, (connection) =>
{
AddParameter(command, "character_common_id", characterCommonId);
AddParameter(command, "quest_id", (uint) questId);
AddParameter(command, "quest_type", (uint) questType);
AddParameter(command, "clear_count", 1);
}) == 1;
return ExecuteNonQuery(connection, SqlInsertIfNotExistCompletedQuestId, command =>
{
AddParameter(command, "character_common_id", characterCommonId);
AddParameter(command, "quest_id", (uint)questId);
AddParameter(command, "quest_type", (uint)questType);
AddParameter(command, "clear_count", 1);
}) == 1;
});
}

public bool ReplaceCompletedQuest(uint characterCommonId, QuestId questId, QuestType questType, uint count = 1)
public bool ReplaceCompletedQuest(uint characterCommonId, QuestId questId, QuestType questType, uint count = 1, DbConnection? connectionIn = null)
{
using TCon connection = OpenNewConnection();
return ReplaceCompletedQuest(connection, characterCommonId, questId, questType, count);
}

public bool ReplaceCompletedQuest(TCon connection, uint characterCommonId, QuestId questId, QuestType questType, uint count = 1)
{
if (!InsertIfNotExistCompletedQuest(connection, characterCommonId, questId, questType))

if (!InsertIfNotExistCompletedQuest(characterCommonId, questId, questType, connectionIn))
{
return UpdateCompletedQuest(connection, characterCommonId, questId, questType, count);
return UpdateCompletedQuest(characterCommonId, questId, questType, count, connectionIn);
}
return true;
}

private bool UpdateCompletedQuest(TCon connection, uint characterCommonId, QuestId questId, QuestType questType, uint count = 1)
private bool UpdateCompletedQuest(uint characterCommonId, QuestId questId, QuestType questType, uint count = 1, DbConnection? connectionIn = null)
{
return ExecuteNonQuery(connection, SqlUpdateCompletedQuestId, command =>
return ExecuteQuerySafe<bool>(connectionIn, (connection) =>
{
AddParameter(command, "character_common_id", characterCommonId);
AddParameter(command, "quest_id", (uint)questId);
AddParameter(command, "quest_type", (uint)questType);
AddParameter(command, "clear_count", count);
}) == 1;
return ExecuteNonQuery(connection, SqlUpdateCompletedQuestId, command =>
{
AddParameter(command, "character_common_id", characterCommonId);
AddParameter(command, "quest_id", (uint)questId);
AddParameter(command, "quest_type", (uint)questType);
AddParameter(command, "clear_count", count);
}) == 1;
});
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions Arrowgene.Ddon.Database/Sql/Core/DdonSqlDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ public void ExecuteReader(DbConnection conn, string sql, Action<DbCommand> comma
base.ExecuteReader((TCon) conn, sql, (command) => commandAction.Invoke((TCom) command), (reader) => readAction.Invoke((TReader) reader));
}

public T ExecuteQuerySafe<T>(DbConnection? connectionIn, Func<TCon, T> work)
{
bool isTransaction = connectionIn is not null;
TCon connection = (TCon)(connectionIn ?? OpenNewConnection());
try
{
return work.Invoke(connection);
}
finally
{
if (!isTransaction) connection.Dispose();
}
}

public void ExecuteQuerySafe(DbConnection? connectionIn, Action<TCon> work)
{
bool isTransaction = connectionIn is not null;
TCon connection = (TCon)(connectionIn ?? OpenNewConnection());
try
{
work.Invoke(connection);
}
finally
{
if (!isTransaction) connection.Dispose();
}
}

public bool MigrateDatabase(DatabaseMigrator migrator, uint toVersion)
{
uint currentVersion = GetMeta().DatabaseVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,26 @@ public bool InsertBBMProgress(TCon connection, uint characterId, ulong startTime
AddParameter(command, "last_ticket_time", lastTicketTime);
}) == 1;
}
public bool UpdateBBMProgress(uint characterId, BitterblackMazeProgress progress)
public bool UpdateBBMProgress(uint characterId, BitterblackMazeProgress progress, DbConnection? connectionIn = null)
{
return UpdateBBMProgress(characterId, progress.StartTime, progress.ContentId, progress.ContentMode, progress.Tier, progress.KilledDeath, progress.LastTicketTime);
return UpdateBBMProgress(characterId, progress.StartTime, progress.ContentId, progress.ContentMode, progress.Tier, progress.KilledDeath, progress.LastTicketTime, connectionIn);
}

public bool UpdateBBMProgress(uint characterId, ulong startTime, uint contentId, BattleContentMode contentMode, uint tier, bool killedDeath, ulong lastTicketTime)
public bool UpdateBBMProgress(uint characterId, ulong startTime, uint contentId, BattleContentMode contentMode, uint tier, bool killedDeath, ulong lastTicketTime, DbConnection? connectionIn = null)
{
using TCon connection = OpenNewConnection();
return UpdateBBMProgress(connection, characterId, startTime, contentId, contentMode, tier, killedDeath, lastTicketTime);
}

public bool UpdateBBMProgress(TCon connection, uint characterId, ulong startTime, uint contentId, BattleContentMode contentMode, uint tier, bool killedDeath, ulong lastTicketTime)
{
return ExecuteNonQuery(connection, SqlUpdateBBMProgress, command =>
return ExecuteQuerySafe(connectionIn, (connection) =>
{
AddParameter(command, "character_id", characterId);
AddParameter(command, "start_time", startTime);
AddParameter(command, "content_id", contentId);
AddParameter(command, "content_mode", (uint) contentMode);
AddParameter(command, "tier", tier);
AddParameter(command, "killed_death", killedDeath);
AddParameter(command, "last_ticket_time", lastTicketTime);
}) == 1; ;
return ExecuteNonQuery(connection, SqlUpdateBBMProgress, command =>
{
AddParameter(command, "character_id", characterId);
AddParameter(command, "start_time", startTime);
AddParameter(command, "content_id", contentId);
AddParameter(command, "content_mode", (uint)contentMode);
AddParameter(command, "tier", tier);
AddParameter(command, "killed_death", killedDeath);
AddParameter(command, "last_ticket_time", lastTicketTime);
}) == 1;
});
}

public bool RemoveBBMProgress(uint characterId)
Expand Down
Loading

0 comments on commit 7f665da

Please sign in to comment.