123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?php
- /**
- * 红包
- * @author solu
- */
- class Redpack extends Model {
- protected $tableName = 'redpack';
- protected $dbKey = 'dw_chat';
- const REDIS_PRE_REDPACK_REWARD_LIST = 'mee:redpack_reward_list:';
- const REWARD_LIST_TTL = 86400 - 120; // 提前两分钟结束
- const MONEY_MIN_AVG = 1;
- const TEAM_DEDUCT = 0.02; // 团队提成
- const STATUS_PLAYING = 0; // 进行中
- const STATUS_FINISH = 1; // 已抢完
- const STATUS_TIMEOUT = 2; // 超时关闭
- private static function getRewardListKey($trxId) {
- return self::REDIS_PRE_REDPACK_REWARD_LIST . $trxId;
- }
- /**
- * 初始化红包
- * @author solu
- * @param $redpack Redpack
- * @param null $objRedis
- * @return bool
- */
- public function initReward($redpack, $objRedis = null) {
- $trxId = $redpack['transfer_trx_id'];
- $total = $redpack['quantity_total_int'];
- $num = $redpack['num_total'];
- !$objRedis && $objRedis = dwRedis::init(Eos::REDIS_SERV);
- $key = self::getRewardListKey($trxId);
- if (($total/$num) < self::MONEY_MIN_AVG) {
- alermErrorMsg("trxId:{$trxId}, total:{$total}, num:{$num} 平均红包金额太小");
- return false;
- }
- $objRedis->del($key);
- $total -= $total * self::TEAM_DEDUCT;
- $list = self::_makeRandomArr($total, $num);
- array_unshift($list, $key);
- call_user_func_array([$objRedis, 'lPush'], $list);
- $objRedis->expire($key, self::REWARD_LIST_TTL);
- self::redpackEvent($redpack['session_id'], $redpack['sender'], $trxId, $redpack['memo']);
- return true;
- }
- private static function redpackEvent($sessionId, $sender, $trxId, $title) {
- $objUserBindInfo = new UserBindInfo();
- $userId = $objUserBindInfo->getUserIdBy($sender, 'eos');
- $objSession = new Session();
- $se = explode('-', $sessionId);
- $data = json_encode(compact('title', 'trxId'));
- $data = Utils::encodeRC4($data);
- if (count($se) > 1) {
- // 发给个人
- $to = 0;
- foreach ($se as $_u) {
- if ($_u != $userId) {
- $to = $_u;
- break;
- }
- }
- // 插入一条聊天消息
- $ret = $objSession->sendPersonMsg($userId, $sessionId, Session::MSG_TYPE_REDPACK, $data, true);
- $eventData = [
- 'type' => 'new_redpack',
- 'from' => $userId,
- 'to' => $to,
- 'hash' => $ret['hash'],
- 'content' => $data
- // 'content' => Utils::encodeRC4($title),
- // 'ext' => compact('trxId'),
- ];
- ThirdApi::pushPersonEvent($to, $eventData);
- } else {
- // 插入一条聊天消息
- $ret = $objSession->sendGroupMsg($userId, $sessionId, Session::MSG_TYPE_REDPACK, $data, true);
- // 发给群组
- $groupId = current($se);
- $eventData = [
- 'type' => 'new_redpack',
- 'group_id' => $groupId,
- 'from' => $userId,
- 'hash' => $ret['hash'],
- 'content' => $data,
- 'userMap' => $ret['userMap'],
- // 'content' => Utils::encodeRC4($title),
- // 'ext' => compact('trxId'),
- ];
- ThirdApi::pushGroupEvent($groupId, $eventData);
- }
- }
- private static function _makeRandomArr($money, $num, $min = 1) {
- $list = [];
- while ($num > 0) {
- if ($num == 1) {
- $list[] = $money;
- break;
- } else {
- $max = ($money / $num) * 2;
- $_tmp = rand($min, $max);
- $_tmp <= $min && $_tmp = $min;
- $list[] = $_tmp;
- $money -= $_tmp;
- }
- $num--;
- }
- return $list;
- }
- /**
- * 抢红包
- * @author solu
- * @param $trxId
- * @param $userId
- * @return array
- * @throws Exception
- */
- public function grab($trxId, $userId) {
- $row = $this->objTable->getRow(['transfer_trx_id' => $trxId]);
- if (!$row) {
- throw new Exception('miss redpack', CODE_NORMAL_ERROR);
- }
- if (self::STATUS_PLAYING != $row['status']) {
- throw new Exception('Too Late', -1001);
- }
- $objUserBindInfo = new UserBindInfo();
- $account = $objUserBindInfo->getAccountByUserId($userId);
- if (!$account) {
- throw new Exception('not bind EOS account', CODE_NORMAL_ERROR);
- }
- $objLog = new TableHelper('redpack_log', 'dw_chat');
- if ($objLog->getCount(['redpack_trx_id' => $trxId, 'user_id' => $userId])) {
- throw new Exception('repeat grab', CODE_NORMAL_ERROR);
- }
- $key = self::getRewardListKey($trxId);
- $objRedis = dwRedis::init(Eos::REDIS_SERV);
- $quantity = $objRedis->rPop($key);
- if (false === $quantity) {
- throw new Exception('Too Late', CODE_NORMAL_ERROR);
- }
- $strQuantity = Eos::toDisplayFormat($quantity);
- $redpackId = $row['id'];
- $data = [
- 'redpack_id' => $redpackId,
- 'redpack_trx_id' => $trxId,
- 'player' => $account,
- 'quantity' => $strQuantity,
- 'quantity_int' => $quantity,
- 'create_time' => NOW,
- 'user_id' => $userId,
- ];
- $objLog->addObject($data);
- $logId = $objLog->getInsertId();
- if (!$logId) {
- throw new Exception('log error', CODE_NORMAL_ERROR);
- }
- $parts = explode('-', $row['session_id']);
- $from = $objUserBindInfo->getUserIdBy($row['sender'], 'eos');
- $eventData = [
- 'type' => 'grab_redpack',
- 'from' => $from,
- 'to' => $userId,
- 'content' => [
- 'title' => $row['memo'],
- 'redpack_id' => $redpackId,
- 'redpack_trx_id' => $trxId,
- 'quantity' => $strQuantity,
- ],
- ];
- if (count($parts) == 1) {
- // 抢红包的信息,要发给发红包的人
- $groupId = current($parts);
- $eventData['group_id'] = $groupId;
- ThirdApi::pushGroupEvent($groupId, $eventData);
- } else {
- ThirdApi::pushPersonEvent($userId, $eventData);
- }
- Eos::grabRedpack($redpackId, $trxId, $logId, $account, $strQuantity);
- $team = round($row['quantity_total_int'] * self::TEAM_DEDUCT); // 团队提成
- $ret = [
- 'from' => $from,
- 'quantity' => $quantity,
- 'type' => 'eos',
- 'num_total' => $row['num_total'],
- 'num_left' => $row['num_left'] - 1,
- 'quantity_total' => $row['quantity_total_int'] - $team,
- 'quantity_left' => $row['quantity_left_int'] - $quantity,
- 'logs' => (new RedpackLog())->getByTrxId($trxId),
- ];
- return $ret;
- }
- /**
- * 红包详情
- * @author solu
- * @param $trxId
- * @return array
- */
- public function detail($trxId) {
- $row = $this->objTable->getRow(['transfer_trx_id' => $trxId]);
- if (!$row) {
- return [];
- }
- $team = round($row['quantity_total_int'] * self::TEAM_DEDUCT); // 团队提成
- $ret = [
- 'from' => $row['sender_id'],
- 'title' => $row['memo'],
- 'type' => 'eos',
- 'redpack_status' => $row['status'],
- 'num_total' => $row['num_total'],
- 'num_left' => $row['num_left'],
- 'quantity_total' => $row['quantity_total_int'] - $team,
- 'quantity_left' => $row['quantity_left_int'],
- 'logs' => (new RedpackLog())->getByTrxId($trxId),
- ];
- return $ret;
- }
- /**
- * 发出的红包
- * @author solu
- * @param $userId
- * @param int $offset
- * @param int $size
- * @return array
- */
- public function sendList($userId, $offset = 0, $size = 20) {
- if (!$userId) {
- return [];
- }
- $list = $this->objTable->getAll(['sender_id' => $userId], [
- '_field' => 'id, session_id, num_total, num_left, quantity_total_int, quantity_left_int, memo as title, status, transfer_trx_id, create_time',
- '_sortKey' => 'id DESC',
- '_limit' => "{$offset}, {$size}",
- ]);
- $total_count = 0;
- $total_quantity = 0;
- if (0 == $offset) {
- $row = $this->objTable->getRow(['sender_id' => $userId], [
- '_field' => 'sum(quantity_total_int) as total_quantity, count(1) as c',
- ]);
- $total_count = (int)$row['c'];
- $total_quantity = (int)$row['total_quantity'];
- }
- return [$list, $total_count, $total_quantity];
- }
- /**
- * 收到的红包
- * @author solu
- * @param $userId
- * @param int $offset
- * @param int $size
- * @return array
- */
- public function receiveList($userId, $offset = 0, $size = 20) {
- if (!$userId) {
- return [];
- }
- $objRedpackLog = new RedpackLog();
- $list = $objRedpackLog->objTable->getAll(['user_id' => $userId], [
- '_field' => 'id, redpack_trx_id, quantity_int, create_time, status, best',
- '_sortKey' => 'id DEsc',
- '_limit' => "{$offset}, {$size}",
- ]);
- $trxIds = array_column($list, 'redpack_trx_id');
- $redpacks = $this->objTable->getAll(['transfer_trx_id' => $trxIds], [
- '_field' => 'id, transfer_trx_id, sender_id, memo, status',
- ]);
- $redpacks = arrayFormatKey($redpacks, 'transfer_trx_id');
- $senderIds = array_column($redpacks, 'sender_id');
- $objUser = new TableHelper('user_info', 'dw_chat');
- $users = $objUser->getAll(['user_id' => $senderIds], ['_field' => 'user_id, nick_name, cover_photo']);
- $users = arrayFormatKey($users, 'user_id', 'nick_name');
- foreach ($list as $k => $v) {
- $redpack = $redpacks[$v['redpack_trx_id']];
- $senderNick = $users[$redpack['sender_id']];
- $v['sender'] = $senderNick;
- $v['title'] = $redpack['memo'];
- $list[$k] = $v;
- }
- $total_count = 0;
- $best_count = 0;
- $total_quantity = 0;
- if (0 == $offset) {
- $row = $this->objTable->getRow(['sender_id' => $userId], [
- '_field' => 'sum(quantity_int) as total_quantity, count(1) as c, sum(best) best_count',
- ]);
- $total_count = (int)$row['c'];
- $total_quantity = (int)$row['total_quantity'];
- $best_count = (int)$row['best_count'];
- }
- return [$list, $total_count, $total_quantity, $best_count];
- }
- }
|