aozhiwei 6db522bf78 1
2020-08-29 00:42:23 +08:00

108 lines
2.9 KiB
PHP

<?php
namespace metatable;
use phpcommon;
/*
配置表规范
getXXXConf:获取表所有数据
getXxxById():通过id获取单个数据
_internalGetXXXConf:获取表所有数据内部实现不对外开放
使用方式
require_once 'metatable/XXXX.php';
!!!注意必须使用require_once
*/
function getExplode($string)
{
$delim = "|";
$drop_multiply = explode($delim, $string);
$delim1 = ":";
$arr = array();
for ($i = 0; $i < count($drop_multiply); $i++) {
$mul = explode($delim1, $drop_multiply[$i]);
array_push($arr, $mul);
}
return $arr;
}
function getShopConf()
{
return _internalGetShopConf();
}
function getShopById($shop_id)
{
$conf = getShopConf();
$shop_id = (int)$shop_id;
return array_key_exists($shop_id, $conf) ? $conf[$shop_id] : null;
}
function randGoods($shop_conf, $exclude_goods)
{
$num = $shop_conf['num'];
$goods_list = explode('|', $shop_conf['goods_list']);
$goods_weight = explode('|', $shop_conf['goods_weight']);
$goods_buy = getExplode($shop_conf['price']);
$goods_discount = explode('|', $shop_conf['discount']);
$discount_num = $shop_conf['discount_num'];
// if (!empty($exclude_goods)) {
// for ($i = count($goods_list) - 1; $i >= 0; --$i) {
// if (array_key_exists($goods_list[$i], $exclude_goods)) {
// array_splice($goods_list, $i, 1);
// array_splice($goods_weight, $i, 1);
// --$num;
// }
// }
// }
$goods = array();
for ($i = 0; $i < $num; ++$i) {
$rand_space = 0;
foreach ($goods_weight as $value) {
$rand_space += $value;
}
if ($rand_space <= 0) {
break;
}
$rnd_val = rand(0, $rand_space);
$curr_val = 0;
$discount = false;
if ($discount_num != 0) {
$discount = true;
$discount_num--;
}
for ($ii = 0; $ii < count($goods_weight); ++$ii) {
$curr_val += $goods_weight[$ii];
if ($rnd_val <= $curr_val) {
array_push($goods, array(
'id' => $goods_list[$ii],
'buy' => $goods_buy[$ii][0],
'price' => $goods_buy[$ii][1],
'status' => 0,
'isdiscount' => $discount,
'dis_num' => $goods_discount[$ii]
));
array_splice($goods_list, $ii, 1);
array_splice($goods_weight, $ii, 1);
array_splice($goods_buy, $ii, 1);
array_splice($goods_discount, $ii, 1);
break;
}
}
}
return $goods;
}
function _internalGetShopConf()
{
global $g_shop_table;
if (!$g_shop_table) {
$g_shop_table = require(getConfigBaseDir() . 'shop@shop.php');
}
return $g_shop_table;
}