/** * save a new connection or updates an existing (drag/drop) between two systems * if a connection is changed (drag&drop) to another system. -> this function is called for update * @param \Base $f3 */ public function save(\Base $f3) { $postData = (array) $f3->get('POST'); $newConnectionData = []; if (isset($postData['connectionData']) && isset($postData['mapData'])) { $mapData = (array) $postData['mapData']; $connectionData = (array) $postData['connectionData']; $activeCharacter = $this->getCharacter(); // get map model and check map access /** * @var Model\MapModel $map */ $map = Model\BasicModel::getNew('MapModel'); $map->getById((int) $mapData['id']); if ($map->hasAccess($activeCharacter)) { $source = $map->getSystemById($connectionData['source']); $target = $map->getSystemById($connectionData['target']); if (!is_null($source) && !is_null($target)) { /** * @var $connection Model\ConnectionModel */ $connection = Model\BasicModel::getNew('ConnectionModel'); $connection->getById((int) $connectionData['id']); // search if systems are neighbors $routeController = new Route(); $routeController->initJumpData(); $route = $routeController->findRoute($connectionData['sourceName'], $connectionData['targetName'], 1); if ($route['routePossible'] == true) { // systems are next to each other $connectionData['scope'] = 'stargate'; $connectionData['type'] = ['stargate']; } elseif ($connectionData['scope'] == 'stargate') { // connection scope changed -> this can not be a stargate $connectionData['scope'] = 'wh'; $connectionData['type'] = ['wh_fresh']; } $connectionData['mapId'] = $map; // "updated" should not be set by client e.g. after manual drag&drop unset($connectionData['updated']); $connection->setData($connectionData); if ($connection->isValid()) { $connection->save(); $newConnectionData = $connection->getData(); } } } } echo json_encode($newConnectionData); }
/** * set default connection type by search route between endpoints */ public function setDefaultTypeData() { if (is_object($this->source) && is_object($this->target)) { $routeController = new Route(); $routeController->initJumpData(); $route = $routeController->findRoute($this->source->name, $this->target->name, 1); if ($route['routePossible']) { $this->scope = 'stargate'; $this->type = ['stargate']; } else { $this->scope = 'wh'; $this->type = ['wh_fresh']; } } }
/** * * @param Model\CharacterModel $character * @param Model\MapModel $map * @return Model\MapModel */ protected function updateMapData(Model\CharacterModel $character, Model\MapModel $map) { // update "map data" cache in case of map (system/connection) changes $clearMapDataCache = false; if (($mapScope = $map->getScope()) && $mapScope->name != 'none' && ($log = $character->getLog())) { // character is currently in a system $sameSystem = false; $sourceExists = true; $targetExists = true; // system coordinates $systemOffsetX = 130; $systemOffsetY = 0; $systemPosX = 0; $systemPosY = 30; $sourceSystemId = (int) $this->getF3()->get(User::SESSION_KEY_CHARACTER_PREV_SYSTEM_ID); $targetSystemId = (int) $log->systemId; if ($sourceSystemId) { $sourceSystem = null; $targetSystem = null; // check if source and target systems are equal // -> NO target system available if ($sourceSystemId === $targetSystemId) { // check if previous (solo) system is already on the map $sourceSystem = $map->getSystemByCCPId($sourceSystemId, ['active' => 1]); $sameSystem = true; } else { // check if previous (source) system is already on the map $sourceSystem = $map->getSystemByCCPId($sourceSystemId, ['active' => 1]); // -> check if system is already on this map $targetSystem = $map->getSystemByCCPId($targetSystemId, ['active' => 1]); } // if systems don´t already exists on map -> get "blank" systems // -> required for system type check (e.g. wormhole, k-space) if (!$sourceSystem && $sourceSystemId) { $sourceExists = false; $sourceSystem = $map->getNewSystem($sourceSystemId); } else { // system exists -> add target to the "right" $systemPosX = $sourceSystem->posX + $systemOffsetX; $systemPosY = $sourceSystem->posY + $systemOffsetY; } if (!$sameSystem && !$targetSystem) { $targetExists = false; $targetSystem = $map->getNewSystem($targetSystemId); } $addSourceSystem = false; $addTargetSystem = false; $addConnection = false; switch ($mapScope->name) { case 'all': if ($sameSystem) { $addSourceSystem = true; } else { $addSourceSystem = true; $addTargetSystem = true; $addConnection = true; } break; case 'k-space': if ($sameSystem) { if (!$sourceSystem->isWormhole()) { $addSourceSystem = true; } } elseif (!$sourceSystem->isWormhole() || !$targetSystem->isWormhole()) { $addSourceSystem = true; $addTargetSystem = true; $addConnection = true; } break; case 'wh': default: if ($sameSystem) { if ($sourceSystem->isWormhole()) { $addSourceSystem = true; } } elseif ($sourceSystem->isWormhole() || $targetSystem->isWormhole()) { $addSourceSystem = true; $addTargetSystem = true; $addConnection = true; } elseif (!$sourceSystem->isWormhole() && !$targetSystem->isWormhole()) { // check distance between systems (in jumps) // -> if > 1 it is !very likely! a wormhole $routeController = new Route(); $routeController->initJumpData(); $route = $routeController->findRoute($sourceSystem->name, $targetSystem->name, 1); if (!$route['routePossible']) { $addSourceSystem = true; $addTargetSystem = true; $addConnection = true; } } break; } // save source system ------------------------------------------------------------------------------------- if ($addSourceSystem && $sourceSystem && !$sourceExists) { $sourceSystem = $map->saveSystem($sourceSystem, $systemPosX, $systemPosY, $character); // get updated maps object if ($sourceSystem) { $map = $sourceSystem->mapId; $sourceExists = true; $clearMapDataCache = true; // increase system position (prevent overlapping) $systemPosX = $sourceSystem->posX + $systemOffsetX; $systemPosY = $sourceSystem->posY + $systemOffsetY; } } // save target system ------------------------------------------------------------------------------------- if ($addTargetSystem && $targetSystem && !$targetExists) { $targetSystem = $map->saveSystem($targetSystem, $systemPosX, $systemPosY, $character); // get updated maps object if ($targetSystem) { $map = $targetSystem->mapId; $clearMapDataCache = true; $targetExists = true; } } // save connection ---------------------------------------------------------------------------------------- if ($addConnection && $sourceExists && $targetExists && $sourceSystem && $targetSystem && !$map->searchConnection($sourceSystem, $targetSystem)) { $connection = $map->getNewConnection($sourceSystem, $targetSystem); $connection = $map->saveConnection($connection); // get updated maps object if ($connection) { $map = $connection->mapId; $clearMapDataCache = true; } } } } if ($clearMapDataCache) { $this->clearMapDataCache($character); } return $map; }