game2006api/webapp/controller/BigwheelController.class.php
aozhiwei adbb0ea437 1
2024-08-22 11:38:22 +08:00

451 lines
15 KiB
PHP

<?php
require_once('mt/Parameter.php');
require_once('mt/Bigwheel.php');
require_once('services/LogService.php');
require_once('services/AwardService.php');
require_once('services/PropertyChgService.php');
use phpcommon\SqlHelper;
use models\User;
use services\LogService;
/*
{
"grid_list":
{
"grid_id": 123,
"grid_state": 0,
"item_id": 123,
"item_num": 314,
"buy_price": 314,
"draw_times": 1,
}
}
*/
class BigwheelController extends BaseAuthedController {
const MAX_DRAW_TIMES = 9;
public function _handlePre()
{
parent::_handlePre();
$this->propertyChgService = new services\PropertyChgService();
$this->awardService = new services\AwardService();
}
public function info()
{
$key = $this->getMidDataKey();
$data = $this->getAndCreateData($key);
$priceInfo = $this->getPriceInfo($data['drawed_times']);
if (empty($priceInfo)) {
myself()->_rspErr(500, 'server internal error');
return;
}
$info = array();
$this->fillInfo($info, $data, $priceInfo);
myself()->_rspData(array(
'info' => $info
));
}
public function drawS()
{
myself()->_verifySwitch('bigWheel');
$drawType = getReqVal('type', 0);
if (!($drawType == 0 || $drawType == 1 || $drawType == 2)) {
myself()->_rspErr(2, 'type param error');
return;
}
$key = $this->getMidDataKey();
$data = $this->getAndCreateData($key);
if ($data['drawed_times'] >= self::MAX_DRAW_TIMES) {
myself()->_rspErr(1, 'The maximum number of lucky draws has been reached');
return;
}
$priceInfo = $this->getPriceInfo($data['drawed_times']);
if (empty($priceInfo)) {
myself()->_rspErr(500, 'server internal error1');
return;
}
if ($priceInfo['cost_item_id'] != V_ITEM_DIAMOND) {
myself()->_rspErr(500, 'server internal error2');
return;
}
$costItemNum = 0;
if ($drawType == 0) {
$costItemNum = $priceInfo['price_double'] * $priceInfo['discount_double'];
if (empty($costItemNum)) {
myself()->_rspErr(3, 'config error');
return;
}
} else {
$costItemNum = $priceInfo['price_single'] * $priceInfo['discount_single'];
if (empty($costItemNum)) {
myself()->_rspErr(3, 'config error');
return;
}
}
$item1Hash = array();
$item1Arr = array();
$item2Hash = array();
$item2Arr = array();
mt\Bigwheel::traverseMeta(
function ($meta) use(&$data, &$item1Hash, &$item2Hash, &$item1Arr, &$item2Arr) {
$gridRef = null;
$this->getGridRefByGridId($data, $meta['id'], $gridRef);
if (empty($gridRef)) {
switch ($meta['Wheel_type']) {
case 1:{
$item1Hash[$meta['id']] = $meta;
array_push($item1Arr, array(
'weight' => 0,
'meta' => $meta
));
}
break;
case 2:{
$item2Hash[$meta['id']] = $meta;
array_push($item2Arr, array(
'weight' => 0,
'meta' => $meta
));
}
break;
}
}
return true;
});
if (count($item1Hash) != count($item2Hash) ||
count($item1Arr) != count($item2Arr) ||
count($item1Arr) != count($item1Hash) ||
count($item1Hash) < 1) {
myself()->_rspErr(1, 'The maximum number of lucky draws has been reached');
return;
}
$randSpace1 = 0;
foreach ($item1Arr as &$item) {
$randSpace1 += $item['meta']['reWeight'];
$item['weight'] = $randSpace1;
}
$randSpace2 = 0;
foreach ($item2Arr as &$item) {
$randSpace2 += $item['meta']['reWeight'];
$item['weight'] = $randSpace2;
}
$rnd1 = rand(0, $randSpace1 - 1);
$rnd2 = rand(0, $randSpace2 - 1);
error_log(json_encode(array(
'randSpace1' => $randSpace1,
'randSpace2' => $randSpace2,
'rnd1' => $rnd1,
'rnd2' => $rnd2,
'item1Arr' => $item1Arr,
'item2Arr' => $item2Arr,
)));
if ($randSpace1 <= 0 ||
$randSpace2 <= 0) {
myself()->_rspErr(1, 'server internal error');
return;
}
$item1 = null;
error_log(json_encode(array(
'item2arr' => $item2Arr
)));
{
foreach ($item1Arr as $itemTmp1) {
if ($rnd1 <= $itemTmp1['weight']) {
$item1 = $itemTmp1;
break;
}
}
}
$item2 = null;
{
error_log(json_encode(array(
'item2arr' => $item2Arr
)));
foreach ($item2Arr as $itemTmp2) {
error_log(json_encode(array(
'rnd2' => $rnd2,
'item' => $itemTmp2,
//'item2_weight' => $item['weight'],
)));
if ($rnd2 <= $itemTmp2['weight']) {
$item2 = $itemTmp2;
break;
}
}
}
error_log(json_encode(array(
'item1' => $item1,
'item2' => $item2,
)));
if (empty($item1) || empty($item2)) {
myself()->_rspErr(1, 'server internal error');
return;
}
$lootMeta1 = myself()->_callMtStatic('LootConfig', 'find', $item1['meta']['content_loot']);
$lootMeta2 = myself()->_callMtStatic('LootConfig', 'find', $item2['meta']['content_loot']);
if (empty($lootMeta2) || empty($lootMeta2)) {
myself()->_rspErr(1, 'server internal error');
return;
}
$drop1 = myself()->_callServiceStatic('LootService', 'dropOutItem', $item1['meta']['content_loot']);
$drop2 = myself()->_callServiceStatic('LootService', 'dropOutItem', $item2['meta']['content_loot']);
error_log(json_encode(array(
'drop1' => $drop1,
'drop2' => $drop2,
)));
++$data['drawed_times'];
$grid1 = array(
'grid_id' => $item1['meta']['id'],
'grid_state' => 1,
'item_id' => $drop1[0]['item_id'],
'item_num' => $drop1[0]['item_num'],
'buy_price' => $priceInfo['price_buy'] * $priceInfo['discount_buy'],
'draw_times' => $data['drawed_times'],
);
$grid2 = array(
'grid_id' => $item2['meta']['id'],
'grid_state' => 1,
'item_id' => $drop2[0]['item_id'],
'item_num' => $drop2[0]['item_num'],
'buy_price' => $priceInfo['price_buy'] * $priceInfo['discount_buy'],
'draw_times' => $data['drawed_times'],
);
$items = array();
if ($drawType == 1) {
$grid2['grid_state'] = 2;
foreach ($drop1 as $itemTemp){
array_push($items, $itemTemp);
}
} else if ($drawType == 2) {
$grid1['grid_state'] = 2;
foreach ($drop2 as $itemTemp){
array_push($items, $itemTemp);
}
} else {
foreach ($drop1 as $itemTemp){
array_push($items, $itemTemp);
}
foreach ($drop2 as $itemTemp){
array_push($items, $itemTemp);
}
}
$hashItems = array();
foreach ($items as $item){
if (isset($hashItems[$item['item_id']])){
$hashItems[$item['item_id']]['item_num'] += $item['item_num'];
}else{
$hashItems[$item['item_id']] = $item;
}
}
array_push($data['grid_list'], $grid1);
array_push($data['grid_list'], $grid2);
$costItems = array(
array(
'item_id' => V_ITEM_DIAMOND,
'item_num' => $costItemNum
),
);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
$this->propertyChgService->addUserChg();
myself()->_addItems($hashItems, $this->awardService, $this->propertyChgService);
myself()->_callModelStatic('MidData', 'setData', $key, json_encode($data));
{
$priceInfo = $this->getPriceInfo($data['drawed_times']);
if (empty($priceInfo)) {
myself()->_rspErr(500, 'server internal error');
return;
}
$info = array();
$this->fillInfo($info, $data, $priceInfo);
myself()->_rspData(array(
'award' => $this->awardService->toDto(),
'property_chg' => $this->propertyChgService->toDto(),
'info' => $info,
'prizes' => array(
$grid1,
$grid2
)
));
}
myself()->_fireEvent('Bigwheel', 'onWinPrize',
array(
'info' => $grid1,
'drop' => $drop1
),
array(
'info' => $grid2,
'drop' => $drop2
));
}
public function buyS()
{
myself()->_verifySwitch('bigWheel');
$key = $this->getMidDataKey();
$data = $this->getAndCreateData($key);
$gridRef = null;
$gridId = getReqVal('grid_id', 0);
$this->getGridRefByGridId($data, $gridId, $gridRef);
if (empty($gridRef) ||
$gridRef['grid_state'] != 2) {
myself()->_rspErr(1, 'cant buy');
return;
}
$costItems = array(
array(
'item_id' => V_ITEM_DIAMOND,
'item_num' => $gridRef['buy_price']
),
);
$lackItem = null;
if (!$this->_hasEnoughItems($costItems, $lackItem)) {
$this->_rspErr(3, $this->_getLackItemErrMsg($lackItem));
return;
}
$this->_decItems($costItems);
$gridRef['grid_state'] = 1;
foreach ($data['grid_list'] as &$grid) {
if ($grid['grid_id'] == $gridId) {
$grid['grid_state'] = 1;
}
}
myself()->_callModelStatic('MidData', 'setData', $key, json_encode($data));
$this->awardService->addItem($gridRef['item_id'], $gridRef['item_num']);
$this->propertyChgService->addUserChg();
$priceInfo = $this->getPriceInfo($data['drawed_times']);
if (empty($priceInfo)) {
myself()->_rspErr(500, 'server internal error');
return;
}
$info = array();
$this->fillInfo($info, $data, $priceInfo);
myself()->_rspData(array(
'award' => $this->awardService->toDto(),
'property_chg' => $this->propertyChgService->toDto(),
'info' => $info
));
myself()->_fireEvent('Bigwheel', 'onBuyOk',
$gridId,
$gridRef['buy_price']
);
}
private function getMidDataKey()
{
return myself()->_getModelConstant('MidData', 'BIG_WHEEL_TYPE');
}
private function getAndCreateData($key)
{
$data = myself()->_callModelStatic('MidData', 'getData', $key);
if (empty($data)) {
$data = array(
'drawed_times' => 0,
"grid_list" => array()
);
}
return $data;
}
private function fillInfo(&$info, &$data, $priceInfo)
{
$info = array(
'drawed_times' => $data['drawed_times'],
'total_times' => self::MAX_DRAW_TIMES,
'single_src_cost' => $priceInfo['price_single'],
'double_src_cost' => $priceInfo['price_double'],
'double_cost' => $priceInfo['price_double'] * $priceInfo['discount_double'],
'single_cost' => $priceInfo['price_single'] * $priceInfo['discount_single'],
'double_cost' => $priceInfo['price_double'] * $priceInfo['discount_double'],
'items1' => array(),
'items2' => array(),
);
mt\Bigwheel::traverseMeta(
function ($meta) use(&$data, &$info) {
$item = array(
'grid_id' => $meta['id'],
'grid_state' => 0,
'item_id' => 0,
'item_num' => 0,
'buy_price' => 0,
);
$gridRef = null;
$this->getGridRefByGridId($data, $meta['id'], $gridRef);
if (!empty($gridRef)) {
$item = $gridRef;
}
switch ($meta['Wheel_type']) {
case 1:{
array_push($info['items1'], $item);
}
break;
case 2:{
array_push($info['items2'], $item);
}
break;
}
return true;
});
}
private function getGridRefByGridId(&$data, $gridId, &$gridRefOut)
{
foreach ($data['grid_list'] as &$grid) {
if ($grid['grid_id'] == $gridId) {
$gridRefOut = $grid;
break;
}
}
}
private function getPriceInfo($drawedTimes)
{
$costItem = mt\Parameter::getVal('gacha_cost_item', 0);
$gachaPriceSingles = mt\Parameter::getListValue('gacha_price_single');
$gachaDiscountSingles = mt\Parameter::getListValue('gacha_discount_single');
$gachaPriceDoubles = mt\Parameter::getListValue('gacha_price_double');
$gachaDiscountDoubles = mt\Parameter::getListValue('gacha_discount_double');
$gachaPriceBuys = mt\Parameter::getListValue('gacha_price_buy');
$gachaDiscountBuys = mt\Parameter::getListValue('gacha_discount_buy');
if (count($gachaPriceSingles) != count($gachaDiscountSingles) &&
count($gachaPriceDoubles) != count($gachaDiscountDoubles) &&
count($gachaPriceBuys) != count($gachaDiscountBuys) &&
count($gachaDiscountBuys) != self::MAX_DRAW_TIMES) {
return null;
}
if ($drawedTimes < 0) {
return null;
}
if ($drawedTimes >= self::MAX_DRAW_TIMES) {
$drawedTimes = self::MAX_DRAW_TIMES - 1;
}
$priceInfo = array(
'cost_item_id' => $costItem,
'price_single' => $gachaPriceSingles[$drawedTimes],
'discount_single' => $gachaDiscountSingles[$drawedTimes],
'price_double' => $gachaPriceDoubles[$drawedTimes],
'discount_double' => $gachaDiscountSingles[$drawedTimes],
'price_buy' => $gachaPriceBuys[$drawedTimes],
'discount_buy' => $gachaDiscountBuys[$drawedTimes],
);
return $priceInfo;
}
}