Skip to content

Commit

Permalink
Merge pull request #10 from exodus4d/develop
Browse files Browse the repository at this point in the history
v1.1.1
  • Loading branch information
exodus4d authored Apr 9, 2018
2 parents c706108 + 5285c86 commit 7f3009f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
6 changes: 5 additions & 1 deletion app/Main/Formatter/SubscriptionFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ static function groupCharactersDataBySystem(array $charactersData) : array {
$data = [];
foreach($charactersData as $characterId => $characterData){
// check if characterData has an active log (active system for character)
$systemId = (int)$characterData['log']['system']['id'];
$systemId = 0;
if(isset($characterData['log']['system']['id'])){
$systemId = (int)$characterData['log']['system']['id'];
}

if( !isset($data[$systemId]) ){
$systemData = (object)[];
$systemData->id = $systemId;
Expand Down
36 changes: 27 additions & 9 deletions app/Main/MapUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ private function subscribe(ConnectionInterface $conn, $subscribeData){
if($characterData = $this->checkCharacterAccess($characterId, $characterToken)){
$this->characters[$characterId][$conn->resourceId] = $conn;

// insert/update characterData cache
// -> even if characterId does not have access to a map "yet"
// -> no maps found but character can get map access at any time later
$this->setCharacterData($characterData);

// valid character -> check map access
$changedSubscriptionsMapIds = [];
foreach((array)$subscribeData['mapData'] as $data){
Expand All @@ -215,9 +220,6 @@ private function subscribe(ConnectionInterface $conn, $subscribeData){
if( $this->checkMapAccess($characterId, $mapId, $mapToken) ){
// valid map subscribe request
$this->subscriptions[$mapId][$characterId] = $characterId;
// insert/update characterData cache
$this->setCharacterData($characterData);

$changedSubscriptionsMapIds[] = $mapId;
}
}
Expand Down Expand Up @@ -606,7 +608,7 @@ private function broadcastMapData(string $task, int $mapId, $load) : int {
* @return int count of connected characters
*/
private function setAccess(string $task, $accessData) : int {
$NewMapCharacterIds = [];
$newMapCharacterIds = [];

if($mapId = (int)$accessData['id']){
$characterIds = (array)$accessData['characterIds'];
Expand All @@ -618,27 +620,32 @@ private function setAccess(string $task, $accessData) : int {
!empty($this->characters[$characterId]) &&
!empty($this->getCharacterData($characterId))
){
$NewMapCharacterIds[$characterId] = $characterId;
$newMapCharacterIds[$characterId] = $characterId;
}
}

$currentMapCharacterIds = (array)$this->subscriptions[$mapId];

// broadcast "map delete" to no longer valid characters ---------------------------------------------------
$removedMapCharacterIds = array_diff(array_keys($currentMapCharacterIds), array_keys($NewMapCharacterIds));
$removedMapCharacterIds = array_keys(array_diff_key($currentMapCharacterIds, $newMapCharacterIds));
$removedMapCharacterConnections = $this->getConnectionsByCharacterIds($removedMapCharacterIds);
$this->broadcastData($removedMapCharacterConnections, $task, $mapId, $removedMapCharacterIds);

// update map subscriptions -------------------------------------------------------------------------------
if( !empty($NewMapCharacterIds) ){
if( !empty($newMapCharacterIds) ){
// set new characters that have map access (overwrites existing subscriptions for that map)
$this->subscriptions[$mapId] = $NewMapCharacterIds;
$this->subscriptions[$mapId] = $newMapCharacterIds;

// check if subscriptions have changed
if( !$this->arraysEqualKeys($currentMapCharacterIds, $newMapCharacterIds) ){
$this->broadcastMapSubscriptions('mapSubscriptions', [$mapId]);
}
}else{
// no characters (left) on this map
unset($this->subscriptions[$mapId]);
}
}
return count($NewMapCharacterIds);
return count($newMapCharacterIds);
}

/**
Expand Down Expand Up @@ -682,6 +689,17 @@ private function setConnectionAccess($connectionAccessData) {
return $response;
}

/**
* compare two assoc arrays by keys. Key order is ignored
* -> if all keys from array1 exist in array2 && all keys from array2 exist in array 1, arrays are supposed to be equal
* @param array $array1
* @param array $array2
* @return bool
*/
protected function arraysEqualKeys(array $array1, array $array2) : bool {
return !array_diff_key($array1, $array2) && !array_diff_key($array2, $array1);
}

/**
* dispatch log writing to a LogFileHandler
* @param array $meta
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
},
"require": {
"php-64bit": ">=7.0",
"ext-zmq": "1.1.*",
"ext-zmq": ">=1.1.3",
"cboden/ratchet": "0.4.x",
"react/zmq": "dev-master"
"react/zmq": "0.3.*"
}
}

0 comments on commit 7f3009f

Please sign in to comment.