refactor: Change event quest updating to use transactions rather than directly commiting

This commit is contained in:
Matthew
2023-11-17 22:18:07 -05:00
parent 800e993c1f
commit 233990f452

View File

@@ -205,11 +205,15 @@ func handleMsgMhfEnumerateQuest(s *Session, p mhfpacket.MHFPacket) {
} }
defer rows.Close() defer rows.Close()
// Commit event quest changes to a transaction instead of doing it one by one for to help with performance
transaction, _ := s.server.db.Begin()
for rows.Next() { for rows.Next() {
var id, mark uint32 var id, mark uint32
var questId, activeDuration, inactiveDuration int var questId, activeDuration, inactiveDuration int
var maxPlayers, questType uint8 var maxPlayers, questType uint8
var startTime time.Time var startTime time.Time
err := rows.Scan(&id, &maxPlayers, &questType, &questId, &mark, &startTime, &activeDuration, &inactiveDuration) err := rows.Scan(&id, &maxPlayers, &questType, &questId, &mark, &startTime, &activeDuration, &inactiveDuration)
if err != nil { if err != nil {
continue continue
@@ -219,7 +223,7 @@ func handleMsgMhfEnumerateQuest(s *Session, p mhfpacket.MHFPacket) {
rotationTime := startTime.Add(time.Duration(activeDuration+inactiveDuration) * 24 * time.Hour) rotationTime := startTime.Add(time.Duration(activeDuration+inactiveDuration) * 24 * time.Hour)
if currentTime.After(rotationTime) { if currentTime.After(rotationTime) {
// The rotation time has passed, update the start time and reset the rotation // The rotation time has passed, update the start time and reset the rotation
_, err := s.server.db.Exec("UPDATE event_quests SET start_time = $1 WHERE quest_id = $2", rotationTime, questId) _, err := transaction.Exec("UPDATE event_quests SET start_time = $1 WHERE quest_id = $2", rotationTime, questId)
if err != nil { if err != nil {
return return
} }
@@ -245,6 +249,9 @@ func handleMsgMhfEnumerateQuest(s *Session, p mhfpacket.MHFPacket) {
} }
} }
// Commit transaction so to write to the database.
transaction.Commit()
type tuneValue struct { type tuneValue struct {
ID uint16 ID uint16
Value uint16 Value uint16