Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SmrSession: allow active session for each game #368

Merged
merged 2 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion engine/Default/game_play_processing.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// register game_id
SmrSession::$game_id = $var['game_id'];
SmrSession::updateGame($var['game_id']);

$player =& SmrPlayer::getPlayer(SmrSession::$account_id, $var['game_id']);
$player->updateLastCPLAction();
Expand Down
15 changes: 14 additions & 1 deletion lib/Default/SmrSession.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,27 @@ class SmrSession {
' WHERE session_id=' . self::$db->escapeString(self::$session_id) . (USING_AJAX ? ' AND last_sn='.self::$db->escapeString(self::$lastSN) : '') . ' LIMIT 1');
}
else {
self::$db->query('DELETE FROM active_session WHERE account_id = ' . self::$db->escapeNumber(self::$account_id) . ' LIMIT 1');
self::$db->query('DELETE FROM active_session WHERE account_id = ' . self::$db->escapeNumber(self::$account_id) . ' AND game_id = ' . self::$db->escapeNumber(self::$game_id));
self::$db->query('INSERT INTO active_session (session_id, account_id, old_account_id, game_id, last_accessed, session_var) VALUES(' . self::$db->escapeString(self::$session_id) . ',' . self::$db->escapeNumber(self::$account_id) . ',' . self::$db->escapeNumber(is_numeric(self::$old_account_id)?self::$old_account_id:0) . ',' . self::$db->escapeNumber(self::$game_id) . ',' . self::$db->escapeNumber(TIME) . ',' . self::$db->escapeBinary($compressed) . ')');
self::$generate = false;
}
// This takes us back to the standard database if we had to change it.
$db = new SmrMySqlDatabase();
}

/**
* Updates the `game_id` attribute of the session and deletes any other
* active sessions in this game for this account.
*/
public static function updateGame($gameID) {
if (self::$game_id == $gameID) {
return;
}
self::$game_id = $gameID;
self::$db->query('DELETE FROM active_session WHERE account_id = ' . self::$db->escapeNumber(self::$account_id) . ' AND game_id = ' . self::$game_id);
self::$db->query('UPDATE active_session SET game_id=' . self::$db->escapeNumber(self::$game_id) . ' WHERE session_id=' . self::$db->escapeString(self::$session_id));
}

public static function updateSN() {
self::$db = new SmrSessionMySqlDatabase();
if(!USING_AJAX)
Expand Down
6 changes: 2 additions & 4 deletions lib/Default/smr.inc
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,12 @@ function do_voodoo() {
$account->log(LOG_TYPE_GAME_ENTERING, 'Player left game '.SmrSession::$game_id);

// reset game id
SmrSession::$game_id = 0;
SmrSession::update();
SmrSession::updateGame(0);

forward(create_container('skeleton.php', 'game_play.php', $var));
}
else if($var['url'] == 'logoff_preprocessing.php') {
SmrSession::$game_id = 0;
SmrSession::update();
SmrSession::updateGame(0);

// log?
$account->log(LOG_TYPE_LOGIN, 'logged off from '.getIpAddress());
Expand Down
4 changes: 2 additions & 2 deletions lib/Login/loginSmarty.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
if(count($loginNews)>0)
$template->assign('LoginNews',$loginNews);


$db->query('SELECT count(*) AS active_sessions FROM active_session WHERE account_id!=0 AND last_accessed > '.$db->escapeNumber(TIME - SmrSession::TIME_BEFORE_EXPIRY));
// Users can have a session for each open game, so only count unique accounts
$db->query('SELECT count(DISTINCT account_id) AS active_sessions FROM active_session WHERE account_id!=0 AND last_accessed > '.$db->escapeNumber(TIME - SmrSession::TIME_BEFORE_EXPIRY));
$db->nextRecord();
$template->assign('ActiveSessions',$db->getField('active_sessions'));

Expand Down