game2006api/webapp/models/EventRanking.php
2022-11-23 15:35:05 +08:00

150 lines
4.1 KiB
PHP

<?php
namespace models;
use mt;
use phpcommon\SqlHelper;
class EventRanking extends BaseModel
{
public static function getSumV(){
$sum = 0 ;
$rows = SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_event_ranking',
array(
'account_id' => myself()->_getAccountId(),
)
);
if ($rows){
foreach ($rows as $row) {
$sum += $row['value'];
}
}
return $sum;
}
public static function preload()
{
if (is_null(self::$dynData)) {
self::$dynData = array();
$rows = SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_event_ranking',
array(
'account_id' => myself()->_getAccountId(),
)
);
foreach ($rows as $row) {
$key = self::calcKey($row['x'], $row['y']);
self::$dynData[$key] = array(
'value' => $row['value'],
'modifytime' => $row['modifytime'],
);
}
}
}
public static function getV($x, $y, $defVal = 0)
{
$valData = self::internalGetV($x, $y, $defVal);
return $valData['value'];
}
public static function setV($accountId,$x, $y, $defVal)
{
$key = self::calcKey($x, $y);
self::internalSetV($accountId,$x, $y, $defVal);
}
public static function calcKey($x, $y)
{
$low32 = (int)$x;
$high32 = (int)$y;
$key = $low32 + ($high32 << 32);
return $key;
}
private static function internalSetV($accountId,$x, $y, $val)
{
if (!is_int($x) || !is_int($y)) {
die('internalSet type error');
return;
}
if (abs($x) > 0xFFFFFFFF) {
die('internalSet x error');
return;
}
if (abs($y) > 0xFFFFFFFF) {
die('internalSet y error');
return;
}
$key = self::calcKey($x, $y);
SqlHelper::upsert
(myself()->_getSelfMysql(),
't_event_ranking',
array(
'account_id' => $accountId,
'wave' => $x,
'type' => $y
),
array(
'value' => $val,
'modifytime' => myself()->_getNowTime()
),
array(
'account_id' => $accountId,
'wave' => $x,
'type' => $y,
'value' => $val,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime()
)
);
if (self::$dynData) {
self::$dynData[$key] = array(
'value' => $val,
'modifytime' => myself()->_getNowTime(),
);
}
}
private static function internalGetV($x, $y, $defVal = 0)
{
$key = self::calcKey($x, $y);
if (!is_null(self::$dynData)) {
if (isset(self::$dynData[$key])) {
return self::$dynData[$key];
} else {
return array(
'value' => $defVal,
'modifytime' => myself()->_getNowTime()
);
}
} else {
++self::$hitCount;
if (self::$hitCount > 5 && is_null(self::$dynData)) {
self::preload();
return self::internalGetV($x, $y, $defVal);
} else {
$row = SqlHelper::ormSelectOne(
myself()->_getSelfMysql(),
't_event_ranking',
array(
'account_id' => myself()->_getAccountId(),
'wave' => $x,
'type' => $y,
)
);
return array(
'value' => $row ? $row['value'] : $defVal,
'modifytime' => $row ? $row['modifytime'] : myself()->_getNowTime(),
);
}
}
}
private static $dynData = null;
private static $hitCount = 0;
}