Skip to content

Commit

Permalink
#2585 WIP fixing/upgrading live route synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
Wotuu committed Oct 25, 2024
1 parent 3c48336 commit e0e2629
Show file tree
Hide file tree
Showing 22 changed files with 235 additions and 137 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/Ajax/AjaxMapIconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ public function store(
$mapIcon->setLinkedAwakenedObeliskByMapIconId($validated['linked_awakened_obelisk_id']);
// Only when icons that are not sticky to the map are saved
$dungeonRoute?->touch();
});
},
// Can be null, it will then default to the dungeon internally
$dungeonRoute);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions app/Http/Controllers/Ajax/AjaxMappingModelBaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ protected function storeModel(
array $validated,
string $modelClass,
?MappingModelInterface $model = null,
?Closure $onSaveSuccess = null
?Closure $onSaveSuccess = null,
?Model $echoContext = null
): Model {
$validated['mapping_version_id'] = $mappingVersion?->id;

/** @var Model $modelClass */
return DB::transaction(function () use ($validated, $modelClass, $model, $onSaveSuccess) {
return DB::transaction(function () use ($validated, $modelClass, $model, $onSaveSuccess, $echoContext) {
/** @var Model|null $beforeModel */
$beforeModel = $model === null ? null : clone $model;

Expand All @@ -68,7 +69,8 @@ protected function storeModel(
}

if (Auth::check()) {
broadcast(new ModelChangedEvent($model->floor->dungeon, Auth::getUser(), $model));
$echoContext = $echoContext ?? $model->floor->dungeon;
broadcast(new ModelChangedEvent($echoContext, Auth::getUser(), $model));
}

return $model;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class BaseModelHandler extends MessageHandler {

/**
* @protected
*
* @param echo {Echo}
* @param message {String}
*/
constructor(echo, message) {
super(echo, message);
}

/**
* Basic checks for if a received echo event is applicable to this model handler.
*
* @param e {Object}
* @returns {boolean}
* @protected
*/
_shouldHandleEchoEvent(e) {
console.assert(this instanceof BaseModelHandler, 'this is not a BaseModelHandler', this);

// Do not process events that WE fired
return e.user.public_key !== getState().getUser().public_key;
}


/**
*
* @param e
* @return boolean
*/
onReceive(e) {
super.onReceive(e);

return this._shouldHandleChangedEchoEvent(e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class KillZoneChangedHandler extends ModelChangedHandler {

constructor(echo) {
super(echo, KillZoneChangedMessage.getName());

}


/**
*
* @param e {KillZoneChangedMessage}
* @return boolean
*/
onReceive(e) {
let shouldHandle = super.onReceive(e);

console.log(`KillZoneChangedHandler::onReceive:`, shouldHandle, e);
if (shouldHandle) {
let killZoneMapObjectGroup = this.echo.map.mapObjectGroupManager.getByName(MAP_OBJECT_GROUP_KILLZONE);

let mapObject = killZoneMapObjectGroup.loadMapObject(e.model, null, e.user);
killZoneMapObjectGroup.setMapObjectVisibility(mapObject, true);
}

return shouldHandle;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class KillZoneDeletedHandler extends ModelDeletedHandler {

constructor(echo) {
super(echo, KillZoneDeletedMessage.getName());
}

/**
*
* @param e {KillZoneDeletedMessage}
*/
onReceive(e) {
let shouldHandle = super.onReceive(e);

console.log(`KillZoneDeletedHandler::onReceive: ${e.model_id} ${e.model_class}`);

if (shouldHandle) {

let killZoneMapObjectGroup = this.echo.map.mapObjectGroupManager.getByName(MAP_OBJECT_GROUP_KILLZONE);

let mapObject = killZoneMapObjectGroup.findMapObjectById(e.model_id);
if (mapObject !== null) {
mapObject.localDelete();
this._showDeletedFromEcho(mapObject, e.user);
}
}

return shouldHandle;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class ModelChangedHandler extends BaseModelHandler {
/**
* Checks if a received _changed_ event is applicable to this map object group.
*
* @param e {Object}
* @returns {boolean}
* @private
*/
_shouldHandleChangedEchoEvent(e) {
console.assert(this instanceof ModelChangedHandler, 'this is not a ModelChangedHandler', this);
console.assert(typeof e.model !== 'undefined', 'model was not defined in received event!', this, e);
console.assert(typeof e.model.floor_id !== 'undefined', 'model.floor_id was not defined in received event!', this, e);

// floor -1 means it's omnipresent (such as killzones)
// @TODO support facades?
return this._shouldHandleEchoEvent(e) && (e.model.floor_id === getState().getCurrentFloor().id || e.model.floor_id === -1);
}


/**
*
* @param e {KillZoneChangedMessage}
* @return boolean
*/
onReceive(e) {
super.onReceive(e);

return this._shouldHandleChangedEchoEvent(e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class ModelDeletedHandler extends BaseModelHandler {

/**
* Checks if a received _deleted_ event is applicable to this map object group.
*
* @param e {Object}
* @returns {boolean}
* @private
*/
_shouldHandleDeletedEchoEvent(e) {
console.assert(this instanceof ModelDeletedHandler, 'this is not a ModelDeletedHandler', this);
console.assert(typeof e.model_id !== 'undefined', 'model_id was not defined in received event!', this, e);

return this._shouldHandleEchoEvent(e);
}

/**
*
* @param localMapObject {MapObject}
* @param user {Object}
* @protected
*/
_showDeletedFromEcho(localMapObject, user) {
console.assert(this instanceof ModelDeletedHandler, 'this is not a ModelDeletedHandler', this);

let state = getState();
if (state.isEchoEnabled() && state.getUser().public_key !== user.public_key && user.name !== null) {
showInfoNotification(lang.get('messages.echo_object_deleted_notification')
.replace('{object}', localMapObject.toString())
.replace('{user}', user.name));
}
}

/**
*
* @param e {KillZoneDeletedMessage}
* @return boolean
*/
onReceive(e) {
super.onReceive(e);

return this._shouldHandleDeletedEchoEvent(e);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class NpcChangedHandler extends MessageHandler {

constructor(echo) {
super(echo, '.npc-changed');
super(echo, NpcChangedMessage.getName());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class NpcDeletedHandler extends MessageHandler {

constructor(echo) {
super(echo, '.npc-deleted');
super(echo, NpcDeletedMessage.getName());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* @property {String} message
*/
class MessageHandler extends Signalable {
/**
* @param echo {Echo}
* @param message {String}
*/
constructor(echo, message) {
super();

Expand All @@ -27,6 +31,7 @@ class MessageHandler extends Signalable {
console.assert(this instanceof MessageHandler, 'this is not a MessageHandler', this);

console.log(`Listening for ${this.getMessage()}`);
// console.log(presenceChannel);
presenceChannel.listen(this.getMessage(), this.onReceive.bind(this));
}

Expand All @@ -36,6 +41,8 @@ class MessageHandler extends Signalable {
onReceive(e) {
console.assert(this instanceof MessageHandler, 'this is not a MessageHandler', this);

console.log(`MessageHandler::onReceive:`, e);

// Try to re-map the received message to an object that we know of
let message = new MessageFactory().create(e.__name, e);

Expand Down
5 changes: 3 additions & 2 deletions resources/assets/js/custom/mapcontrols/echocontrols.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class EchoControls extends MapControl {
this._onEchoCursorsEnabledChanged();

this.mapControlOptions = {
onAdd: function (leafletMap) {
onAdd: function () {
return jQuery('<span>', {
// @TODO translate
text: 'Connecting...'
})[0];
}
Expand Down Expand Up @@ -168,7 +169,7 @@ class EchoControls extends MapControl {
cursorsEnabled: getState().getDungeonMap().options.edit,
cursorsActive: getState().getEchoCursorsEnabled(),
users: getState().getEcho().getUsers().slice(0, c.map.echo.userOverflowCount),
hasUsersOverflow: getState().getEcho().getUsers().length >= c.map.echo.userOverflowCount,
hasUsersOverflow: getState().getEcho().getUsers().length > c.map.echo.userOverflowCount,
usersOverflow: getState().getEcho().getUsers().slice(c.map.echo.userOverflowCount),
type: getState().getMapContext().getType(),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class KillZoneMapObjectGroup extends MapObjectGroup {
return getState().getMapContext().getKillZones();
}

_loadMapObject(remoteMapObject, layer = null, user = null) {
loadMapObject(remoteMapObject, layer = null, user = null) {
/** @type {KillZone} */
let mapObject = super._loadMapObject(remoteMapObject, layer, user);
let mapObject = super.loadMapObject(remoteMapObject, layer, user);

// If this was received from Echo..
if (user !== null && remoteMapObject.lat !== null && remoteMapObject.lng !== null) {
Expand Down Expand Up @@ -227,7 +227,7 @@ class KillZoneMapObjectGroup extends MapObjectGroup {
}

let lastKillZone = this._findLastKillZone();
let killZone = this._loadMapObject({
let killZone = this.loadMapObject({
color: c.map.killzone.polygonOptions.color(lastKillZone !== null ? lastKillZone.color : null),
floor_id: null, // Only for the killzone location which is not set from a 'new pull'
enemies: enemyIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class KillZonePathMapObjectGroup extends PolylineMapObjectGroup {
createNewPath(vertices, options) {
console.assert(this instanceof KillZonePathMapObjectGroup, 'this is not a KillZonePathMapObjectGroup', this);

let path = this._loadMapObject($.extend(true, {}, {
let path = this.loadMapObject($.extend(true, {}, {
id: this.currentId++,
polyline: {
color: c.map.polyline.killzonepath.color,
Expand Down
Loading

0 comments on commit e0e2629

Please sign in to comment.