* 4.0.0 第四版: 言灵世界 商城模块
* 2.2.0 第三版: 独立的游戏币<元宝>, 面向多渠道的充值模式, 拉起充值成功后, 给玩家发放元宝. 游戏内部消费元宝,钻石和金币.可以用元宝购买金币和钻石.这一版在上面的PayProc中 * 2015年9月23日
* 2.1.0 mn_pay mn_AddUserCash ... 以 mn_ 打头的函数是第二版, 修改为伪托管模式,除了充值的时候跟腾讯通讯,其余时间自己管理游戏币:钻石. * 2015年7月23日
* 2.0.1 m_pay m_AddUserCash ... 以 m_ 打头的函数是第一版, 完全按照应用宝官方要求托管游戏币的模式开发的. * 2015年4月23日
* 1.0.0 Created at 2016-4-20. by --gwang
* @author gwang (mail@wanggangzero.cn)
* @copyright © 2016-4-20, SJZ LoyalSoft Corporation & gwang. All rights reserved.
*/
class PayProc {
const orderTab = 'tpl_order_tab';
/**
* 逻辑分发
* 所有的Proc中必须有这样一个方法
* @param Req $req
*/
public static function procMain($req) {
switch ($req->cmd) {
// 支付相关的活动
case CmdCode::cmd_pay_getfirstpaygift: # 8802 领取首充礼包
return PayProc::m_GetFirstPayGift($req);
//
case CmdCode::cmd_mpay_pay: # 8807 消费..
return self::m_pay($req); # ::==> 购买普通商城物品
case CmdCode::cmd_mpay_get_balance: # 8808 刷新账号余额
return self::m_refreshChargeOrders($req); # ::==> 刷新订单-领取充值金额
case CmdCode::cmd_mpay_buyDynamic: # 8809 购买动态商城物品
return self::m_pay_buyDynamic($req);
case CmdCode::cmd_mpay_getDynamic: # 8810 刷新神秘商城物品
return self::m_pay_getDynamic($req);
case CmdCode::cmd_mpay_selfCheckOrders: # 8811 自助检查异常订单
return self::selfhelpCheckOrders($req);
case CmdCode::cmd_mpay_getPayRecords: # 8812 查询订单记录
return self::GetPayRecoreds($req);
//
default:
return Resp::err(ErrCode::cmd_err);
}
}
/**
* 8812 查询充值记录
* @param Req $req
*/
static function GetPayRecoreds($req) {
$uid = $req->uid;
$table = "tpl_order_tab";
$arr = daoInst()->select(" cpOrderId as '订单' ,product_name as '道具',amount as '金额' ,from_unixtime(order_ts) as '时间' ")->from($table)
->where("uid")->eq($uid)
->andWhere("status")->eq(1)
->fetchall();
return Resp::ok($arr);
}
//
//
/**
* 刷新充值订单(领取充值结果)
* @param Req $req
*/
public static function m_refreshChargeOrders($req) {
# 客户端参数解析
$uid = $req->uid; # userID
$zoneid = $req->zoneid; # 分区Id
if (count($req->paras) <= 0) { # 参数为空, 执行自检逻辑
return self::selfhelpCheckOrders($req);
}
$cpOrderId = $req->paras[0]; # 后台订单编号
if (!$uid || ($zoneid < 1) || strlen($cpOrderId) != 16) { # 订单编号长16位
return Resp::err(ErrCode::paras_err);
}
$order = daoInst()->select()->from(self::orderTab) # 取订单数据
->where('cpOrderId')->eq($cpOrderId)
->limit(1)->fetch();
if (!$order) {
// var_dump($order);
CLog::pay(' 订单不存在! ' . $cpOrderId);
return Resp::err(ErrCode::pay_order_no);
}
if (false === strpos($uid, $order->uid)) { # UID不符
return Resp::err(ErrCode::pay_order_uid);
}
if ($order->status != 1) {
CLog::pay(' 订单尚未完成支付!');
return Resp::err(ErrCode::pay_order_paystatus);
}
if ($order->drawed_ts > 0) { # 定单已经发过货了
CLog::pay(' 订单重复请求发货!' . $cpOrderId);
return Resp::err(ErrCode::pay_order_drawed);
}
// 准备发货
$product_id = $order->product_id;
$product_count = $order->product_count;
$typid = substr("$product_id", 0, 3); # 提取商品编号前缀
switch ($typid) {
case '802': # 神秘商城物品
$item = GameConfig::secretshop_typeId_getItem($product_id);
break;
case '801': # 商城物品
$item = GameConfig::shop_getItem($product_id);
break;
default :
return Resp::err(ErrCode::pay_systembusy_err);
}
if (!$item) {
return Resp::err(ErrCode::pay_shopItem_cosnt_goods_err);
}
if ($item->pricetype != 0) {
CLog::pay(" 道具付费类型异常:" . $product_id);
return Resp::err(ErrCode::pay_m_type_err);
}
for ($i = 0; $i < $product_count; $i++) { # 默认为1,还没有设计为n的情况
StoreProc::AddMultiItemInStore($req, $item->goods, 5); # 发货
}
$n = daoInst()->update(self::orderTab)
->data(array('drawed_ts' => now()))
->where('cpOrderId')->eq($cpOrderId)
->exec();
my_Assert($n > 0, ErrCode::err_db); # 数据库操作失败
$req->userInfo->game->baseInfo->charge_amt += $order->amount; # 历史充值记录(单位分)
UserProc::updateUserInfo(); # 更新玩家数据信息
Event::trigger('pay', array('amt' => $order->amount)); # 触发充值事件
CLog::pay("订单发货成功: $cpOrderId");
return Resp::ok(# 返回
array('cash' => $req->userInfo->game->baseInfo->cash, # # 直接将游戏币的最新值返回给客户端
'charge_amt' => $req->userInfo->game->baseInfo->charge_amt, # 充值总记录
'store' => $req->userInfo->game->store, # # 背包
'goods' => $item->goods, # # 发货内容
'count' => $product_count, # # 发货数量
)
);
}
/**
* 检查内侧订单
* @param Req $req
*/
static function checkDeltest($req) {
$uid = $req->uid;
if (!isset($req->userInfo->game->privateState->deltest)) {
$orders = daoInst()->select()->from('tpl_order_tab_deltest')
->where('uid')->eq(substr($uid, strpos($uid, "-") + 1))
->andwhere('status')->eq(1)
->fetchall();
if (count($orders) > 0) { # 有充值记录
foreach ($orders as $order) {
// 准备补发
$product_id = $order->product_id;
$product_count = $order->product_count;
$typid = substr("$product_id", 0, 3); # 提取商品编号前缀
switch ($typid) {
case '802': # 神秘商城物品
$item = GameConfig::secretshop_typeId_getItem($product_id);
break;
case '801': # 商城物品
$item = GameConfig::shop_getItem($product_id);
break;
default :
return Resp::err(ErrCode::pay_systembusy_err);
}
if (!$item) {
return Resp::err(ErrCode::pay_shopItem_cosnt_goods_err);
}
if ($item->pricetype != 0) { # 并非人民币定价
CLog::pay(" 道具付费类型异常:" . $product_id);
return Resp::err(ErrCode::pay_m_type_err);
}
for ($i = 0; $i < $product_count; $i++) { # 默认为1,还没有设计为n的情况
EmailProc::SendDelTestMail($req->zoneid, $uid, $item->name, $item->goods);
}
CLog::pay($req->uid . '发送删档内侧补偿邮件' . $order->cpOrderId);
}
}
$req->userInfo->game->privateState->deltest = 0;
}
}
/**
* 检查异常订单
* @param Req $req
*/
public static function selfhelpCheckOrders($req) {
if ('ios' == PLAT) { # ios版的创建订单时带着前缀了。。。
$uid = $req->uid;
} else {
$uid = substr($req->uid, strpos($req->uid, '-') + 1);
}
$orders = daoInst()->select()
->from('tpl_order_tab')
->where('uid')->eq($uid)
->andWhere('status')->eq(1)
->andWhere('drawed_ts')->le(0)
->fetchAll();
if (count($orders) > 0) {
foreach ($orders as $order) {
// 准备补发
$product_id = $order->product_id;
$product_count = $order->product_count;
$typid = substr("$product_id", 0, 3); # 提取商品编号前缀
$goods = '';
switch ($typid) {
case '802': # 神秘商城物品
$item = GameConfig::secretshop_typeId_getItem($product_id);
break;
case '801': # 商城物品
$item = GameConfig::shop_getItem($product_id);
break;
default :
return Resp::err(ErrCode::pay_systembusy_err);
}
if (!$item) {
return Resp::err(ErrCode::pay_shopItem_cosnt_goods_err);
}
if ($item->pricetype != 0) { # 并非人民币定价
CLog::pay(" 道具付费类型异常:" . $product_id);
return Resp::err(ErrCode::pay_m_type_err);
}
for ($i = 0; $i < $product_count; $i++) { # 默认为1,还没有设计为n的情况
StoreProc::AddMultiItemInStore($req, $item->goods, 5); # 发货
}
$goods .= $item->goods;
$n = daoInst()->update(self::orderTab) # 更新订单状态
->data(array('drawed_ts' => now()))
->where('cpOrderId')->eq($order->cpOrderId)
->exec();
if (!$n) { # 影响条数应为1
CLog::pay($req->uid . ' 登录补发订单失败 ' . $order->cpOrderId);
return Resp::err(ErrCode::err_db); # 数据库操作失败
}
$req->userInfo->game->baseInfo->charge_amt += $order->amount; # 历史充值记录(单位分)
UserProc::updateUserInfo(); # 回写玩家数据
CLog::pay($req->uid . ' 登录补发订单 ' . $order->cpOrderId);
}
return Resp::ok(# 返回
array('cash' => $req->userInfo->game->baseInfo->cash, # # 直接将游戏币的最新值返回给客户端
'store' => $req->userInfo->game->store, # # 背包
'goods' => $goods, # # 发货内容
'count' => count($orders), # # 发货数量
'msg' => "找到并处理" . count($orders) . "条未发货订单."
));
}
return Resp::ok('ok'); # everything is ok!
}
//
//
//
/**
* 【移动支付】获取神秘商城物品
* 刷新规则: 根据玩家拥有的英雄、装备、道具等数据进行刷新。(因英雄、道具、装备数量不足以支撑该刷新规则,目前先按照随机刷新做,几率平等)
* @param Req $req
*/
public static function m_pay_getDynamic($req) {
$user = $req->userInfo->game;
$userSecretshop = new Info_UserSecretshop($user->userSecretshop);
// 参数提取
$refreshType = $req->paras[0]; # 刷新类型(参数)0,不刷,1,免费刷,2,钻石刷
switch ($refreshType) {
case 1: # 免费刷
if (now() < $userSecretshop->lastRefreshTs + glc()->secretshop_refresh_interval) { // 检查是否达到免费刷新时间了, 执行自动更新
return Resp::err(ErrCode::pay_secretshopt_freeRefresh_Time);
}
break;
case 2: # 钻石刷
if (glc()->secretshop_refresh_maxtimes <= $userSecretshop->refreshedTimes) { // 检查刷新次数, 已达上限, 返回错误信息
return Resp::err(ErrCode::pay_refresh_times);
} # 可以继续刷新,
$cishu = $userSecretshop->refreshedTimes + 1; # 下次
$amt = GameConfig::secretshop_refresh_getItem($cishu)->price;
if (!Data_UserGame::Consume_Cash($user->baseInfo, $amt)) { # 扣除本次所需费用, 余额不足, 返回错误信息
return Resp::err(ErrCode::notenough_cash_msg);
}
$userSecretshop->refreshedTimes++; # 增加当天付费刷新计数
break;
case 0: # 不刷
default : # 默认不刷
// do nothing.
break;
}
if ($refreshType != 0) { # 是否刷新
$err = self::refreshDynamicShopItems($req, $userSecretshop); # 更新物品表
if ($err) {
return Resp::err($err);
}
$user->userSecretshop = $userSecretshop;
$req->userInfo->game = $user;
UserProc::updateUserInfo();
}
// 返回最新物品表
return Resp::ok(array(# # 成功后将最新的玩家数据返回给客户端
'gold' => $user->baseInfo->gold,
'tili' => $user->baseInfo->tili,
'cash' => $user->baseInfo->cash,
'uss' => $userSecretshop, # # 当前神秘商城数据
));
}
/**
* 更新神秘商城物品
* @param Req $req
* @param Info_UserSecretshop $userSecretshop Description
*/
private static function refreshDynamicShopItems($req, &$userSecretshop) {
$userSecretshop->lastRefreshTs = now();
// todo: 这里补完更新物品的函数, // 第一版: 随机
$userSecretshop->currentItems = ObjectInit();
for ($i = 1; $i <= 3; $i++) { # 3种类型的商品
$arr = GameConfig::secretshop_goodsType_getItem($i);
if (count($arr) > 0) {
$err = self::Dice(GameConfig::secretshop_goodsType_getItem($i), 1, $userSecretshop); # 一个种类一次1个物品
if ($err) {
return $err;
}
}
}
return ErrCode::ok;
}
/**
* 投骰子
* @param assoc_array $arr 奖池物品
* @param int $number 提取数量
* @return string itemid,num;itemid,num;...
*/
static function Dice($arr, $number, &$userSecretshop) {
$max = 0; # 计算物品权重总和
array_walk($arr, function ($value) use(&$max) {
$max += $value->probability;
});
if (!$max) { # 配置数据有问题
return ErrCode::err_const_no;
}
for ($i = 0; $i < $number; $i++) {
$rnd = CommUtil::random(1, $max); # 投骰子
$start = 0;
$rew = null;
foreach ($arr as $item) { # 循环判断落入那个物品上
if (CommUtil::isPropertyExists($item, 'probability') // #
&& $start < $rnd && $rnd <= $start + $item->probability) { # 落入区间
$rew = $item; # 记录物品
break;
}
$start += $item->probability; # 继续判断是否落入下一物品的区间
} # foreach end
if (!$rew) {
return ErrCode::lottery_noselecteditem;
}
$id = $rew->typeId;
$userSecretshop->currentItems->$id = 0;
} # for end
return ErrCode::ok;
}
/**
* 清理每日各种上限
* @param Req $req
*/
private static function clearDailyLimits($req) {
$userSecretshop = new Info_UserSecretshop($req->userInfo->game->userSecretshop);
$userSecretshop->refreshedTimes = 0;
$req->userInfo->game->userSecretshop = $userSecretshop;
}
/**
* 【移动支付】购买神秘商城物品
* 规则: 商城物品会刷新
* @param Req $req
*/
public static function m_pay_buyDynamic($req) {
$user = new Data_UserGame($req->userInfo->game);
$userSecretshop = new Info_UserSecretshop($user->userSecretshop);
// 参数提取
$itemId = $req->paras[0]; # 商品id (点一次购买一个)
$num = 1; # 暂时固定为一次购买一个
#
if (!CommUtil::isPropertyExists($userSecretshop->currentItems, $itemId)) {
return Resp::err(ErrCode::pay_secretshop_noitem_err);
}
$cishu = $userSecretshop->currentItems->$itemId; # 购买次数
if ($cishu >= glc()->secretshop_itembuy_maxtimes) { # 判断购买次数
return Resp::err(ErrCode::pay_secretshop_buytimes);
}
$shopItem = GameConfig::secretshop_typeId_getItem($itemId); # 常量数据
$amt = $shopItem->price * pow(2, $cishu); # 计算价格
switch ($shopItem->pricetype) {
case 1:
if (!Data_UserGame::Consume_Cash($user->baseInfo, $amt)) { # 扣除钻石
return Resp::err(ErrCode::notenough_cash_msg);
}
break;
case 2:
if (!Data_UserGame::Consume_Gold($user->baseInfo, $amt)) { # 扣除金币
return Resp::err(ErrCode::notenough_gold_msg);
}
break;
default :
return Resp::err(ErrCode::pay_m_type_err);
}
// 道具解包/发货
$err = self::expendSecretShopItem($shopItem, $num, $req); # 发放商品()
if ($err != ErrCode::ok) { # 发货失败
return Resp::err($err);
}
$userSecretshop->currentItems->$itemId += 1; # 购买次数+1
$user->userSecretshop = $userSecretshop;
$req->userInfo->game = $user;
UserProc::updateUserInfo(); # 回写数据
// StatProc::secretShopbuy($req->zoneid, $req->uid, $itemId, $num); # 统计/监控数据
return Resp::ok(array(# # 成功后将最新的玩家数据返回给客户端
'gold' => $user->baseInfo->gold,
'tili' => $user->baseInfo->tili,
'cash' => $user->baseInfo->cash,
'store' => $user->baseInfo->store, # # 背包,(装备、碎片、。。)
'heros' => $user->baseInfo->heros # # 角色
));
//
}
/**
*
* @param sm_secretshop $shopItem
* @param type $num
* @param type $req
*/
private static function expendSecretShopItem($shopItem, $num, $req) {
for ($i = 0; $i < $num; $i++) {
$err = StoreProc::AddMultiItemInStore($req, $shopItem->goods, '神秘商城');
if ($err) {
return $err;
}
}
return ErrCode::ok;
}
/**
* 【移动支付】 购买普通商城物品
* @param Req $req
* @return type
*/
public static function m_pay($req) {
$zoneid = $req->zoneid;
$uid = $req->uid;
$user = $req->userInfo->game; # user引用
if (count($req->paras) < 3) { # 参数不足
return Resp::err(ErrCode::parasnotenough_msg);
}
$paytype = $req->paras[0]; # 付费类型, 1.钻石, 2.金币
$itemId = $req->paras[1]; # 道具ID
$num = $req->paras[2]; # 数量, 默认为1
if ($num < 1) { # 参数非法,
return Resp::err(ErrCode::paras_err);
}
$shopItem = GameConfig::shop_getItem($itemId); # 取商品的常量数据
if ($shopItem == null) { # 检测是否存在道具的常量数据
return Resp::err(ErrCode::err_const_no);
}
if ($shopItem->pricetype != $paytype) { # 商品定价类型检查
return Resp::err(ErrCode::pay_price_err);
}
$amt = $shopItem->price; # 道具价格(钻石)
if ($amt <= 0) { # 商品定价数值检查
return Resp::err(ErrCode::pay_price_err);
}
$bDeal = false; # 成交标志位
switch ($paytype) { # 1. 钻石, 2. 金币, other:非法
case 1: # 钻石
$err = self::_SaveUserCash($req, $amt * $num); # 更新(扣除)玩家游戏币(钻石)余额
if (ErrCode::ok != $err) {
return Resp::err($err);
}
$bDeal = true;
break;
case 2: # 金币
if (!Data_UserGame::Consume_Gold($user->baseInfo, $amt * $num)) { # 更新玩家金币余额
return Resp::err(ErrCode::notenough_gold_msg);
} # 更新(扣除)玩家游戏币(金币)余额
$bDeal = true;
break;
default : # 未知的支付类型
return Resp::err(ErrCode::pay_m_type_err);
}
if ($bDeal) {
// var_dump($shopItem);
$err = self::expendShopItem($shopItem, $num, $req); # 发放商品(普通商城只有金币和体力)
if ($err != ErrCode::ok) { # 发货失败
return Resp::err($err);
}
UserProc::updateUserInfo(); # 回写数据
// StatProc::shopbuy($zoneid, $uid, $itemId, $num); # 统计/监控数据
return Resp::ok(array(# # 成功后将最新的玩家数据返回给客户端
'gold' => $user->baseInfo->gold,
'tili' => $user->baseInfo->tili,
'cash' => $user->baseInfo->cash,
'store' => $user->store
));
}
return Resp::err(ErrCode::err_innerfault);
}
/**
* 解包商城物品
* @param sm_Shop $shopItem
* @param int $num 数量
* @param Req $req
*/
static function expendShopItem($shopItem, $num, $req) {
for ($i = 0; $i < $num; $i++) {
$err = StoreProc::AddMultiItemInStore($req, $shopItem->goods, '商城');
if ($err) {
return $err;
}
}
return ErrCode::ok;
}
/**
* 扣除玩家钻石
* @param Req $req
* @param int $amt
* @return int
*/
static function _SaveUserCash($req, $amt) {
$err = ErrCode::ok;
if ($amt > 0) { # 防御,数量不可能小于0
$user = $req->userInfo->game;
if ($user->cash < $amt) { # 余额不足
$err = ErrCode::notenough_cash_msg;
} else { # 更新玩家游戏币余额
Data_UserGame::set_Cash($user, $user->cash - $amt);
}
}
return $err;
}
/**
* 扣除玩家金币
* @param req $req
* @param type $amt
* @param type $source
* @param type $itemId
* @return type
*/
static function _SaveUserGold($req, $amt) {
$err = ErrCode::ok;
$user = $req->userInfo->game;
if (!Data_UserGame::Consume_Gold($user->baseInfo, $amt)) { # 更新玩家金币余额
$err = ErrCode::notenough_gold_msg;
}
return $err;
}
//
//
//
//
/**
* 领取首付礼包
* @param Req $req
* @return Resp
*/
public static function m_GetFirstPayGift($req) {
$itemId = glc()->FirstPay_ItemId;
$privateState = $req->userInfo->game->privateState;
if (CommUtil::isPropertyExists($privateState, "firstPayGift") #
&& $privateState->firstPayGift) { # 如果已经领取首付礼包
return Resp::err(ErrCode::pay_firstpaygetted);
}
if ($req->userInfo->game->baseInfo->charge_amt <= 0) { # 如果尚未完成首付
return Resp::err(ErrCode::pay_firstpayno_err);
}
$itemModel = GameConfig::shop_getItem($itemId); # 取商品常量
if ($itemModel == null) { # 检测首付礼包是否存在
return Resp::err(ErrCode::err_const_no);
}
$req->userInfo->game->privateState->firstPayGift = true; # 设置首付标志
StoreProc::AddMultiItemInStore($req, $itemModel->goods); # 发放首付礼包到玩家仓库
UserProc::updateUserInfo(); # 更新玩家数据
return Resp::ok(array('itemid' => $itemId,
'rewardstr' => $itemModel->goods,
'store' => $req->userInfo->game->store)); # 回送成功信息
}
//
//
//
//
}