Browse Source

Refactor badges

spaghetti 7 years ago
parent
commit
99f08adc0e

+ 53
- 62
classes/badges.class.php View File

7
    * @return array of BadgeIDs
7
    * @return array of BadgeIDs
8
    */
8
    */
9
   public static function get_badges($UserID) {
9
   public static function get_badges($UserID) {
10
-    $Result = [];
11
-
12
-    if (G::$Cache->get_value('user_badges_'.$UserID)) {
13
-      return G::$Cache->get_value('user_badges_'.$UserID);
14
-    }
15
-
16
-    $QueryID = G::$DB->get_query_id();
17
-    G::$DB->query("
18
-      SELECT BadgeID, Displayed
19
-      FROM users_badges
20
-      WHERE UserID = ".$UserID);
21
-
22
-    if (G::$DB->has_results()) {
23
-      while (list($BadgeID, $Displayed) = G::$DB->next_record()) {
24
-        $Result[] = array('BadgeID' => $BadgeID, 'Displayed' => $Displayed);
25
-      }
26
-    }
27
-
28
-    G::$DB->set_query_id($QueryID);
29
-
30
-    G::$Cache->cache_value('user_badges_'.$UserID, $Result);
31
-
32
-    return $Result;
10
+    return Users::user_info($UserID)['Badges'];
33
   }
11
   }
34
 
12
 
35
   /**
13
   /**
40
    * @return bool success?
18
    * @return bool success?
41
    */
19
    */
42
   public static function award_badge($UserID, $BadgeID) {
20
   public static function award_badge($UserID, $BadgeID) {
43
-    if (self::has_badge($UserID, array('BadgeID' => $BadgeID))) {
21
+    if (self::has_badge($UserID, $BadgeID)) {
44
       return false;
22
       return false;
45
     } else {
23
     } else {
46
       $QueryID = G::$DB->get_query_id();
24
       $QueryID = G::$DB->get_query_id();
51
           ($UserID, $BadgeID)");
29
           ($UserID, $BadgeID)");
52
       G::$DB->set_query_id($QueryID);
30
       G::$DB->set_query_id($QueryID);
53
 
31
 
54
-      G::$Cache->delete_value('user_badges_'.$UserID);
32
+      G::$Cache->delete_value('user_info_'.$UserID);
55
 
33
 
56
       return true;
34
       return true;
57
     }
35
     }
68
 
46
 
69
     $Badges = self::get_badges($UserID);
47
     $Badges = self::get_badges($UserID);
70
 
48
 
71
-    foreach ($Badges as $Badge) {
72
-      if ($Badge['Displayed'])
49
+    foreach ($Badges as $Badge => $Displayed) {
50
+      if ($Displayed)
73
         $Result[] = $Badge;
51
         $Result[] = $Badge;
74
     }
52
     }
75
     return $Result;
53
     return $Result;
79
    * Returns true if the given user owns the given badge
57
    * Returns true if the given user owns the given badge
80
    *
58
    *
81
    * @param int $UserID
59
    * @param int $UserID
82
-   * @param $Badge
60
+   * @param int $BadgeID
83
    * @return bool
61
    * @return bool
84
    */
62
    */
85
-  public static function has_badge($UserID, $Badge) {
63
+  public static function has_badge($UserID, $BadgeID) {
86
     $Badges = self::get_badges($UserID);
64
     $Badges = self::get_badges($UserID);
87
 
65
 
88
-    foreach ($Badges as $B) {
89
-      if ($B['BadgeID'] == $Badge['BadgeID'])
90
-        return true;
91
-    }
92
-
93
-    return false;
66
+    return array_key_exists($BadgeID, $Badges);
94
   }
67
   }
95
 
68
 
96
   /**
69
   /**
97
    * Creates HTML for displaying a badge.
70
    * Creates HTML for displaying a badge.
98
    *
71
    *
99
-   * @param $Badge
72
+   * @param int $BadgeID
100
    * @param bool $Tooltip Should HTML contain a tooltip?
73
    * @param bool $Tooltip Should HTML contain a tooltip?
101
    * @return string HTML
74
    * @return string HTML
102
    */
75
    */
103
-  public static function display_badge($Badge, $Tooltip = false) {
76
+  public static function display_badge($BadgeID, $Tooltip = false) {
104
     $html = "";
77
     $html = "";
105
 
78
 
106
-    if (G::$Cache->get_value('badge_'.$Badge['BadgeID'])) {
107
-      extract(G::$Cache->get_value('badge_'.$Badge['BadgeID']));
79
+    if (($Badges = G::$Cache->get_value('badges')) && array_key_exists($BadgeID, $Badges)) {
80
+      extract($Badges[$BadgeID]);
81
+    } else {
82
+      self::update_badge_cache();
83
+      if (($Badges = G::$Cache->get_value('badges')) && array_key_exists($BadgeID, $Badges)) {
84
+        extract($Badges[$BadgeID]);
85
+      } else {
86
+        global $Debug;
87
+        $Debug->analysis('Invalid BadgeID ' . $BadgeID . ' requested.');
88
+      }
89
+    }
90
+
91
+    if ($Tooltip) {
92
+      $html .= '<a class="badge_icon"><img class="tooltip" alt="'.$Name.'" title="'.$Name.'</br>'.$Description.'" src="'.$Icon.'" /></a>';
93
+    } else {
94
+      $html .= '<a class="badge_icon"><img alt="'.$Name.'" title="'.$Name.'" src="'.$Icon.'" /></a>';
95
+    }
96
+
97
+    return $html;
98
+  }
99
+
100
+  public static function display_badges($BadgeIDs, $Tooltip = false) {
101
+    $html = "";
102
+    foreach ($BadgeIDs as $BadgeID) {
103
+      $html .= self::display_badge($BadgeID, $Tooltip);
108
     }
104
     }
109
-    if (!isset($Icon)) {
105
+    return $html;
106
+  }
107
+
108
+  private static function update_badge_cache() {
110
       $QueryID = G::$DB->get_query_id();
109
       $QueryID = G::$DB->get_query_id();
110
+
111
       G::$DB->query("
111
       G::$DB->query("
112
         SELECT
112
         SELECT
113
-        Icon, Name, Description
114
-        FROM badges
115
-        WHERE ID = ".$Badge['BadgeID']);
113
+        ID, Icon, Name, Description
114
+        FROM badges");
116
 
115
 
116
+      $badges = [];
117
       if (G::$DB->has_results()) {
117
       if (G::$DB->has_results()) {
118
-        list($Icon, $Name, $Description) = G::$DB->next_record();
119
-        G::$Cache->cache_value('badge_'.$Badge['BadgeID'], array('Icon' => $Icon, 'Name' => $Name, 'Description' => $Description));
118
+        while(list($id, $icon, $name, $description) = G::$DB->next_record()) {
119
+          $badges[$id] = array('Icon' => $icon, 'Name' => $name, 'Description' => $Description);
120
+        }
121
+        G::$Cache->cache_value('badges', $badges);
120
       }
122
       }
121
 
123
 
122
       G::$DB->set_query_id($QueryID);
124
       G::$DB->set_query_id($QueryID);
123
-
124
-    }
125
-
126
-    if (isset($Icon)) {
127
-      if ($Tooltip) {
128
-        $html .= '<a class="badge_icon"><img class="tooltip" alt="'.$Name.'" title="'.$Name.'</br>'.$Description.'" src="'.$Icon.'" /></a>';
129
-      } else {
130
-        $html .= '<a class="badge_icon"><img alt="'.$Name.'" title="'.$Name.'" src="'.$Icon.'" /></a>';
131
-      }
132
-    }
133
-
134
-    return $html;
135
   }
125
   }
136
 
126
 
137
-  public static function display_badges($Badges, $Tooltip = false) {
138
-    $html = "";
139
-    foreach ($Badges as $Badge) {
140
-      $html .= self::display_badge($Badge, $Tooltip);
127
+  public static function get_all_badges() {
128
+    if (($Badges = G::$Cache->get_value('badges'))) {
129
+      return $Badges;
130
+    } else {
131
+      update_badge_cache();
132
+      return G::$Cache->get_value('badges');
141
     }
133
     }
142
-    return $html;
143
   }
134
   }
144
 }
135
 }
145
 ?>
136
 ?>

+ 14
- 0
classes/users.class.php View File

45
    *  boolean Visible - If false, they don't show up on peer lists
45
    *  boolean Visible - If false, they don't show up on peer lists
46
    *  array   ExtraClasses - Secondary classes.
46
    *  array   ExtraClasses - Secondary classes.
47
    *  int     EffectiveClass - the highest level of their main and secondary classes
47
    *  int     EffectiveClass - the highest level of their main and secondary classes
48
+   *  array   Badges - list of all the user's badges of the form BadgeID => Displayed
48
    */
49
    */
49
   public static function user_info($UserID) {
50
   public static function user_info($UserID) {
50
     global $Classes;
51
     global $Classes;
103
           $UserInfo['Paranoia'] = [];
104
           $UserInfo['Paranoia'] = [];
104
         }
105
         }
105
         $UserInfo['Class'] = $Classes[$UserInfo['PermissionID']]['Level'];
106
         $UserInfo['Class'] = $Classes[$UserInfo['PermissionID']]['Level'];
107
+
108
+        G::$DB->query("
109
+          SELECT BadgeID, Displayed
110
+          FROM users_badges
111
+          WHERE UserID = ".$UserID);
112
+
113
+        if (G::$DB->has_results()) {
114
+          $Badges = [];
115
+          while (list($BadgeID, $Displayed) = G::$DB->next_record()) {
116
+            $Badges[$BadgeID] = $Displayed;
117
+          }
118
+          $UserInfo['Badges'] = $Badges;
119
+        }
106
       }
120
       }
107
 
121
 
108
       if (isset($UserInfo['LockedAccount']) && $UserInfo['LockedAccount'] == "") {
122
       if (isset($UserInfo['LockedAccount']) && $UserInfo['LockedAccount'] == "") {

+ 10
- 3
sections/store/coinbadge.php View File

20
 
20
 
21
 <div class="thin">
21
 <div class="thin">
22
 
22
 
23
-<? if (isset($_GET['confirm']) && $_GET['confirm'] == 1) {
23
+<? if (isset($_GET['confirm']) && $_GET['confirm'] == 1 && !Badges::has_badge($UserID, 255)) {
24
   $DB->query("
24
   $DB->query("
25
     SELECT BonusPoints
25
     SELECT BonusPoints
26
     FROM users_main
26
     FROM users_main
68
     </div>
68
     </div>
69
 <? } ?>
69
 <? } ?>
70
 <?
70
 <?
71
-} else { ?>
71
+} else {
72
+  if (Badges::has_badge($UserID, 255)) {
73
+?>
74
+  <h2 id="general">Oppaicoin Status</h2>
75
+<?
76
+  } else {
77
+?>
72
   <h2 id="general">Purchase Oppaicoin Badge?</h2>
78
   <h2 id="general">Purchase Oppaicoin Badge?</h2>
79
+<? } ?>
73
   <div class="box pad">
80
   <div class="box pad">
74
     <p><?=number_format($Purchases)?> people have bought this badge</p>
81
     <p><?=number_format($Purchases)?> people have bought this badge</p>
75
     <p>Current cost: <?=number_format($Price)?> <?=BONUS_POINTS?></p>
82
     <p>Current cost: <?=number_format($Price)?> <?=BONUS_POINTS?></p>
76
-    <? if (Badges::has_badge($UserID, ['BadgeID' => 255])) { ?>
83
+    <? if (Badges::has_badge($UserID, 255)) { ?>
77
     <p>You already own this badge</p>
84
     <p>You already own this badge</p>
78
     <? } else { ?>
85
     <? } else { ?>
79
     <form action="store.php">
86
     <form action="store.php">

+ 3
- 6
sections/store/store.php View File

246
       $BadgeText = $Badge['Name']
246
       $BadgeText = $Badge['Name']
247
 
247
 
248
 ?>
248
 ?>
249
-        <td class="nobr"><?=Badges::display_badge($Badge)?><span class="badge_name" style="margin-left: 10px;"><?=$BadgeText?></span></td>
249
+        <td class="nobr"><?=Badges::display_badge($Badge['BadgeID'])?><span class="badge_name" style="margin-left: 10px;"><?=$BadgeText?></span></td>
250
         <td class="nobr"><?=$Badge['Description']?></td>
250
         <td class="nobr"><?=$Badge['Description']?></td>
251
       </tr>
251
       </tr>
252
 <?
252
 <?
259
   WHERE Name='Oppaicoin'");
259
   WHERE Name='Oppaicoin'");
260
 if ($DB->has_results()) {
260
 if ($DB->has_results()) {
261
   $CoinBadge = $DB->to_array()[0];
261
   $CoinBadge = $DB->to_array()[0];
262
-  $BadgeText = $CoinBadge['Name'];
263
-  if (!Badges::has_badge($LoggedUser['ID'], $CoinBadge)) {
264
-    $BadgeText = '<a href="store.php?item=coinbadge">'.$BadgeText.'</a>';
265
-  }
262
+  $BadgeText = '<a href="store.php?item=coinbadge">'.$CoinBadge['Name'].'</a>';
266
 ?>
263
 ?>
267
       <tr class="row">
264
       <tr class="row">
268
-      <td class="nobr"><?=Badges::display_badge($CoinBadge)?><span class="badge_name" style="margin-left: 10px;"><?=$BadgeText?></span></td>
265
+      <td class="nobr"><?=Badges::display_badge($CoinBadge['BadgeID'])?><span class="badge_name" style="margin-left: 10px;"><?=$BadgeText?></span></td>
269
         <td class="nobr"><?=$CoinBadge['Description']?></td>
266
         <td class="nobr"><?=$CoinBadge['Description']?></td>
270
       </tr>
267
       </tr>
271
 <? } ?>
268
 <? } ?>

+ 2
- 2
sections/user/edit.php View File

409
     if (empty($Badges)) {
409
     if (empty($Badges)) {
410
       ?><span>You have no badges :(</span><?
410
       ?><span>You have no badges :(</span><?
411
     } else {
411
     } else {
412
-      foreach ($Badges as $Key => $Badge) {
413
-        ?><input type="checkbox" name="badges[]" class="badge_checkbox" value="<?=$Badge['BadgeID']?>" <?=($Badge['Displayed'])?"checked ":""?>/><?=Badges::display_badge($Badge, true)?>
412
+      foreach ($Badges as $BadgeID => $Displayed) {
413
+        ?><input type="checkbox" name="badges[]" class="badge_checkbox" value="<?=$BadgeID?>" <?=($Displayed)?"checked ":""?>/><?=Badges::display_badge($BadgeID, true)?>
414
   <?    if (($Key+1) % 5 == 0) {
414
   <?    if (($Key+1) % 5 == 0) {
415
           ?><br /><?
415
           ?><br /><?
416
         }
416
         }

+ 13
- 20
sections/user/take_edit.php View File

256
   $BadgeIDs = [];
256
   $BadgeIDs = [];
257
 }
257
 }
258
 
258
 
259
-$BadgesChanged = false;
260
 $NewBadges = [];
259
 $NewBadges = [];
261
-if ($Cache->get_value('user_badges_'.$UserID)) {
262
-  $Badges = $Cache->get_value('user_badges_'.$UserID);
263
-  foreach ($Badges as $Badge) {
264
-    if (in_array($Badge['BadgeID'], $BadgeIDs)) { // Is the current badge in the list of badges the user wants to display?
265
-      $Displayed = true;
266
-      $DisplayedBadgeIDs[] = $Badge['BadgeID'];
267
-      if ($Badge['Displayed'] == 0) { // The user wants to display a badge that wasn't displayed before
268
-        $BadgesChanged = true;
269
-      }
270
-    } else { // The user no longer wants to display a badge that was displayed before
271
-      $Displayed = false;
260
+$BadgesChanged = false;
261
+$Badges = Users::user_info($UserID)['Badges'];
262
+foreach ($Badges as $BadgeID => $OldDisplayed) {
263
+  if (in_array($BadgeID, $BadgeIDs)) { // Is the current badge in the list of badges the user wants to display?
264
+    $Displayed = true;
265
+    $DisplayedBadgeIDs[] = $BadgeID;
266
+    if ($OldDisplayed == 0) { // The user wants to display a badge that wasn't displayed before
272
       $BadgesChanged = true;
267
       $BadgesChanged = true;
273
     }
268
     }
274
-    $NewBadges[] = ['BadgeID' => $Badge['BadgeID'], 'Displayed' => $Displayed?'1':'0'];
275
-
269
+  } else { // The user no longer wants to display a badge that was displayed before
270
+    $Displayed = false;
271
+    $BadgesChanged = true;
276
   }
272
   }
277
-} else {
278
-  $BadgesChanged = true;
279
-}
280
-if ($BadgesChanged) {
281
-  $Cache->cache_value('user_badges_'.$UserID, $NewBadges);
273
+  $NewBadges[$BadgeID] = $Displayed?'1':'0';
282
 }
274
 }
283
 // End Badge settings
275
 // End Badge settings
284
 
276
 
285
 $Cache->begin_transaction("user_info_$UserID");
277
 $Cache->begin_transaction("user_info_$UserID");
286
 $Cache->update_row(false, [
278
 $Cache->update_row(false, [
287
   'Avatar' => display_str($_POST['avatar']),
279
   'Avatar' => display_str($_POST['avatar']),
288
-  'Paranoia' => $Paranoia
280
+	'Paranoia' => $Paranoia,
281
+	'Badges' => $NewBadges
289
 ]);
282
 ]);
290
 $Cache->commit_transaction(0);
283
 $Cache->commit_transaction(0);
291
 
284
 

+ 11
- 16
sections/user/user.php View File

276
       </div>
276
       </div>
277
     </div>
277
     </div>
278
 <? }
278
 <? }
279
-    $Badges = Badges::get_badges($UserID);
279
+    $Badges = array_keys(Badges::get_badges($UserID));
280
     if (!empty($Badges)) { ?>
280
     if (!empty($Badges)) { ?>
281
     <div class="box">
281
     <div class="box">
282
       <div class="head colhead_dark">Badges</div>
282
       <div class="head colhead_dark">Badges</div>
1278
       <td class="label">Badges Owned:</td>
1278
       <td class="label">Badges Owned:</td>
1279
       <td>
1279
       <td>
1280
 <?
1280
 <?
1281
-    $DB->query("
1282
-      SELECT ID AS BadgeID, Icon, Name, Description
1283
-      FROM badges");
1284
-    if ($DB->has_results()) { //If the DB has no results here, something is dangerously fucked
1285
-      $AllBadges = $DB->to_array();
1286
-      $UserBadgeIDs = [];
1287
-      foreach (Badges::get_badges($UserID) as $Badge) {
1288
-        $UserBadgeIDs[] = $Badge['BadgeID'];
1289
-      }
1290
-      $i = 0;
1291
-      foreach ($AllBadges as $Badge) {
1292
-        ?><input type="checkbox" name="badges[]" class="badge_checkbox" value="<?=$Badge['BadgeID']?>" <?=(in_array($Badge['BadgeID'], $UserBadgeIDs))?" checked":""?>/><?=Badges::display_badge($Badge, true)?>
1281
+    $AllBadges = Badges::get_all_badges();
1282
+    $UserBadgeIDs = [];
1283
+    foreach (array_keys(Badges::get_badges($UserID)) as $b) {
1284
+      $UserBadgeIDs[] = $b;
1285
+    }
1286
+    $i = 0;
1287
+    foreach (array_keys($AllBadges) as $BadgeID) {
1288
+      ?><input type="checkbox" name="badges[]" class="badge_checkbox" value="<?=$BadgeID?>" <?=(in_array($BadgeID, $UserBadgeIDs))?" checked":""?>/><?=Badges::display_badge($BadgeID, true)?>
1293
 <?      $i++;
1289
 <?      $i++;
1294
-        if ($i % 8 == 0) {
1295
-          echo "<br />";
1296
-        }
1290
+      if ($i % 8 == 0) {
1291
+        echo "<br />";
1297
       }
1292
       }
1298
     }
1293
     }
1299
 ?>
1294
 ?>

Loading…
Cancel
Save