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]; } }