/[drupal]/contributions/modules/game_character/game_character.module
ViewVC logotype

Contents of /contributions/modules/game_character/game_character.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Fri Jan 2 22:35:51 2009 UTC (10 months, 3 weeks ago) by aaron
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +2 -2 lines
File MIME type: text/x-php
On insert/update, set the node as its owner's currently active character (aaron).
1 <?php
2 // $Id: game_character.module,v 1.2 2009/01/02 22:34:37 aaron Exp $
3
4 /**
5 * @file
6 * Utility to set certain node types as characters for users.
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function game_character_help($path, $arg) {
13 switch ($path) {
14 case 'admin/settings/game_character':
15 return '<p>'. t('Configure game characters with these settings.') .'</p>';
16 }
17 }
18
19 /**
20 * Implementation of hook_menu().
21 */
22 function game_character_menu() {
23 $items = array();
24
25 $items['admin/settings/game_character'] = array(
26 'access arguments' => array('administer game characters'),
27 'description' => 'Configure game characters with these settings.',
28 'file' => 'game_character.admin.inc',
29 'page callback' => 'drupal_get_form',
30 'page arguments' => array('game_character_settings'),
31 'title' => 'Game characters',
32 );
33 $items['game_character/claim/%node'] = array(
34 'access' => 'game_character_claim_access',
35 'access arguments' => array(2),
36 'file' => 'game_character.pages.inc',
37 'page callback' => 'game_character_character_claim',
38 'page arguments' => array(2),
39 'title' => 'Claim game character',
40 'type' => MENU_CALLBACK,
41 );
42 $items['game_character/activate/%node'] = array(
43 'access' => 'game_character_activate_access',
44 'access arguments' => array(2),
45 'file' => 'game_character.pages.inc',
46 'page callback' => 'game_character_character_activate',
47 'page arguments' => array(2),
48 'title' => 'Activate game character',
49 'type' => MENU_CALLBACK,
50 );
51 $items['game_character/inactivate/%node'] = array(
52 'access' => 'game_character_inactivate_access',
53 'access arguments' => array(2),
54 'file' => 'game_character.pages.inc',
55 'page callback' => 'game_character_character_inactivate',
56 'page arguments' => array(2),
57 'title' => 'Inactivate game character',
58 'type' => MENU_CALLBACK,
59 );
60
61 return $items;
62 }
63
64 /**
65 * Implementation of hook_perm().
66 */
67 function game_character_perm() {
68 return array(
69 'administer game characters',
70 'play any game characters',
71 'play own game characters',
72 'claim anonymous game characters',
73 );
74 }
75
76 function game_character_activate_access($node) {
77 global $user;
78 $active = game_character_get_active();
79 return $active['nid'] != $node->nid && (user_access('administer game characters') || user_access('play any game characters') || (($node->uid == $user->uid) && user_access('play own game characters')));
80 }
81
82 function game_character_inactivate_access($node) {
83 $active = game_character_get_active();
84 return $active['nid'] == $node->nid;
85 }
86
87 /**
88 * Access callback for game_character/claim/%node.
89 * Returns TRUE if a node is owned by the anonymous user, is a game character
90 * node type, and the user has the proper access.
91 */
92 function game_character_claim_access($node) {
93 global $user;
94 if (game_character_verify_node($node, $user->uid)) {
95 return user_access('claim anonymous game characters') && (!$node->uid);
96 }
97 return FALSE;
98 }
99
100 /**
101 * Verifies that the node is of a type playable as a game character,
102 * and the user has proper access.
103 */
104 function game_character_verify_node($node, $uid) {
105 $types = variable_get('game_character_types', array());
106 if (!$types[$node->type]) {
107 return FALSE;
108 }
109 if (user_access('administer game characters') || user_access('play any game characters')) {
110 return TRUE;
111 }
112 return ($node->uid == $uid) && user_access('play own game characters');
113 }
114
115 /**
116 * Returns the current active game character node for the user.
117 * If stored in the session, then return that. Otherwise, read the latest
118 * from the database.
119 * @param $uid
120 * (optional) The user's UID. If NULL, then use the global $user.
121 * @param $reset
122 * (optional) If TRUE, then reset the global cache.
123 * @return
124 * An array with the following information:
125 * 'node' => The character node,
126 * 'nid' => The node's nid,
127 * 'uid' => The user's uid,
128 * 'access' => The time of last access.
129 */
130 function game_character_get_active($uid = NULL, $reset = FALSE) {
131 global $game_characters, $user;
132 if (is_null($game_characters) || $reset) {
133 $game_characters = array();
134 }
135 if (is_null($uid)) {
136 $uid = $user->uid;
137 }
138 if (!is_array($game_characters[$uid])) {
139 if (variable_get('game_character_store_active_in_session', TRUE) && ($uid == $user->uid) && $_SESSION['game_character_active']) {
140 $node = node_load($_SESSION['game_character_active']);
141 if (game_character_verify_node($node, $uid)) {
142 $game_characters[$uid] = array(
143 'node' => $node,
144 'nid' => $node->nid,
145 'uid' => $uid,
146 'access' => time(),
147 );
148 game_character_write_active($game_characters[$uid]);
149 }
150 }
151 }
152 if (!is_array($game_characters[$uid]) && $uid) {
153 $node = node_load(db_result(db_query_range("SELECT nid FROM {game_characters} WHERE uid=%d ORDER BY access DESC", $uid, 0, 1)));
154 if (game_character_verify_node($node, $uid)) {
155 $game_characters[$uid] = array(
156 'node' => $node,
157 'nid' => $node->nid,
158 'uid' => $uid,
159 'access' => time(),
160 );
161 game_character_write_active($game_characters[$uid], TRUE);
162 }
163 }
164 return $game_characters[$uid];
165 }
166
167 /**
168 * Sets the active game character for the user.
169 * @param $uid
170 * (optional) The user's uid.
171 * @param $node
172 * (optional) The character node. If not provided, we assume the character
173 * node has already been set, either from an earlier call or from
174 * game_character_get_active().
175 * @return
176 * The game character array (as for game_character_get_active) is returned
177 * if successful. Otherwise NULL.
178 */
179 function game_character_set_active($uid = NULL, $node = NULL) {
180 global $game_characters, $user;
181 if (is_null($game_characters)) {
182 $game_characters = array();
183 }
184
185 // Set the default arguments.
186 if (is_null($uid)) {
187 $uid = $user->uid;
188 }
189 if (is_null($node)) {
190 $node = $game_characters[$uid];
191 }
192
193 // Make sure we unset the current character node, in case it's been made invalid, deleted, etc.
194 unset($game_characters[$uid]);
195
196 if (game_character_verify_node($node, $uid)) {
197 $game_characters[$uid] = array(
198 'node' => $node,
199 'nid' => $node->nid,
200 'uid' => $user->uid,
201 'access' => time(),
202 );
203 game_character_write_active($game_characters[$uid]);
204 }
205 return $game_characters[$uid];
206 }
207
208 /**
209 * Write the active game character to the database.
210 * @param $character
211 * A game character array, as for game_character_get_active.
212 * @param $update
213 * If TRUE, then this is an update. Otherwise, we don't know,
214 * and must look up whether the entry exists.
215 */
216 function game_character_write_active($character, $update = FALSE) {
217 global $user;
218
219 // Set the session's active character, if applicable.
220 if ($character['nid'] && variable_get('game_character_store_active_in_session', TRUE) && ($user->uid == $character['uid'])) {
221 $_SESSION['game_character_active'] = $character['nid'];
222 }
223 else {
224 unset($_SESSION['game_character_active']);
225 }
226
227 // If a user may not control multiple game characters at a time, then wipe out their existing records.
228 if ($character['uid'] && !variable_get('game_character_multiple_active', TRUE)) {
229 $update = FALSE;
230 db_query("DELETE FROM {game_characters} WHERE uid=%d", $character['uid']);
231 }
232
233 if ($character['uid'] && $character['nid']) {
234 // Store the information in the database.
235 if ($update || db_result(db_query("SELECT nid FROM {game_characters} WHERE uid=%d AND nid=%d", $uid, $node->nid))) {
236 return drupal_write_record('game_characters', $character, array('nid', 'uid'));
237 }
238 else {
239 return drupal_write_record('game_characters', $character);
240 }
241 }
242 }
243
244 /**
245 * Implements hook_link().
246 */
247 function game_character_link($type, $node, $teaser = FALSE) {
248 if ($type == 'node') {
249 $types = variable_get('game_character_types', array());
250
251 // Don't add any links to nodes that are not game characters.
252 if (!$types[$node->type]) {
253 return;
254 }
255 global $user;
256 $active = game_character_get_active();
257 $links = array();
258
259 // Add a claim code to anonymous characters in session, if the user is allowed to claim it.
260 if (!$node->uid && $user->uid && user_access('claim anonymous game characters') && variable_get('game_character_store_active_in_session', TRUE) && ($_SESSION['game_character_active'] == $node->nid)) {
261 $claim = db_result(db_query("SELECT claim FROM {game_character_claims} WHERE nid=%d", $node->nid));
262 $links['game_character_claim'] = array(
263 'title' => t('claim character'),
264 'href' => 'game_character/claim/'. $node->nid .'/'. $claim,
265 'attributes' => array(
266 'title' => t('Claim this character as your own.'),
267 ),
268 );
269 }
270
271 // Add an 'activate character' link if the user has proper perms.
272 if (game_character_activate_access($node)) {
273 $links['game_character'] = array(
274 'title' => t('activate character'),
275 'href' => 'game_character/activate/'. $node->nid,
276 'attributes' => array(
277 'title' => t('Activate this game character.'),
278 ),
279 );
280 }
281
282 // Add an 'inactivate character' link if the node is currently active.
283 if ($active['nid'] == $node->nid) {
284 $links['game_character'] = array(
285 'title' => t('inactivate character'),
286 'href' => 'game_character/inactivate/'. $node->nid,
287 'attributes' => array(
288 'title' => t('Inactivate this game character.'),
289 ),
290 );
291 }
292 return $links;
293 }
294 }
295
296 /**
297 * Implements hook_nodeapi().
298 */
299 function game_character_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
300 switch ($op) {
301 case 'delete':
302 db_query("DELETE FROM {game_characters} WHERE nid=%d", $node->nid);
303 db_query("DELETE FROM {game_character_claims} WHERE nid=%d", $node->nid);
304 if ($_SESSION['game_character_active'] == $node->nid) {
305 unset($_SESSION['game_character_active']);
306 }
307 break;
308 case 'insert':
309 case 'update':
310 // Set the node as its owner's currently active character.
311 game_character_set_active($node->uid, $node);
312 if ($node->uid) {
313 db_query("DELETE FROM {game_character_claims} WHERE nid=%d", $node->nid);
314 }
315 else if (variable_get('game_character_allow_claims', TRUE) || variable_get('game_character_allow_claim_ownership', TRUE)) {
316 if (!db_result(db_query("SELECT claim FROM {game_character_claims} WHERE nid=%d", $node->nid))) {
317 $claim = md5('claim:'. $node->nid .':'. time());
318 db_query("INSERT INTO {game_character_claims} (nid, claim) VALUES (%d, '%s')", $node->nid, $claim);
319 drupal_set_message(t('You may later claim the game character %title with !link.', array('%title' => $node->title, '!link' => l(t('this link'), 'game_character/claim/'. $node->nid .'/'. $claim))));
320 }
321 }
322 break;
323 }
324 }

  ViewVC Help
Powered by ViewVC 1.1.2