303 lines
10 KiB
PHP
303 lines
10 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 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()
|
|
{
|
|
$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 error');
|
|
return;
|
|
}
|
|
if ($priceInfo['cost_item_id'] != V_ITEM_DIAMOND) {
|
|
myself()->_rspErr(500, 'server internal error');
|
|
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'], $girdRef);
|
|
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;
|
|
}
|
|
error_log(json_encode(array(
|
|
'randSpace1' => $randSpace1,
|
|
'randSpace2' => $randSpace2,
|
|
'item1Arr' => $item1Arr,
|
|
'item2Arr' => $item2Arr,
|
|
)));
|
|
if ($randSpace1 <= 0 ||
|
|
$randSpace2 <= 0) {
|
|
myself()->_rspErr(1, 'server internal error');
|
|
return;
|
|
}
|
|
$item1 = null;
|
|
{
|
|
$rnd = rand(0, $randSpace1 - 1);
|
|
foreach ($item1Arr as $item) {
|
|
if ($item['weight'] <= $rnd) {
|
|
$item1 = $item;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
$item2 = null;
|
|
{
|
|
$rnd = rand(0, $randSpace2 - 2);
|
|
foreach ($item2Arr as $item) {
|
|
if ($item['weight'] <= $rnd) {
|
|
$item2 = $item;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
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' => 2,
|
|
'item_id' => $drop1[0]['item_id'],
|
|
'item_num' => $drop1[0]['item_num'],
|
|
'buy_price' => 0,
|
|
'draw_times' => $data['drawed_times'],
|
|
);
|
|
$grid2 = array(
|
|
'grid_id' => $item2['meta']['id'],
|
|
'grid_state' => 2,
|
|
'item_id' => $drop2[0]['item_id'],
|
|
'item_num' => $drop2[0]['item_num'],
|
|
'buy_price' => 0,
|
|
'draw_times' => $data['drawed_times'],
|
|
);
|
|
array_push($data['grid_list'], $grid1);
|
|
array_push($data['grid_list'], $grid2);
|
|
myself()->_callModelStatic('MidData', 'setData', $key, json_encode($data));
|
|
}
|
|
|
|
public function buyS()
|
|
{
|
|
$key = $this->getMidDataKey();
|
|
$data = $this->getAndCreateData($key);
|
|
}
|
|
|
|
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_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'], $girdRef);
|
|
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;
|
|
}
|
|
|
|
}
|