일단 push
This commit is contained in:
+14
-242
@@ -1385,36 +1385,30 @@ function isLock() {
|
||||
return DB::db()->queryFirstField("SELECT plock from plock limit 1") != 0;
|
||||
}
|
||||
|
||||
function tryLock() {
|
||||
function tryLock():bool{
|
||||
//NOTE: 게임 로직과 관련한 모든 insert, update 함수들은 lock을 거칠것을 권장함.
|
||||
$db = DB::db();
|
||||
//테이블 락
|
||||
$db->query("lock tables plock write");
|
||||
|
||||
// 잠금
|
||||
$db->update('plock', [
|
||||
'plock'=>1
|
||||
], true);
|
||||
'plock'=>1,
|
||||
'locktime'=>TimeUtil::now(true)
|
||||
], 'plock=1');
|
||||
|
||||
$isUnlocked = $db->affectedRows() > 0;
|
||||
|
||||
//테이블 언락
|
||||
$db->query("unlock tables");
|
||||
|
||||
return $isUnlocked;
|
||||
return $db->affectedRows() > 0;
|
||||
}
|
||||
|
||||
function unlock() {
|
||||
function unlock():bool{
|
||||
// 풀림
|
||||
//NOTE: unlock에는 table lock이 필요없는가?
|
||||
$db = DB::db();
|
||||
$db->query("lock tables plock write");
|
||||
$db->update('plock', [
|
||||
'plock'=>0
|
||||
], true);
|
||||
$db->query("unlock tables");
|
||||
|
||||
return $db->affectedRows() > 0;
|
||||
}
|
||||
|
||||
function timeover() {
|
||||
function timeover():bool {
|
||||
$db = DB::db();
|
||||
$gameStor = KVStorage::getStorage($db, 'game_env');
|
||||
|
||||
@@ -1424,8 +1418,8 @@ function timeover() {
|
||||
$t = min($turnterm, 5);
|
||||
|
||||
$term = $diff;
|
||||
if($term >= $t || $term < 0) { return 1; }
|
||||
else { return 0; }
|
||||
if($term >= $t || $term < 0) { return true; }
|
||||
else { return false; }
|
||||
}
|
||||
|
||||
function checkDelay() {
|
||||
@@ -1451,17 +1445,16 @@ function checkDelay() {
|
||||
//지연 해야할 밀린 턴 횟수
|
||||
$iter = intdiv($timeMinDiff, $term);
|
||||
if($iter > $threshold) {
|
||||
$minute = $iter * $term;
|
||||
$minute = ($iter - $threshold) * $term;
|
||||
$newTurntime = $turntime->add(new \DateInterval("PT{$minute}M"));
|
||||
$newNextTurntime = $turntime->add(new \DateInterval("PT{$term}M"));
|
||||
$gameStor->turntime = $newTurntime->format('Y-m-d H:i:s');
|
||||
$gameStor->starttime = (new \DateTimeImmutable($gameStor->starttime))
|
||||
->add(new \DateInterval("PT{$minute}M"))
|
||||
->format('Y-m-d H:i:s');
|
||||
|
||||
$db->update('general', [
|
||||
'turntime'=> $db->sqleval('DATE_ADD(turntime, INTERVAL %i MINUTE)', $minute)
|
||||
], 'turntime<=DATE_ADD(turntime, INTERVAL %i MINUTE)', $term);
|
||||
], true);
|
||||
$db->update('auction', [
|
||||
'expire'=> $db->sqleval('DATE_ADD(expire, INTERVAL %i MINUTE)', $minute)
|
||||
], true);
|
||||
@@ -1517,227 +1510,6 @@ function updateOnline() {
|
||||
$gameStor->onlinenation = $onnationstr;
|
||||
}
|
||||
|
||||
function checkTurn() {
|
||||
$db = DB::db();
|
||||
$gameStor = KVStorage::getStorage($db, 'game_env');
|
||||
$connect=$db->get();
|
||||
|
||||
$alllog = [];
|
||||
|
||||
// 잦은 갱신 금지 현재 5초당 1회
|
||||
if(!timeover()) { return; }
|
||||
// 현재 처리중이면 접근 불가
|
||||
|
||||
// 파일락 획득
|
||||
//FIXME:이미 DB 테이블로 lock을 시도하는데 이게 따로 필요한가?
|
||||
$fp = fopen(__dir__.'/lock.txt', 'r');
|
||||
if(!flock($fp, LOCK_EX)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!tryLock()){
|
||||
return;
|
||||
}
|
||||
|
||||
$session = Session::getInstance();
|
||||
|
||||
pushLockLog(["- checkTurn() : ".TimeUtil::now()." : ".$session->userName]);
|
||||
|
||||
// 파일락 해제
|
||||
if(!flock($fp, LOCK_UN)) { return; }
|
||||
// 세마포어 해제
|
||||
//if(!@sem_release($sema)) { echo "치명적 에러! Hide_D에게 문의하세요!"; exit(1); }
|
||||
|
||||
pushLockLog(["- checkTurn() 입 : ".TimeUtil::now()." : ".$session->userName]);
|
||||
|
||||
//if(STEP_LOG) delStepLog();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', 진입');
|
||||
|
||||
//천통시에는 동결
|
||||
if($gameStor->isunited == 2) {
|
||||
$db->update('plock', ['plock'=>1], true);
|
||||
return;
|
||||
}
|
||||
$gameStor->cacheAll();
|
||||
// 1턴이상 갱신 없었으면 서버 지연
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', checkDelay');
|
||||
checkDelay();
|
||||
// 접속자수, 접속국가, 국가별 접속장수 갱신
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', checkDelay');
|
||||
updateOnline();
|
||||
//접속자 수 따라서 갱신제한 변경
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', CheckOverhead');
|
||||
CheckOverhead();
|
||||
//서버정보
|
||||
|
||||
$date = TimeUtil::now();
|
||||
// 최종 처리 월턴의 다음 월턴시간 구함
|
||||
$prevTurn = cutTurn($gameStor->turntime, $gameStor->turnterm);
|
||||
$nextTurn = addTurn($prevTurn, $gameStor->turnterm);
|
||||
// 현재 턴 이전 월턴까지 모두처리.
|
||||
//최종 처리 이후 다음 월턴이 현재 시간보다 전이라면
|
||||
while($nextTurn <= $date) {
|
||||
// 월턴이전 장수 모두 처리
|
||||
$query = "select no,name,turntime,turn0,npc from general where turntime < '$nextTurn' order by turntime";
|
||||
$result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
||||
$gencount = MYDB_num_rows($result);
|
||||
for($i=0; $i < $gencount; $i++) {
|
||||
$general = MYDB_fetch_array($result);
|
||||
|
||||
//if(PROCESS_LOG) $processlog[0] = "[{$date}] 월턴 이전 갱신: name({$general['name']}), no({$general['no']}), turntime({$general['turntime']}), turn0({$general['turn0']})";
|
||||
//if(PROCESS_LOG) pushProcessLog($processlog);
|
||||
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', processAI');
|
||||
if($general['npc'] >= 2) { processAI($general['no']); } // npc AI 처리
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', PreprocessCommand');
|
||||
PreprocessCommand($general['no']);
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', processCommand');
|
||||
processCommand($general['no']);
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', updateCommand');
|
||||
updateCommand($general['no']);
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', updateTurntime');
|
||||
updateTurntime($general['no']);
|
||||
|
||||
}
|
||||
|
||||
// 트래픽 업데이트
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', updateTraffic');
|
||||
updateTraffic();
|
||||
// 1달마다 처리하는 것들, 벌점 감소 및 건국,전턴,합병 -1, 군량 소모
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', preUpdateMonthly');
|
||||
$result = preUpdateMonthly();
|
||||
if($result == false) {
|
||||
pushLockLog(["-- checkTurn() 오류출 : ".TimeUtil::now()." : ".$session->userName]);
|
||||
|
||||
// 잡금 해제
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', unlock');
|
||||
$gameStor->resetCache(true);
|
||||
unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
// 그 시각 년도,월 저장
|
||||
list($gameStor->year, $gameStor->month) = turnDate($nextTurn);
|
||||
|
||||
pushLockLog(["-- checkTurn() ".$gameStor->month."월 : ".TimeUtil::now()." : ".$session->userName]);
|
||||
|
||||
// 이벤트 핸들러 동작
|
||||
foreach (DB::db()->query('SELECT * from event') as $rawEvent) {
|
||||
$eventID = $rawEvent['id'];
|
||||
$cond = Json::decode($rawEvent['condition']);
|
||||
$action = Json::decode($rawEvent['action']);
|
||||
$event = new Event\EventHandler($cond, $action);
|
||||
|
||||
$event->tryRunEvent(['currentEventID'=>$eventID] + $gameStor->getAll(true));
|
||||
}
|
||||
|
||||
// 분기계산. 장수들 턴보다 먼저 있다면 먼저처리
|
||||
if($gameStor->month == 1) {
|
||||
// NPC 등장
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', processGoldIncome');
|
||||
processGoldIncome();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', processSpring');
|
||||
processSpring();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', updateYearly');
|
||||
updateYearly();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', updateQuaterly');
|
||||
updateQuaterly();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', disaster');
|
||||
disaster();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', tradeRate');
|
||||
tradeRate();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', addAge');
|
||||
addAge();
|
||||
// 새해 알림
|
||||
$alllog[] = "<C>◆</>{$gameStor->month}월:<C>{$gameStor->year}</>년이 되었습니다.";
|
||||
pushGeneralPublicRecord($alllog, $gameStor->year, $gameStor->month);
|
||||
} elseif($gameStor->month == 4) {
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', updateQuaterly');
|
||||
updateQuaterly();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', disaster');
|
||||
disaster();
|
||||
} elseif($gameStor->month == 7) {
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', processRiceIncome');
|
||||
processRiceIncome();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', processFall');
|
||||
processFall();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', updateQuaterly');
|
||||
updateQuaterly();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', disaster');
|
||||
disaster();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', tradeRate');
|
||||
tradeRate();
|
||||
} elseif($gameStor->month == 10) {
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', updateQuaterly');
|
||||
updateQuaterly();
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', disaster');
|
||||
disaster();
|
||||
}
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', postUpdateMonthly');
|
||||
postUpdateMonthly();
|
||||
|
||||
// 다음달로 넘김
|
||||
$prevTurn = $nextTurn;
|
||||
$nextTurn = addTurn($prevTurn, $gameStor->turnterm);
|
||||
}
|
||||
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', '.__LINE__);
|
||||
|
||||
// 이시각 정각 시까지 업데이트 완료했음
|
||||
$gameStor->turntime = $prevTurn;
|
||||
|
||||
// 그 시각 년도,월 저장
|
||||
list($gameStor->year, $gameStor->month) = turnDate($prevTurn);
|
||||
// 현재시간의 월턴시간 이후 분단위 장수 처리
|
||||
do {
|
||||
$query = "select no,name,turntime,turn0,npc from general where turntime<='$date' order by turntime";
|
||||
$result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
||||
$gencount = MYDB_num_rows($result);
|
||||
|
||||
for($i=0; $i < $gencount; $i++) {
|
||||
$general = MYDB_fetch_array($result);
|
||||
|
||||
//if(PROCESS_LOG) $processlog[0] = "[{$date}] 월턴 이후 갱신: name({$general['name']}), no({$general['no']}), turntime({$general['turntime']}), turn0({$general['turn0']})";
|
||||
//if(PROCESS_LOG) pushProcessLog($processlog);
|
||||
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', processAI');
|
||||
if($general['npc'] >= 2) { processAI($general['no']); } // npc AI 처리
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', PreprocessCommand');
|
||||
PreprocessCommand($general['no']);
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', processCommand');
|
||||
processCommand($general['no']);
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', updateCommand');
|
||||
updateCommand($general['no']);
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', updateTurntime');
|
||||
updateTurntime($general['no']);
|
||||
}
|
||||
} while($gencount > 0);
|
||||
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', '.__LINE__);
|
||||
|
||||
$gameStor->turntime = $date;
|
||||
|
||||
// 부상 과도 제한
|
||||
$query = "update general set injury='80' where injury>'80'";
|
||||
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
|
||||
//토너먼트 처리
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', processTournament');
|
||||
processTournament();
|
||||
//거래 처리
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', processAuction');
|
||||
processAuction();
|
||||
// 잡금 해제
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', unlock');
|
||||
$gameStor->resetCache(true);
|
||||
unlock();
|
||||
|
||||
pushLockLog(["- checkTurn() 출 : ".TimeUtil::now()." : ".$session->userName]);
|
||||
|
||||
//if(STEP_LOG) pushStepLog(TimeUtil::now().', finish');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function addAge() {
|
||||
$db = DB::db();
|
||||
$gameStor = KVStorage::getStorage($db, 'game_env');
|
||||
|
||||
Reference in New Issue
Block a user