diff --git a/common/repositories/zone_repository.h b/common/repositories/zone_repository.h index 09b91a7885..8b005d8652 100644 --- a/common/repositories/zone_repository.h +++ b/common/repositories/zone_repository.h @@ -44,7 +44,20 @@ class ZoneRepository: public BaseZoneRepository { */ // Custom extended repository methods here + static bool SetTimeZone(Database& db, uint32 zone_id, uint32 instance_version, uint32 timezone) + { + auto results = db.QueryDatabase( + fmt::format( + "UPDATE `{}` SET `timezone` = {} WHERE `zoneidnumber` = {} AND `version` = {}", + TableName(), + timezone, + zone_id, + instance_version + ) + ); + return results.Success(); + } }; #endif //EQEMU_ZONE_REPOSITORY_H diff --git a/zone/gm_commands/set/time_zone.cpp b/zone/gm_commands/set/time_zone.cpp index cb96700c6a..91c48bb483 100755 --- a/zone/gm_commands/set/time_zone.cpp +++ b/zone/gm_commands/set/time_zone.cpp @@ -3,7 +3,7 @@ void SetTimeZone(Client *c, const Seperator *sep) { const auto arguments = sep->argnum; - if (arguments < 2 || sep->IsNumber(2)) { + if (arguments < 2 || !sep->IsNumber(2)) { c->Message(Chat::White, "Usage: #set time_zone [Hour] [Minute]"); c->Message( Chat::White, @@ -15,12 +15,13 @@ void SetTimeZone(Client *c, const Seperator *sep) return; } - uint8 minutes = 0; uint8 hours = Strings::ToUnsignedInt(sep->arg[2]); - if (hours > 24) { - hours = 24; - } + EQ::Clamp(hours, static_cast(0), static_cast(24)); + + uint8 minutes = sep->IsNumber(3) ? Strings::ToUnsignedInt(sep->arg[3]) : 0; + + EQ::Clamp(minutes, static_cast(0), static_cast(59)); uint8 real_hours = ( (hours - 1) > 0 ? @@ -28,14 +29,6 @@ void SetTimeZone(Client *c, const Seperator *sep) 0 ); - if (sep->IsNumber(3)) { - minutes = Strings::ToUnsignedInt(sep->arg[3]); - - if (minutes > 59) { - minutes = 59; - } - } - c->Message( Chat::White, fmt::format( @@ -46,7 +39,7 @@ void SetTimeZone(Client *c, const Seperator *sep) const int new_timezone = ((hours * 60) + minutes); zone->zone_time.setEQTimeZone(new_timezone); - content_db.SetZoneTZ(zone->GetZoneID(), zone->GetInstanceVersion(), new_timezone); + content_db.SetZoneTimezone(zone->GetZoneID(), zone->GetInstanceVersion(), new_timezone); auto outapp = new EQApplicationPacket(OP_TimeOfDay, sizeof(TimeOfDay_Struct)); diff --git a/zone/zone.cpp b/zone/zone.cpp index 3e206b909c..7650e36c1e 100644 --- a/zone/zone.cpp +++ b/zone/zone.cpp @@ -1217,7 +1217,7 @@ bool Zone::Init(bool is_static) { Expedition::CacheAllFromDatabase(); LogInfo("Loading timezone data"); - zone_time.setEQTimeZone(content_db.GetZoneTZ(zoneid, GetInstanceVersion())); + zone_time.setEQTimeZone(content_db.GetZoneTimezone(zoneid, GetInstanceVersion())); LogInfo("Zone booted successfully zone_id [{}] time_offset [{}]", zoneid, zone_time.getEQTimeZone()); diff --git a/zone/zonedb.cpp b/zone/zonedb.cpp index 8e91c18c21..dfc2ee04d4 100755 --- a/zone/zonedb.cpp +++ b/zone/zonedb.cpp @@ -49,6 +49,7 @@ #include "../common/repositories/character_data_repository.h" #include "../common/repositories/character_corpses_repository.h" #include "../common/repositories/character_corpse_items_repository.h" +#include "../common/repositories/zone_repository.h" #include #include @@ -2574,35 +2575,27 @@ void ZoneDatabase::DeleteMerchantTemp(uint32 npc_id, uint32 slot_id, uint32 zone ); } -//New functions for timezone -uint32 ZoneDatabase::GetZoneTZ(uint32 zoneid, uint32 version) { - - std::string query = StringFormat("SELECT timezone FROM zone WHERE zoneidnumber = %i " - "AND (version = %i OR version = 0) ORDER BY version DESC", - zoneid, version); - auto results = QueryDatabase(query); - if (!results.Success()) { - return 0; - } +uint32 ZoneDatabase::GetZoneTimezone(uint32 zone_id, uint32 instance_version) +{ + const auto& l = ZoneRepository::GetWhere( + *this, + fmt::format( + "`zoneidnumber` = {} AND (`version` = {} OR `version` = 0) ORDER BY `version` DESC", + zone_id, + instance_version + ) + ); - if (results.RowCount() == 0) - return 0; + if (l.empty()) { + return 0; + } - auto& row = results.begin(); - return Strings::ToInt(row[0]); + return l[0].id ? l[0].timezone : 0; } -bool ZoneDatabase::SetZoneTZ(uint32 zoneid, uint32 version, uint32 tz) { - - std::string query = StringFormat("UPDATE zone SET timezone = %i " - "WHERE zoneidnumber = %i AND version = %i", - tz, zoneid, version); - auto results = QueryDatabase(query); - if (!results.Success()) { - return false; - } - - return results.RowsAffected() == 1; +bool ZoneDatabase::SetZoneTimezone(uint32 zone_id, uint32 instance_version, uint32 timezone) +{ + return ZoneRepository::SetTimeZone(*this, zone_id, instance_version, timezone); } void ZoneDatabase::RefreshGroupFromDB(Client *client){ diff --git a/zone/zonedb.h b/zone/zonedb.h index 449990150f..3484cfe540 100644 --- a/zone/zonedb.h +++ b/zone/zonedb.h @@ -627,8 +627,8 @@ class ZoneDatabase : public SharedDatabase { bool SetTrapData(Trap* trap, bool repopnow = false); /* Time */ - uint32 GetZoneTZ(uint32 zoneid, uint32 version); - bool SetZoneTZ(uint32 zoneid, uint32 version, uint32 tz); + uint32 GetZoneTimezone(uint32 zoneid, uint32 version); + bool SetZoneTimezone(uint32 zoneid, uint32 version, uint32 tz); /* Group */ void RefreshGroupFromDB(Client *c);