/* Increments the credits of all home objects that have economy. Usage: -i -n -m */ #include #include #include #include "../include/swserv-plugins.h" typedef struct { time_t next_update, /* In seconds */ last_update, update_int; sw_credits_t credits_inc, credits_limit; } Core; #define CORE(p) ((Core *)(p)) static void EcoInflateIterate(Core *core, SWServContext *ctx); #define DEF_UPDATE_INVERVAL 10l /* In seconds */ #define DEF_CREDITS_INCREMENT 100.0 /* Per update interval */ #define DEF_CREDITS_LIMIT 100000.0 /* Max */ #define USAGE_MESG "\ Usage: -i -n -m " #define ATOI(s) (((s) != NULL) ? atoi(s) : 0) #define ATOL(s) (((s) != NULL) ? atol(s) : 0) #define ATOF(s) (((s) != NULL) ? atof(s) : 0.0f) #define STRDUP(s) (((s) != NULL) ? strdup(s) : NULL) #define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define CLIP(a,l,h) (MIN(MAX((a),(l)),(h))) #define STRLEN(s) (((s) != NULL) ? strlen(s) : 0) #define STRISEMPTY(s) (((s) != NULL) ? (*(s) == '\0') : TRUE) /* * Increases the credits of all home objects with economy by one * increment. */ static void EcoInflateIterate(Core *core, SWServContext *ctx) { const int total = SWServTotalObjects(ctx); int i; xsw_object_struct *obj; xsw_ecodata_struct *eco; xsw_score_struct *score; for(i = 0; i < total; i++) { obj = SWServObjectGetPointer(ctx, i); if(obj == NULL) continue; if(obj->type != SW_OBJ_TYPE_HOME) continue; eco = obj->eco; if(eco == NULL) continue; score = obj->score; if(score == NULL) continue; score->credits += core->credits_inc; if(score->credits > core->credits_limit) score->credits = core->credits_limit; } } /* * SWServ Plugin initialize. */ SWPLUGIN_INIT_FUNCRTN SWPLUGIN_INIT_FUNC( int argc, char **argv, int con_num, SWServContext *ctx ) { int i; const char *arg; Core *core = CORE(calloc(1, sizeof(Core))); if(core == NULL) return(1); core->update_int = DEF_UPDATE_INVERVAL; core->next_update = 0l; core->last_update = 0l; core->credits_inc = DEF_CREDITS_INCREMENT; core->credits_limit = DEF_CREDITS_LIMIT; SWServSetData(ctx, core); for(i = 0; i < argc; i++) { arg = argv[i]; if(arg == NULL) continue; if(!strcasecmp(arg, "--help") || !strcasecmp(arg, "-help") || !strcasecmp(arg, "--h") || !strcasecmp(arg, "-h") ) { if(con_num > -1) SWServConNotify( ctx, con_num, USAGE_MESG ); else SWServPrint( ctx, USAGE_MESG "\n" ); return(1); } /* Interval */ else if(!strcasecmp(arg, "--interval") || !strcasecmp(arg, "-interval") || !strcasecmp(arg, "--int") || !strcasecmp(arg, "-int") || !strcasecmp(arg, "-i") ) { /* In seconds */ i++; if(i < argc) core->update_int = (time_t)MAX(atol(argv[i]), 1l); } /* Credits increment */ else if(!strcasecmp(arg, "--increment") || !strcasecmp(arg, "-increment") || !strcasecmp(arg, "--inc") || !strcasecmp(arg, "-inc") || !strcasecmp(arg, "-n") ) { i++; if(i < argc) core->credits_inc = MAX(atof(argv[i]), 0.0f); } /* Credits max */ else if(!strcasecmp(arg, "--max") || !strcasecmp(arg, "-max") || !strcasecmp(arg, "--m") || !strcasecmp(arg, "-m") ) { i++; if(i < argc) core->credits_limit = MAX(atof(argv[i]), 0.0f); } } core->next_update = SWServCurrentTimeSeconds(ctx); return(0); } /* * SWServ Plugin manage. */ SWPLUGIN_MANAGE_FUNCRTN SWPLUGIN_MANAGE_FUNC(SWServContext *ctx) { Core *core = CORE(SWServGetData(ctx)); const time_t t = SWServCurrentTimeSeconds(ctx); if(core == NULL) return(1); if(t < core->last_update) { core->last_update = t; core->next_update = t; } /* Time to update? */ if(core->next_update <= t) { EcoInflateIterate(core, ctx); core->next_update = t + core->update_int; core->last_update = t; } return(0); } /* * SWServ Plugin shutdown. */ SWPLUGIN_SHUTDOWN_FUNCRTN SWPLUGIN_SHUTDOWN_FUNC(SWServContext *ctx) { Core *core = CORE(SWServGetData(ctx)); if(core == NULL) return; free(core); SWServSetData(ctx, NULL); }