#include "copyright.h"
#include "autoconf.h"
#include "config.h"
#include "externs.h"
#include "attrs.h"
Include dependency graph for player_c.cpp:
Go to the source code of this file.
Data Structures | |
struct | player_cache |
structure to hold cached data for player-type objects. More... | |
Defines | |
#define | PF_REF 0x0002 |
#define | PF_MONEY_CH 0x0004 |
Typedefs | |
typedef player_cache | PCACHE |
structure to hold cached data for player-type objects. | |
Functions | |
void | pcache_init (void) |
Initializes the player cache. | |
static void | pcache_reload1 (dbref player, PCACHE *pp) |
Updates player cache items from the database. | |
static PCACHE * | pcache_find (dbref player) |
Returns a player's cache record. | |
static void | pcache_save (PCACHE *pp) |
Saves any dirty player data items to the database. | |
void | pcache_reload (dbref player) |
Re-initializes Money and QueueMax items from the database. | |
void | pcache_trim (void) |
Ages and trims the player cache of stale entries. | |
void | pcache_sync (void) |
Flushes any dirty player items to the database. | |
int | a_Queue (dbref player, int adj) |
Adjusts the count of queued commands up or down. | |
int | QueueMax (dbref player) |
Returns the player's upper limit of queued commands. | |
int | Pennies (dbref obj) |
Returns how many coins are in a player's or things's purse. | |
void | s_Pennies (dbref obj, int howfew) |
Sets the number of coins in a player's or thing's purse. | |
void | s_PenniesDirect (dbref obj, int howfew) |
A shortcut method of initializing the coins in a object's purse. | |
Variables | |
static CHashTable | pcache_htab |
Hash Table which maps player dbref to PCACHE entry. | |
static PCACHE * | pcache_head |
The head of a singly-linked list of all PCACHE entries. |
Frequenty-used items which appear on every object generally find a home in the db[] structure managed in db.cpp. However, there are a few items related only to players which are still accessed frequently enough that they should be cached. These items are money, current number of queued commands, and the limit on the number of queued commands.
Definition in file player_c.cpp.
#define PF_MONEY_CH 0x0004 |
Definition at line 42 of file player_c.cpp.
Referenced by pcache_reload1(), pcache_save(), and s_Pennies().
#define PF_REF 0x0002 |
typedef struct player_cache PCACHE |
structure to hold cached data for player-type objects.
int a_Queue | ( | dbref | player, | |
int | adj | |||
) |
Adjusts the count of queued commands up or down.
cque.cpp uses this as it schedules and performs queued commands.
player | dbref of player object responsible for command. | |
adj | new (+) or completed (-) commands being queued. |
Definition at line 239 of file player_c.cpp.
References Good_obj, OwnsOthers, pcache_find(), and player_cache::queue.
Referenced by CallBack_HaltQueue(), CallBack_NotifySemaphoreDrainOrAll(), halt_que(), setup_que(), and Task_RunQueueEntry().
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 }
Returns a player's cache record.
Whether created from scratch or found in the cache, pcache_find() always returns a valid player cache record for the requested player object dbref. This function uses Hash Table access primarily, but it maintains the singly-linked list as well.
player | player object dbref. |
Definition at line 109 of file player_c.cpp.
References alloc_pcache, player_cache::cflags, hashaddLEN(), hashfindLEN(), player_cache::next, pcache_head, pcache_htab, pcache_reload1(), PF_REF, player_cache::player, and player_cache::queue.
Referenced by a_Queue(), pcache_reload(), Pennies(), QueueMax(), and s_Pennies().
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 }
void pcache_init | ( | void | ) |
Initializes the player cache.
This is called once to initialize the player cache and supporting data structures: Player cache structures are pooled, the Hash Table initializes itself, and the singly-linked list is started.
Definition at line 53 of file player_c.cpp.
References pcache_head, pool_init(), and POOL_PCACHE.
Referenced by main().
00054 { 00055 pool_init(POOL_PCACHE, sizeof(PCACHE)); 00056 pcache_head = NULL; 00057 }
void pcache_reload | ( | dbref | player | ) |
Re-initializes Money and QueueMax items from the database.
player | player object dbref. |
Definition at line 151 of file player_c.cpp.
References Good_obj, OwnsOthers, pcache_find(), pcache_reload1(), and pcache_save().
Referenced by atr_add_raw_LEN(), and atr_clr().
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 }
Updates player cache items from the database.
The Money and QueueMax attributes are used to initialize the corresponding items in the player cache. If a Money attribute does not exist for some strange reason, it it initialized to zero and marked as dirty. If a QueueMax attribute doesn't exist or is negative, then the game will choose a reasonable limit later in QueueMax().
player | player object to begin caching. | |
pp | pointer to PCACHE structure. |
Definition at line 72 of file player_c.cpp.
References A_MONEY, A_QUEUEMAX, atr_get_raw(), player_cache::cflags, player_cache::money, mux_atol(), PF_MONEY_CH, and player_cache::qmax.
Referenced by pcache_find(), and pcache_reload().
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 }
static void pcache_save | ( | PCACHE * | pp | ) | [static] |
Saves any dirty player data items to the database.
pp | pointer to potentially dirty PCACHE structure. |
Definition at line 134 of file player_c.cpp.
References A_MONEY, atr_add_raw(), player_cache::cflags, player_cache::money, mux_ltoa(), PF_MONEY_CH, and player_cache::player.
Referenced by pcache_reload(), pcache_sync(), and pcache_trim().
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 }
void pcache_sync | ( | void | ) |
Flushes any dirty player items to the database.
The primary access is via the singly-linked list. Upon return, all the player cache records are marked as clean.
Definition at line 220 of file player_c.cpp.
References pcache_head, and pcache_save().
Referenced by do_dbclean(), do_restart(), do_shutdown(), dump_database(), fork_and_dump(), and sighandler().
00221 { 00222 PCACHE *pp = pcache_head; 00223 while (pp) 00224 { 00225 pcache_save(pp); 00226 pp = pp->next; 00227 } 00228 }
void pcache_trim | ( | void | ) |
Ages and trims the player cache of stale entries.
pcache_trim() relies primarily on the singly-linked list, but it also maintains the Hash Table. To be trimmed, a player cache record must not have outstanding commands in the command queue.
The one level of aging is accomplished with PR_REF. On the first pass through the linked list, the PR_REF bit is removed. On the second pass through the list, the record is trimmed.
Definition at line 175 of file player_c.cpp.
References player_cache::cflags, free_pcache, hashdeleteLEN(), player_cache::next, pcache_head, pcache_htab, pcache_save(), PF_REF, player_cache::player, and player_cache::queue.
Referenced by dispatch_FreeListReconstruction().
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 // This entry either has outstanding commands in the queue or we 00186 // need to let it age. 00187 // 00188 pp->cflags &= ~PF_REF; 00189 pplast = pp; 00190 } 00191 else 00192 { 00193 // Unlink and destroy this entry. 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 }
int Pennies | ( | dbref | obj | ) |
Returns how many coins are in a player's or things's purse.
player | dbref of player object. |
Definition at line 295 of file player_c.cpp.
References A_MONEY, atr_get_raw(), statedata::bStandAlone, Good_obj, player_cache::money, mudstate, mux_atol(), OwnsOthers, and pcache_find().
Referenced by canpayfees(), check_pennies(), CGuests::Create(), db_write_object(), debug_examine(), destroy_obj(), do_chown(), do_clone(), do_decomp(), do_examine(), do_kill(), do_poor(), do_score(), FUNCTION(), give_money(), giveto(), CGuests::MakeGuestChar(), move_object(), payfor(), and shutdownsock().
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 }
int QueueMax | ( | dbref | player | ) |
Returns the player's upper limit of queued commands.
If a QueueMax is set on the player, we use that. Otherwise, there is a configurable game-wide limit (given by player_queue_limit) unless the player is a Wizard in which case, we reason that well behaved Wizard code should be able to schedule as much work as there are objects in the database -- larger game, more work to be expected in the queue.
player | dbref of player object. |
Definition at line 263 of file player_c.cpp.
References statedata::db_top, Good_obj, mudconf, mudstate, OwnsOthers, pcache_find(), player_cache::qmax, confdata::queuemax, and Wizard.
Referenced by setup_que().
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 // @queuemax was not valid so we use the game-wide limit. 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 }
void s_Pennies | ( | dbref | obj, | |
int | howfew | |||
) |
Sets the number of coins in a player's or thing's purse.
This changes the number of coins a player holds and sets this attribute as dirty so that it will be updated in the attribute database later.
player | dbref of player object responsible for command. | |
howfew | Number of coins |
Definition at line 328 of file player_c.cpp.
References A_MONEY, atr_add_raw(), statedata::bStandAlone, player_cache::cflags, Good_obj, player_cache::money, mudstate, mux_ltoa(), OwnsOthers, pcache_find(), and PF_MONEY_CH.
Referenced by check_pennies(), CGuests::Create(), create_obj(), db_make_minimal(), destroy_bad_obj(), destroy_obj(), do_clone(), do_fixdb(), do_poor(), do_toad(), giveto(), CGuests::MakeGuestChar(), and payfor().
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 }
void s_PenniesDirect | ( | dbref | obj, | |
int | howfew | |||
) |
A shortcut method of initializing the coins in a object's purse.
This method should only be used from db_rw.cpp while loading the database. From there, the object will be in a half-way state, and has not been fully loaded. The object type is not known. Likewise, at database load time, using the player cache is ineffective -- causing a read request for A_MONEY to obtain a value for coins (probably zero) that we immediate change again.
obj | dbref of object. | |
howfew | Number of coins |
Definition at line 361 of file player_c.cpp.
References A_MONEY, atr_add_raw(), and mux_ltoa().
Referenced by db_read().
00362 { 00363 IBUF tbuf; 00364 mux_ltoa(howfew, tbuf); 00365 atr_add_raw(obj, A_MONEY, tbuf); 00366 }
PCACHE* pcache_head [static] |
The head of a singly-linked list of all PCACHE entries.
Definition at line 39 of file player_c.cpp.
Referenced by pcache_find(), pcache_init(), pcache_sync(), and pcache_trim().
CHashTable pcache_htab [static] |
Hash Table which maps player dbref to PCACHE entry.
Definition at line 35 of file player_c.cpp.
Referenced by pcache_find(), and pcache_trim().