game2006api/webapp/models/FragmentPool.php
aozhiwei cf1c9c46c3 1
2022-09-22 11:17:51 +08:00

106 lines
2.7 KiB
PHP

<?php
namespace models;
use mt;
use phpcommon\SqlHelper;
class FragmentPool extends BaseModel {
public static function dropHero($type)
{
$items = self::internalGet(0, $type);
if (count($items)) {
$idx = array_rand($items, 1);
$fragmentId = $items[$idx]['fragment_id'];
self::decNum($fragmentId);
return $fragmentId;
}
return 0;
}
public static function dropGun($type)
{
$items = self::internalGet(1, $type);
if (count($items)) {
$idx = array_rand($items, 1);
$fragmentId = $items[$idx]['fragment_id'];
self::decNum($fragmentId);
return $fragmentId;
}
return 0;
}
public static function getHeroNum($type)
{
$items = self::internalGet(0, $type);
$num = 0;
foreach ($items as $item) {
$num += $item['fragment_num'];
}
return $num;
}
public static function getGunNum($type)
{
$items = self::internalGet(1, $type);
$num = 0;
foreach ($items as $item) {
$num += $item['fragment_num'];
}
return $num;
}
private static function internalGet($fragmentType, $type)
{
$allocTime = self::getAllocTime();
$rows = SqlHelper::ormSelect
(myself()->_getSelfMysql(),
't_fragment_pool',
array(
'type' => $type,
'fragment_type' => $fragmentType,
'alloc_time' => $allocTime,
)
);
$items = array();
if ($rows) {
foreach ($rows as $row) {
if ($row['fragment_num'] > 0) {
array_push($items, $row);
}
}
}
return $items;
}
private static function decNum($itemId)
{
$allocTime = self::getAllocTime();
SqlHelper::update
(myself()->_getSelfMysql(),
't_fragment_pool',
array(
'fragment_id' => $itemId,
'alloc_time' => $allocTime,
),
array(
'fragment_num' => function () {
return 'GREATEST(0, fragment_num - 1)';
},
'alloced_num' => function () {
return 'alloced_num + 1';
}
)
);
}
private static function getAllocTime()
{
$allocTime = myself()->_getNowDaySeconds() +
intval((myself()->_getNowTime() - myself()->_getNowDaySeconds()) / 3600) * 3600;
return $allocTime;
}
}