From: kainazzzo Date: Mon, 27 Oct 2003 22:13:21 +0000 (+0000) Subject: Added functions to check and see if midnight has passed. If midnight has passed,... X-Git-Url: https://jfr.im/git/irc/gameservirc.git/commitdiff_plain/44ea29f75e5600eaa0f1996e8d0e4aac4e760cc8?hp=5b72cf505989156218880f602e68f151c6ea3f3a Added functions to check and see if midnight has passed. If midnight has passed, then reset all player data for a new day. git-svn-id: https://svn.code.sf.net/p/gameservirc/code/trunk@38 bc333340-6410-0410-a689-9d09f3c113fa --- diff --git a/gameserv/config.h.in b/gameserv/config.h.in index df91fef..38e3c84 100644 --- a/gameserv/config.h.in +++ b/gameserv/config.h.in @@ -9,6 +9,9 @@ /* Define if you have the ANSI C header files. */ #undef STDC_HEADERS +/* Define if you can safely include both and . */ +#undef TIME_WITH_SYS_TIME + /* Define if you have the socket function. */ #undef HAVE_SOCKET diff --git a/gameserv/configure b/gameserv/configure index eb9a56c..7b0989f 100755 --- a/gameserv/configure +++ b/gameserv/configure @@ -900,21 +900,56 @@ EOF fi +echo $ac_n "checking whether time.h and sys/time.h may both be included""... $ac_c" 1>&6 +echo "configure:905: checking whether time.h and sys/time.h may both be included" >&5 +if eval "test \"`echo '$''{'ac_cv_header_time'+set}'`\" = set"; then + echo $ac_n "(cached) $ac_c" 1>&6 +else + cat > conftest.$ac_ext < +#include +#include +int main() { +struct tm *tp; +; return 0; } +EOF +if { (eval echo configure:919: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then + rm -rf conftest* + ac_cv_header_time=yes +else + echo "configure: failed program was:" >&5 + cat conftest.$ac_ext >&5 + rm -rf conftest* + ac_cv_header_time=no +fi +rm -f conftest* +fi + +echo "$ac_t""$ac_cv_header_time" 1>&6 +if test $ac_cv_header_time = yes; then + cat >> confdefs.h <<\EOF +#define TIME_WITH_SYS_TIME 1 +EOF + +fi + for ac_hdr in limits.h unistd.h crypt.h do ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'` echo $ac_n "checking for $ac_hdr""... $ac_c" 1>&6 -echo "configure:908: checking for $ac_hdr" >&5 +echo "configure:943: checking for $ac_hdr" >&5 if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < EOF ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out" -{ (eval echo configure:918: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; } +{ (eval echo configure:953: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; } ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"` if test -z "$ac_err"; then rm -rf conftest* @@ -942,12 +977,12 @@ done echo $ac_n "checking for working const""... $ac_c" 1>&6 -echo "configure:946: checking for working const" >&5 +echo "configure:981: checking for working const" >&5 if eval "test \"`echo '$''{'ac_cv_c_const'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext <&5; (eval $ac_compile) 2>&5; }; then +if { (eval echo configure:1035: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then rm -rf conftest* ac_cv_c_const=yes else @@ -1017,12 +1052,12 @@ EOF fi echo $ac_n "checking for size_t""... $ac_c" 1>&6 -echo "configure:1021: checking for size_t" >&5 +echo "configure:1056: checking for size_t" >&5 if eval "test \"`echo '$''{'ac_cv_type_size_t'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext < #if STDC_HEADERS @@ -1053,12 +1088,12 @@ fi for ac_func in socket strcspn strspn strtol strtok do echo $ac_n "checking for $ac_func""... $ac_c" 1>&6 -echo "configure:1057: checking for $ac_func" >&5 +echo "configure:1092: checking for $ac_func" >&5 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then echo $ac_n "(cached) $ac_c" 1>&6 else cat > conftest.$ac_ext <&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then +if { (eval echo configure:1120: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then rm -rf conftest* eval "ac_cv_func_$ac_func=yes" else @@ -1105,7 +1140,6 @@ else fi done - trap '' 1 2 15 cat > confcache <<\EOF # This file is a shell script that caches the results of configure diff --git a/gameserv/configure.in b/gameserv/configure.in index 44233d5..48762b3 100644 --- a/gameserv/configure.in +++ b/gameserv/configure.in @@ -13,6 +13,7 @@ AC_CHECK_LIB(crypt, crypt) dnl Checks for header files. AC_HEADER_STDC +AC_HEADER_TIME AC_CHECK_HEADERS(limits.h unistd.h crypt.h) dnl Checks for typedefs, structures, and compiler characteristics. @@ -21,5 +22,4 @@ AC_TYPE_SIZE_T dnl Checks for library functions. AC_CHECK_FUNCS(socket strcspn strspn strtol strtok) - AC_OUTPUT(Makefile) diff --git a/gameserv/extern.h b/gameserv/extern.h index 242a283..7e80946 100644 --- a/gameserv/extern.h +++ b/gameserv/extern.h @@ -9,6 +9,25 @@ #include #include "player.h" #include "list.h" +#include "config.h" + +#if TIME_WITH_SYS_TIME +# include +# include +#else +# if HAVE_SYS_TIME_H +# include +# else +# include +# endif +#endif + + +// The timestamp from load time to be compared against the current midnight time. +E long timestamp; +E void save_timestamp(); +E void load_timestamp(); +E long int midnight(long int offset = 8); // The socket E int sock; @@ -80,7 +99,7 @@ E void delete_monsters(); E void delete_masters(); E Monster *getNewMonster(Monster *m); E void deleteMonster(Monster *m); -E void refresh(aClient *ni); +E void refresh(Player *p); E void refreshall(); E void reset(aClient *ni); diff --git a/gameserv/gameserv.cpp b/gameserv/gameserv.cpp index b60ed0d..225c1bf 100644 --- a/gameserv/gameserv.cpp +++ b/gameserv/gameserv.cpp @@ -64,7 +64,7 @@ long int pow (int x, int y); long int stringtoint(char *number); char *spaces(int len, char *seperator); -void refresh(aClient *ni); +void refresh(Player *p); void refreshall(); void reset(aClient *ni); void init_masters(); @@ -111,7 +111,6 @@ int hpbonus[11] = {10, 15, 20, 30, 50, 75, 125, 185, 250, 350, 550}; int strbonus[11] = {5, 7, 10, 12, 20, 35, 50, 75, 110, 150, 200}; int defbonus[11] = {2, 3, 5, 10, 15, 22, 35, 60, 80, 120, 150}; - void gameserv(char *source, char *buf) { char *cmd; @@ -121,11 +120,20 @@ void gameserv(char *source, char *buf) cmd++; // Get rid of that : at the beginning of the :text (command) cout << "Source: " << source << "\ncmd: " << cmd << endl; + long int mn = midnight(); + + if (mn > timestamp) + { + refreshall(); + timestamp = mn; + save_timestamp(); + } + if (strnicmp(cmd, ":\1PING", 6) == 0) { - char *timestamp; - timestamp = strtok(NULL, "\1"); - notice(s_GameServ, source, "\1PING %s\1", timestamp); + char *ts; + ts = strtok(NULL, "\1"); + notice(s_GameServ, source, "\1PING %s\1", ts); } else if (stricmp(cmd, ":\1VERSION\1") == 0) { notice(s_GameServ, source, "\1VERSION GameServ v1.0b\1"); } else if (stricmp(cmd, "SEARCH") == 0) { @@ -205,6 +213,15 @@ void gameserv(char *source, char *buf) { notice(s_GameServ, source, "SYNTAX: /msg %S RAW "); } + } else if (stricmp(cmd, "TESTSTAMP") == 0) { + char *pass = strtok(NULL, " "); + if (pass != NULL && (stricmp(pass, adminpass) == 0)) + { + mn = time(NULL); + notice(s_GameServ, source, "Resetting timestamp"); + } + else + notice(s_GameServ, source, "SYNTAX: /msg %S TESTSTAMP "); } source--; // Bring the : back so we don't leak memory @@ -2990,3 +3007,29 @@ void showBankBalance(const char *u) notice(s_GameServ, u, "Account Balance: %ld Gold On hand: %ld", p->bank, p->gold); } + +void refreshall() +{ + ListNode *it; + Player *p; + + it = players.First(); + + while (it) + { + p = it->getData()->stats; + refresh(p); + it = it->Next(); + } +} + +void refresh(Player *p) +{ + if (!p) + return; + + p->hp = p->maxhp; + p->forest_fights = 100; + p->player_fights = 3; + p->alive = true; +} diff --git a/gameserv/tcpclient.cpp b/gameserv/tcpclient.cpp index 17375ae..bcc98fa 100644 --- a/gameserv/tcpclient.cpp +++ b/gameserv/tcpclient.cpp @@ -20,21 +20,26 @@ #include #include #include +#include #include -#include #include int sock; +long timestamp; + List clients; +void save_timestamp(); +void load_timestamp(); + int main(int argc, char *argv[]) { char buffer[1024], buf[1024]; int connected = 1; char *cmd, *source = NULL; srand(time(NULL)); - - load_config_file(); + + load_config_file(); if (argc == 1) { argc = 3; @@ -74,6 +79,8 @@ int main(int argc, char *argv[]) init_monsters(); init_masters(); load_gs_dbase(); + load_timestamp(); + while (connected) { if (sock_gets(sock,buffer,sizeof(buffer)) == -1) { connected = 0; @@ -139,6 +146,7 @@ int main(int argc, char *argv[]) } } save_gs_dbase(); + save_timestamp(); delete_monsters(); delete_masters(); @@ -210,3 +218,46 @@ aClient *findbynick(const char *nick) return client; } +void load_timestamp() +{ + ifstream infile; + + infile.open(".gstimestamp"); + + if (infile.fail()) + { + cerr << "Error opening .gstimestamp" << endl; + cerr << "Generating new timestamp" << endl; + generate: + timestamp = midnight(); + save_timestamp(); + return; + } + + infile >> timestamp; + infile.close(); + if (timestamp < 1000000) + goto generate; +} + +void save_timestamp() +{ + ofstream outfile; + + outfile.open(".gstimestamp"); + + if (outfile.fail()) + { + cerr << "Error creating new file." << endl; + return; + } + + outfile << timestamp << endl; + + outfile.close(); +} + +long int midnight(long int offset) +{ + return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600); +}