Added full source code

This commit is contained in:
Emmanuel BENOîT 2016-01-10 11:01:49 +01:00
commit 33f8586698
1377 changed files with 123808 additions and 0 deletions
scripts/game/main/ticks

View file

@ -0,0 +1,150 @@
<?php
//---------------------------------------------------------------
// "Day tick": adds vacation credits to registered players who
// have less than 240 credits (2 months), deletes old logs,
// warn inactive players or delete their accounts
// Executed once per day at 07:00 ST
//---------------------------------------------------------------
class main_ticks_day_library {
function main_ticks_day_library($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->main = $this->lib->game->getLib();
$this->accounts = $this->lib->game->getLib("main/account");
}
function runTick() {
// Delete logs and update vacation credits
$this->db->safeTransaction(array($this, "updateStatus"));
// Manage inactive / leaving accounts
$this->db->safeTransaction(array($this, "terminateAccounts"));
// Send mails if needed
foreach ($this->mails as $mail) {
if ($this->main->call('sendMail', $mail[0], $mail[1], $mail[2])) {
l::info($mail[3]);
} else {
l::warn("Failed to send mail {$mail[0]} to {$mail[1]}");
}
}
}
public function updateStatus() {
// Delete old logs
$this->db->query(
"DELETE FROM account_log "
. "WHERE UNIX_TIMESTAMP(NOW())-t>3600*24*60 AND action IN ('IN','OUT')"
);
// Add vacation credits
$this->db->query(
"UPDATE account SET vac_credits=vac_credits + 1 "
. "WHERE status='STD' AND vac_credits<240 AND vac_start IS NULL"
);
}
public function terminateAccounts() {
$this->mails = array();
// Send mails to accounts that have been inactive for 21 days
$this->warnInactive();
// Close really inactive accounts
$this->closeInactive();
// Close accounts on request
$this->closeQuitters();
// Delete accounts that haven't been confirmed yet
$this->deleteUnconfirmed();
}
private function sendMail($file, $addr, $data, $log) {
array_push($this->mails, array($file, $addr, $data, $log));
}
private function warnInactive() {
// Get accounts that have been inactive for 21 days
$q = $this->db->query(
"SELECT id, name, email FROM account "
. "WHERE NOT admin AND status='STD' "
. "AND last_logout IS NOT NULL AND last_logout > last_login "
. "AND (UNIX_TIMESTAMP(NOW()) - last_logout BETWEEN 24*3600*21 AND 24 * 3600 * 22)"
);
while ($r = dbFetchArray($q)) {
list($id, $name, $addr) = $r;
// Get the user's language
$l = $this->accounts->call('getLanguage', $id);
// Send the warning e-mail
$this->sendMail("mail-warn-inactive.$l.txt", $addr, array("USER" => $name),
"main/day: inactivity warning mail sent to player $name");
}
}
private function closeInactive() {
// Disable accounts that have been inactive for 28 days
$q = $this->db->query(
"SELECT id, name, email FROM account "
. "WHERE NOT admin AND status='STD' AND last_logout IS NOT NULL "
. "AND UNIX_TIMESTAMP(NOW()) - last_logout >= 24 * 3600 * 28"
);
while ($r = dbFetchArray($q)) {
list($uid, $name, $addr) = $r;
$this->closeAccount($uid, $name, $addr, 'INAC', 'kick-inactive');
}
}
private function closeQuitters() {
// Close accounts for players who requested to quit
$q = $this->db->query(
"SELECT id, name, email FROM account "
. "WHERE (status='STD' OR status='VAC') AND quit_ts IS NOT NULL "
. "AND UNIX_TIMESTAMP(NOW()) > quit_ts + 86400"
);
while ($r = dbFetchArray($q)) {
list($uid, $name, $addr) = $r;
$this->closeAccount($uid, $name, $addr, 'QUIT', 'quit');
}
}
private function closeAccount($uid, $name, $addr, $reason, $mail) {
// Get the user's language
$l = $this->accounts->call('getLanguage', $uid);
// Send the termination e-mail
$this->sendMail("mail-$mail.$l.txt", $addr, array("USER" => $name),
"main/day: mail sent to player $name ($reason)");
// Terminate the account
$this->accounts->call('terminate', $uid, $reason);
}
private function deleteUnconfirmed() {
// Silently delete accounts that have the 'NEW' status and have been created more than 6 days ago
$q = $this->db->query(
"SELECT a.id FROM account a,account_log l "
. "WHERE a.status='NEW' AND l.account=a.id AND l.action='CREATE' "
. "AND UNIX_TIMESTAMP(NOW()) - l.t > 6 * 24 * 3600"
);
$nd = array();
while ($r = dbFetchArray($q)) {
array_push($nd, $r[0]);
}
if (count($nd)) {
$this->db->query("DELETE FROM account WHERE id IN (" . join(',', $nd) . ")");
}
}
}
?>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,12 @@
<?php
// Empty tick, to be run every 5 seconds. Should make the scheduler
// react faster if there are no ticks.
class main_ticks_mark_library {
public function __construct($lib) { }
public function runTick() { }
}
?>

View file

@ -0,0 +1,68 @@
<?php
//---------------------------------------------------------------
// "Session tick": removes outdated sessions and tracking data
// Executed once per minute, starting from 7:00:40 ST
//---------------------------------------------------------------
class main_ticks_session_library {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->accounts = $this->lib->game->getLib("main/account");
}
public function runTick() {
$this->db->safeTransaction(array($this, 'removeTracking'));
$this->db->safeTransaction(array($this, 'removeSessions'));
}
public function removeTracking() {
// Removes old tracking data (> 3 months or > 1 day and last used within a minute of creation)
$this->db->query(
"UPDATE web_tracking SET stored_data = '' "
. "WHERE UNIX_TIMESTAMP(NOW()) - last_used > 7776000"
);
$this->db->query(
"DELETE FROM web_tracking "
. "WHERE (UNIX_TIMESTAMP(NOW()) - last_used > 86400 AND last_used - created < 60)"
);
}
public function removeSessions() {
// Removes old sessions
$q = $this->db->query(
"SELECT s.id, t.id, a.id FROM web_session s, web_tracking t, account a "
. "WHERE UNIX_TIMESTAMP(NOW()) - s.last_used > 3600 "
. "AND a.id = s.account "
. "AND t.id = s.tracking "
. "FOR UPDATE OF s, t, a"
);
if (! dbCount($q)) {
return;
}
$accounts = array();
while ($r = dbFetchArray($q)) {
list($sid, $tid, $uid) = $r;
if (is_null($uid)) {
continue;
}
$this->accounts->call('log', $uid, 'o');
array_push($accounts, $uid);
l::info("Player #$uid logged out automatically");
}
$this->db->query(
"UPDATE account SET last_logout = UNIX_TIMESTAMP(NOW()) "
. "WHERE id IN (" . join(',', $accounts) . ")"
);
$this->db->query("DELETE FROM web_session WHERE UNIX_TIMESTAMP(NOW()) - last_used > 3600");
}
}
?>

View file

@ -0,0 +1,62 @@
<?php
//--------------------------------------------------------------------
// "Vacation tick": decrements credits for players in vacation mode
// and, should they reach 0, cancel vacation mode. Check for
// accounts which must enter vacation mode as well.
// Executed four times a day
//--------------------------------------------------------------------
class main_ticks_vacation_library {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->main = $this->lib->game->getLib();
$this->vacation = $this->lib->game->getLib("main/vacation");
}
public function runTick() {
$this->db->safeTransaction(array($this, "decreaseCredits"));
$this->db->safeTransaction(array($this, "enterVacation"));
}
public function decreaseCredits() {
// Decrease credits for players in vacation mode
$q = $this->db->query(
"SELECT id FROM account WHERE status = 'VAC' AND vac_credits = 0 FOR UPDATE"
);
if ($q && dbCount($q)) {
while ($r = dbFetchArray($q)) {
list($uid) = $r;
$this->vacation->call('leave', $uid);
}
}
$this->db->query("UPDATE account SET vac_credits = vac_credits - 1 WHERE status = 'VAC'");
}
public function enterVacation() {
// Look for players who should enter vacation mode
$q = $this->db->query(
"SELECT id FROM account "
. "WHERE status='STD' AND vac_start IS NOT NULL AND UNIX_TIMESTAMP(NOW()) > vac_start "
. "FOR UPDATE"
);
if (! dbCount($q)) {
return;
}
while ($r = dbFetchArray($q)) {
list($uid) = $r;
$this->vacation->call('start', $uid);
}
$this->db->query(
"UPDATE account SET status = 'VAC', vac_start = NULL $vsWhere"
. "WHERE status='STD' AND vac_start IS NOT NULL "
. "AND UNIX_TIMESTAMP(NOW()) > vac_start"
);
}
}
?>