【问题标题】:Implementing not automatic badges with PHP and MYSQL使用 PHP 和 MYSQL 实现非自动徽章
【发布时间】:2011-04-16 20:03:07
【问题描述】:

我有用户表users,我在其中存储post_count 等信息。我想拥有大约 50 个徽章,而且将来会更多。

所以,我想要一个网站成员可以去拿徽章的页面,而不是像 SO 中那样自动给他。在他点击一个名为 smth 的按钮后,系统会检查他是否已经发布了 10 个帖子并且还没有这个徽章,如果没问题,给他徽章并插入新的表徽章的 id 和 user_id 该成员不能拿两次。

但是我有这么多徽章,所以我真的需要放这么多 if's 来检查所有徽章吗?您对此有何建议?如果可能的话,我怎样才能使它更优化?

谢谢。

【问题讨论】:

标签: php mysql badge


【解决方案1】:

也许将信息放入表格并进行检查?如果它基于帖子的数量,有 badge_namepost_count 的字段并检查吗?

【讨论】:

    【解决方案2】:

    恕我直言,以下是最佳的:

    为用户提供一个对象,该对象具有返回用户特定属性/指标的函数,您使用正确的用户 ID 进行初始化(您可能希望将其设为某些元素的单例/静态...):

    <?
    class User {
     public function initUser($id) {
      /* initialise the user. maby load all metrics now, or if they 
      are intensive on demand when the functions are called.
      you can cache them in a class variable*/
    
     }
     public function getPostCount() {
      // return number of posts
     }
     public function getRegisterDate() {
      // return register date
     }
     public function getNumberOfLogins() {
      // return the number of logins the user has made over time
     }
    }
    ?>
    

    拥有一个使用 id/key 初始化的徽章对象,并从您的数据库加载依赖项:

    <?
    class Badge {
     protected $dependencies = array();
     public function initBadge($id) {
      $this->loadDependencies($id);
     }
     protected function loadDependencies() {
    
      // load data from mysql and store it into dependencies like so:
    
      $dependencies = array(array(
       'value' => 300,
       'type' => 'PostCount',
       'compare => 'greater',
      ),...);
      $this->dependencies =  $dependencies;
    
     }
     public function getDependencies() {
      return $this->dependencies;
     }
    }
    ?>
    

    那么你可以有一个控制批次授予的类(你也可以在用户内部进行......) 并检查依赖项并打印失败的依赖项等...

      <?
            class BadgeAwarder {
             protected $badge = null;
             protected $user = null;
    
             public function awardBadge($userid,$badge) {
              if(is_null($this->badge))  {
               $this->badge = new Badge; // or something else for strange freaky badges, passed by $badge
    }
               $this->badge->initBadge($badge);
    
              if(is_null($this->user))  {
               $this->user = new User;
               $this->user->initUser($userid);
              }
              $allowed = $this->checkDependencies();
              if($allowed === true) {
               // grant badge, print congratulations
              } else if(is_array($failed)) {
               // sorry, you failed tu full fill thef ollowing dependencies: print_r($failed);
              } else {
               echo "error?";
              }
             }
             protected function checkDependencies() {
              $failed = array();
              foreach($this->badge->getDependencies() as $depdency) {
               $value = call_user_func(array($this->badge, 'get'.$depdency['type']));
               if(!$this->compare($value,$depdency['value'],$dependency['compare'])) { 
    
                $failed[] = $dependency;
               }
              }
              if(count($failed) > 0) {
               return $failed;
              } else {
               return true;
              }
             }
        protected function compare($val1,$val2,$operator) {
         if($operator == 'greater') {
          return ($val1 > $val2);
        }
        }
            }
            ?>
    

    如果您有非常自定义的批次需要奇怪的计算,您可以扩展到此类。

    希望我带你走上正轨。 未经测试并且可能充满语法错误。 欢迎来到面向对象编程的世界。还想这样做吗?

    【讨论】:

    • 嘿,它帮了我很多。只是在浏览答案,并为我找到了完美的答案。谢谢。
    猜你喜欢
    • 2021-11-17
    • 2019-03-07
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多