Skip to content

Commit

Permalink
#345 Enemies and floor switches are now synced properly (more testing…
Browse files Browse the repository at this point in the history
… required, but should work).
  • Loading branch information
Wotuu committed Aug 2, 2020
1 parent 24ddbad commit 5566ec5
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 40 deletions.
66 changes: 66 additions & 0 deletions app/Events/Dungeon/ModelChangedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Events\Dungeon;

use App\Models\Dungeon;
use App\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ModelChangedEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/** @var Dungeon $_dungeon */
private Dungeon $_dungeon;

/** @var Model $_model */
private Model $_model;

/** @var User $_user */
private User $_user;

/**
* Create a new event instance.
*
* @param $dungeon Dungeon
* @param $model Model
* @param $user User
* @return void
*/
public function __construct(Dungeon $dungeon, Model $model, User $user)
{
$this->_dungeon = $dungeon;
$this->_model = $model;
$this->_user = $user;
}

/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PresenceChannel(sprintf('%s-dungeon-edit.%s', env('APP_TYPE'), $this->_dungeon->id));
}

public function broadcastAs()
{
return 'model-changed';
}

public function broadcastWith()
{
return [
'model' => $this->_model,
'class' => get_class($this->_model),
'user' => $this->_user->name
];
}
}
65 changes: 65 additions & 0 deletions app/Events/Dungeon/ModelDeletedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Events\Dungeon;

use App\Models\Dungeon;
use App\User;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ModelDeletedEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/** @var Dungeon $_dungeon */
private Dungeon $_dungeon;

/** @var Model $_model */
private Model $_model;

/** @var User $_user */
private User $_user;

/**
* Create a new event instance.
*
* @param $dungeon Dungeon
* @param $model Model
* @param $user User
* @return void
*/
public function __construct(Dungeon $dungeon, Model $model, User $user)
{
$this->_dungeon = $dungeon;
$this->_model = $model;
$this->_user = $user;
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PresenceChannel(sprintf('%s-dungeon-edit.%s', env('APP_TYPE'), $this->_dungeon->id));
}

public function broadcastAs()
{
return 'model-deleted';
}

public function broadcastWith()
{
return [
'id' => $this->_model->id,
'class' => get_class($this->_model),
'user' => $this->_user->name
];
}
}
18 changes: 15 additions & 3 deletions app/Http/Controllers/APIDungeonFloorSwitchMarkerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace App\Http\Controllers;

use App\Events\Dungeon\ModelChangedEvent;
use App\Events\Dungeon\ModelDeletedEvent;
use App\Http\Controllers\Traits\ChecksForDuplicates;
use App\Http\Controllers\Traits\ListsDungeonFloorSwitchMarkers;
use App\Models\DungeonFloorSwitchMarker;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Teapot\StatusCode\Http;

class APIDungeonFloorSwitchMarkerController extends Controller
Expand Down Expand Up @@ -38,8 +41,12 @@ function store(Request $request)
$this->checkForDuplicate($dungeonFloorSwitchMarker);
}

if (!$dungeonFloorSwitchMarker->save()) {
throw new \Exception("Unable to save dungeon start marker!");
if ($dungeonFloorSwitchMarker->save()) {
if (Auth::check()) {
broadcast(new ModelChangedEvent($dungeonFloorSwitchMarker->floor->dungeon, $dungeonFloorSwitchMarker, Auth::getUser()));
}
} else {
throw new \Exception('Unable to save dungeon floor switch marker!');
}

return ['id' => $dungeonFloorSwitchMarker->id];
Expand All @@ -53,7 +60,12 @@ function store(Request $request)
function delete(Request $request, DungeonFloorSwitchMarker $dungeonfloorswitchmarker)
{
try {
$dungeonfloorswitchmarker->delete();
$dungeon = $dungeonfloorswitchmarker->floor->dungeon;
if( $dungeonfloorswitchmarker->delete() ) {
if (Auth::check()) {
broadcast(new ModelDeletedEvent($dungeon, $dungeonfloorswitchmarker, Auth::getUser()));
}
}
$result = response()->noContent();
} catch (\Exception $ex) {
$result = response('Not found', Http::NOT_FOUND);
Expand Down
30 changes: 22 additions & 8 deletions app/Http/Controllers/APIEnemyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Http\Controllers;

use App\Events\Dungeon\ModelChangedEvent;
use App\Events\Dungeon\ModelDeletedEvent;
use App\Http\Controllers\Traits\ChecksForDuplicates;
use App\Http\Controllers\Traits\ListsEnemies;
use App\Http\Controllers\Traits\PublicKeyDungeonRoute;
Expand All @@ -10,7 +12,11 @@
use App\Models\Enemy;
use App\Models\Npc;
use App\Models\RaidMarker;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Teapot\StatusCode\Http;

Expand Down Expand Up @@ -46,7 +52,7 @@ function list(Request $request)
/**
* @param Request $request
* @return array
* @throws \Exception
* @throws Exception
*/
function store(Request $request)
{
Expand All @@ -71,7 +77,7 @@ function store(Request $request)
$enemy->lng = $request->get('lng');

if (!$enemy->save()) {
throw new \Exception('Unable to save enemy!');
throw new Exception('Unable to save enemy!');
}

$result = ['id' => $enemy->id];
Expand All @@ -80,15 +86,19 @@ function store(Request $request)
$result['npc'] = Npc::findOrFail($enemy->npc_id);
}

if (Auth::check()) {
broadcast(new ModelChangedEvent($enemy->floor->dungeon, $enemy, Auth::getUser()));
}

return $result;
}

/**
* @param Request $request
* @param DungeonRoute $dungeonroute
* @param Enemy $enemy
* @return array|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
* @throws \Illuminate\Auth\Access\AuthorizationException
* @return array|ResponseFactory|Response
* @throws AuthorizationException
*/
function setRaidMarker(Request $request, DungeonRoute $dungeonroute, Enemy $enemy)
{
Expand All @@ -113,7 +123,7 @@ function setRaidMarker(Request $request, DungeonRoute $dungeonroute, Enemy $enem
$result = ['name' => ''];
}

} catch (\Exception $ex) {
} catch (Exception $ex) {
$result = response('Not found', Http::NOT_FOUND);
}

Expand All @@ -123,14 +133,18 @@ function setRaidMarker(Request $request, DungeonRoute $dungeonroute, Enemy $enem
/**
* @param Request $request
* @param Enemy $enemy
* @return array|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
* @return array|ResponseFactory|Response
*/
function delete(Request $request, Enemy $enemy)
{
try {
$enemy->delete();
if( $enemy->delete() ){
if (Auth::check()) {
broadcast(new ModelDeletedEvent($enemy->floor->dungeon, $enemy, Auth::getUser()));
}
}
$result = response()->noContent();
} catch (\Exception $ex) {
} catch (Exception $ex) {
$result = response('Not found', Http::NOT_FOUND);
}

Expand Down
11 changes: 8 additions & 3 deletions app/Models/DungeonFloorSwitchMarker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Models;

use Eloquent;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property $id int
Expand All @@ -12,11 +14,14 @@
* @property $lng float
* @property $direction string
*
* @mixin \Eloquent
* @property Floor $floor
*
* @mixin Eloquent
*/
class DungeonFloorSwitchMarker extends Model
{
protected $appends = ['direction'];
protected $hidden = ['floor', 'targetfloor'];

public $timestamps = false;

Expand All @@ -27,15 +32,15 @@ public function getDirectionAttribute(){
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return BelongsTo
*/
function floor()
{
return $this->belongsTo('App\Models\Floor');
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return BelongsTo
*/
function targetfloor()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,48 @@ class DungeonFloorSwitchMarkerMapObjectGroup extends MapObjectGroup {
_restoreObject(remoteMapObject, username = null) {
console.assert(this instanceof DungeonFloorSwitchMarkerMapObjectGroup, 'this is not a DungeonFloorSwitchMarkerMapObjectGroup', this);

// Fetch the existing dungeonFloorSwitchMarker if it exists
let dungeonFloorSwitchMarker = this.findMapObjectById(remoteMapObject.id);

// Only create a new one if it's new for us
let layer;
switch(remoteMapObject.direction){
case 'up':
layer = new LeafletDungeonFloorSwitchMarkerUp();
break;
case 'down':
layer = new LeafletDungeonFloorSwitchMarkerDown();
break;
case 'left':
layer = new LeafletDungeonFloorSwitchMarkerLeft();
break;
case 'right':
layer = new LeafletDungeonFloorSwitchMarkerRight();
break;
default:
layer = new LeafletDungeonFloorSwitchMarker();
break;
if (dungeonFloorSwitchMarker === null) {
switch (remoteMapObject.direction) {
case 'up':
layer = new LeafletDungeonFloorSwitchMarkerUp();
break;
case 'down':
layer = new LeafletDungeonFloorSwitchMarkerDown();
break;
case 'left':
layer = new LeafletDungeonFloorSwitchMarkerLeft();
break;
case 'right':
layer = new LeafletDungeonFloorSwitchMarkerRight();
break;
default:
layer = new LeafletDungeonFloorSwitchMarker();
break;
}

layer.setLatLng(L.latLng(remoteMapObject.lat, remoteMapObject.lng));

/** @type DungeonFloorSwitchMarker */
dungeonFloorSwitchMarker = this.createNew(layer);
} else {
// Update position
dungeonFloorSwitchMarker.layer.setLatLng(L.latLng(remoteMapObject.lat, remoteMapObject.lng));
}

layer.setLatLng(L.latLng(remoteMapObject.lat, remoteMapObject.lng));

let dungeonFloorSwitchMarker = this.createNew(layer);
dungeonFloorSwitchMarker.loadRemoteMapObject(remoteMapObject);

// We just downloaded the floor switch marker, it's synced alright!
dungeonFloorSwitchMarker.setSynced(true);

// Show echo notification or not
this._showReceivedFromEcho(dungeonFloorSwitchMarker, username);

return dungeonFloorSwitchMarker;
}
}

0 comments on commit 5566ec5

Please sign in to comment.