使用PHP和MYSQL实现非自动徽章

时间:2010-09-24 14:46:05

标签: php mysql badge

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

所以,我想有一个页面,网站的成员可以去拿徽章,而不是像SO一样自动给他。在他点击一个名为smth的按钮,如“Take'Made 10 posts'徽章”之后,系统会检查他是否已发布10个帖子并且还没有此徽章,如果没有,请给他徽章并插入新的表格徽章的id和user_id,该成员无法接受两次。

但是我有这么多徽章,所以我真的需要这么多才能检查所有徽章吗?你对此有什么建议?如果可能的话,我怎样才能使它更优化呢?

谢谢。

2 个答案:

答案 0 :(得分: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);
    }
    }
        }
        ?>

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

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

答案 1 :(得分:0)

也许把信息扔进一张桌子并检查一下?如果它基于帖子数量,请包含badge_namepost_count的字段并检查该方式?