Skip to content

Commit

Permalink
Merge pull request #826 from exodus4d/develop
Browse files Browse the repository at this point in the history
v1.5.3
  • Loading branch information
exodus4d authored Jul 27, 2019
2 parents bdc8ecb + dc319a4 commit 7d11851
Show file tree
Hide file tree
Showing 72 changed files with 194 additions and 88 deletions.
10 changes: 5 additions & 5 deletions app/main/controller/api/map.php
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ protected function updateMapByCharacter(Pathfinder\MapModel $map, Pathfinder\Cha
$targetSystemId = (int)$targetLog->systemId;

// get 'character log' from source system. If not log found -> assume $sourceLog == $targetLog
$sourceLog = $character->getLogPrevSystem($targetSystemId) ? : $targetLog;
$sourceLog = $character->getLogPrevSystem($map->_id, $targetSystemId) ? : $targetLog;
$sourceSystemId = (int)$sourceLog->systemId;

if($sourceSystemId){
Expand Down Expand Up @@ -1020,12 +1020,12 @@ protected function updateMapByCharacter(Pathfinder\MapModel $map, Pathfinder\Cha
break;
case 'k-space':
if($sameSystem){
if( !$sourceSystem->isWormhole() ){
if($sourceSystem->isKspace()){
$addSourceSystem = true;
}
}elseif(
!$sourceSystem->isWormhole() ||
!$targetSystem->isWormhole()
$sourceSystem->isKspace() ||
$targetSystem->isKspace()
){
$addSourceSystem = true;
$addTargetSystem = true;
Expand All @@ -1035,7 +1035,7 @@ protected function updateMapByCharacter(Pathfinder\MapModel $map, Pathfinder\Cha
case 'wh':
default:
if($sameSystem){
if( $sourceSystem->isWormhole() ){
if($sourceSystem->isWormhole()){
$addSourceSystem = true;
}
}elseif(
Expand Down
24 changes: 7 additions & 17 deletions app/main/model/pathfinder/characterlogmodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

use DB\SQL\Schema;

/**
* Class CharacterLogModel
* @package Model\Pathfinder
* @property CharacterModel $characterId
*/
class CharacterLogModel extends AbstractPathfinderModel {

/**
Expand Down Expand Up @@ -244,26 +249,11 @@ public function clearCacheData(){
* @param string $action
*/
protected function updateLogsHistory(string $action){
// add new log history entry if 'systemId' changed
// -> if e.g. 'shipTypeId', 'stationId',.. changed -> no new entry (for now)
if(
!empty($this->fieldChanges) &&
array_key_exists('systemId', $this->fieldChanges) && // new history entry
$this->valid() &&
is_object($this->characterId)
){
$oldLog = clone $this;

// get 'updated' timestamp and reapply after __set() fields data
// -> because any __set() call updates 'updated' col
$updated = $oldLog->updated;
foreach($this->fieldChanges as $key => $change){
if($oldLog->exists($key)){
$oldLog->$key = $change['old'];
}
}
$oldLog->updated = $updated;

$oldLog->characterId->updateLogsHistory($oldLog, $action);
$this->characterId->updateLogsHistory($this, $action);
}
}

Expand Down
153 changes: 128 additions & 25 deletions app/main/model/pathfinder/charactermodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class CharacterModel extends AbstractPathfinderModel {

/**
* max count of historic character logs
* -> this includes logs where just e.g. shipTypeId has changed but no systemId change!
*/
const MAX_LOG_HISTORY_DATA = 5;
const MAX_LOG_HISTORY_DATA = 10;

/**
* TTL for historic character logs
Expand Down Expand Up @@ -249,7 +250,7 @@ public function getData($addLogData = false, $addLogHistoryData = false){
}

if($addLogHistoryData && $characterData->log){
$characterData->logHistory = $this->getLogsHistory();
$characterData->logHistory = $this->getLogHistoryJumps($characterData->log->system->id);
}

// temp "authStatus" should not be cached
Expand Down Expand Up @@ -1033,12 +1034,34 @@ public function updateLog($additionalOptions = []) : self {
}

/**
* filter'character log' history data by $callback
* get 'character log' history data. Filter all data that does not represent a 'jump' (systemId change)
* -> e.g. If just 'shipTypeId' has changed, this entry is filtered
* @param int $systemIdPrev
* @return array
*/
protected function getLogHistoryJumps(int $systemIdPrev = 0) : array {
return $this->filterLogsHistory(function(array $historyEntry) use (&$systemIdPrev) : bool {
$addEntry = false;
if(
!empty($historySystemId = (int)$historyEntry['log']['system']['id']) &&
$historySystemId !== $systemIdPrev
){
$addEntry = true;
$systemIdPrev = $historySystemId;
}

return $addEntry;
});
}

/**
* filter 'character log' history data by $callback
* -> reindex array keys! Otherwise json_encode() on result would return object!
* @param \Closure $callback
* @return array
*/
protected function filterLogsHistory(\Closure $callback) : array {
return array_filter($this->getLogsHistory() , $callback);
return array_values(array_filter($this->getLogsHistory() , $callback));
}

/**
Expand All @@ -1056,27 +1079,83 @@ public function getLogsHistory() : array {
* @param CharacterLogModel $characterLog
* @param string $action
*/
public function updateLogsHistory(CharacterLogModel $characterLog, string $action = 'update'){
public function updateLogsHistory(CharacterLogModel $characterLog, string $action = 'update') : void {
if(
!$this->dry() &&
$this->valid() &&
$this->_id === $characterLog->get('characterId', true)
){
$logHistoryData = $this->getLogsHistory();
$task = 'add';
$mapIds = [];
$historyLog = $characterLog->getDataAsArray();

if($logHistoryData = $this->getLogsHistory()){
// skip logging if no relevant fields changed
list($historyEntryPrev) = $logHistoryData;
if($historyLogPrev = $historyEntryPrev['log']){
if(
$historyLog['system']['id'] === $historyLogPrev['system']['id'] &&
$historyLog['ship']['typeId'] === $historyLogPrev['ship']['typeId'] &&
$historyLog['station']['id'] === $historyLogPrev['station']['id'] &&
$historyLog['structure']['id'] === $historyLogPrev['structure']['id']
){
// no changes in 'relevant' fields -> just update timestamp
$task = 'update';
$mapIds = (array)$historyEntryPrev['mapIds'];
}
}
}

$historyEntry = [
'stamp' => strtotime($characterLog->updated),
'action' => $action,
'log' => $characterLog->getDataAsArray()
'mapIds' => $mapIds,
'log' => $historyLog
];

array_unshift($logHistoryData, $historyEntry);
if($task == 'update'){
$logHistoryData[0] = $historyEntry;
}else{
array_unshift($logHistoryData, $historyEntry);

// limit max history data
array_splice($logHistoryData, self::MAX_LOG_HISTORY_DATA);
// limit max history data
array_splice($logHistoryData, self::MAX_LOG_HISTORY_DATA);
}

$this->updateCacheData($logHistoryData, self::DATA_CACHE_KEY_LOG_HISTORY, self::TTL_LOG_HISTORY);
}
}

/**
* try to update existing 'character log' history entry (replace data)
* -> matched by 'stamp' timestamp
* @param array $historyEntry
* @return bool
*/
protected function updateLogHistoryEntry(array $historyEntry) : bool {
$updated = false;

if(
$this->valid() &&
($logHistoryData = $this->getLogsHistory())
){
$map = function(array $entry) use ($historyEntry, &$updated) : array {
if($entry['stamp'] === $historyEntry['stamp']){
$updated = true;
$entry = $historyEntry;
}
return $entry;
};

$logHistoryData = array_map($map, $logHistoryData);

if($updated){
$this->updateCacheData($logHistoryData, self::DATA_CACHE_KEY_LOG_HISTORY, self::TTL_LOG_HISTORY);
}
}

return $updated;
}

/**
* broadcast characterData
*/
Expand Down Expand Up @@ -1145,24 +1224,48 @@ public function getLog() : ?CharacterLogModel {
/**
* get the first matched (most recent) log entry before $systemId.
* -> The returned log entry *might* be previous system for this character
* @param int $mapId
* @param int $systemId
* @return CharacterLogModel|null
*/
public function getLogPrevSystem(int $systemId) : ?CharacterLogModel {
$logHistoryData = $this->filterLogsHistory(function(array $historyEntry) use ($systemId) : bool {
return (
!empty($historySystemId = (int)$historyEntry['log']['system']['id']) &&
$historySystemId !== $systemId
);
});

public function getLogPrevSystem(int $mapId, int $systemId) : ?CharacterLogModel {
$characterLog = null;
if(!empty($historyEntry = reset($logHistoryData))){
/**
* @var $characterLog CharacterLogModel
*/
$characterLog = $this->rel('characterLog');
$characterLog->setData($historyEntry['log']);

if($mapId && $systemId){
$skipRest = false;
$logHistoryData = $this->filterLogsHistory(function(array $historyEntry) use ($mapId, $systemId, &$skipRest) : bool {
$addEntry = false;
if(in_array($mapId, (array)$historyEntry['mapIds'], true)){
$skipRest = true;
}

if(
!$skipRest &&
!empty($historySystemId = (int)$historyEntry['log']['system']['id']) &&
$historySystemId !== $systemId
){
$addEntry = true;
$skipRest = true;
}

return $addEntry;
});

if(
!empty($historyEntry = reset($logHistoryData)) &&
is_array($historyEntry['mapIds'])
){
/**
* @var $characterLog CharacterLogModel
*/
$characterLog = $this->rel('characterLog');
$characterLog->setData($historyEntry['log']);

// mark $historyEntry data as "checked" for $mapId
array_push($historyEntry['mapIds'], $mapId);

$this->updateLogHistoryEntry($historyEntry);
}
}

return $characterLog;
Expand Down
12 changes: 10 additions & 2 deletions app/main/model/pathfinder/systemmodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,15 +656,23 @@ public function getStructuresData() : array {
}

/**
* check whether this system is a wormhole
* check whether this system is in w-space
* @return bool
*/
public function isWormhole() : bool {
return ($this->typeId->id === 1);
}

/**
* check whether this system is an Abyss system
* check whether this system is in k-space
* @return bool
*/
public function isKspace() : bool {
return ($this->typeId->id === 2);
}

/**
* check whether this system is in a-space
* @return bool
*/
public function isAbyss() : bool {
Expand Down
2 changes: 1 addition & 1 deletion app/pathfinder.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ NAME = Pathfinder
; e.g. public/js/vX.X.X/app.js
; Syntax: String (current version)
; Default: v1.5.0
VERSION = v1.5.2
VERSION = v1.5.3

; Contact information [optional]
; Shown on 'licence', 'contact' page.
Expand Down
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ let trackTable = {
// https://www.npmjs.com/package/uglify-es
let uglifyJsOptions = {
warnings: true,
toplevel: false
toplevel: false,
ecma: 8
};

// Sourcemaps options
Expand Down
2 changes: 1 addition & 1 deletion js/app/map/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ define([
// open Overlay -------------------------------------------------------------------------------------------
if( !isOpen(overlay) ){
let promiseStore = MapUtil.getLocaleData('map', mapId);
promiseStore.then(function(dataStore){
promiseStore.then(dataStore => {
if(
dataStore &&
dataStore.showLocal
Expand Down
18 changes: 9 additions & 9 deletions js/app/map/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ define([
// loop all active pilots and build cache-key
let cacheArray = [];
for(let tempUserData of data.user){
cacheArray.push(tempUserData.id + '_' + tempUserData.log.ship.id);
cacheArray.push(tempUserData.id + '_' + tempUserData.log.ship.typeId);
}

// make sure cacheArray values are sorted for key comparison
Expand All @@ -223,9 +223,8 @@ define([

// we need to add "view mode" option to key
// -> if view mode change detected -> key no longer valid
cacheArray.unshift(compactView ? 'compact' : 'default');

let cacheKey = cacheArray.join('_').hashCode();
let cacheKey = compactView ? 'compact' : 'default';
cacheKey += '_' + cacheArray.join('_').hashCode();

// check for if cacheKey has changed
if(cacheKey !== oldCacheKey){
Expand Down Expand Up @@ -2772,11 +2771,12 @@ define([

// update "local" overlay for this map
mapContainer.on('pf:updateLocal', function(e, userData){
let mapElement = $(this);
let mapOverlay = MapOverlayUtil.getMapOverlay(mapElement, 'local');
let mapId = Util.getObjVal(userData, 'config.id') || 0;

if(userData && userData.config && userData.config.id){
let currentMapData = Util.getCurrentMapData(userData.config.id);
if(mapId){
let mapElement = $(this);
let mapOverlay = MapOverlayUtil.getMapOverlay(mapElement, 'local');
let currentMapData = Util.getCurrentMapData(mapId);
let currentCharacterLog = Util.getCurrentCharacterLog();
let clearLocal = true;

Expand All @@ -2785,7 +2785,7 @@ define([
currentCharacterLog &&
currentCharacterLog.system
){
let currentSystemData = currentMapData.data.systems.filter(function(system){
let currentSystemData = currentMapData.data.systems.filter(system => {
return system.systemId === currentCharacterLog.system.id;
});

Expand Down
2 changes: 1 addition & 1 deletion js/app/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ define([
if(changes.charactersIds){
updateTasks.push(updateHeaderCharacterSwitch(userData, changes.characterId));
}
if(changes.characterSystemId || changes.characterShipType){
if(changes.characterSystemId || changes.characterShipType || changes.characterLogHistory){
updateTasks.push(updateHeaderCharacterLocation(userData, changes.characterShipType));
}

Expand Down
Loading

0 comments on commit 7d11851

Please sign in to comment.