Skip to content

Commit

Permalink
[Zones] Convert Get/Set of Zone Timezone to Repositories (#3946)
Browse files Browse the repository at this point in the history
* [Zones] Convert Get/Set of Zone Timezone to Repositories

- Convert `GetZoneTimezone()` and `SetZoneTimeZone()` to repositories.

* Update time_zone.cpp
  • Loading branch information
Kinglykrab authored Jan 13, 2024
1 parent 73a099c commit d41bd8f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
13 changes: 13 additions & 0 deletions common/repositories/zone_repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 7 additions & 14 deletions zone/gm_commands/set/time_zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,27 +15,20 @@ 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<uint8>(0), static_cast<uint8>(24));

uint8 minutes = sep->IsNumber(3) ? Strings::ToUnsignedInt(sep->arg[3]) : 0;

EQ::Clamp(minutes, static_cast<uint8>(0), static_cast<uint8>(59));

uint8 real_hours = (
(hours - 1) > 0 ?
(hours - 1) :
0
);

if (sep->IsNumber(3)) {
minutes = Strings::ToUnsignedInt(sep->arg[3]);

if (minutes > 59) {
minutes = 59;
}
}

c->Message(
Chat::White,
fmt::format(
Expand All @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion zone/zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
43 changes: 18 additions & 25 deletions zone/zonedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ctime>
#include <iostream>
Expand Down Expand Up @@ -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){
Expand Down
4 changes: 2 additions & 2 deletions zone/zonedb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d41bd8f

Please sign in to comment.