game2006api/webapp/models/FragmentPool.php
aozhiwei 5f304768fb 1
2022-09-20 09:58:02 +08:00

106 lines
2.6 KiB
PHP

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