From 47ca3023844b6eee1424e142d059622483cc91a2 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 18 Nov 2023 03:18:35 -0500 Subject: [PATCH] feat: Cycle simulation (simulate cycles if beyond a single set) --- config.json | 4 ++-- server/channelserver/handlers_quest.go | 24 ++++++++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/config.json b/config.json index d3de778bb..b175451e4 100644 --- a/config.json +++ b/config.json @@ -119,9 +119,9 @@ ], "Database": { "Host": "localhost", - "Port": 5432, + "Port": 5433, "User": "postgres", - "Password": "", + "Password": "admin", "Database": "erupe" }, "Sign": { diff --git a/server/channelserver/handlers_quest.go b/server/channelserver/handlers_quest.go index 37ae822a8..90c477708 100644 --- a/server/channelserver/handlers_quest.go +++ b/server/channelserver/handlers_quest.go @@ -190,6 +190,14 @@ func makeEventQuest(s *Session, rows *sql.Rows) ([]byte, error) { return bf.Data(), nil } +func calculateNumberOfCycles(duration time.Duration, lastStartTime time.Time) int { + timeDifference := time.Now().Sub(lastStartTime) + println(timeDifference.String()) + println(float64(duration)) + numberOfCycles := int(timeDifference.Nanoseconds() / int64(duration)) + return numberOfCycles +} + func handleMsgMhfEnumerateQuest(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfEnumerateQuest) var totalCount, returnedCount uint16 @@ -219,14 +227,22 @@ func handleMsgMhfEnumerateQuest(s *Session, p mhfpacket.MHFPacket) { continue } + // Count the number of cycles necessary to align quest with actual time. + cycleCount := calculateNumberOfCycles(time.Duration(activeDuration+inactiveDuration)*24*time.Hour, startTime) + // Calculate the rotation time based on start time, active duration, and inactive duration - rotationTime := startTime.Add((time.Duration(activeDuration+inactiveDuration) * 24) * time.Hour) + rotationTime := startTime.Add(time.Duration(activeDuration+inactiveDuration) * 24 * time.Duration(cycleCount) * time.Hour) if currentTime.After(rotationTime) { - // The rotation time has passed, update the start time and reset the rotation - _, err := transaction.Exec("UPDATE event_quests SET start_time = $1 WHERE id = $2", rotationTime, id) + // take the rotationTime and normalize it to midnight + newRotationTime := time.Date(rotationTime.Year(), rotationTime.Month(), rotationTime.Day(), 0, 0, 0, 0, rotationTime.Location()) + newRotationTime = newRotationTime.Add(time.Duration(TimeMidnight().Add(13 * time.Hour).Nanosecond())) + + _, err := transaction.Exec("UPDATE event_quests SET start_time = $1 WHERE id = $2", newRotationTime, id) if err != nil { - continue + transaction.Rollback() // Rollback if an error occurs + break } + startTime = newRotationTime // Set the new start time so the quest can be used immediatelyw } // Check if the quest is currently active