game2006api/webapp/services/LootService.php
hujiabin 358f127b0c 1
2024-02-26 16:35:11 +08:00

212 lines
7.0 KiB
PHP

<?php
namespace services;
require_once('mt/LootConfig.php');
use mt;
class LootService
{
const EMPTY_DROP_TYPE = 1; //空掉落
const WHERE_DROP_TYPE = 2; //条件掉落
const ORDER_DROP_TYPE = 3; //有序全掉落
const HAVE_RAND_DROP_TYPE = 4; //有放回随机掉落
const NOT_HAVE_RAND_DROP_TYPE = 5; //无放回随机掉落
const DYN_RAND_DROP_TYPE = 6; //动态概率掉落
const STEP_RAND_DROP_TYPE = 7; //步长随机掉落
public static function dropOutReward($dropIndex){
$lootMeta = mt\LootConfig::find($dropIndex);
if (! $lootMeta){
return ;
}
$items = array();
switch ($lootMeta['method']){
// case self::EMPTY_DROP_TYPE: {
//
// }
// break;
case self::WHERE_DROP_TYPE: {
//暂定
}
break;
case self::ORDER_DROP_TYPE: {
$items = self::disposeDropContent1($lootMeta);
}
break;
case self::HAVE_RAND_DROP_TYPE: {
$items = self::disposeDropContent2($lootMeta);
}
case self::NOT_HAVE_RAND_DROP_TYPE: {
$items = self::disposeDropContent3($lootMeta);
}
break;
case self::DYN_RAND_DROP_TYPE: {
$items = self::disposeDropContent4($lootMeta);
}
break;
}
return $items;
}
private static function disposeDropContent4($lootMeta) {
$totalTimes = myself()->_getV(TN_TOTAL_LOOT_TIMES,0);
$recycleTimes = myself()->_getV(TN_TOTAL_LOOT_TIMES,1);
$contentArr = explode("|",$lootMeta['content']);
$initTimes = $contentArr[0];
$po = $contentArr[1];
$pd = $contentArr[2];
$pm = $contentArr[3];
$index1 = $contentArr[4];
$index2 = $contentArr[5];
if ($totalTimes == 0){
$recycleTimes = $initTimes;
}
$p = min($po + ($recycleTimes-1) * $pd,$pm) * 100 ;
$rnd = rand(1,100);
myself()->_incV(TN_TOTAL_LOOT_TIMES,0,1);
if ($rnd <= $p){
myself()->_setV(TN_TOTAL_LOOT_TIMES,1,1);
return self::dropOutReward($index1);
}else{
myself()->_incV(TN_TOTAL_LOOT_TIMES,1,1);
return self::dropOutReward($index2);
}
}
private static function disposeDropContent3($lootMeta) {
$numPara = explode("|",$lootMeta['numPara']);
$items = array();
$times = $numPara[0];
$number = $numPara[1];
$contentArr = explode("|",$lootMeta['content']);
for ($i=0;$i<$times;$i++){
for ($j=0;$j<$number;$j++){
self::randWeight2($contentArr,$items);
}
}
return $items;
}
private static function randWeight2(&$contentArr,&$items){
$totalWeight = 0;
foreach ($contentArr as $value){
$contentArrEx = explode(":",$value);
$totalWeight += $contentArrEx[3];
}
$rnd = rand() % $totalWeight;
$currWeight = 0;
foreach ($contentArr as $key=>$value){
$contentArrEx = explode(":",$value);
$currWeight += $contentArrEx[3];
if ($currWeight > $rnd){
switch ($contentArrEx[0]){
case 1: {
array_push($items,array(
"item_id" => $contentArrEx[1],
"item_num" => $contentArrEx[2],
));
}
break;
case 2: {
$rewards = self::dropOutReward($contentArrEx[1]);
$items = array_merge($items,$rewards);
}
break;
}
unset($contentArr[$key]);
break;
}
}
}
private static function disposeDropContent2($lootMeta) {
$numPara = explode("|",$lootMeta['numPara']);
$items = array();
$times = $numPara[0];
$number = $numPara[1];
for ($i=0;$i<$times;$i++){
for ($j=0;$j<$number;$j++){
$result = self::randWeight($lootMeta['content']);
// array_push($items,$result);
$items = array_merge($items,$result);
}
}
$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;
}
}
$finalItems = array();
foreach ($hashItems as $hashItem){
array_push($finalItems,$hashItem);
}
return $finalItems;
}
private static function randWeight($content){
$contentArr = explode("|",$content);
$totalWeight = 0;
foreach ($contentArr as $value){
$contentArrEx = explode(":",$value);
$totalWeight += $contentArrEx[3];
}
$rnd = rand() % $totalWeight;
$currWeight = 0;
$items = array();
foreach ($contentArr as $value){
$contentArrEx = explode(":",$value);
$currWeight += $contentArrEx[3];
if ($currWeight > $rnd){
switch ($contentArrEx[0]){
case 1: {
array_push($items,array(
"item_id" => $contentArrEx[1],
"item_num" => $contentArrEx[2],
));
}
break;
case 2: {
$items = self::dropOutReward($contentArrEx[1]);
}
break;
}
break;
}
}
return $items;
}
private static function disposeDropContent1($lootMeta){
$contentArr = explode("|",$lootMeta['content']);
$items = array();
if (count($contentArr) > 0){
foreach ($contentArr as $value){
$contentArrEx = explode(":",$value);
switch ($contentArrEx[0]){
case 1: {
array_push($items,array(
"item_id" => $contentArrEx[1],
"item_num" => $contentArrEx[2],
));
}
break;
case 2: {
$rewards = self::dropOutReward($contentArrEx[1]);
$items = array_merge($items,$rewards);
}
break;
}
}
}
return $items;
}
}