57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace mt;
|
|
|
|
class BlindBox {
|
|
|
|
public static function randItem($boxId, $groupId)
|
|
{
|
|
self::mustBeGroupHash();
|
|
$key = $boxId . '_' . $groupId;
|
|
$spaceKey = $key . '_rand_space';
|
|
|
|
$spaceVal = getXVal(self::$groupHash, $spaceKey, 0);
|
|
$rndVal = $spaceVal > 0 ? rand() % $spaceVal : 0;
|
|
|
|
$groups = getXVal(self::$groupHash, $key);
|
|
foreach ($groups as $group) {
|
|
if ($rndVal <= $group['_weight']) {
|
|
return $group;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
protected static function mustBeGroupHash()
|
|
{
|
|
if (is_null(self::$groupHash)) {
|
|
self::$groupHash = array();
|
|
foreach (self::getMetaList() as $meta) {
|
|
$key = $meta['box_id'] . '_' . $meta['group_id'];
|
|
$spaceKey = $key . '_rand_space';
|
|
|
|
$spaceVal = getXVal(self::$groupHash, $spaceKey, 0) + $meta['weight'];
|
|
self::$groupHash[$spaceKey] = $spaceVal;
|
|
|
|
$meta['_weight'] = $spaceVal;
|
|
if (!getXVal(self::$groupHash, $key)) {
|
|
self::$groupHash[$key] = array();
|
|
}
|
|
array_push(self::$groupHash[$key], $meta);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected static function getMetaList()
|
|
{
|
|
if (!self::$metaList) {
|
|
self::$metaList = getMetaTable('blindBox@blindBox.php');
|
|
}
|
|
return self::$metaList;
|
|
}
|
|
|
|
protected static $metaList;
|
|
protected static $groupHash;
|
|
|
|
};
|