diff --git a/patch-schema/time-fix.sql b/patch-schema/time-fix.sql new file mode 100644 index 000000000..00b6d2a96 --- /dev/null +++ b/patch-schema/time-fix.sql @@ -0,0 +1,63 @@ +BEGIN; + +ALTER TABLE IF EXISTS public.characters + ALTER COLUMN daily_time TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.characters + ALTER COLUMN guild_post_checked TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.characters + ALTER COLUMN boost_time TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.characters + ALTER COLUMN cafe_reset TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.distribution + ALTER COLUMN deadline TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.events + ALTER COLUMN start_time TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.feature_weapon + ALTER COLUMN start_time TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.guild_alliances + ALTER COLUMN created_at TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.guild_applications + ALTER COLUMN created_at TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.guild_characters + ALTER COLUMN joined_at TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.guild_posts + ALTER COLUMN created_at TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.characters + ALTER COLUMN daily_time TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.guilds + ALTER COLUMN created_at TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.mail + ALTER COLUMN created_at TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.stamps + ALTER COLUMN hl_next TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.stamps + ALTER COLUMN ex_next TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.titles + ALTER COLUMN unlocked_at TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.titles + ALTER COLUMN updated_at TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.users + ALTER COLUMN last_login TYPE TIMESTAMP WITH TIME ZONE; + +ALTER TABLE IF EXISTS public.users + ALTER COLUMN return_expires TYPE TIMESTAMP WITH TIME ZONE; + +END; \ No newline at end of file diff --git a/server/channelserver/handlers.go b/server/channelserver/handlers.go index f09f13e2f..433c29e44 100644 --- a/server/channelserver/handlers.go +++ b/server/channelserver/handlers.go @@ -144,7 +144,7 @@ func handleMsgSysLogin(s *Session, p mhfpacket.MHFPacket) { updateRights(s) bf := byteframe.NewByteFrame() - bf.WriteUint32(uint32(Time_Current_Adjusted().Unix())) // Unix timestamp + bf.WriteUint32(uint32(TimeAdjusted().Unix())) // Unix timestamp _, err := s.server.db.Exec("UPDATE servers SET current_players=$1 WHERE server_id=$2", len(s.server.sessions), s.server.ID) if err != nil { @@ -156,7 +156,7 @@ func handleMsgSysLogin(s *Session, p mhfpacket.MHFPacket) { panic(err) } - _, err = s.server.db.Exec("UPDATE characters SET last_login=$1 WHERE id=$2", Time_Current().Unix(), s.charID) + _, err = s.server.db.Exec("UPDATE characters SET last_login=$1 WHERE id=$2", TimeAdjusted().Unix(), s.charID) if err != nil { panic(err) } @@ -216,7 +216,7 @@ func logoutPlayer(s *Session) { var timePlayed int var sessionTime int _ = s.server.db.QueryRow("SELECT time_played FROM characters WHERE id = $1", s.charID).Scan(&timePlayed) - sessionTime = int(Time_Current_Adjusted().Unix()) - int(s.sessionStart) + sessionTime = int(TimeAdjusted().Unix()) - int(s.sessionStart) timePlayed += sessionTime var rpGained int @@ -276,7 +276,7 @@ func handleMsgSysTime(s *Session, p mhfpacket.MHFPacket) { resp := &mhfpacket.MsgSysTime{ GetRemoteTime: false, - Timestamp: uint32(Time_Current_Adjusted().Unix()), // JP timezone + Timestamp: uint32(TimeAdjusted().Unix()), // JP timezone } s.QueueSendMHF(resp) } @@ -1505,7 +1505,7 @@ func handleMsgMhfGetEtcPoints(s *Session, p mhfpacket.MHFPacket) { var dailyTime time.Time _ = s.server.db.QueryRow("SELECT COALESCE(daily_time, $2) FROM characters WHERE id = $1", s.charID, time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)).Scan(&dailyTime) - if Time_Current_Adjusted().After(dailyTime) { + if TimeAdjusted().After(dailyTime) { s.server.db.Exec("UPDATE characters SET bonus_quests = 0, daily_quests = 0 WHERE id=$1", s.charID) } diff --git a/server/channelserver/handlers_cafe.go b/server/channelserver/handlers_cafe.go index b623efae6..b1aa57b28 100644 --- a/server/channelserver/handlers_cafe.go +++ b/server/channelserver/handlers_cafe.go @@ -37,11 +37,8 @@ func handleMsgMhfUpdateCafepoint(s *Session, p mhfpacket.MHFPacket) { func handleMsgMhfCheckDailyCafepoint(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfCheckDailyCafepoint) - // get next midday - var t = Time_static() - year, month, day := t.Date() - midday := time.Date(year, month, day, 12, 0, 0, 0, t.Location()) - if t.After(midday) { + midday := TimeMidnight().Add(12 * time.Hour) + if TimeAdjusted().After(midday) { midday = midday.Add(24 * time.Hour) } @@ -54,7 +51,7 @@ func handleMsgMhfCheckDailyCafepoint(s *Session, p mhfpacket.MHFPacket) { var bondBonus, bonusQuests, dailyQuests uint32 bf := byteframe.NewByteFrame() - if t.After(dailyTime) { + if midday.After(dailyTime) { addPointNetcafe(s, 5) bondBonus = 5 // Bond point bonus quests bonusQuests = 3 // HRP bonus quests? @@ -80,7 +77,7 @@ func handleMsgMhfGetCafeDuration(s *Session, p mhfpacket.MHFPacket) { cafeReset = TimeWeekNext() s.server.db.Exec(`UPDATE characters SET cafe_reset=$1 WHERE id=$2`, cafeReset, s.charID) } - if Time_Current_Adjusted().After(cafeReset) { + if TimeAdjusted().After(cafeReset) { cafeReset = TimeWeekNext() s.server.db.Exec(`UPDATE characters SET cafe_time=0, cafe_reset=$1 WHERE id=$2`, cafeReset, s.charID) s.server.db.Exec(`DELETE FROM cafe_accepted WHERE character_id=$1`, s.charID) @@ -92,7 +89,7 @@ func handleMsgMhfGetCafeDuration(s *Session, p mhfpacket.MHFPacket) { panic(err) } if s.FindCourse("NetCafe").ID != 0 || s.FindCourse("N").ID != 0 { - cafeTime = uint32(Time_Current_Adjusted().Unix()) - uint32(s.sessionStart) + cafeTime + cafeTime = uint32(TimeAdjusted().Unix()) - uint32(s.sessionStart) + cafeTime } bf.WriteUint32(cafeTime) // Total cafe time bf.WriteUint16(0) @@ -165,7 +162,7 @@ func handleMsgMhfReceiveCafeDurationBonus(s *Session, p mhfpacket.MHFPacket) { SELECT ch.cafe_time + $2 FROM characters ch WHERE ch.id = $1 - ) >= time_req`, s.charID, Time_Current_Adjusted().Unix()-s.sessionStart) + ) >= time_req`, s.charID, TimeAdjusted().Unix()-s.sessionStart) if err != nil { doAckBufSucceed(s, pkt.AckHandle, bf.Data()) } else { @@ -222,7 +219,7 @@ func addPointNetcafe(s *Session, p int) error { func handleMsgMhfStartBoostTime(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfStartBoostTime) bf := byteframe.NewByteFrame() - boostLimit := Time_Current_Adjusted().Add(100 * time.Minute) + boostLimit := TimeAdjusted().Add(120 * time.Minute) s.server.db.Exec("UPDATE characters SET boost_time=$1 WHERE id=$2", boostLimit, s.charID) bf.WriteUint32(uint32(boostLimit.Unix())) doAckBufSucceed(s, pkt.AckHandle, bf.Data()) @@ -255,7 +252,7 @@ func handleMsgMhfGetBoostRight(s *Session, p mhfpacket.MHFPacket) { doAckBufSucceed(s, pkt.AckHandle, []byte{0x00, 0x00, 0x00, 0x00}) return } - if boostLimit.Unix() > Time_Current_Adjusted().Unix() { + if boostLimit.After(TimeAdjusted()) { doAckBufSucceed(s, pkt.AckHandle, []byte{0x00, 0x00, 0x00, 0x01}) } else { doAckBufSucceed(s, pkt.AckHandle, []byte{0x00, 0x00, 0x00, 0x02}) diff --git a/server/channelserver/handlers_diva.go b/server/channelserver/handlers_diva.go index 48e6dcecf..0daabe70c 100644 --- a/server/channelserver/handlers_diva.go +++ b/server/channelserver/handlers_diva.go @@ -15,7 +15,7 @@ func cleanupDiva(s *Session) { func generateDivaTimestamps(s *Session, start uint32, debug bool) []uint32 { timestamps := make([]uint32, 6) - midnight := Time_Current_Midnight() + midnight := TimeMidnight() if debug && start <= 3 { midnight := uint32(midnight.Unix()) switch start { @@ -43,7 +43,7 @@ func generateDivaTimestamps(s *Session, start uint32, debug bool) []uint32 { } return timestamps } - if start == 0 || Time_Current_Adjusted().Unix() > int64(start)+2977200 { + if start == 0 || TimeAdjusted().Unix() > int64(start)+2977200 { cleanupDiva(s) // Generate a new diva defense, starting midnight tomorrow start = uint32(midnight.Add(24 * time.Hour).Unix()) diff --git a/server/channelserver/handlers_event.go b/server/channelserver/handlers_event.go index 6de9fd416..21638f432 100644 --- a/server/channelserver/handlers_event.go +++ b/server/channelserver/handlers_event.go @@ -61,7 +61,7 @@ func handleMsgMhfGetWeeklySchedule(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfGetWeeklySchedule) var features []activeFeature - rows, _ := s.server.db.Queryx(`SELECT start_time, featured FROM feature_weapon WHERE start_time=$1 OR start_time=$2`, Time_Current_Midnight().Add(-24*time.Hour), Time_Current_Midnight()) + rows, _ := s.server.db.Queryx(`SELECT start_time, featured FROM feature_weapon WHERE start_time=$1 OR start_time=$2`, TimeMidnight().Add(-24*time.Hour), TimeMidnight()) for rows.Next() { var feature activeFeature rows.StructScan(&feature) @@ -71,19 +71,19 @@ func handleMsgMhfGetWeeklySchedule(s *Session, p mhfpacket.MHFPacket) { if len(features) < 2 { if len(features) == 0 { feature := generateFeatureWeapons(s.server.erupeConfig.FeaturedWeapons) - feature.StartTime = Time_Current_Midnight().Add(-24 * time.Hour) + feature.StartTime = TimeMidnight().Add(-24 * time.Hour) features = append(features, feature) s.server.db.Exec(`INSERT INTO feature_weapon VALUES ($1, $2)`, feature.StartTime, feature.ActiveFeatures) } feature := generateFeatureWeapons(s.server.erupeConfig.FeaturedWeapons) - feature.StartTime = Time_Current_Midnight() + feature.StartTime = TimeMidnight() features = append(features, feature) s.server.db.Exec(`INSERT INTO feature_weapon VALUES ($1, $2)`, feature.StartTime, feature.ActiveFeatures) } bf := byteframe.NewByteFrame() bf.WriteUint8(2) - bf.WriteUint32(uint32(Time_Current_Adjusted().Add(-5 * time.Minute).Unix())) + bf.WriteUint32(uint32(TimeAdjusted().Add(-5 * time.Minute).Unix())) for _, feature := range features { bf.WriteUint32(uint32(feature.StartTime.Unix())) bf.WriteUint32(feature.ActiveFeatures) @@ -119,129 +119,97 @@ func generateFeatureWeapons(count int) activeFeature { } type loginBoost struct { - WeekReq, WeekCount uint8 - Available bool - Expiration uint32 + WeekReq uint8 `db:"week_req"` + WeekCount uint8 + Active bool + Expiration time.Time `db:"expiration"` + Reset time.Time `db:"reset"` } func handleMsgMhfGetKeepLoginBoostStatus(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfGetKeepLoginBoostStatus) - var loginBoostStatus []loginBoost - insert := false - boostState, err := s.server.db.Query("SELECT week_req, week_count, available, end_time FROM login_boost_state WHERE char_id=$1 ORDER BY week_req ASC", s.charID) + bf := byteframe.NewByteFrame() + + var loginBoosts []loginBoost + rows, err := s.server.db.Queryx("SELECT week_req, expiration, reset FROM login_boost WHERE char_id=$1 ORDER BY week_req", s.charID) if err != nil { - panic(err) + doAckBufSucceed(s, pkt.AckHandle, make([]byte, 35)) + return } - for boostState.Next() { - var boost loginBoost - err = boostState.Scan(&boost.WeekReq, &boost.WeekCount, &boost.Available, &boost.Expiration) - if err != nil { - panic(err) + for rows.Next() { + var temp loginBoost + rows.StructScan(&temp) + loginBoosts = append(loginBoosts, temp) + } + if len(loginBoosts) == 0 { + temp := TimeWeekStart() + loginBoosts = []loginBoost{ + {WeekReq: 1, Expiration: temp}, + {WeekReq: 2, Expiration: temp}, + {WeekReq: 3, Expiration: temp}, + {WeekReq: 4, Expiration: temp}, + {WeekReq: 5, Expiration: temp}, } - loginBoostStatus = append(loginBoostStatus, boost) - } - if len(loginBoostStatus) == 0 { - // create default Entries (should only been week 1 with ) - insert = true - loginBoostStatus = []loginBoost{ - { - WeekReq: 1, // weeks needed - WeekCount: 0, // weeks passed - Available: true, // available - Expiration: 0, //uint32(t.Add(120 * time.Minute).Unix()), // uncomment to enable permanently - }, - { - WeekReq: 2, - WeekCount: 0, - Available: true, - Expiration: 0, - }, - { - WeekReq: 3, - WeekCount: 0, - Available: true, - Expiration: 0, - }, - { - WeekReq: 4, - WeekCount: 0, - Available: true, - Expiration: 0, - }, - { - WeekReq: 5, - WeekCount: 0, - Available: true, - Expiration: 0, - }, + for _, boost := range loginBoosts { + s.server.db.Exec(`INSERT INTO login_boost VALUES ($1, $2, $3, $4)`, s.charID, boost.WeekReq, boost.Expiration, time.Time{}) } } - resp := byteframe.NewByteFrame() - CurrentWeek := Time_Current_Week_uint8() - for d := range loginBoostStatus { - if CurrentWeek == 1 && loginBoostStatus[d].WeekCount <= 5 { - loginBoostStatus[d].WeekCount = 0 + + for _, boost := range loginBoosts { + // Reset if next week + if !boost.Reset.IsZero() && boost.Reset.Before(TimeAdjusted()) { + boost.Expiration = TimeWeekStart() + boost.Reset = time.Time{} + s.server.db.Exec(`UPDATE login_boost SET expiration=$1, reset=$2 WHERE char_id=$3 AND week_req=$4`, boost.Expiration, boost.Reset, s.charID, boost.WeekReq) } - if loginBoostStatus[d].WeekReq == CurrentWeek || loginBoostStatus[d].WeekCount != 0 { - loginBoostStatus[d].WeekCount = CurrentWeek + + boost.WeekCount = uint8((TimeAdjusted().Unix()-boost.Expiration.Unix())/604800 + 1) + + if boost.WeekCount >= boost.WeekReq { + boost.Active = true + boost.WeekCount = boost.WeekReq } - if !loginBoostStatus[d].Available && loginBoostStatus[d].WeekCount >= loginBoostStatus[d].WeekReq && uint32(time.Now().In(time.FixedZone("UTC+1", 1*60*60)).Unix()) >= loginBoostStatus[d].Expiration { - loginBoostStatus[d].Expiration = 1 + + // Show reset timer on expired boosts + if boost.Reset.After(TimeAdjusted()) { + boost.Active = true + boost.WeekCount = 0 } - if !insert { - _, err := s.server.db.Exec(`UPDATE login_boost_state SET week_count=$1, end_time=$2 WHERE char_id=$3 AND week_req=$4`, loginBoostStatus[d].WeekCount, loginBoostStatus[d].Expiration, s.charID, loginBoostStatus[d].WeekReq) - if err != nil { - panic(err) - } + + bf.WriteUint8(boost.WeekReq) + bf.WriteBool(boost.Active) + bf.WriteUint8(boost.WeekCount) + if !boost.Reset.IsZero() { + bf.WriteUint32(uint32(boost.Expiration.Unix())) + } else { + bf.WriteUint32(0) } } - for _, v := range loginBoostStatus { - if insert { - _, err := s.server.db.Exec(`INSERT INTO login_boost_state (char_id, week_req, week_count, available, end_time) VALUES ($1,$2,$3,$4,$5)`, s.charID, v.WeekReq, v.WeekCount, v.Available, v.Expiration) - if err != nil { - panic(err) - } - } - resp.WriteUint8(v.WeekReq) - resp.WriteUint8(v.WeekCount) - resp.WriteBool(v.Available) - resp.WriteUint32(v.Expiration) - } - doAckBufSucceed(s, pkt.AckHandle, resp.Data()) + + doAckBufSucceed(s, pkt.AckHandle, bf.Data()) } func handleMsgMhfUseKeepLoginBoost(s *Session, p mhfpacket.MHFPacket) { - // Directly interacts with MhfGetKeepLoginBoostStatus - // TODO: make these states persistent on a per character basis pkt := p.(*mhfpacket.MsgMhfUseKeepLoginBoost) - var t = time.Now().In(time.FixedZone("UTC+1", 1*60*60)) - resp := byteframe.NewByteFrame() - resp.WriteUint8(0) - - // response is end timestamp based on input + var expiration time.Time + bf := byteframe.NewByteFrame() + bf.WriteUint8(0) switch pkt.BoostWeekUsed { case 1: - t = t.Add(120 * time.Minute) - resp.WriteUint32(uint32(t.Unix())) - case 2: - t = t.Add(240 * time.Minute) - resp.WriteUint32(uint32(t.Unix())) + fallthrough case 3: - t = t.Add(120 * time.Minute) - resp.WriteUint32(uint32(t.Unix())) + expiration = TimeAdjusted().Add(120 * time.Minute) case 4: - t = t.Add(180 * time.Minute) - resp.WriteUint32(uint32(t.Unix())) + expiration = TimeAdjusted().Add(180 * time.Minute) + case 2: + fallthrough case 5: - t = t.Add(240 * time.Minute) - resp.WriteUint32(uint32(t.Unix())) + expiration = TimeAdjusted().Add(240 * time.Minute) } - _, err := s.server.db.Exec(`UPDATE login_boost_state SET available='false', end_time=$1 WHERE char_id=$2 AND week_req=$3`, uint32(t.Unix()), s.charID, pkt.BoostWeekUsed) - if err != nil { - panic(err) - } - doAckBufSucceed(s, pkt.AckHandle, resp.Data()) + bf.WriteUint32(uint32(expiration.Unix())) + s.server.db.Exec(`UPDATE login_boost SET expiration=$1, reset=$2 WHERE char_id=$3 AND week_req=$4`, expiration, TimeWeekNext(), s.charID, pkt.BoostWeekUsed) + doAckBufSucceed(s, pkt.AckHandle, bf.Data()) } func handleMsgMhfGetRestrictionEvent(s *Session, p mhfpacket.MHFPacket) {} diff --git a/server/channelserver/handlers_festa.go b/server/channelserver/handlers_festa.go index e41c5d1a1..2659aae1d 100644 --- a/server/channelserver/handlers_festa.go +++ b/server/channelserver/handlers_festa.go @@ -41,7 +41,7 @@ func handleMsgMhfEnumerateRanking(s *Session, p mhfpacket.MHFPacket) { // Unk // Start? // End? - midnight := Time_Current_Midnight() + midnight := TimeMidnight() switch state { case 1: bf.WriteUint32(uint32(midnight.Unix())) @@ -60,13 +60,13 @@ func handleMsgMhfEnumerateRanking(s *Session, p mhfpacket.MHFPacket) { bf.WriteUint32(uint32(midnight.Add(7 * 24 * time.Hour).Unix())) default: bf.WriteBytes(make([]byte, 16)) - bf.WriteUint32(uint32(Time_Current_Adjusted().Unix())) // TS Current Time + bf.WriteUint32(uint32(TimeAdjusted().Unix())) // TS Current Time bf.WriteUint8(3) bf.WriteBytes(make([]byte, 4)) doAckBufSucceed(s, pkt.AckHandle, bf.Data()) return } - bf.WriteUint32(uint32(Time_Current_Adjusted().Unix())) // TS Current Time + bf.WriteUint32(uint32(TimeAdjusted().Unix())) // TS Current Time bf.WriteUint8(3) ps.Uint8(bf, "", false) bf.WriteUint16(0) // numEvents @@ -101,7 +101,7 @@ func cleanupFesta(s *Session) { func generateFestaTimestamps(s *Session, start uint32, debug bool) []uint32 { timestamps := make([]uint32, 5) - midnight := Time_Current_Midnight() + midnight := TimeMidnight() if debug && start <= 3 { midnight := uint32(midnight.Unix()) switch start { @@ -126,7 +126,7 @@ func generateFestaTimestamps(s *Session, start uint32, debug bool) []uint32 { } return timestamps } - if start == 0 || Time_Current_Adjusted().Unix() > int64(start)+2977200 { + if start == 0 || TimeAdjusted().Unix() > int64(start)+2977200 { cleanupFesta(s) // Generate a new festa, starting midnight tomorrow start = uint32(midnight.Add(24 * time.Hour).Unix()) @@ -170,7 +170,7 @@ func handleMsgMhfInfoFesta(s *Session, p mhfpacket.MHFPacket) { timestamps = generateFestaTimestamps(s, start, false) } - if timestamps[0] > uint32(Time_Current_Adjusted().Unix()) { + if timestamps[0] > uint32(TimeAdjusted().Unix()) { doAckBufSucceed(s, pkt.AckHandle, make([]byte, 4)) return } @@ -183,7 +183,7 @@ func handleMsgMhfInfoFesta(s *Session, p mhfpacket.MHFPacket) { for _, timestamp := range timestamps { bf.WriteUint32(timestamp) } - bf.WriteUint32(uint32(Time_Current_Adjusted().Unix())) + bf.WriteUint32(uint32(TimeAdjusted().Unix())) bf.WriteUint8(4) ps.Uint8(bf, "", false) bf.WriteUint32(0) diff --git a/server/channelserver/handlers_guild.go b/server/channelserver/handlers_guild.go index 07b68ca2f..9aef12fbd 100644 --- a/server/channelserver/handlers_guild.go +++ b/server/channelserver/handlers_guild.go @@ -1755,7 +1755,7 @@ func handleMsgMhfLoadGuildCooking(s *Session, p mhfpacket.MHFPacket) { if err != nil { continue } - if mealData.Expires > uint32(Time_Current_Adjusted().Add(-60*time.Minute).Unix()) { + if mealData.Expires > uint32(TimeAdjusted().Unix()) { count++ temp.WriteUint32(mealData.ID) temp.WriteUint32(mealData.MealID) @@ -1778,7 +1778,7 @@ func handleMsgMhfRegistGuildCooking(s *Session, p mhfpacket.MHFPacket) { s.logger.Error("Failed to delete meal in db", zap.Error(err)) } } - _, err := s.server.db.Exec("INSERT INTO guild_meals (guild_id, meal_id, level, expires) VALUES ($1, $2, $3, $4)", guild.ID, pkt.MealID, pkt.Success, Time_Current_Adjusted().Add(30*time.Minute).Unix()) + _, err := s.server.db.Exec("INSERT INTO guild_meals (guild_id, meal_id, level, expires) VALUES ($1, $2, $3, $4)", guild.ID, pkt.MealID, pkt.Success, TimeAdjusted().Unix()) if err != nil { s.logger.Error("Failed to register meal in db", zap.Error(err)) } diff --git a/server/channelserver/handlers_guild_adventure.go b/server/channelserver/handlers_guild_adventure.go index 304b5721f..8e953bf24 100644 --- a/server/channelserver/handlers_guild_adventure.go +++ b/server/channelserver/handlers_guild_adventure.go @@ -52,7 +52,7 @@ func handleMsgMhfLoadGuildAdventure(s *Session, p mhfpacket.MHFPacket) { func handleMsgMhfRegistGuildAdventure(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfRegistGuildAdventure) guild, _ := GetGuildInfoByCharacterId(s, s.charID) - _, err := s.server.db.Exec("INSERT INTO guild_adventures (guild_id, destination, depart, return) VALUES ($1, $2, $3, $4)", guild.ID, pkt.Destination, Time_Current_Adjusted().Unix(), Time_Current_Adjusted().Add(6*time.Hour).Unix()) + _, err := s.server.db.Exec("INSERT INTO guild_adventures (guild_id, destination, depart, return) VALUES ($1, $2, $3, $4)", guild.ID, pkt.Destination, TimeAdjusted().Unix(), TimeAdjusted().Add(6*time.Hour).Unix()) if err != nil { s.logger.Error("Failed to register guild adventure", zap.Error(err)) } @@ -87,7 +87,7 @@ func handleMsgMhfChargeGuildAdventure(s *Session, p mhfpacket.MHFPacket) { func handleMsgMhfRegistGuildAdventureDiva(s *Session, p mhfpacket.MHFPacket) { pkt := p.(*mhfpacket.MsgMhfRegistGuildAdventureDiva) guild, _ := GetGuildInfoByCharacterId(s, s.charID) - _, err := s.server.db.Exec("INSERT INTO guild_adventures (guild_id, destination, charge, depart, return) VALUES ($1, $2, $3, $4, $5)", guild.ID, pkt.Destination, pkt.Charge, Time_Current_Adjusted().Unix(), Time_Current_Adjusted().Add(1*time.Hour).Unix()) + _, err := s.server.db.Exec("INSERT INTO guild_adventures (guild_id, destination, charge, depart, return) VALUES ($1, $2, $3, $4, $5)", guild.ID, pkt.Destination, pkt.Charge, TimeAdjusted().Unix(), TimeAdjusted().Add(1*time.Hour).Unix()) if err != nil { s.logger.Error("Failed to register guild adventure", zap.Error(err)) } diff --git a/server/channelserver/handlers_guild_tresure.go b/server/channelserver/handlers_guild_tresure.go index c04007151..3d0918a84 100644 --- a/server/channelserver/handlers_guild_tresure.go +++ b/server/channelserver/handlers_guild_tresure.go @@ -27,7 +27,7 @@ func handleMsgMhfEnumerateGuildTresure(s *Session, p mhfpacket.MHFPacket) { } bf := byteframe.NewByteFrame() hunts := 0 - rows, _ := s.server.db.Queryx("SELECT id, host_id, destination, level, return, acquired, claimed, hunters, treasure, hunt_data FROM guild_hunts WHERE guild_id=$1 AND $2 < return+604800", guild.ID, Time_Current_Adjusted().Unix()) + rows, _ := s.server.db.Queryx("SELECT id, host_id, destination, level, return, acquired, claimed, hunters, treasure, hunt_data FROM guild_hunts WHERE guild_id=$1 AND $2 < return+604800", guild.ID, TimeAdjusted().Unix()) for rows.Next() { hunt := &TreasureHunt{} err = rows.StructScan(&hunt) @@ -101,7 +101,7 @@ func handleMsgMhfRegistGuildTresure(s *Session, p mhfpacket.MHFPacket) { } } _, err = s.server.db.Exec("INSERT INTO guild_hunts (guild_id, host_id, destination, level, return, hunt_data, cats_used) VALUES ($1, $2, $3, $4, $5, $6, $7)", - guild.ID, s.charID, destination, level, Time_Current_Adjusted().Unix(), huntData.Data(), catsUsed) + guild.ID, s.charID, destination, level, TimeAdjusted().Unix(), huntData.Data(), catsUsed) if err != nil { panic(err) } diff --git a/server/channelserver/handlers_mercenary.go b/server/channelserver/handlers_mercenary.go index c52802c73..e44d9f1f2 100644 --- a/server/channelserver/handlers_mercenary.go +++ b/server/channelserver/handlers_mercenary.go @@ -43,9 +43,9 @@ func handleMsgMhfLoadLegendDispatch(s *Session, p mhfpacket.MHFPacket) { Unk uint32 Timestamp uint32 }{ - {0, uint32(Time_Current_Midnight().Add(-12 * time.Hour).Unix())}, - {0, uint32(Time_Current_Midnight().Add(12 * time.Hour).Unix())}, - {0, uint32(Time_Current_Midnight().Add(36 * time.Hour).Unix())}, + {0, uint32(TimeMidnight().Add(-12 * time.Hour).Unix())}, + {0, uint32(TimeMidnight().Add(12 * time.Hour).Unix())}, + {0, uint32(TimeMidnight().Add(36 * time.Hour).Unix())}, } bf.WriteUint8(uint8(len(legendDispatch))) for _, dispatch := range legendDispatch { @@ -164,8 +164,8 @@ func handleMsgMhfReadMercenaryW(s *Session, p mhfpacket.MHFPacket) { bf.WriteUint32(pactID) bf.WriteUint32(cid) bf.WriteBool(false) // ? - bf.WriteUint32(uint32(Time_Current_Adjusted().Add(time.Hour * 24 * -8).Unix())) - bf.WriteUint32(uint32(Time_Current_Adjusted().Add(time.Hour * 24 * -1).Unix())) + bf.WriteUint32(uint32(TimeAdjusted().Add(time.Hour * 24 * -8).Unix())) + bf.WriteUint32(uint32(TimeAdjusted().Add(time.Hour * 24 * -1).Unix())) bf.WriteBytes(stringsupport.PaddedString(name, 18, true)) } else { bf.WriteUint8(0) @@ -180,8 +180,8 @@ func handleMsgMhfReadMercenaryW(s *Session, p mhfpacket.MHFPacket) { rows.Scan(&name, &cid, &pactID) temp.WriteUint32(pactID) temp.WriteUint32(cid) - temp.WriteUint32(uint32(Time_Current_Adjusted().Add(time.Hour * 24 * -8).Unix())) - temp.WriteUint32(uint32(Time_Current_Adjusted().Add(time.Hour * 24 * -1).Unix())) + temp.WriteUint32(uint32(TimeAdjusted().Add(time.Hour * 24 * -8).Unix())) + temp.WriteUint32(uint32(TimeAdjusted().Add(time.Hour * 24 * -1).Unix())) temp.WriteBytes(stringsupport.PaddedString(name, 18, true)) } bf.WriteUint8(loans) @@ -346,7 +346,7 @@ func getGuildAirouList(s *Session) []CatDefinition { FROM guild_hunts gh INNER JOIN characters c ON gh.host_id = c.id - WHERE c.id=$1 AND gh.return+$2>$3`, s.charID, tempBanDuration, Time_Current_Adjusted().Unix()) + WHERE c.id=$1 AND gh.return+$2>$3`, s.charID, tempBanDuration, TimeAdjusted().Unix()) if err != nil { s.logger.Warn("Failed to get recently used airous", zap.Error(err)) } diff --git a/server/channelserver/handlers_shop_gacha.go b/server/channelserver/handlers_shop_gacha.go index 29c3f4c64..2b07b3168 100644 --- a/server/channelserver/handlers_shop_gacha.go +++ b/server/channelserver/handlers_shop_gacha.go @@ -517,7 +517,7 @@ func handleMsgMhfGetStepupStatus(s *Session, p mhfpacket.MHFPacket) { } bf := byteframe.NewByteFrame() bf.WriteUint8(step) - bf.WriteUint32(uint32(Time_Current_Adjusted().Unix())) + bf.WriteUint32(uint32(TimeAdjusted().Unix())) doAckBufSucceed(s, pkt.AckHandle, bf.Data()) } diff --git a/server/channelserver/handlers_tournament.go b/server/channelserver/handlers_tournament.go index 11b8645c3..84c2c8e8f 100644 --- a/server/channelserver/handlers_tournament.go +++ b/server/channelserver/handlers_tournament.go @@ -13,7 +13,7 @@ func handleMsgMhfInfoTournament(s *Session, p mhfpacket.MHFPacket) { switch pkt.Unk0 { case 0: - bf.WriteUint32(uint32(Time_Current_Adjusted().Unix())) + bf.WriteUint32(uint32(TimeAdjusted().Unix())) bf.WriteUint32(0) // Tied to schedule ID? case 1: diff --git a/server/channelserver/sys_session.go b/server/channelserver/sys_session.go index 2ebc6d080..5f3a46572 100644 --- a/server/channelserver/sys_session.go +++ b/server/channelserver/sys_session.go @@ -73,7 +73,7 @@ func NewSession(server *Server, conn net.Conn) *Session { cryptConn: network.NewCryptConn(conn), sendPackets: make(chan packet, 20), clientContext: &clientctx.ClientContext{}, // Unused - sessionStart: Time_Current_Adjusted().Unix(), + sessionStart: TimeAdjusted().Unix(), stageMoveStack: stringstack.New(), } return s diff --git a/server/channelserver/sys_time.go b/server/channelserver/sys_time.go new file mode 100644 index 000000000..396ae4cf5 --- /dev/null +++ b/server/channelserver/sys_time.go @@ -0,0 +1,25 @@ +package channelserver + +import ( + "time" +) + +func TimeAdjusted() time.Time { + baseTime := time.Now().In(time.FixedZone("UTC+9", 9*60*60)) + return time.Date(baseTime.Year(), baseTime.Month(), baseTime.Day(), baseTime.Hour(), baseTime.Minute(), baseTime.Second(), baseTime.Nanosecond(), baseTime.Location()) +} + +func TimeMidnight() time.Time { + baseTime := time.Now().In(time.FixedZone("UTC+9", 9*60*60)) + return time.Date(baseTime.Year(), baseTime.Month(), baseTime.Day(), 0, 0, 0, 0, baseTime.Location()) +} + +func TimeWeekStart() time.Time { + midnight := TimeMidnight() + offset := (int(midnight.Weekday()) - 1) * -24 + return midnight.Add(time.Hour * time.Duration(offset)) +} + +func TimeWeekNext() time.Time { + return TimeWeekStart().Add(time.Hour * 24 * 7) +} diff --git a/server/channelserver/sys_timefix.go b/server/channelserver/sys_timefix.go deleted file mode 100644 index 4cde6a319..000000000 --- a/server/channelserver/sys_timefix.go +++ /dev/null @@ -1,58 +0,0 @@ -package channelserver - -import ( - "fmt" - "time" -) - -var ( - Offset = 9 - YearAdjust = -7 - MonthAdjust = 0 - DayAdjust = 0 -) - -var ( - TimeStatic = time.Time{} -) - -func Time_Current() time.Time { - baseTime := time.Now().In(time.FixedZone(fmt.Sprintf("UTC+%d", Offset), Offset*60*60)) - return baseTime -} - -func Time_Current_Adjusted() time.Time { - baseTime := time.Now().In(time.FixedZone(fmt.Sprintf("UTC+%d", Offset), Offset*60*60)).AddDate(YearAdjust, MonthAdjust, DayAdjust) - return time.Date(baseTime.Year(), baseTime.Month(), baseTime.Day(), baseTime.Hour(), baseTime.Minute(), baseTime.Second(), baseTime.Nanosecond(), baseTime.Location()) -} - -func Time_Current_Midnight() time.Time { - baseTime := time.Now().In(time.FixedZone(fmt.Sprintf("UTC+%d", Offset), Offset*60*60)).AddDate(YearAdjust, MonthAdjust, DayAdjust) - return time.Date(baseTime.Year(), baseTime.Month(), baseTime.Day(), 0, 0, 0, 0, baseTime.Location()) -} - -func TimeWeekStart() time.Time { - midnight := Time_Current_Midnight() - offset := (int(midnight.Weekday()) - 1) * -24 - return midnight.Add(time.Hour * time.Duration(offset)) -} - -func TimeWeekNext() time.Time { - return TimeWeekStart().Add(time.Hour * 24 * 7) -} - -func Time_Current_Week_uint8() uint8 { - baseTime := time.Now().In(time.FixedZone(fmt.Sprintf("UTC+%d", Offset), Offset*60*60)).AddDate(YearAdjust, MonthAdjust, DayAdjust) - - _, thisWeek := baseTime.ISOWeek() - _, beginningOfTheMonth := time.Date(baseTime.Year(), baseTime.Month(), 1, 0, 0, 0, 0, baseTime.Location()).ISOWeek() - - return uint8(1 + thisWeek - beginningOfTheMonth) -} - -func Time_static() time.Time { - if TimeStatic.IsZero() { - TimeStatic = Time_Current_Adjusted() - } - return TimeStatic -} diff --git a/server/entranceserver/make_resp.go b/server/entranceserver/make_resp.go index 0c007c640..d2004d938 100644 --- a/server/entranceserver/make_resp.go +++ b/server/entranceserver/make_resp.go @@ -66,7 +66,7 @@ func encodeServerInfo(config *config.Config, s *Server, local bool) []byte { bf.WriteUint16(0x3039) } } - bf.WriteUint32(uint32(channelserver.Time_Current_Adjusted().Unix())) + bf.WriteUint32(uint32(channelserver.TimeAdjusted().Unix())) bf.WriteUint32(0x0000003C) return bf.Data() }