2026-05-29 19:54:56 +08:00

118 lines
2.7 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: zn
* Date: 2017/6/14
* Time: 14:34
*/
namespace common\models;
use common\services\GameConfigServices;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
/**观赏宠物
*
* @property int $id id
* @property int $user_id 用户ID
* @property int $config_id
* @property int $feed
* @property int $is_out 是否出勤
* @property int $last_update_time 上次计算时间
* @property integer $created_at
* @property integer $updated_at
*/
class EnjoyPet extends ActiveRecord
{
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
public function rules()
{
return [
['id', 'default', 'value' => 0],
['user_id', 'exist', 'targetClass' => '\common\models\User', 'targetAttribute' => 'id'],
['config_id', 'default', 'value' => ''],
['feed', 'default', 'value' => 0],
['is_out', 'default', 'value' => 0],
['last_update_time', 'default', 'value' => 0],
['created_at', 'default', 'value' => 0],
['updated_at', 'default', 'value' => 0],
];
}
/**
* @param $id
* @return EnjoyPet
*/
public static function findByID($id)
{
return self::findOne(['id' => $id]);
}
/**
* @param $userID
* @return EnjoyPet[]
*/
public static function findListByUserID($userID)
{
return self::findAll(['user_id' => $userID]);
}
/**
* @param $userID
* @return static
*/
public static function findOutByUserID($userID)
{
return self::findOne(['user_id' => $userID, 'is_out' => 1]);
}
public static function findOneByUserID($userID, $configID)
{
return self::findOne(['user_id' => $userID, 'config_id' => $configID]);
}
public function updateFeed()
{
if ($this->is_out == 1) {
$dis = time() - $this->last_update_time;
$period = intval(GameConfigServices::getCommonNumByID(GameConfigServices::COMMON_FEED_EXPEND_PERIOD));
$expendPoint = intval(GameConfigServices::getCommonNumByID(GameConfigServices::COMMON_FEED_EXPEND_POINT));
if ($expendPoint <= 0) return;
$point = intval($expendPoint * floatval($dis) / $period);
if ($point > 0) {
$this->feed -= $point;
if ($this->feed < 0) $this->feed = 0;
$this->last_update_time = time();
}
} else {
$this->last_update_time = time();
}
}
/**
* 生成要发送给客户的数据
* @return array
*/
public
function genClientData()
{
return $this->toArray();
}
}