Skip to content

Commit

Permalink
#400 You can now automatically apply the pull gradient to your pulls,…
Browse files Browse the repository at this point in the history
… on a per-route basis.
  • Loading branch information
Wotuu committed Jul 24, 2020
1 parent a3304c5 commit 1a8b7a1
Show file tree
Hide file tree
Showing 15 changed files with 324 additions and 126 deletions.
20 changes: 20 additions & 0 deletions app/Http/Controllers/APIDungeonRouteController.php
Expand Up @@ -207,6 +207,26 @@ function store(APIDungeonRouteFormRequest $request, SeasonService $seasonService
return ['key' => $dungeonroute->public_key];
}

/**
* @param Request $request
* @param SeasonService $seasonService
* @param DungeonRoute $dungeonroute
*/
function storePullGradient(Request $request, SeasonService $seasonService, DungeonRoute $dungeonroute)
{
$this->authorize('edit', $dungeonroute);

$dungeonroute->pull_gradient = $request->get('pull_gradient', '');
$dungeonroute->pull_gradient_apply_always = $request->get('pull_gradient_apply_always', false);

// Update or insert it
if (!$dungeonroute->save()) {
abort(500, 'Unable to save dungeonroute');
}

return response()->noContent();
}

/**
* @param Request $request
* @param DungeonRoute $dungeonroute
Expand Down
6 changes: 6 additions & 0 deletions app/Models/DungeonRoute.php
Expand Up @@ -38,6 +38,9 @@
* @property $avg_rating double
* @property $rating_count int
*
* @property $pull_gradient string
* @property $pull_gradient_apply_always boolean
*
* @property $thumbnail_updated_at string
* @property $updated_at string
* @property $created_at string
Expand Down Expand Up @@ -508,6 +511,9 @@ public function saveFromRequest(Request $request, SeasonService $seasonService)
$this->seasonal_index = $request->get('seasonal_index', $this->seasonal_index);
$this->teeming = intval($request->get('teeming', $this->teeming) ?? 0);

$this->pull_gradient = $request->get('pull_gradient', '');
$this->pull_gradient_apply_always = $request->get('pull_gradient_apply_always', 0);

if (Auth::check()) {
$user = User::findOrFail(Auth::id());
if ($user->hasPaidTier(PaidTier::UNLISTED_ROUTES)) {
Expand Down
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddPullGradientColumnToDungeonRoutesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('dungeon_routes', function (Blueprint $table)
{
$table->mediumText('pull_gradient')->after('demo');
$table->boolean('pull_gradient_apply_always')->after('pull_gradient');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('dungeon_routes', function (Blueprint $table)
{
$table->dropColumn('pull_gradient_apply_always');
$table->dropColumn('pull_gradient');
});
}
}
97 changes: 44 additions & 53 deletions resources/assets/js/custom/inline/common/maps/editsidebar.js
Expand Up @@ -4,7 +4,6 @@ class CommonMapsEditsidebar extends InlineCode {

this.sidebar = new SidebarNavigation(options);

this._colorPicker = null;
this._grapick = null;

getState().register('focusedenemy:changed', this, this._onFocusedEnemyChanged.bind(this));
Expand Down Expand Up @@ -73,7 +72,7 @@ class CommonMapsEditsidebar extends InlineCode {
});

// Restore pull_gradient if set
let handlers = this._getHandlersFromCookie();
let handlers = getState().getPullGradientHandlers();
for (let index in handlers) {
if (handlers.hasOwnProperty(index)) {
this._grapick.addHandler(handlers[index][0], handlers[index][1]);
Expand All @@ -89,41 +88,35 @@ class CommonMapsEditsidebar extends InlineCode {
pullGradient.push(handler.position + ' ' + self._parseHandlerColor(handler.color));
}
let result = pullGradient.join(',');
Cookies.set('pull_gradient', result);

getState().setPullGradient(result);
});

$('#edit_route_freedraw_options_gradient_apply_to_pulls').bind('click', function () {
let killZoneMapObjectGroup = getState().getDungeonMap().mapObjectGroupManager.getByName(MAP_OBJECT_GROUP_KILLZONE);
$('#edit_route_freedraw_options_gradient_apply_to_pulls').hide();
$('#edit_route_freedraw_options_gradient_apply_to_pulls_saving').show();

let count = killZoneMapObjectGroup.objects.length;
for (let i = 0; i < count; i++) {
for (let killZoneIndex in killZoneMapObjectGroup.objects) {
if (killZoneMapObjectGroup.objects.hasOwnProperty(killZoneIndex)) {
let killZone = killZoneMapObjectGroup.objects[killZoneIndex];
if (killZone.getIndex() === (i + 1)) {
// Prevent division by 0
killZone.color = pickHexFromHandlers(self._getHandlersFromCookie(), count === 1 ? 50 : (i / count) * 100);
killZone.setSynced(true);
break;
}
}
}
}
let killZoneMapObjectGroup = getState().getDungeonMap().mapObjectGroupManager.getByName(MAP_OBJECT_GROUP_KILLZONE);

killZoneMapObjectGroup.saveAll(['color']);
killZoneMapObjectGroup.applyPullGradient(true, function(){
$('#edit_route_freedraw_options_gradient_apply_to_pulls').show();
$('#edit_route_freedraw_options_gradient_apply_to_pulls_saving').hide();
});
});

let $alwaysApplyPullGradient = $('#pull_gradient_apply_always');
let alwaysApplyPullGradient = Cookies.get('pull_gradient_apply_always');
if (typeof alwaysApplyPullGradient !== 'undefined' && alwaysApplyPullGradient === '1') {
let alwaysApplyPullGradient = getState().getPullGradientApplyAlways();
if (alwaysApplyPullGradient) {
$alwaysApplyPullGradient.attr('checked', 'checked');
} else {
$alwaysApplyPullGradient.removeAttr('checked');
}
$alwaysApplyPullGradient.bind('change', function () {
console.log('change', $(this).is(':checked'));
Cookies.set('pull_gradient_apply_always', $(this).is(':checked') ? '1' : '0');
getState().setPullGradientApplyAlways($(this).is(':checked'));
});

// Draw settings save button
$('#save_draw_settings').bind('click', this._savePullGradientSettings.bind(this));
}

/**
Expand All @@ -146,36 +139,6 @@ class CommonMapsEditsidebar extends InlineCode {
}
}

/**
* Fetches a handler structure from a cookie
* @returns {[]}
* @private
*/
_getHandlersFromCookie() {
let result = [];

let pullGradient = Cookies.get('pull_gradient');
if (typeof pullGradient !== 'undefined' && pullGradient.length > 0) {
let handlers = pullGradient.split(',');
for (let index in handlers) {
if (handlers.hasOwnProperty(index)) {
let handler = handlers[index];
let values = handler.trim().split(' ');
// Only RGB values
if (values[1].indexOf('#') === 0) {
result.push([parseInt(('' + values[0]).replace('%', '')), values[1]]);
} else {
console.warn('Invalid handler found:', handler);
}
}
}
} else {
result = c.map.editsidebar.pullGradient.defaultHandlers;
}

return result;
}

/**
* Parses a color from a handler, and return an #FF0000 hex color.
* @param handlerColor
Expand All @@ -186,6 +149,34 @@ class CommonMapsEditsidebar extends InlineCode {
return handlerColor.indexOf('rgba') === 0 ? rgbToHex(parseRgba(handlerColor)) : handlerColor;
}

/**
*
* @private
*/
_savePullGradientSettings() {
$.ajax({
type: 'POST',
url: `/ajax/${getState().getDungeonRoute().publicKey}/pullgradient`,
dataType: 'json',
data: {
pull_gradient: getState().getPullGradient(),
pull_gradient_apply_always: getState().getPullGradientApplyAlways() ? '1' : '0',
_method: 'PATCH'
},
beforeSend: function () {
$('#save_draw_settings').hide();
$('#save_draw_settings_saving').show();
},
success: function (json) {
showSuccessNotification(lang.get('messages.pull_gradient_settings_saved'));
},
complete: function () {
$('#save_draw_settings').show();
$('#save_draw_settings_saving').hide();
}
});
}

cleanup() {
super.cleanup();

Expand Down
Expand Up @@ -126,6 +126,7 @@ class CommonMapsKillzonessidebar extends InlineCode {
let newColor = '#' + color.toHEXA().join('');
// Only save when the color is valid
if (killZone.color !== newColor && newColor.length === 7) {
console.log(newColor, killZone.color);
killZone.color = newColor;
killZone.save();
}
Expand Down Expand Up @@ -494,7 +495,7 @@ class CommonMapsKillzonessidebar extends InlineCode {
// Listen to changes in the killzone
killZone.register(['killzone:enemyadded', 'killzone:enemyremoved', 'synced'], self, function (killZoneChangedEvent) {
// Do not change the sidebar as we're refreshing the map; that's pointless (lots of adds/removes going on)
if( !self.map.isRefreshingMap() ) {
if (!self.map.isRefreshingMap()) {
self._refreshKillZone(killZoneChangedEvent.context);
}
});
Expand Down Expand Up @@ -566,7 +567,10 @@ class CommonMapsKillzonessidebar extends InlineCode {
}
}

killZoneMapObjectGroup.saveAll(['index']);
if( getState().getPullGradientApplyAlways() ) {
killZoneMapObjectGroup.applyPullGradient();
}
killZoneMapObjectGroup.saveAll(['index', 'color']);
}
this._dragHasSwitchedOrder = false;
}
Expand Down
Expand Up @@ -143,22 +143,57 @@ class KillZoneMapObjectGroup extends MapObjectGroup {
// set by calling save() below. That will then trigger object:add and the killzone will have it's ID for the UI
local: true
});

// Change the color as necessary
if (getState().getPullGradientApplyAlways()) {
this.applyPullGradient();
}

killZone.save();

this.signal('killzone:new', {newKillZone: killZone});
return killZone;
}

/**
* Applies the pull gradient to killzones
* @param save {boolean}
* @param saveOnComplete {function|null}
*/
applyPullGradient(save = false, saveOnComplete = null) {
console.assert(this instanceof KillZoneMapObjectGroup, 'this is not a KillZoneMapObjectGroup', this);

let count = this.objects.length;
let handlers = getState().getPullGradientHandlers();
for (let i = 0; i < count; i++) {
for (let killZoneIndex in this.objects) {
if (this.objects.hasOwnProperty(killZoneIndex)) {
let killZone = this.objects[killZoneIndex];
if (killZone.getIndex() === (i + 1)) {
// Prevent division by 0
killZone.color = pickHexFromHandlers(handlers, count === 1 ? 50 : (i / count) * 100);
break;
}
}
}
}

if (save) {
this.saveAll(['color'], saveOnComplete);
}
}

/**
* Saves all KillZones using the mass update endpoint.
* @param fields {string|array}
* @param onComplete {function|null} Called when saveAll completed
*/
saveAll(fields = '*') {
saveAll(fields = '*', onComplete = null) {
console.assert(this instanceof KillZoneMapObjectGroup, 'this is not a KillZoneMapObjectGroup', this);
let self = this;

let killZonesData = [];
for(let i = 0; i < this.objects.length; i++ ){
for (let i = 0; i < this.objects.length; i++) {
let killZone = this.objects[i];

killZonesData.push(killZone.getSaveData(fields));
Expand All @@ -171,11 +206,16 @@ class KillZoneMapObjectGroup extends MapObjectGroup {
data: {
killzones: killZonesData
},
success: function(json){
for(let i = 0; i < self.objects.length; i++ ){
success: function (json) {
for (let i = 0; i < self.objects.length; i++) {
self.objects[i].setSynced(true);
self.objects[i].onSaveSuccess(json);
}
},
complete: function () {
if (typeof onComplete === 'function') {
onComplete();
}
}
});
}
Expand Down Expand Up @@ -209,7 +249,7 @@ class KillZoneMapObjectGroup extends MapObjectGroup {
killZone.setEnemies([...killZone.enemies]);

// Only display the kill zone's kill area if it's on our current floor
if( killZone.layer !== null && killZone.floor_id === getState().getCurrentFloor().id ) {
if (killZone.layer !== null && killZone.floor_id === getState().getCurrentFloor().id) {
this.setMapObjectVisibility(killZone, true);
}
}
Expand Down
2 changes: 1 addition & 1 deletion resources/assets/js/custom/models/mapobject.js
Expand Up @@ -734,7 +734,7 @@ class MapObject extends Signalable {
getSaveData(fields = '*') {
console.assert(this instanceof MapObject, 'this is not a MapObject', this);

if (!fields.includes('id')) {
if (typeof fields === 'object' && !fields.includes('id')) {
fields.push('id');
}

Expand Down

0 comments on commit 1a8b7a1

Please sign in to comment.