Skip to content

Commit

Permalink
close #47 updated "map tracking"
Browse files Browse the repository at this point in the history
  • Loading branch information
exodus4d committed Oct 11, 2015
1 parent ae53013 commit a3be406
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 99 deletions.
19 changes: 6 additions & 13 deletions app/main/controller/api/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use controller\MailController;
use Model;
use Exception;
use DB;

class User extends Controller\Controller{

Expand Down Expand Up @@ -70,10 +71,12 @@ private function logUserIn($userName, $password){

// set Session login
$dateTime = new \DateTime();
$this->f3->set('SESSION.user.time', $dateTime->getTimestamp());
$this->f3->set('SESSION.user.name', $user->name);
$this->f3->set('SESSION.user.id', $user->id);

$this->f3->set('SESSION.user', [
'time' => $dateTime->getTimestamp(),
'name' => $user->name,
'id' => $user->id
]);

// save user login information
$user->touch('lastLogin');
Expand Down Expand Up @@ -144,19 +147,9 @@ public function deleteLog($f3){

if($characterLog = $character->getLog()){
$characterLog->erase();
$characterLog->save();

$character->clearCacheData();

// delete log cache key as well
$f3->clear('LOGGED.user.character.id_' . $characterLog->characterId->id . '.systemId');
$f3->clear('LOGGED.user.character.id_' . $characterLog->characterId->id . '.shipId');

}
}
}


}

/**
Expand Down
113 changes: 58 additions & 55 deletions app/main/controller/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,72 @@ protected function setDB($database = 'PF'){
protected function _getUser($ttl = 5){

$user = false;
$userId = $this->f3->get('SESSION.user.id');

if($userId > 0){
$userModel = Model\BasicModel::getNew('UserModel');
$userModel->getById($userId, $ttl);
if( $this->f3->exists('SESSION.user.id') ){
$userId = (int)$this->f3->get('SESSION.user.id');

if( !$userModel->dry() ){
$user = $userModel;
if($userId > 0){
$userModel = Model\BasicModel::getNew('UserModel');
$userModel->getById($userId, $ttl);

if( !$userModel->dry() ){
$user = $userModel;
}
}
}

return $user;
}

/**
* log the current user out
* @param $f3
*/
public function logOut($f3){

// destroy session
$f3->clear('SESSION');

if( !$f3->get('AJAX') ){
// redirect to landing page
$f3->reroute('@landing');
}else{
$return = (object) [];
$return->reroute = self::getEnvironmentData('URL') . $f3->alias('landing');
$return->error[] = $this->getUserLoggedOffError();

echo json_encode($return);
die();
}
}

/**
* verifies weather a given username and password is valid
* @param $userName
* @param $password
* @return Model\UserModel|null
*/
protected function _verifyUser($userName, $password) {

$validUser = null;

$user = Model\BasicModel::getNew('UserModel', 0);

$user->getByName($userName);

// check userName is valid
if( !$user->dry() ){
// check if password is valid
$isValid = $user->verify($password);

if($isValid === true){
$validUser = $user;
}
}

return $validUser;
}

/**
* check weather the page is IGB trusted or not
* @return mixed
Expand Down Expand Up @@ -152,55 +204,6 @@ static function isIGB(){
return $isIGB;
}

/**
* verifies weather a given username and password is valid
* @param $userName
* @param $password
* @return Model\UserModel|null
*/
protected function _verifyUser($userName, $password) {

$validUser = null;

$user = Model\BasicModel::getNew('UserModel', 0);

$user->getByName($userName);

// check userName is valid
if( !$user->dry() ){
// check if password is valid
$isValid = $user->verify($password);

if($isValid === true){
$validUser = $user;
}
}

return $validUser;
}

/**
* log the current user out
* @param $f3
*/
public function logOut($f3){

// destroy session
$f3->clear('SESSION.user');
$f3->sync('SESSION');

if( !$f3->get('AJAX') ){
// redirect to landing page
$f3->reroute('@landing');
}else{
$return = (object) [];
$return->reroute = self::getEnvironmentData('URL') . $f3->alias('landing');
$return->error[] = $this->getUserLoggedOffError();

echo json_encode($return);
die();
}
}

/**
* get error object is a user is not found/logged of
Expand Down
22 changes: 20 additions & 2 deletions app/main/cron/characterupdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace cron;
use Controller;
use DB;
use Model;


class CharacterUpdate {

Expand All @@ -21,8 +23,24 @@ function deleteLogData($f3){

DB\Database::instance()->setDB('PF');

$sqlDeleteCharacterLogs = "TRUNCATE TABLE character_log";
$f3->get('DB')->exec($sqlDeleteCharacterLogs);
$characterLogModel = Model\BasicModel::getNew('CharacterLogModel');

// find "old" character logs
$characterLogs = $characterLogModel->find([
'TIMESTAMPDIFF(SECOND, updated, NOW() ) > :lifetime',
':lifetime' => (int)$f3->get('PATHFINDER.CACHE.CHARACTER_LOG')
]);

if(is_object($characterLogs)){
foreach($characterLogs as $characterLog){
// delete log and all cached values
$characterLog->erase();
}
}




}

}
24 changes: 24 additions & 0 deletions app/main/model/characterlogmodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ class CharacterLogModel extends BasicModel {
]
];

public function __construct($db = NULL, $table = NULL, $fluid = NULL, $ttl = 0){

parent::__construct($db, $table, $fluid, $ttl);

// events -----------------------------------------
$this->beforeerase(function($self){
$self->clearCacheData();
});
}

/**
* get all character log data
* @return object
Expand All @@ -38,5 +48,19 @@ public function getData(){
return $logData;
}

/**
* see parent
*/
public function clearCacheData(){
parent::clearCacheData();

// delete log cache key as well
$f3 = self::getF3();
$character = $this->characterId;

$character->clearCacheData();
$f3->clear('LOGGED.user.character.id_' . $character->id);

return true;
}
}
56 changes: 31 additions & 25 deletions app/main/model/usermodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,44 +431,51 @@ public function updateApiData(){
* @throws \Exception
*/
public function updateCharacterLog($ttl = 0){
$apiController = Controller\CcpApiController::getIGBHeaderData();
$headerData = Controller\CcpApiController::getIGBHeaderData();

// check if IGB Data is available
if( !empty($apiController->values) ){
if( !empty($headerData->values) ){
$f3 = self::getF3();

// check if system has changed since the last call
// current location is stored in session to avoid unnecessary DB calls
$sessionCharacterKey = 'LOGGED.user.character.id_' . $apiController->values['charid'];
// current location is stored (global) to avoid unnecessary DB calls
$sessionCharacterKey = 'LOGGED.user.character.id_' . $headerData->values['charid'];

if($f3->exists($sessionCharacterKey)){
// cache data exists
$cacheData = $f3->get($sessionCharacterKey);
}else{
// new cache data
$cacheData = [
'systemId' => 0,
'shipId' => 0
];
}

if(
!$f3->exists($sessionCharacterKey) ||
$f3->get($sessionCharacterKey . '.systemId') != $apiController->values['solarsystemid'] ||
$f3->get($sessionCharacterKey . '.shipId') != $apiController->values['shiptypeid']
$cacheData['systemId'] != $headerData->values['solarsystemid'] ||
$cacheData['shipId'] != $headerData->values['shiptypeid']
){

$cacheData = [
'systemId' => $apiController->values['solarsystemid'],
'shipId' => $apiController->values['shiptypeid']
];
$cacheData['systemId'] = (int)$headerData->values['solarsystemid'];
$cacheData['shipId'] = (int)$headerData->values['shiptypeid'];

// character has changed system, or character just logged on
$character = self::getNew('CharacterModel');
$character->getById( (int)$apiController->values['charid'] );
$character->getById( (int)$headerData->values['charid'] );

if( $character->dry() ){
// this can happen if a valid user plays the game with a not registered character
// whose API is not registered -> save new character or update character data
$corporationId = array_key_exists('corpid', $apiController->values) ? $apiController->values['corpid'] : null;
$allianceId = array_key_exists('allianceid', $apiController->values) ? $apiController->values['allianceid'] : null;
$corporationId = array_key_exists('corpid', $headerData->values) ? $headerData->values['corpid'] : null;
$allianceId = array_key_exists('allianceid', $headerData->values) ? $headerData->values['allianceid'] : null;

// check if corp exists
if( !is_null($corporationId) ){
$corporation = self::getNew('CorporationModel');
$corporation->getById( (int)$corporationId );
if( $corporation->dry() ){
$corporation->id = $corporationId;
$corporation->name = $apiController->values['corpname'];
$corporation->name = $headerData->values['corpname'];
$corporation->save();
}
}
Expand All @@ -479,13 +486,13 @@ public function updateCharacterLog($ttl = 0){
$alliance->getById( (int)$allianceId );
if( $alliance->dry() ){
$alliance->id = $allianceId;
$alliance->name = $apiController->values['alliancename'];
$alliance->name = $headerData->values['alliancename'];
$alliance->save();
}
}

$character->id = (int) $apiController->values['charid'];
$character->name = $apiController->values['charname'];
$character->id = (int) $headerData->values['charid'];
$character->name = $headerData->values['charname'];
$character->corporationId = $corporationId;
$character->allianceId = $allianceId;
$character->save();
Expand All @@ -498,11 +505,11 @@ public function updateCharacterLog($ttl = 0){

// set character log values
$characterLog->characterId = $character;
$characterLog->systemId = $apiController->values['solarsystemid'];
$characterLog->systemName = $apiController->values['solarsystemname'];
$characterLog->shipId = $apiController->values['shiptypeid'];
$characterLog->shipName = $apiController->values['shipname'];
$characterLog->shipTypeName = $apiController->values['shiptypename'];
$characterLog->systemId = (int)$headerData->values['solarsystemid'];
$characterLog->systemName = $headerData->values['solarsystemname'];
$characterLog->shipId = (int)$headerData->values['shiptypeid'];
$characterLog->shipName = $headerData->values['shipname'];
$characterLog->shipTypeName = $headerData->values['shiptypename'];

$characterLog->save();

Expand All @@ -512,7 +519,6 @@ public function updateCharacterLog($ttl = 0){
// cache character log information
$f3->set($sessionCharacterKey, $cacheData, $ttl);
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions app/pathfinder.ini
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ INVITE = 0
INVITE_LIMIT = 50

; ======================================================================================================
; Lifetime for map types
; Lifetime for map types (days)
[PATHFINDER.MAP.PRIVATE]
LIFETIME = 2

Expand All @@ -111,7 +111,7 @@ LIFETIME = 99999
[PATHFINDER.CACHE]

; cache character log informations in seconds. This is ignored if ship/system switch was detected
CHARACTER_LOG = 600
CHARACTER_LOG = 300

; cache time for all system data within a constellation (this will never change) 30d
CONSTELLATION_SYSTEMS = 2592000
Expand Down
4 changes: 2 additions & 2 deletions public/templates/dialog/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ <h6>Done</h6>
<blockquote>
<p>
API Key(s) are required to use <em class="pf-brand">pathfinder</em>.
Don't have one? - Create a new key with an empty 'Access Mask'.
Don't have one? - Create a new key with an empty 'Access Mask' (0).
</p>
<small>Get your new/custom API Key from
<a href="https://community.eveonline.com/support/api-key/" target="_blank">here</a>
<a href="https://support.eveonline.com/api/Key/CreatePredefined/8/0/" target="_blank">here</a>
and come back to this page.
</small>
</blockquote>
Expand Down

0 comments on commit a3be406

Please sign in to comment.