Skip to content

Commit

Permalink
Weight Scales/Presets:
Browse files Browse the repository at this point in the history
 * moved wt_presets to DB
 * scales are now saved sensibly instead of just being dumped as is (also, tables have now constrains)
 * added admin=weight-presets to edit presets
 * added an internal user on id:0 who 'owns' the wt-presets and the +1 rates on new comments
 * consequently added constraints to comment-related tables

Misc:
 * Util::toJSON() priorizes its flags over CFG_DEBUG, wich fixes the modelviewer-redButton
 * moved terrible javascript-dump from admin-page to template
  • Loading branch information
Sarjuuk committed Mar 12, 2016
1 parent 71e44e5 commit e80015a
Show file tree
Hide file tree
Showing 18 changed files with 1,516 additions and 411 deletions.
74 changes: 0 additions & 74 deletions datasets/weight-presets

This file was deleted.

35 changes: 25 additions & 10 deletions includes/ajaxHandler/account.class.php
Expand Up @@ -50,19 +50,34 @@ protected function handleWeightscales()
if (!$this->_post['scale'])
return 0;

if (!$this->_post['id'])
$id = 0;

if ($id = $this->_post['id'])
{
$res = DB::Aowow()->selectRow('SELECT MAX(id) AS max, count(id) AS num FROM ?_account_weightscales WHERE userId = ?d', User::$id);
if ($res['num'] < 5) // more or less hard-defined in LANG.message_weightscalesaveerror
$this->_post['id'] = ++$res['max'];
else
if (!DB::Aowow()->selectCell('SELECT 1 FROM ?_account_weightscales WHERE userId = ?d AND id = ?d', User::$id, $id))
return 0;
}

if (DB::Aowow()->query('REPLACE INTO ?_account_weightscales VALUES (?d, ?d, ?, ?)', $this->_post['id'], User::$id, $this->_post['name'], $this->_post['scale']))
return $this->_post['id'];
else
return 0;
{
$nScales = DB::Aowow()->selectCell('SELECT COUNT(id) FROM ?_account_weightscales WHERE userId = ?d', User::$id);
if ($nScales >= 5) // more or less hard-defined in LANG.message_weightscalesaveerror
return 0;

$id = DB::Aowow()->query('INSERT INTO ?_account_weightscales (`userId`, `name`) VALUES (?d, ?)', User::$id, $this->_post['name']);
}

DB::Aowow()->query('DELETE FROM ?_account_weightscale_data WHERE id = ?d', $id);

foreach (explode(',', $this->_post['scale']) as $s)
{
list($k, $v) = explode(':', $s);
if (!in_array($k, Util::$weightScales) || $v < 1)
continue;

DB::Aowow()->query('INSERT INTO ?_account_weightscale_data VALUES (?d, ?, ?d)', $id, $k, $v);
}

return $id;
}
else if ($this->_post['delete'] && $this->_post['id'])
DB::Aowow()->query('DELETE FROM ?_account_weightscales WHERE id = ?d AND userId = ?d', $this->_post['id'], User::$id);
Expand All @@ -77,4 +92,4 @@ protected function checkScale($val)

return null;
}
}
}
93 changes: 90 additions & 3 deletions includes/ajaxHandler/admin.class.php
Expand Up @@ -5,7 +5,7 @@

class AjaxAdmin extends AjaxHandler
{
protected $validParams = ['screenshots', 'siteconfig'];
protected $validParams = ['screenshots', 'siteconfig', 'weight-presets'];
protected $_get = array(
'action' => [FILTER_SANITIZE_STRING, 0xC], // FILTER_FLAG_STRIP_LOW | *_HIGH
'id' => [FILTER_CALLBACK, ['options' => 'AjaxAdmin::checkId']],
Expand All @@ -17,7 +17,10 @@ class AjaxAdmin extends AjaxHandler
'val' => [FILTER_UNSAFE_RAW, null]
);
protected $_post = array(
'alt' => [FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW],
'alt' => [FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW],
'id' => [FILTER_SANITIZE_NUMBER_INT, null],
'scale' => [FILTER_CALLBACK, ['options' => 'AjaxAccount::checkScale']],
'__icon' => [FILTER_CALLBACK, ['options' => 'AjaxAdmin::checkKey']],
);

public function __construct(array $params)
Expand All @@ -30,7 +33,7 @@ public function __construct(array $params)

if ($this->params[0] == 'screenshots')
{
if (!User::isInGroup(U_GROUP_STAFF | U_GROUP_SCREENSHOT)) // comment_mod, handleSSmod, vi_mod ?
if (!User::isInGroup(U_GROUP_ADMIN | U_GROUP_BUREAU | U_GROUP_SCREENSHOT))
return;

if ($this->_get['action'] == 'list')
Expand Down Expand Up @@ -60,6 +63,14 @@ public function __construct(array $params)
else if ($this->_get['action'] == 'update')
$this->handler = 'confUpdate';
}
else if ($this->params[0] == 'weight-presets')
{
if (!User::isInGroup(U_GROUP_DEV | U_GROUP_ADMIN | U_GROUP_BUREAU))
return;

if ($this->_get['action'] == 'save')
$this->handler = 'wtSave';
}
}

// get all => null (optional)
Expand Down Expand Up @@ -304,6 +315,74 @@ protected function confUpdate()
return '';
}

protected function wtSave()
{
if (!$this->_post['id'] || !$this->_post['__icon'])
return 3;

$writeFile = function($file, $content)
{
$success = false;
if ($handle = @fOpen($file, "w"))
{
if (fWrite($handle, $content))
$success = true;

fClose($handle);
}
else
die('me no file');

if ($success)
@chmod($file, Util::FILE_ACCESS);

return $success;
};


// save to db

DB::Aowow()->query('DELETE FROM ?_account_weightscale_data WHERE id = ?d', $this->_post['id']);
DB::Aowow()->query('UPDATE ?_account_weightscales SET `icon`= ? WHERE `id` = ?d', $this->_post['__icon'], $this->_post['id']);

foreach (explode(',', $this->_post['scale']) as $s)
{
list($k, $v) = explode(':', $s);

if (!in_array($k, Util::$weightScales) || $v < 1)
continue;

if (DB::Aowow()->query('INSERT INTO ?_account_weightscale_data VALUES (?d, ?, ?d)', $this->_post['id'], $k, $v) === null)
return 1;
}


// write dataset

$wtPresets = [];
$scales = DB::Aowow()->select('SELECT id, name, icon, class FROM ?_account_weightscales WHERE userId = 0 ORDER BY class, id ASC');

foreach ($scales as $s)
{
$weights = DB::Aowow()->selectCol('SELECT field AS ARRAY_KEY, val FROM ?_account_weightscale_data WHERE id = ?d', $s['id']);
if (!$weights)
continue;

$wtPresets[$s['class']]['pve'][$s['name']] = array_merge(['__icon' => $s['icon']], $weights);
}

$toFile = "var wt_presets = ".Util::toJSON($wtPresets).";";
$file = 'datasets/weight-presets';

if (!$writeFile($file, $toFile))
return 2;


// all done

return 0;
}

protected function checkId($val)
{
// expecting id-list
Expand Down Expand Up @@ -331,4 +410,12 @@ protected function checkUser($val)

return null;
}

protected function checkScale($val)
{
if (preg_match('/^((\w+:\d+)(,\w+:\d+)*)$/', $val))
return $val;

return null;
}
}
2 changes: 1 addition & 1 deletion includes/shared.php
@@ -1,6 +1,6 @@
<?php

define('AOWOW_REVISION', 18);
define('AOWOW_REVISION', 19);
define('CLI', PHP_SAPI === 'cli');


Expand Down
30 changes: 8 additions & 22 deletions includes/user.class.php
Expand Up @@ -521,31 +521,17 @@ public static function getUserGlobals()

public static function getWeightScales()
{
$data = [];
$result = [];

$res = DB::Aowow()->select('SELECT * FROM ?_account_weightscales WHERE userId = ?d', self::$id);
foreach ($res as $i)
{
$set = array (
'name' => $i['name'],
'id' => $i['id']
);
$res = DB::Aowow()->selectCol('SELECT id AS ARRAY_KEY, name FROM ?_account_weightscales WHERE userId = ?d', self::$id);
if (!$res)
return $result;

$weights = explode(',', $i['weights']);
foreach ($weights as $weight)
{
$w = explode(':', $weight);
$weights = DB::Aowow()->selectCol('SELECT id AS ARRAY_KEY, `field` AS ARRAY_KEY2, val FROM ?_account_weightscale_data WHERE id IN (?a)', array_keys($res));
foreach ($weights as $id => $data)
$result[] = array_merge(['name' => $res[$id], 'id' => $id], $data);

if ($w[1] === 'undefined')
$w[1] = 0;

$set[$w[0]] = $w[1];
}

$data[] = $set;
}

return $data;
return $result;
}

public static function getCharacters()
Expand Down
14 changes: 13 additions & 1 deletion includes/utilities.php
Expand Up @@ -174,6 +174,18 @@ class Util
'clothChestArmor', 'leatherChestArmor', 'mailChestArmor', 'plateChestArmor'
);

public static $weightScales = array(
'agi', 'int', 'sta', 'spi', 'str', 'health', 'mana', 'healthrgn', 'manargn',
'armor', 'blockrtng', 'block', 'defrtng', 'dodgertng', 'parryrtng', 'resirtng',
'atkpwr', 'feratkpwr', 'armorpenrtng', 'critstrkrtng', 'exprtng', 'hastertng', 'hitrtng', 'splpen',
'splpwr', 'arcsplpwr', 'firsplpwr', 'frosplpwr', 'holsplpwr', 'natsplpwr', 'shasplpwr',
'dmg', 'mledps', 'rgddps', 'mledmgmin', 'rgddmgmin', 'mledmgmax', 'rgddmgmax', 'mlespeed', 'rgdspeed',
'arcres', 'firres', 'frores', 'holres', 'natres', 'shares',
'mleatkpwr', 'mlecritstrkrtng', 'mlehastertng', 'mlehitrtng', 'rgdatkpwr', 'rgdcritstrkrtng', 'rgdhastertng', 'rgdhitrtng',
'splcritstrkrtng', 'splhastertng', 'splhitrtng', 'spldmg', 'splheal',
'nsockets'
);

public static $dateFormatInternal = "Y/m/d H:i:s";

public static $changeLevelString = '<a href="javascript:;" onmousedown="return false" class="tip" style="color: white; cursor: pointer" onclick="$WH.g_staticTooltipLevelClick(this, null, 0)" onmouseover="$WH.Tooltip.showAtCursor(event, \'<span class=\\\'q2\\\'>\' + LANG.tooltip_changelevel + \'</span>\')" onmousemove="$WH.Tooltip.cursorUpdate(event)" onmouseout="$WH.Tooltip.hide()"><!--lvl-->%s</a>';
Expand Down Expand Up @@ -1573,7 +1585,7 @@ public static function toJSON($data, $forceFlags = 0)
{
$flags = $forceFlags ?: (JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE);

if (CFG_DEBUG)
if (CFG_DEBUG && !$forceFlags)
$flags |= JSON_PRETTY_PRINT;

$json = json_encode($data, $flags);
Expand Down

0 comments on commit e80015a

Please sign in to comment.