WowPress-Tailwind/theme/wowpress/Models/Raid.php

264 lines
8.3 KiB
PHP
Raw Normal View History

2024-04-15 23:07:29 +02:00
<?php
namespace WoWPress\Models;
2024-04-22 15:38:22 +02:00
use MO;
2024-04-15 23:07:29 +02:00
use Wenprise\Eloquent\Model;
use WoWPress\Api\BattleNet;
2024-04-22 15:38:22 +02:00
use WoWPress\Api\WoWAudit;
2024-04-15 23:07:29 +02:00
class Raid extends Model
{
protected $table = "wowpress_raids";
public $timestamps = false;
protected $primaryKey = 'ID';
protected $guarded = ['ID'];
2024-04-22 15:38:22 +02:00
public $signups;
2024-04-16 15:25:22 +02:00
private $api;
2024-04-22 15:38:22 +02:00
public $notes;
public $encounters;
public $status;
public $partial;
2024-04-16 15:25:22 +02:00
2024-04-15 23:07:29 +02:00
public function __construct($attrs = [])
{
parent::__construct($attrs);
$options = get_option('wowpress_api');
2024-04-22 15:38:22 +02:00
if (isset($options['bnet'])) {
2024-04-15 23:07:29 +02:00
$key = get_option('wowpress_api')['wowaudit']['key'];
2024-04-22 15:38:22 +02:00
} else {
2024-04-15 23:07:29 +02:00
return false;
}
2024-04-22 15:38:22 +02:00
$this->api = new WoWAudit(null, $key);
}
private function sortRoles($a, $b)
{
switch ($a->role) {
case 'Tank':
$asort = 0;
break;
case 'Heal':
$asort = 1;
break;
case 'Ranged':
$asort = 2;
break;
case 'Melee':
$asort = 3;
break;
default:
$asort = 4;
break;
}
switch ($b->role) {
case 'Tank':
$bsort = 0;
break;
case 'Heal':
$bsort = 1;
break;
case 'Ranged':
$bsort = 2;
break;
case 'Melee':
$bsort = 3;
break;
default:
$bsort = 4;
break;
}
return $asort - $bsort;
2024-04-15 23:07:29 +02:00
}
2024-04-22 15:38:22 +02:00
public function sync($force = false)
{
if (!empty($this->id_wowaudit)) {
$raid = (object)$this->api->getRaid($this->id_wowaudit, $force);
if (!empty($raid->id)) {
$this->title = $raid->instance;
$this->start = $raid->date . " " . $raid->start_time;
$this->end = $raid->date . " " . $raid->end_time;
$this->difficulty = $raid->difficulty;
$this->status = $raid->status;
$encounters = $raid->encounters;
$partial = false;
$signups = [];
$sup = [];
2024-04-23 00:44:42 +02:00
2024-04-22 15:38:22 +02:00
foreach ($encounters as $key => &$encounter) {
$partial = $encounter['enabled'] || $partial;
if (empty($encounter['selections'])) {
$encounter['selections'] = [];
}
$encounters[$key]['selections'] = collect($encounter['selections']);
}
2024-04-23 00:44:42 +02:00
2024-04-22 15:38:22 +02:00
$this->partial = $partial;
foreach ($raid->signups as $signup) {
$character = Character::whereName($signup['character']['name'])->whereRealm($signup['character']['realm'])->first();
if (empty($character)) {
$character = new Character();
$character->name = $signup['character']['name'];
$character->realm = $signup['character']['realm'];
$character->updateFromAPI();
}
if ($character->class != $signup['class']) {
if (!empty($character->user)) {
$newchar = $character->user->characters()->where('class', $signup['class'])->first();
if (!empty($newchar->ID)) {
$character = $newchar;
}
}
}
$character->id_wowaudit = $signup['character']['id'];
$character->save();
$sup[$character->id_wowaudit] = $signup['status'];
$s = new Signup();
$s->character_id = $character->ID;
$s->raid_ID = $this->ID;
if ($this->partial) {
foreach ($encounters as $encounter) {
if ($signup['selected']) break;
if (empty($encounter['selections'])) continue;
foreach ($encounter['selections'] as $selection) {
if ($selection['character_id'] == $signup['character']['id']) {
$signup['selected'] = $selection['selected'] || $signup['selected'];
if ($signup['selected']) {
break 2;
}
}
}
}
}
$s->status = $signup['selected'] ? "Selected" : $signup['status'];
$s->role = $signup['status'] == "Unknown" ? "Unknown" : $signup['role'];
$signups[] = $s;
}
2024-04-23 00:44:42 +02:00
2024-04-22 15:38:22 +02:00
$this->signups = collect($signups);
2024-04-23 00:44:42 +02:00
foreach ($encounters as &$encounter) {
2024-04-22 15:38:22 +02:00
foreach ($encounter['selections'] as $key => &$selection) {
$character = Character::where('id_wowaudit', $selection['character_id'])->first();
$selection['status'] = $sup[$character->id_wowaudit];
$s = new Signup();
$s->character_id = $character->ID;
$s->raid_ID = $this->ID;
$s->boss_id = $encounter['id'];
$s->status = $selection['selected'] ? "Selected" : $selection['status'];
$s->role = $selection['role'];
$encounter['selections'][$key] = $s;
}
}
2024-04-23 00:44:42 +02:00
2024-04-22 15:38:22 +02:00
array_unshift($encounters, [
'name' => "Alle Bosse",
'id' => 0,
'enabled' => true,
'notes' => $raid->notes,
'selections' => $this->signups,
]);
2024-04-15 23:07:29 +02:00
2024-04-22 15:38:22 +02:00
foreach ($encounters as &$encounter) {
$encounter['selections'] = collect($encounter['selections'])->sortBy([fn ($a, $b) => $this->sortRoles($a, $b), 'character_id']);
}
$this->encounters = collect($encounters);
2024-04-23 00:44:42 +02:00
}else{
return false;
2024-04-22 15:38:22 +02:00
}
2024-04-16 15:25:22 +02:00
}
2024-04-22 15:38:22 +02:00
2024-04-23 00:44:42 +02:00
return $this;
2024-04-16 15:25:22 +02:00
}
2024-04-22 15:38:22 +02:00
public function getColorAttribute($type = "")
{
switch ($this->difficulty) {
case 'Normal':
return "shaman";
case 'Heroic':
return "warlock";
case 'Mythic':
return "deathknight";
case 'Teamspeak':
return "hunter";
default:
return "priest";
}
}
public function getLinkAttribute()
{
return "/raid/" . $this->ID;
2024-04-16 15:25:22 +02:00
}
2024-04-22 15:38:22 +02:00
public function getPreviousAttribute()
{
2024-04-16 15:25:22 +02:00
$id = $this->ID;
2024-04-22 15:38:22 +02:00
while ($id) {
2024-04-16 15:25:22 +02:00
$raid = Raid::find(--$id);
2024-04-22 15:38:22 +02:00
if (!empty($raid->ID)) {
2024-04-16 15:25:22 +02:00
return $raid;
}
}
return false;
}
2024-04-22 15:38:22 +02:00
public function getNextAttribute()
{
2024-04-16 15:25:22 +02:00
$id = $this->ID;
2024-04-22 15:38:22 +02:00
while ($id <= Raid::max('ID')) {
2024-04-16 15:25:22 +02:00
$raid = Raid::find(++$id);
2024-04-22 15:38:22 +02:00
if (!empty($raid->ID)) {
2024-04-16 15:25:22 +02:00
return $raid;
}
}
return false;
}
2024-04-22 15:38:22 +02:00
public function showSignup($character, $showRole = false, $showStatus = false, $vertical = false)
{
$u = $character->user;
if (!empty($u->ID)) {
$chars = $u->characters;
} else {
$chars = [$character];
}
2024-04-23 00:44:42 +02:00
if(!empty($this->id_wowaudit)) $this->sync();
2024-04-22 15:38:22 +02:00
if (!empty($character->ID) && !empty($this->signups)) {
foreach ($this->signups as $signup) {
foreach ($chars as $character) {
if ($signup->character->ID == $character->ID) {
return $signup->showForm($showRole, $showStatus, $vertical);
}
}
}
}
2024-04-17 22:10:17 +02:00
2024-04-22 15:38:22 +02:00
$s = new Signup();
$s->raid_id = $this->ID;
$s->character_id = $character->ID;
return $s->showForm($showRole,$showStatus,$vertical);
}
}