public function sendMsg($usra_id, $usrb_id, $msg) { $c = new CDbCriteria(); $c->addCondition('(usra_id=:usra_id AND usrb_id=:usrb_id) OR (usra_id=:usrb_id AND usrb_id=:usra_id)'); $c->params[':usra_id'] = $usra_id; $c->params[':usrb_id'] = $usrb_id; $talk = Talk::model()->find($c); if (empty($talk)) { $talk = new Talk(); $talk->usra_id = $usra_id; $talk->usrb_id = $usrb_id; $talk->content = $usra_id . '&' . $usrb_id; $talk->save(); } $path = Yii::app()->basePath . '/../assets/talks/' . $talk->content; if (file_exists($path)) { $buf = file_get_contents($path); $messages = unserialize($buf); } else { $messages = array(); } $msg = array('usr_id' => $usra_id, 'msg' => $msg, 'timestamp' => time(), 'is_read' => FALSE); array_unshift($messages, $msg); $buf = serialize($messages); file_put_contents($path, $buf); }
public function start() { $r = Yii::app()->db->createCommand('SELECT aid, name, type, tx, d_rq FROM dc_animal ORDER BY d_rq DESC')->queryAll(); $prev_rank = Yii::app()->cache->get('d_rq_rank_report'); if (isset($prev_rank)) { foreach ($r as $k => $v) { $rank[$v['aid']] = $k; if (isset($prev_rank[$v['aid']]) && $prev_rank[$v['aid']] > $k) { $r[$k]['change'] = 1; } else { if (isset($prev_rank[$v['aid']]) && $prev_rank[$v['aid']] < $k) { $r[$k]['change'] = -1; } else { $r[$k]['change'] = 0; } } } } else { foreach ($r as $k => $v) { $rank[$v['aid']] = $k; $r[$k]['change'] = 0; } } //奖励计算 $total_member = Yii::app()->db->createCommand('SELECT COUNT(*) FROM dc_user')->queryScalar(); $total_a = Yii::app()->db->createCommand('SELECT COUNT(aid), SUM(d_rq) FROM dc_animal')->queryRow(); $all_gold = $total_member * RANK_REWARD_E; $total_popularity = $total_a['SUM(d_rq)']; $reward_count = round($total_a['COUNT(aid)'] * 30 / 100); for ($i = 0; $i < $reward_count; $i++) { $aid = $r[$i]['aid']; $single_popularity = $r[$i]['d_rq']; $total_gold = $all_gold * $single_popularity / $total_popularity; if ($total_gold != 0) { $circles = Circle::model()->findAllByAttributes(array('aid' => $aid)); foreach ($circles as $circle) { $result_money = round($total_gold * $circle->d_contri / $single_popularity); $user = User::model()->findByPk($circle->usr_id); $user->gold += $result_money; $user->saveAttributes(array('gold')); $self_rank = $i + 1; $msg = "您的王国/家族在人气日排行榜中获得第" . $self_rank . "名,您获得" . $result_money . "金币奖励"; Talk::model()->sendMsg(NPC_SYSTEM_USRID, $user->usr_id, $msg); } } } Yii::app()->cache->set('d_rq_rank_report', $rank, 3600 * 24); Yii::app()->cache->set('d_rq_rank', $r, 3600 * 24); }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Topic(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Topic'])) { $model->attributes = $_POST['Topic']; $start_time = $_POST['Topic']['start_time']; $end_time = $_POST['Topic']['end_time']; if (!empty($start_time)) { $model->start_time = strtotime("{$start_time}"); } if (!empty($end_time)) { if ($end_time < $start_time) { $end_time = $start_time; } $model->end_time = strtotime("{$end_time}"); } $model->create_time = time(); if ($model->save()) { $file = CUploadedFile::getInstance($model, 'img'); if (isset($file)) { $path = Yii::app()->basePath . '/../images/topic/' . $model->topic_id . '.' . $file->getExtensionName(); $file->saveAs($path); $model->img = $model->topic_id . '.' . $file->getExtensionName(); $model->saveAttributes(array('img')); } $users = User::model()->findAll(); foreach ($users as $user) { Talk::model()->sendMsg(NPC_SYSTEM_USRID, $user->usr_id, "汪汪!宠物星球开展了一个" . $model->topic . "活动!参加的宠物有机会得到礼品哦~羡慕死汪了,快去看看吧~"); } $this->redirect(array('view', 'id' => $model->topic_id)); } } $this->render('create', array('model' => $model)); }
public function actionCommentApi() { $img_id = $_POST['img_id']; $body = $_POST['body']; $image = Image::model()->findByPk($img_id); $user = User::model()->findByPk($this->usr_id); $ex_gold = $user->gold; $ex_exp = $user->exp; $ex_lv = $user->lv; /* $comment = array( 'usr_id' => $this->usr_id, 'name' => $user->name, 'body' => $body, 'create_time' => time(), ); */ if (isset($_POST['reply_id'])) { $image->comments = 'usr_id:' . $this->usr_id . ',' . 'name:' . $user->name . ',' . 'reply_id:' . $_POST['reply_id'] . ',' . 'reply_name:' . $_POST['reply_name'] . ',' . 'body:' . $body . ',' . 'create_time:' . time() . ';' . $image->comments; //PMail::create($_POST['reply_id'], $user, $user->name.'在'.$image->img_id.'回复了你'); } else { $image->comments = 'usr_id:' . $this->usr_id . ',' . 'name:' . $user->name . ',' . 'body:' . $body . ',' . 'create_time:' . time() . ';' . $image->comments; } if ($image->saveAttributes(array('comments'))) { $animal = Animal::model()->findByPk($image->aid); $animal->t_rq += 5; $animal->d_rq += 5; $animal->w_rq += 5; $animal->m_rq += 5; $animal->saveAttributes(array('t_rq', 'd_rq', 'w_rq', 'm_rq')); $session = Yii::app()->session; if (isset($session['comment_count'])) { $session['comment_count'] += 1; } else { $session['comment_count'] = 1; } if ($session['comment_count'] <= 15) { $user->comment(); } if (isset($_POST['reply_id'])) { Talk::model()->sendMsg(NPC_IMAGE_USRID, $_POST['reply_id'], "[" . $image->img_id . "]" . $user->name . "回复了你:" . $body); } else { Talk::model()->sendMsg(NPC_IMAGE_USRID, $animal->master_id, "[" . $image->img_id . "]" . $user->name . "评论了你:" . $body); } $this->echoJsonData(array('exp' => $user->exp - $ex_exp, 'gold' => $user->gold - $ex_gold, 'lv' => $user->lv - $ex_lv)); } }
public function actionRegisterApi($aid = NULL, $name, $gender, $age, $type, $u_name, $u_gender, $u_city, $code) { /* if (empty($name)) { throw new PException('注册信息不完整'); } */ $session = Yii::app()->session; $session->open(); $id = $session['id']; if (empty($id)) { $this->response->setExpired(); //重新登录 $this->response->render(); return; } $device = Device::model()->findByPk($id); if (empty($device)) { $this->response->setExpired(); //重新登录 $this->response->render(); return; } $pattern = '/^[a-zA-Z0-9\\x{30A0}-\\x{30FF}\\x{3040}-\\x{309F}\\x{4E00}-\\x{9FBF}]+$/u'; if (empty($aid)) { //$namelen = (strlen($name)+mb_strlen($name,"UTF8"))/2; $namelen = mb_strlen($name, "UTF8"); if ($namelen > 8) { throw new PException('宠物昵称超过最大长度'); } if (!preg_match($pattern, $name)) { throw new PException('宠物昵称含有特殊字符'); } } //$u_namelen = (strlen($u_name)+mb_strlen($u_name,"UTF8"))/2; $u_namelen = mb_strlen($u_name, "UTF8"); if ($u_namelen > 8) { throw new PException('用户名超过最大长度'); } if (User::model()->isNameExist(trim($u_name))) { throw new PException('用户名已被注册'); } if (!preg_match($pattern, $u_name)) { throw new PException('用户名含有特殊字符'); } if ($code != '') { if (!($inviter = User::model()->getUserIdByCode(strtolower(trim($code))))) { throw new PException('邀请ID不存在'); } } $transaction = Yii::app()->db->beginTransaction(); try { $user = $device->register($aid, trim($name), $gender, $age, $type, trim($u_name), $u_gender, $u_city, empty($invter) ? NULL : $inviter); $session['usr_id'] = $device->usr_id; $session['not_registered'] = FALSE; $user->login(); Talk::model()->sendMsg(NPC_SYSTEM_USRID, $user->usr_id, "HI~我是的汪汪大使,欢迎来到宠物星球,以后星球上的新鲜事,汪汪会光速传达你,放心交给我吧,汪汪"); $transaction->commit(); } catch (Exception $e) { $transaction->rollback(); throw $e; } $this->echoJsonData(array('isSuccess' => true)); }
public function actionSearchApi($usr_id) { $c = new CDbCriteria(); $c->addCondition('(usra_id=:usra_id AND usrb_id=:usrb_id) OR (usra_id=:usrb_id AND usrb_id=:usra_id)'); $c->params[':usra_id'] = $this->usr_id; $c->params[':usrb_id'] = $usr_id; $talk = Talk::model()->find($c); if (empty($talk)) { $talk = new Talk(); $talk->usra_id = $this->usr_id; $talk->usrb_id = $usr_id; $talk->content = $this->usr_id . '&' . $usr_id; $talk->save(); } $this->echoJsonData(array('talk_id' => $talk->talk_id)); }