- added multi character support per user, #314
- improved client site storage (IndexedDB) setup
This commit is contained in:
@@ -318,17 +318,81 @@ class Controller {
|
||||
}
|
||||
|
||||
/**
|
||||
* checks whether a user is currently logged in
|
||||
* get current character data from session
|
||||
* @return array
|
||||
*/
|
||||
protected function getSessionCharacterData(){
|
||||
$data = [];
|
||||
$currentSessionCharacters = (array)$this->getF3()->get(Api\User::SESSION_KEY_CHARACTERS);
|
||||
$requestedCharacterId = 0;
|
||||
|
||||
// get all characterData from currently active characters
|
||||
if($this->getF3()->get('AJAX')){
|
||||
// Ajax request -> get characterId from Header (if already available!)
|
||||
$header = $this->getRequestHeaders();
|
||||
$requestedCharacterId = (int)$header['Pf-Character'];
|
||||
|
||||
if(
|
||||
$requestedCharacterId > 0 &&
|
||||
(int)$this->getF3()->get(Api\User::SESSION_KEY_TEMP_CHARACTER_ID) === $requestedCharacterId
|
||||
){
|
||||
// characterId is available in Javascript
|
||||
// -> clear temp characterId for next character login/switch
|
||||
$this->getF3()->clear(Api\User::SESSION_KEY_TEMP_CHARACTER_ID);
|
||||
}
|
||||
}
|
||||
|
||||
if($requestedCharacterId <= 0){
|
||||
// Ajax BUT characterID not yet set as HTTP header
|
||||
// OR non Ajax -> get characterId from temp session (e.g. from HTTP redirect)
|
||||
$requestedCharacterId = (int)$this->getF3()->get(Api\User::SESSION_KEY_TEMP_CHARACTER_ID);
|
||||
}
|
||||
|
||||
if($requestedCharacterId > 0){
|
||||
// search for session character data
|
||||
foreach($currentSessionCharacters as $characterData){
|
||||
if($requestedCharacterId === (int)$characterData['ID']){
|
||||
$data = $characterData;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}elseif( !empty($currentSessionCharacters) ){
|
||||
// no character was requested ($requestedCharacterId = 0) AND session characters were found
|
||||
// -> get first matched character (e.g. user open browser tab)
|
||||
$data = $currentSessionCharacters[0];
|
||||
}
|
||||
|
||||
if( !empty($data) ){
|
||||
// check if character still exists on DB (e.g. was manually removed in the meantime)
|
||||
// -> This should NEVER happen just for security and "local development"
|
||||
$character = Model\BasicModel::getNew('CharacterModel');
|
||||
$character->getById( (int)$data['ID']);
|
||||
|
||||
if(
|
||||
$character->dry() ||
|
||||
!$character->hasUserCharacter()
|
||||
){
|
||||
// character data is invalid!
|
||||
$data = [];
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks whether a user/character is currently logged in
|
||||
* @param \Base $f3
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkLogTimer($f3){
|
||||
$loginCheck = false;
|
||||
$characterData = $this->getSessionCharacterData();
|
||||
|
||||
if($f3->get(Api\User::SESSION_KEY_CHARACTER_TIME) > 0){
|
||||
if( !empty($characterData) ){
|
||||
// check logIn time
|
||||
$logInTime = new \DateTime();
|
||||
$logInTime->setTimestamp( $f3->get(Api\User::SESSION_KEY_CHARACTER_TIME) );
|
||||
$logInTime->setTimestamp( (int)$characterData['TIME'] );
|
||||
$now = new \DateTime();
|
||||
|
||||
$timeDiff = $now->diff($logInTime);
|
||||
@@ -346,35 +410,66 @@ class Controller {
|
||||
}
|
||||
|
||||
/**
|
||||
* get current character model
|
||||
* get current character
|
||||
* @param int $ttl
|
||||
* @return Model\CharacterModel|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getCharacter($ttl = 0){
|
||||
$character = null;
|
||||
$characterData = $this->getSessionCharacterData();
|
||||
|
||||
if( $this->getF3()->exists(Api\User::SESSION_KEY_CHARACTER_ID) ){
|
||||
$characterId = (int)$this->getF3()->get(Api\User::SESSION_KEY_CHARACTER_ID);
|
||||
if($characterId){
|
||||
/**
|
||||
* @var $characterModel Model\CharacterModel
|
||||
*/
|
||||
$characterModel = Model\BasicModel::getNew('CharacterModel');
|
||||
$characterModel->getById($characterId, $ttl);
|
||||
if( !empty($characterData) ){
|
||||
/**
|
||||
* @var $characterModel Model\CharacterModel
|
||||
*/
|
||||
$characterModel = Model\BasicModel::getNew('CharacterModel');
|
||||
$characterModel->getById( (int)$characterData['ID'], $ttl);
|
||||
|
||||
if(
|
||||
!$characterModel->dry() &&
|
||||
$characterModel->hasUserCharacter()
|
||||
){
|
||||
$character = &$characterModel;
|
||||
}
|
||||
if(
|
||||
!$characterModel->dry() &&
|
||||
$characterModel->hasUserCharacter()
|
||||
){
|
||||
$character = &$characterModel;
|
||||
}
|
||||
}
|
||||
|
||||
return $character;
|
||||
}
|
||||
|
||||
/**
|
||||
* get current user
|
||||
* @param int $ttl
|
||||
* @return Model\UserModel|null
|
||||
*/
|
||||
public function getUser($ttl = 0){
|
||||
$user = null;
|
||||
|
||||
if( $this->getF3()->exists(Api\User::SESSION_KEY_USER_ID) ){
|
||||
$userId = (int)$this->getF3()->get(Api\User::SESSION_KEY_USER_ID);
|
||||
if($userId){
|
||||
/**
|
||||
* @var $userModel Model\UserModel
|
||||
*/
|
||||
$userModel = Model\BasicModel::getNew('UserModel');
|
||||
$userModel->getById($userId, $ttl);
|
||||
|
||||
if(
|
||||
!$userModel->dry() &&
|
||||
$userModel->hasUserCharacters()
|
||||
){
|
||||
$user = &$userModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function getCharacterSessionData(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* log out current character
|
||||
* @param \Base $f3
|
||||
@@ -384,7 +479,7 @@ class Controller {
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// delete server side cookie validation data
|
||||
// for the current character as well
|
||||
// for the active character
|
||||
if(
|
||||
$params['clearCookies'] === '1' &&
|
||||
( $activeCharacter = $this->getCharacter())
|
||||
|
||||
Reference in New Issue
Block a user