00001
00013 #include "copyright.h"
00014 #include "autoconf.h"
00015 #include "config.h"
00016 #include "externs.h"
00017
00018 #include "attrs.h"
00019
00023 typedef struct player_cache
00024 {
00025 dbref player;
00026 int money;
00027 int queue;
00028 int qmax;
00029 int cflags;
00030 struct player_cache *next;
00031 } PCACHE;
00032
00035 static CHashTable pcache_htab;
00036
00039 static PCACHE *pcache_head;
00040
00041 #define PF_REF 0x0002
00042 #define PF_MONEY_CH 0x0004
00043
00053 void pcache_init(void)
00054 {
00055 pool_init(POOL_PCACHE, sizeof(PCACHE));
00056 pcache_head = NULL;
00057 }
00058
00072 static void pcache_reload1(dbref player, PCACHE *pp)
00073 {
00074 const char *cp = atr_get_raw(player, A_MONEY);
00075 if (cp && *cp)
00076 {
00077 pp->money = mux_atol(cp);
00078 }
00079 else
00080 {
00081 pp->cflags |= PF_MONEY_CH;
00082 pp->money = 0;
00083 }
00084
00085 int m = -1;
00086 cp = atr_get_raw(player, A_QUEUEMAX);
00087 if (cp && *cp)
00088 {
00089 m = mux_atol(cp);
00090 if (m < 0)
00091 {
00092 m = -1;
00093 }
00094 }
00095 pp->qmax = m;
00096 }
00097
00109 static PCACHE *pcache_find(dbref player)
00110 {
00111 PCACHE *pp = (PCACHE *)hashfindLEN(&player, sizeof(player), &pcache_htab);
00112 if (pp)
00113 {
00114 pp->cflags |= PF_REF;
00115 return pp;
00116 }
00117 pp = alloc_pcache("pcache_find");
00118 pp->queue = 0;
00119 pp->cflags = PF_REF;
00120 pp->player = player;
00121 pcache_reload1(player, pp);
00122 pp->next = pcache_head;
00123 pcache_head = pp;
00124 hashaddLEN(&player, sizeof(player), pp, &pcache_htab);
00125 return pp;
00126 }
00127
00134 static void pcache_save(PCACHE *pp)
00135 {
00136 if (pp->cflags & PF_MONEY_CH)
00137 {
00138 IBUF tbuf;
00139 mux_ltoa(pp->money, tbuf);
00140 atr_add_raw(pp->player, A_MONEY, tbuf);
00141 pp->cflags &= ~PF_MONEY_CH;
00142 }
00143 }
00144
00151 void pcache_reload(dbref player)
00152 {
00153 if ( Good_obj(player)
00154 && OwnsOthers(player))
00155 {
00156 PCACHE *pp = pcache_find(player);
00157 pcache_save(pp);
00158 pcache_reload1(player, pp);
00159 }
00160 }
00161
00175 void pcache_trim(void)
00176 {
00177 PCACHE *pp = pcache_head;
00178 PCACHE *pplast = NULL;
00179 while (pp)
00180 {
00181 PCACHE *ppnext = pp->next;
00182 if ( pp->queue
00183 || (pp->cflags & PF_REF))
00184 {
00185
00186
00187
00188 pp->cflags &= ~PF_REF;
00189 pplast = pp;
00190 }
00191 else
00192 {
00193
00194
00195 if (pplast)
00196 {
00197 pplast->next = ppnext;
00198 }
00199 else
00200 {
00201 pcache_head = ppnext;
00202 }
00203
00204 pcache_save(pp);
00205 hashdeleteLEN(&(pp->player), sizeof(pp->player), &pcache_htab);
00206 free_pcache(pp);
00207 }
00208 pp = ppnext;
00209 }
00210 }
00211
00220 void pcache_sync(void)
00221 {
00222 PCACHE *pp = pcache_head;
00223 while (pp)
00224 {
00225 pcache_save(pp);
00226 pp = pp->next;
00227 }
00228 }
00229
00239 int a_Queue(dbref player, int adj)
00240 {
00241 if ( Good_obj(player)
00242 && OwnsOthers(player))
00243 {
00244 PCACHE *pp = pcache_find(player);
00245 pp->queue += adj;
00246 return pp->queue;
00247 }
00248 return 0;
00249 }
00250
00263 int QueueMax(dbref player)
00264 {
00265 int m = 0;
00266 if ( Good_obj(player)
00267 && OwnsOthers(player))
00268 {
00269 PCACHE *pp = pcache_find(player);
00270 if (pp->qmax >= 0)
00271 {
00272 m = pp->qmax;
00273 }
00274 else
00275 {
00276
00277
00278 m = mudconf.queuemax;
00279 if ( Wizard(player)
00280 && m < mudstate.db_top + 1)
00281 {
00282 m = mudstate.db_top + 1;
00283 }
00284 }
00285 }
00286 return m;
00287 }
00288
00295 int Pennies(dbref obj)
00296 {
00297 if (Good_obj(obj))
00298 {
00299 if ( !mudstate.bStandAlone
00300 && OwnsOthers(obj))
00301 {
00302 PCACHE *pp = pcache_find(obj);
00303 return pp->money;
00304 }
00305 else
00306 {
00307 const char *cp = atr_get_raw(obj, A_MONEY);
00308 if (cp)
00309 {
00310 return mux_atol(cp);
00311 }
00312 }
00313 }
00314 return 0;
00315 }
00316
00317
00328 void s_Pennies(dbref obj, int howfew)
00329 {
00330 if (Good_obj(obj))
00331 {
00332 if ( !mudstate.bStandAlone
00333 && OwnsOthers(obj))
00334 {
00335 PCACHE *pp = pcache_find(obj);
00336 pp->money = howfew;
00337 pp->cflags |= PF_MONEY_CH;
00338 }
00339 else
00340 {
00341 IBUF tbuf;
00342 mux_ltoa(howfew, tbuf);
00343 atr_add_raw(obj, A_MONEY, tbuf);
00344 }
00345 }
00346 }
00347
00361 void s_PenniesDirect(dbref obj, int howfew)
00362 {
00363 IBUF tbuf;
00364 mux_ltoa(howfew, tbuf);
00365 atr_add_raw(obj, A_MONEY, tbuf);
00366 }