game2006api/webapp/models/DynData.php
2024-06-28 16:11:16 +08:00

294 lines
8.6 KiB
PHP

<?php
namespace models;
use mt;
use phpcommon\SqlHelper;
class DynData extends BaseModel {
public static function preload()
{
if (is_null(self::$dynData)) {
self::$lastAccountId = myself()->_getAccountId();
self::$dynData = array();
$rows = SqlHelper::ormSelect(
myself()->_getSelfMysql(),
't_dyndata',
array(
'account_id' => myself()->_getAccountId(),
)
);
foreach ($rows as $row) {
$key = self::calcKey($row['x'], $row['y']);
self::$dynData[$key] = array(
'val' => $row['val'],
'modifytime' => $row['modifytime'],
);
}
}
}
public static function getVEx($accountId, $x, $y, $defVal = 0)
{
$row = SqlHelper::ormSelectOne(
myself()->_getMysql($accountId),
't_dyndata',
array(
'account_id' => $accountId,
'x' => $x,
'y' => $y,
)
);
return $row ? $row['val'] : $defVal;
}
public static function getV($x, $y, $defVal = 0)
{
$valData = self::internalGetV($x, $y, $defVal);
return $valData['val'];
}
public static function setV($x, $y, $defVal)
{
$key = self::calcKey($x, $y);
self::internalSetV($x, $y, $defVal);
}
public static function incV($x, $y, $val)
{
$key = self::calcKey($x, $y);
$oldVal = self::getV($x, $y);
self::internalSetV($x, $y, $oldVal + $val);
}
public static function incVEx($accountId, $x, $y, $val)
{
$oldVal = self::getVEx($accountId, $x, $y);
SqlHelper::upsert
(myself()->_getMysql($accountId),
't_dyndata',
array(
'account_id' => $accountId,
'x' => $x,
'y' => $y
),
array(
'val' => $oldVal + $val,
'modifytime' => myself()->_getNowTime()
),
array(
'account_id' => $accountId,
'x' => $x,
'y' => $y,
'val' => $val,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime()
)
);
}
public static function setVEx($accountId, $x, $y, $val)
{
SqlHelper::upsert
(myself()->_getMysql($accountId),
't_dyndata',
array(
'account_id' => $accountId,
'x' => $x,
'y' => $y
),
array(
'val' => $val,
'modifytime' => myself()->_getNowTime()
),
array(
'account_id' => $accountId,
'x' => $x,
'y' => $y,
'val' => $val,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime()
)
);
}
public static function decV($x, $y, $val)
{
self::incV($x, $y, 0 - $val);
}
public static function getDailyV($x, $y, $defVal = 0)
{
$valData = self::internalGetV($x, $y, $defVal);
if (myself()->_getDaySeconds($valData['modifytime']) < myself()->_getNowDaySeconds()) {
$valData['val'] = $defVal;
}
return $valData['val'];
}
public static function setDailyV($x, $y, $val)
{
self::setV($x, $y, $val);
}
public static function incDailyV($x, $y, $val)
{
$key = self::calcKey($x, $y);
$oldVal = self::getDailyV($x, $y);
self::internalSetV($x, $y, $oldVal + $val);
}
public static function decDailyV($x, $y, $val)
{
self::incDailyV($x, $y, 0 - $val);
}
public static function getWeeklyV($x, $y, $defVal = 0)
{
$valData = self::internalGetV($x, $y, $defVal);
if (myself()->_getDaySeconds($valData['modifytime']) < myself()->_getMondaySeconds()) {
$valData['val'] = $defVal;
}
return $valData['val'];
}
public static function setWeeklyV($x, $y, $val)
{
self::setV($x, $y, $val);
}
public static function incWeeklyV($x, $y, $val)
{
$key = self::calcKey($x, $y);
$oldVal = self::getWeeklyV($x, $y);
self::internalSetV($x, $y, $oldVal + $val);
}
public static function decWeeklyV($x, $y, $val)
{
self::incWeeklyV($x, $y, 0 - $val);
}
public static function calcKey($x, $y)
{
$low32 = (int)$x;
$high32 = (int)$y;
$key = $low32 + ($high32 << 32);
return $key;
}
public static function getPeriodV($x, $y,$time, $defVal = 0)
{
$valData = self::internalGetV($x, $y, $defVal);
if (myself()->_getDaySeconds($valData['modifytime']) < myself()->_getDaySeconds($time)) {
$valData['val'] = $defVal;
}
return $valData['val'];
}
public static function incPeriodV($x, $y, $time, $val){
$key = self::calcKey($x, $y);
$oldVal = self::getPeriodV($x, $y, $time);
self::internalSetV($x, $y, $oldVal + $val);
}
private static function internalSetV($x, $y, $val)
{
if (!is_int($x) || !is_int($y)) {
error_log("X type is".gettype($x) . "| Y type is ".gettype($y));
die('internalSet type error');
return;
}
if (abs($x) > 0xFFFFFFFF) {
error_log("X abs is".abs($x));
die('internalSet x error');
return;
}
if (abs($y) > 0xFFFFFFFF) {
error_log("Y abs is".abs($y));
die('internalSet y error');
return;
}
$key = self::calcKey($x, $y);
SqlHelper::upsert
(myself()->_getSelfMysql(),
't_dyndata',
array(
'account_id' => myself()->_getAccountId(),
'x' => $x,
'y' => $y
),
array(
'val' => $val,
'modifytime' => myself()->_getNowTime()
),
array(
'account_id' => myself()->_getAccountId(),
'x' => $x,
'y' => $y,
'val' => $val,
'createtime' => myself()->_getNowTime(),
'modifytime' => myself()->_getNowTime()
)
);
if (self::$dynData) {
self::$dynData[$key] = array(
'val' => $val,
'modifytime' => myself()->_getNowTime(),
);
}
}
private static function internalGetV($x, $y, $defVal = 0)
{
self::checkLastUser();
$key = self::calcKey($x, $y);
if (!is_null(self::$dynData)) {
if (isset(self::$dynData[$key])) {
return self::$dynData[$key];
} else {
return array(
'val' => $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_dyndata',
array(
'account_id' => myself()->_getAccountId(),
'x' => $x,
'y' => $y,
)
);
return array(
'val' => $row ? $row['val'] : $defVal,
'modifytime' => $row ? $row['modifytime'] : myself()->_getNowTime(),
);
}
}
}
private static function checkLastUser()
{
if (!empty(self::$lastAccountId)) {
if (self::$lastAccountId != myself()->_getAccountId()) {
self::$lastAccountId = myself()->_getAccountId();
self::$dynData = null;
self::$hitCount = 0;
}
}
}
private static $lastAccountId = '';
private static $dynData = null;
private static $hitCount = 0;
}