Skip to content

Instantly share code, notes, and snippets.

@AnnieRuru
Last active September 21, 2020 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnnieRuru/82335a62e8a6133511cda473ade8d322 to your computer and use it in GitHub Desktop.
Save AnnieRuru/82335a62e8a6133511cda473ade8d322 to your computer and use it in GitHub Desktop.
#include "common/hercules.h"
#include "map/pc.h"
#include "map/npc.h"
#include "map/clif.h"
#include "common/memmgr.h"
#include "plugins/HPMHooking.h"
#include "common/HPMDataCheck.h"
HPExport struct hplugin_info pinfo = {
"attendance_staylogin",
SERVER_TYPE_MAP,
"",
HPM_VERSION,
};
int require_stay_seconds = 90; // change to 3600 as 1 hour
// Returns YYYYMMDD of now
int date_get_date(void)
{
time_t t;
struct tm * lt;
t = time(NULL);
lt = localtime(&t);
return
(lt->tm_year + 1900) * 10000 +
(lt->tm_mon + 1) * 100 +
(lt->tm_mday);
}
struct player_data {
int unixtime_login;
};
// trigger when login
static int pc_reg_received_post(int retVal, struct map_session_data *sd) {
if (retVal == 0)
return 0;
struct player_data *ssd;
CREATE(ssd, struct player_data, true);
ssd->unixtime_login = (int)time(NULL);
addToMSD(sd, ssd, 0, true);
if (pc_readaccountreg(sd, script->add_variable("#Attendance_date")) != date_get_date()) {
pc_setaccountreg(sd, script->add_variable("#Attendance_date"), date_get_date());
pc_setaccountreg(sd, script->add_variable("#Attendance_staylogin"), 0);
pc_setaccountreg(sd, script->add_variable("#Attendance_claimed"), 0);
}
return 1;
}
// trigger when logout
static int map_quit_pre(struct map_session_data **sd) {
struct player_data *ssd = getFromMSD(*sd, 0);
if (pc_readaccountreg(*sd, script->add_variable("#Attendance_claimed")) == 0)
pc_setaccountreg(*sd, script->add_variable("#Attendance_staylogin"), pc_readaccountreg(*sd, script->add_variable("#Attendance_staylogin")) + (int)time(NULL) - ssd->unixtime_login);
return 0;
}
// deny claim reward
static void clif_parse_attendance_reward_request_pre(int *fd, struct map_session_data **sd) {
if ((*sd)->state.trading || pc_isdead(*sd) || pc_isvending(*sd))
return;
if ((*sd)->status.base_level < 75) {
clif->messagecolor_self((*sd)->fd, COLOR_RED, "You must be at least level 75 before you qualify for Daily Attendance.");
hookStop();
return;
}
struct player_data *ssd = getFromMSD(*sd, 0);
int var_staylogin = pc_readaccountreg(*sd, script->add_variable("#Attendance_staylogin"));
if (var_staylogin + time(NULL) - ssd->unixtime_login < require_stay_seconds) {
char tmp[CHAT_SIZE_MAX];
safesnprintf(tmp, CHAT_SIZE_MAX, "You still need %d seconds to claim the reward", require_stay_seconds - (var_staylogin + (int)time(NULL) - ssd->unixtime_login));
clif->messagecolor_self((*sd)->fd, COLOR_RED, tmp);
hookStop();
}
return;
}
// after collect reward, set variable
static void clif_parse_attendance_reward_request_post(int fd, struct map_session_data *sd) {
pc_setaccountreg(sd, script->add_variable("#Attendance_claimed"), 1);
return;
}
// reset variable when server touch 12am
static int npc_event_do_clock_post(int retVal, int tid, int64 tick, int id, intptr_t data) {
static struct tm ev_tm_b; // tracks previous execution time
time_t clock = time(NULL);
struct tm* t = localtime(&clock);
if (t->tm_min != ev_tm_b.tm_min && t->tm_hour == 0 && t->tm_min == 0) {
struct map_session_data *sd;
struct player_data *ssd;
struct s_mapiterator *iter = mapit->alloc(MAPIT_NORMAL, BL_PC);
for (sd = (struct map_session_data*)mapit->first(iter); mapit->exists(iter); sd = (struct map_session_data*)mapit->next(iter)) {
if ((ssd = getFromMSD(sd, 0))) {
pc_setaccountreg(sd, script->add_variable("#Attendance_date"), date_get_date());
pc_setaccountreg(sd, script->add_variable("#Attendance_staylogin"), 0);
pc_setaccountreg(sd, script->add_variable("#Attendance_claimed"), 0);
ssd->unixtime_login = 0;
}
}
mapit->free(iter);
}
memcpy(&ev_tm_b,t,sizeof(ev_tm_b));
return retVal;
}
HPExport void plugin_init (void) {
addHookPost(pc, reg_received, pc_reg_received_post);
addHookPre(map, quit, map_quit_pre);
addHookPre(clif, pAttendanceRewardRequest, clif_parse_attendance_reward_request_pre);
addHookPost(clif, pAttendanceRewardRequest, clif_parse_attendance_reward_request_post);
addHookPost(npc, event_do_clock, npc_event_do_clock_post);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment