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

145 lines
3.8 KiB
PHP
Raw Normal View History

2024-04-22 15:38:22 +02:00
<?php
namespace WoWPress\Models;
use Wenprise\Eloquent\Model;
use WoWPress\Api\BattleNet;
class SKS extends Model
{
protected $table = "wowpress_sks";
public $timestamps = false;
protected $primaryKey = 'ID';
protected $guarded = ['ID'];
2024-04-23 22:43:54 +02:00
public static function getList($name)
{
return static::where('list_name', $name)->orderBy('rank')->get();
2024-04-22 15:38:22 +02:00
}
2024-04-23 22:43:54 +02:00
public static function getLists()
{
return static::groupBy('list_name')->get();
2024-04-22 15:38:22 +02:00
}
2024-04-23 22:43:54 +02:00
public function getCharacterAttribute()
{
$char = Character::where('name', $this->char_name)->where('realm', $this->realm_name)->first();
if (!empty($char->ID)) {
2024-04-23 00:44:42 +02:00
return $char;
}
$char = new Character();
$char->name = $this->char_name;
$char->realm = $this->realm_name;
$char->class = "yellow";
return $char;
}
2024-04-22 15:38:22 +02:00
2024-04-23 22:43:54 +02:00
public function getAfter($active = true)
{
$list = static::where('list_name', $this->list_name)->where('rank', ">", $this->rank);
if ($active) {
$list = $list->where('active', true);
}
return $list->orderBy('rank')->get();
}
public function getPrevious($active = true)
{
$prev = SKS::where('list_name', $this->list_name)->where('rank', '<', $this->rank);
if ($active) {
$prev = $prev->where('active', true);
}
$prev = $prev->orderBy('rank', 'DESC')->first();
if (!empty($prev->ID)) {
return $prev->rank;
} else {
return $this->rank;
}
}
public function getLastRankAttribute()
{
$lastSK = SKS::where('list_name', $this->list_name)
->where('active', true)
->orderBy('rank', 'DESC')->get()->first();
if (!empty($lastSK->ID)) {
return $lastSK->rank;
}
return false;
}
public function roll()
{
$list = SKS::where('list_name', $this->list_name)->get();
$ranks = range(1, count($list));
shuffle($ranks);
2024-05-02 23:31:44 +02:00
Log::write(json_encode($list->map->only(['rank','char_name','realm_name'])->values()),Log::LOG_ROLL);
2024-04-23 22:43:54 +02:00
foreach ($list as $key => $sk) {
$sk->rank = $ranks[$key];
$sk->save();
}
}
public function loot()
{
$after = $this->getAfter();
2024-05-02 23:31:44 +02:00
$rank = $this->rank;
2024-04-23 22:43:54 +02:00
$last_rank = $this->last_rank;
foreach ($after as $id => &$sk) {
/**
* Ignoriere inaktive Charaktere & Charaktere
* die auf einem höheren Rang als der Looter sind
* und überspringe die restlichen Anweisungen
*/
if (!$sk->active || $sk->rank < $this->rank) {
continue;
}
/**
* Setze den Rang des Charakters auf den Rang des vorherigen aktiven
*/
$after[$id]->rank = $sk->getPrevious(true);
}
/**
* Setze den Rang des Looters auf den letzen aktiven Rang
*/
$this->rank = $last_rank;
$this->save();
2024-05-02 23:31:44 +02:00
Log::write(json_encode(['char' => $this->char_name."-".$this->realm_name,'rank' => $this->rank,'previous' => $rank,'list' => $this->list_name]),Log::LOG_LOOT);
2024-04-23 22:43:54 +02:00
foreach ($after as $sk) {
$sk->save();
}
}
2024-09-20 20:02:10 +02:00
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);
}
2024-04-23 22:43:54 +02:00
}