From b1f5904b918f8a355e9336d1d3724e3a31bb6f87 Mon Sep 17 00:00:00 2001 From: Seph Date: Fri, 20 Sep 2024 20:02:10 +0200 Subject: [PATCH] Refractor SKS --- theme/functions.php | 3 + theme/inc/config-routing.php | 40 +-- theme/options/misc.php | 38 +++ theme/options/register.php | 12 + theme/pages/sks.php | 212 +++++++------- theme/pages/sks_backup.php | 186 ++++++++++++ theme/request.php | 131 +++++---- .../Database/CreateSKSHistoryTable.php | 18 ++ theme/wowpress/Models/Raid.php | 2 +- theme/wowpress/Models/SKS.php | 14 + theme/wowpress/Models/SKSHistory.php | 266 ++++++++++++++++++ 11 files changed, 715 insertions(+), 207 deletions(-) create mode 100644 theme/options/misc.php create mode 100644 theme/pages/sks_backup.php create mode 100644 theme/wowpress/Database/CreateSKSHistoryTable.php create mode 100644 theme/wowpress/Models/SKSHistory.php diff --git a/theme/functions.php b/theme/functions.php index aec5eb8..501710f 100644 --- a/theme/functions.php +++ b/theme/functions.php @@ -9,6 +9,7 @@ use WoWPress\Database\CreateLogTable; use WoWPress\Database\CreateNotificationTable; use WoWPress\Database\CreateRaidTable; use WoWPress\Database\CreateSignupTable; +use WoWPress\Database\CreateSKSHistoryTable; use WoWPress\Database\CreateSKSTable; use WoWPress\Frontend\Widgets\Birthdays; use WoWPress\Frontend\Widgets\BossKills; @@ -167,6 +168,8 @@ if (!function_exists('wowpress_database')) : CreateNotificationTable::sql(), CreateCharListsTable::sql(), CreateCharListItemTable::sql(), + CreateSKSHistoryTable::sql(), + ]); } diff --git a/theme/inc/config-routing.php b/theme/inc/config-routing.php index 9b96b9a..e2a85c8 100644 --- a/theme/inc/config-routing.php +++ b/theme/inc/config-routing.php @@ -5,6 +5,7 @@ use WoWPress\Models\CharacterList; use WoWPress\Models\Log; use WoWPress\Models\Raid; use WoWPress\Models\SKS; +use WoWPress\Models\SKSHistory; use WoWPress\Models\User; add_filter('generate_rewrite_rules', function ($wp_rewrite) { @@ -76,39 +77,28 @@ add_action('template_redirect', function () { include plugin_dir_path(__FILE__) . '../pages/roster.php'; die; break; + case 'import_sks': + isAllowed('wowpress_edit_site'); + echo "
";
+
+            foreach(SKS::getLists() as $list){
+                $list_there = SKSHistory::where('list_name',$list->list_name)->first();
+                if(empty($list_there)){
+                    $export = $list->export();
+                    $sks = SKSHistory::createList($list->list_name,$export);
+                    $sks->save();
+                }
+            }
+            die;
+            break;
         case 'sks':
             isAllowed('wowpress_view_raids');
             $GLOBALS['wowpress']['page_title'] = "SKS-Liste";
             $GLOBALS['wowpress']['liste'] = get_query_var('list_name', null);
-
             include plugin_dir_path(__FILE__) . '../pages/sks.php';
             die;
             break;
         case 'logs':
-
-           # $cl = new CharacterList();
-           # $cl->list_name = "Strike-Liste TWW Season 1";
-           # $cl->border_color = "red";
-           # $cl->max_count = 6;
-           # $cl->notify = true;
-           # $cl->save();
-
-           #$cl = CharacterList::find(14);
-
-
-
-
-            #$char = Character::first();
-
-            #$char = Character::find(8);
-
-
-            #$cl->addItem($char,'Richtig Dumm','2024-09-26');
-
-            #dd($cl->characters);
-
-
-            #exit;
             isAllowed('wowpress_edit_raids');
             $GLOBALS['wowpress']['page_title'] = "Logs";
             echo "
"; diff --git a/theme/options/misc.php b/theme/options/misc.php new file mode 100644 index 0000000..489f8eb --- /dev/null +++ b/theme/options/misc.php @@ -0,0 +1,38 @@ + + +
+

Allgemeine Optionen für

+ + +
+

Einstellungen gespeichert!

+
+ +

SKS-Liste neu Würfeln

+ +
+ + + get() as $list): ?> +

+ + +
+

Neue SKS-Liste

+
+ + + +

+
+
+first(); - if (empty($liste->list_name)) { - $liste = SKS::first(); - } -} - - -if (empty($liste->list_name)) { - - $listen = SKS::getLists(); - - get_header(); +if (empty($GLOBALS['wowpress']['liste'])): + $listen = SKSHistory::groupBy('list_name')->get(); ?>
- n +
-
- -
+
- "list_name ?>" + SKS-Liste "list_name ?>"
+ +
+ getPreviousList()): + ?> + + + getNextList()): + ?> + + + +
+
-
+
@@ -64,83 +78,73 @@ if (empty($liste->list_name)) { - - list_name) as $sk) : - ?> - odd:bg-slate-900"> - - odd:bg-slate-900"> - - - + + + - + + + + + - - - - - - - - - - - - + +
-
-
+ list_array as $list_item) : + switch ($list_item['rank']) { + case 1: + $rank_color = "text-[#bfac1d]"; + break; + case 2: + $rank_color = "text-[#9c9b9a]"; + break; + case 3: + $rank_color = "text-[#9e4f00]"; + break; + default: + $rank_color = "text-white"; + } + $char = $liste->getCharData($list_item['char']); + ?> - - +
name ?> +
+ +
+ + + + + +
+ +
+ + + + + +
+ - +
+ +
rank ?>character->name ?> -
- active) : ?> -
- - - - -
- -
- - - - -
- - -
- -
- - - - -
- +
- list_name)) : ?> - - -
\ No newline at end of file diff --git a/theme/pages/sks_backup.php b/theme/pages/sks_backup.php new file mode 100644 index 0000000..ef17406 --- /dev/null +++ b/theme/pages/sks_backup.php @@ -0,0 +1,186 @@ +first(); + if (empty($liste->list_name)) { + $liste = SKS::first(); + } +} + + +if (empty($liste->list_name)) { + + $listen = SKS::getLists(); + + get_header(); +?> +
+
+ n +
+
+
+
+
+ + list_name ?> + +
+
+
+ +
+
+ "list_name ?>" +
+
+
+
+
+ + + + + + + + + + + + + + + list_name) as $sk) : + ?> + odd:bg-slate-900"> + + + + + + + + + + + + + + + + +
RangCharakterAktion
+
+
+ + + + + + + + +
+ +
+ + +
rank ?>character->name ?> +
+ active) : ?> +
+ + + + +
+ +
+ + + + +
+ + +
+ +
+ +
+
+
+ +
+ + list_name)) : ?> + + + +
+ + \ No newline at end of file diff --git a/theme/request.php b/theme/request.php index 70ec4e2..1259ce0 100644 --- a/theme/request.php +++ b/theme/request.php @@ -14,6 +14,7 @@ use WoWPress\Models\Notification; use WoWPress\Models\Raid; use WoWPress\Models\Signup; use WoWPress\Models\SKS; +use WoWPress\Models\SKSHistory; use WoWPress\Models\User; if (empty($_POST['action'])) { @@ -151,40 +152,37 @@ switch ($_POST['action']) { } } break; + case 'createSKS': + isAllowed('wowpress_edit_raids'); + if (isset($_POST['createSKS_nonce']) && wp_verify_nonce($_POST['createSKS_nonce'], 'createSKS')) { + if(isset($_POST['list_name'])){ + if ($list = SKSHistory::getLatestList($_POST['list_name'])) { + break; + } + $list = new SKSHistory(); + $list->list_name = $_POST['list_name']; + $list->active = true; + $list->fillWithRaiders(); + $list->save(); + } + } + break; case 'addSKS': isAllowed('wowpress_edit_raids'); if (isset($_POST['addSKS_nonce']) && wp_verify_nonce($_POST['addSKS_nonce'], 'addSKS')) { if (isset($_POST['name']) && isset($_POST['realm']) && isset($_POST['list_name'])) { - $char = Character::whereName($_POST['name'])->whereRealm($_POST['realm'])->first(); - if (empty($char->ID)) { - $char = new Character(); - $char->name = $_POST['name']; - $char->realm = $_POST['realm']; + if ($list = SKSHistory::getLatestList($_POST['list_name'])) { + $list->addChar($_POST['name'],$_POST['realm']); } - - - - $sks = new SKS(); - $sks->char_name = $char->name; - $sks->realm_name = $char->realm; - $sks->list_name = $_POST['list_name']; - $sks->active = true; - $sks_last = SKS::where('list_name', $sks->list_name)->orderBy('rank', 'desc')->first(); - - $sks->rank = !empty($sks_last->rank) ? $sks_last->rank + 1 : 1; - - $sks->save(); } } break; case 'toggleSKS': isAllowed('wowpress_edit_raids'); if (isset($_POST['toggleSKS_nonce']) && wp_verify_nonce($_POST['toggleSKS_nonce'], 'toggleSKS')) { - if (isset($_POST['sks_id'])) { - $sks = SKS::find($_POST['sks_id']); - if ($sks->ID) { - $sks->active = !$sks->active; - $sks->save(); + if (isset($_POST['list_name']) && isset($_POST['rank'])) { + if ($list = SKSHistory::getLatestList($_POST['list_name'])) { + $list->toggleActive($_POST['rank']); } } } @@ -192,16 +190,9 @@ switch ($_POST['action']) { case 'deleteSKS': isAllowed('wowpress_edit_raids'); if (isset($_POST['deleteSKS_nonce']) && wp_verify_nonce($_POST['deleteSKS_nonce'], 'deleteSKS')) { - if (isset($_POST['sks_id'])) { - $sks = SKS::find($_POST['sks_id']); - if ($sks->ID) { - $after = $sks->getAfter(false); - foreach ($after as $sk) { - $sk->rank = $sk->rank - 1; - $sk->save(); - } - - $sks->delete(); + if (isset($_POST['list_name']) && isset($_POST['char'])) { + if ($list = SKSHistory::getLatestList($_POST['list_name'])) { + $list->removeChar($_POST['char']); } } } @@ -209,10 +200,29 @@ switch ($_POST['action']) { case 'lootSKS': isAllowed('wowpress_edit_raids'); if (isset($_POST['lootSKS_nonce']) && wp_verify_nonce($_POST['lootSKS_nonce'], 'lootSKS')) { - if (isset($_POST['sks_id'])) { - $sks = SKS::find($_POST['sks_id']); - if ($sks->ID) { - $sks->loot(); + if (isset($_POST['list_name']) && isset($_POST['char'])) { + if ($list = SKSHistory::getLatestList($_POST['list_name'])) { + $list->loot($_POST['char']); + } + } + } + break; + case 'undoSKS': + isAllowed('wowpress_edit_raids'); + if (isset($_POST['undoSKS_nonce']) && wp_verify_nonce($_POST['undoSKS_nonce'], 'undoSKS')) { + if (isset($_POST['list_name'])) { + if ($list = SKSHistory::getLatestList($_POST['list_name'])) { + $list->undo(); + } + } + } + break; + case 'redoSKS': + isAllowed('wowpress_edit_raids'); + if (isset($_POST['redoSKS_nonce']) && wp_verify_nonce($_POST['redoSKS_nonce'], 'redoSKS')) { + if (isset($_POST['list_name'])) { + if ($list = SKSHistory::getLatestList($_POST['list_name'])) { + $list->redo(); } } } @@ -221,22 +231,8 @@ switch ($_POST['action']) { isAllowed('wowpress_edit_raids'); if (isset($_POST['rollSKS_nonce']) && wp_verify_nonce($_POST['rollSKS_nonce'], 'rollSKS')) { if (isset($_POST['list_name'])) { - $sks = SKS::where('list_name', $_POST['list_name'])->first(); - if (!empty($sks->ID)) { - # $sks->roll(); - } - } - } - break; - case 'fixSKS': - isAllowed('wowpress_edit_raids'); - if (isset($_POST['fixSKS_nonce']) && wp_verify_nonce($_POST['fixSKS_nonce'], 'fixSKS')) { - if (isset($_POST['sks_id']) && isset($_POST['rank'])) { - $sks = SKS::find($_POST['sks_id']); - if ($sks->ID) { - - $sks->rank = intval($_POST['rank']); - $sks->save(); + if ($list = SKSHistory::getLatestList($_POST['list_name'])) { + $list->shuffle(); } } } @@ -359,8 +355,8 @@ switch ($_POST['action']) { } case 'addList': isAllowed('wowpress_edit_raids'); - if(checkNonce('addList')){ - if(isset($_POST['list_name'],$_POST['border_color'])){ + if (checkNonce('addList')) { + if (isset($_POST['list_name'], $_POST['border_color'])) { $list = new CharacterList(); $list->list_name = $_POST['list_name']; $list->border_color = $_POST['border_color']; @@ -373,40 +369,39 @@ switch ($_POST['action']) { isAllowed('wowpress_edit_raids'); if (isset($_POST['addCharToList_nonce']) && wp_verify_nonce($_POST['addCharToList_nonce'], 'addCharToList')) { - if(isset($_POST['character_ID']) && isset($_POST['list_id'])){ + if (isset($_POST['character_ID']) && isset($_POST['list_id'])) { $list = CharacterList::find($_POST['list_id']); $char = Character::find($_POST['character_ID']); - if($char && $list){ + if ($char && $list) { $comment = ""; $date = date('Y-m-d'); - if(!empty($_POST['comment'])){ + if (!empty($_POST['comment'])) { $comment = $_POST['comment']; } - if(!empty($_POST['date'])){ + if (!empty($_POST['date'])) { $date = $_POST['date']; } - $list->addItem($char,$comment,$date); + $list->addItem($char, $comment, $date); } } - } break; case 'removeListItem': isAllowed('wowpress_edit_raids'); - if(checkNonce('removeListItem')){ - if(isset($_POST['item_id'])){ + if (checkNonce('removeListItem')) { + if (isset($_POST['item_id'])) { $item_id = intval($_POST['item_id']); $item = CharacterListItem::find($item_id); - if($item){ + if ($item) { $item->delete(); - #CharacterListItem::deleteOrphans(); + #CharacterListItem::deleteOrphans(); } - } + } } break; case 'cron': - if(isset($_POST['cron'])){ - switch($_POST['cron']){ + if (isset($_POST['cron'])) { + switch ($_POST['cron']) { case 'update_applications': break; diff --git a/theme/wowpress/Database/CreateSKSHistoryTable.php b/theme/wowpress/Database/CreateSKSHistoryTable.php new file mode 100644 index 0000000..280033b --- /dev/null +++ b/theme/wowpress/Database/CreateSKSHistoryTable.php @@ -0,0 +1,18 @@ + 'text NOT NULL', + 'list_json' => 'json NOT NULL', + 'active' => 'boolean', + 'created_at' => 'timestamp NOT NULL', + 'updated_at' => 'timestamp NOT NULL', + ]; + +} diff --git a/theme/wowpress/Models/Raid.php b/theme/wowpress/Models/Raid.php index 3b3e1df..7c7afe5 100644 --- a/theme/wowpress/Models/Raid.php +++ b/theme/wowpress/Models/Raid.php @@ -27,7 +27,7 @@ class Raid extends Model { parent::__construct($attrs); $options = get_option('wowpress_api'); - if (isset($options['bnet'])) { + if (isset($options['wowaudit'])) { $key = get_option('wowpress_api')['wowaudit']['key']; } else { return false; diff --git a/theme/wowpress/Models/SKS.php b/theme/wowpress/Models/SKS.php index 8eccb21..2194263 100644 --- a/theme/wowpress/Models/SKS.php +++ b/theme/wowpress/Models/SKS.php @@ -126,5 +126,19 @@ class SKS extends Model } } + public function export(){ + $list = $this->getList($this->list_name); + foreach($list as &$list_item){ + $list_item['char'] = $list_item['char_name']."-".$list_item['realm_name']; + unset($list_item['list_name']); + unset($list_item['char_name']); + unset($list_item['realm_name']); + unset($list_item['ID']); + + } + $list->sort(fn($a,$b) => $a['rank'] - $b['rank']); + return json_encode($list); + } + } diff --git a/theme/wowpress/Models/SKSHistory.php b/theme/wowpress/Models/SKSHistory.php new file mode 100644 index 0000000..628ba0d --- /dev/null +++ b/theme/wowpress/Models/SKSHistory.php @@ -0,0 +1,266 @@ +list_name)->where('active', true)->latest('created_at')->first(); + } + + public static function getLatestList($list_name) + { + return static::where('list_name', $list_name)->where('active', true)->latest('created_at')->first(); + } + + private function setList($list_array) + { + $this->list_json = json_encode($list_array); + } + + + public static function createList(string $list_name, $list_json) + { + $sks = new static(); + $sks->list_name = $list_name; + if (is_array($list_json)) { + $list_json = json_encode($list_json); + } + $sks->list_json = $list_json; + $sks->active = true; + $sks->fixOrder(); + return $sks; + } + + public function fillWithRaiders() + { + $options = get_option('wowpress_api'); + if (isset($options['wowaudit'])) { + $key = get_option('wowpress_api')['wowaudit']['key']; + } else { + return false; + } + $api = new WoWAudit(null, $key); + $roster = $api->getRoster(); + $list = []; + foreach ($roster as $id => $character) { + if (!empty($character['name'])) { + $list[] = [ + 'char' => $character['name'] . "-" . $character['realm'], + 'rank' => $id + 1, + 'active' => true, + ]; + } + } + + + $this->list_json = json_encode($list); + } + + public function getListArrayAttribute() + { + return json_decode($this->list_json, true); + } + + public function shuffle() + { + $ranks = range(1, count($this->list_array)); + shuffle($ranks); + $list = $this->list_array; + foreach ($list as $id => &$list_item) { + $list_item['rank'] = array_pop($ranks); + } + $this->saveList($list); + } + + private function getChar($char) + { + foreach ($this->list_array as $list_item) { + if ($list_item['char'] == $char) { + return $list_item; + } + } + return false; + } + + private function sort($list) + { + usort($list, fn($a, $b) => $a['rank'] - $b['rank']); + return $list; + } + + + public function fixOrder() + { + $data = $this->list_array; + + // Separate active and inactive items + $activeItems = []; + $inactiveItems = []; + foreach ($data as $item) { + if ($item["active"]) { + $activeItems[] = $item; + } else { + $inactiveItems[] = $item; + } + } + + // Sort the active items by rank + usort($activeItems, function ($a, $b) { + return $a["rank"] - $b["rank"]; + }); + + // Reassign ranks starting from 1 for active items + $rankCounter = 0; + foreach ($activeItems as $key => $item) { + $rankCounter += 1; + foreach ($inactiveItems as $inactive_item) { + if ($inactive_item['rank'] == $rankCounter) { + $rankCounter += 1; + } + } + $item["rank"] = $rankCounter; + $activeItems[$key] = $item; + } + + // Merge active and inactive items back + $data = array_merge($inactiveItems, $activeItems); + + $data = $this->sort($data); + + + $this->setList($data); + } + + public function loot($char) + { + if ($looter = $this->getChar($char)) { + if ($looter['active']) { + $list = $this->list_array; + foreach ($list as $id => &$list_item) { + if ($list_item['rank'] == $looter['rank']) { + $list[$id]['rank'] = PHP_INT_MAX; //Ans Ende der Liste mit dem Looter + break; + } + } + + return $this->saveList($list); + } + } + return false; + } + + public function saveList($list_array) + { + $this->active = false; + $this->save(); + $new = self::createList($this->list_name, $list_array); + $new->save(); + return $new; + } + + public function toggleActive($rank) + { + $list = $this->list_array; + foreach ($list as &$list_item) { + if ($rank == $list_item['rank']) { + $list_item['active'] = !$list_item['active']; + return $this->saveList($list); + } + } + return false; + } + + public function getList() + { + return $this->list_array; + } + + public function getCharData($char) + { + list($name, $realm) = explode("-", $char); + $character = Character::whereName($name)->whereRealm($realm)->first(); + if (empty($character)) { + $character = new Character(); + $character->name = $name; + $character->realm = $realm; + $character->class = "priest"; + } + + return $character; + } + + public function getPreviousList() + { + return SKSHistory::where('list_name', $this->list_name)->whereNot('active')->where('created_at', '<', $this->created_at)->latest()->first(); + } + + public function getNextList() + { + return SKSHistory::where('list_name', $this->list_name)->whereNot('active')->where('created_at', '>', $this->created_at)->first(); + } + + public function undo() + { + $lastItem = $this->getPreviousList(); + $lastItem->active = true; + $lastItem->save(); + $this->active = false; + $this->save(); + } + + public function redo() + { + $nextItem = $this->getNextList(); + $nextItem->active = true; + $nextItem->save(); + $this->active = false; + $this->save(); + } + + public function removeChar($char, $realm = "") + { + if ($realm) { + $char = $char . "-" . $realm; + } + $list = $this->list_array; + foreach ($list as $id => $list_item) { + if (!$list_item['active'] && $list_item['char'] == $char) { + unset($list[$id]); + } + } + $this->saveList($list); + } + + public function addChar($char, $realm = "") + { + if ($realm) { + $char = $char . "-" . $realm; + } + $list = $this->list_array; + foreach ($list as $list_item) { + if ($list_item['char'] == $char) { + return false; + } + } + $list[] = [ + 'rank' => PHP_INT_MAX, + 'active' => true, + 'char' => $char + ]; + $this->saveList($list); + } +}