Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
TsumegoButton
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
6 / 6
17
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 createFromSetConnection
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 render
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
9
 generateTooltip
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 createBoard
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createBoardFromSgf
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3class TsumegoButton
4{
5    public function __construct(int $tsumegoID, int $setConnectionID, int $order, ?string $status, ?float $rating = 0)
6    {
7        $this->tsumegoID = $tsumegoID;
8        $this->setConnectionID = $setConnectionID;
9        $this->order = $order;
10        $this->status = $status;
11        $this->rating = $rating;
12    }
13
14    public static function createFromSetConnection($setConnection): TsumegoButton
15    {
16        $tsumegoStatus = ClassRegistry::init('TsumegoStatus')->find('first', ['conditions' => [
17            'tsumego_id' => $setConnection['tsumego_id'],
18            'user_id' => Auth::getUserID()]]);
19        return new TsumegoButton(
20            $setConnection['tsumego_id'],
21            $setConnection['id'],
22            $setConnection['num'],
23            $tsumegoStatus ? $tsumegoStatus['TsumegoStatus']['status'] : null);
24    }
25
26    public function render()
27    {
28        $num = '<div class="setViewButtons1"' . ($this->isCurrentlyOpened ? ' id="currentNavigationButton"' : '') . '>' . $this->order . '</div>';
29
30        // Calculate accuracy (performance) as percentage
31        if (empty($this->performance))
32        {
33            $persormanceS = 0;
34            $persormanceF = 0;
35        }
36        else
37        {
38            $persormanceS = substr_count($this->performance, '1');
39            $persormanceF = substr_count($this->performance, 'F');
40        }
41        if ($persormanceS == 0 && $persormanceF == 0)
42            $num2 = '-';
43        else
44            $num2 = $persormanceS . '/' . $persormanceF;
45        $num2 = '<div class="setViewButtons2">' . $num2 . '</div>';
46
47        // Calculate time
48        if ($this->seconds == 0 || $this->seconds == '')
49            $num3 = '-';
50        else
51            $num3 = $this->seconds . 's';
52        $num3 = '<div class="setViewButtons3">' . $num3 . '</div>';
53
54        echo '<li class="status' . ($this->status ?: 'N') . ($this->isCurrentlyOpened ? ' statusCurrent' : '') . '">';
55        echo '<a class="tooltip" href="/' . $this->setConnectionID . '" onmouseover="' . $this->generateTooltip() . '">' . $num . $num2 . $num3 . '<span class="tooltip-box"></span></a>';
56        echo '</li>';
57    }
58
59    private function generateTooltip(): string
60    {
61        if (!$this->sgf)
62        {
63            $sgfObject = ClassRegistry::init('Sgf')->find('first', ['limit' => 1, 'order' => 'id DESC', 'conditions' => ['tsumego_id' => $this->tsumegoID]]);
64            if (!$sgfObject)
65                return '';
66            $this->sgf = $sgfObject['Sgf']['sgf'];
67        }
68        $result = '';
69        $result .= 'if (this.querySelector(\'svg\')) return;';
70        $result .= $this->createBoard('this', 'createPreviewBoard');
71        return $result;
72    }
73
74    public function createBoard($target, $functionName, $diff = '')
75    {
76        return self::createBoardFromSgf($this->sgf, $target, $functionName, $diff);
77    }
78
79    public static function createBoardFromSgf($sgf, $target, $functionName, $diff = '')
80    {
81        $sgf = SgfParser::process($sgf);
82        $black = '\'' . implode("", array_map(fn($stone) => BoardPosition::toLetters($stone), $sgf->filterStonesPositions(SgfBoard::BLACK))) . '\'';
83        $white = '\'' . implode("", array_map(fn($stone) => BoardPosition::toLetters($stone), $sgf->filterStonesPositions(SgfBoard::WHITE))) . '\'';
84        return $functionName . '(' . $target . ', ' . $black . ', ' . $white . ',' . $sgf->info[0] . ', ' . $sgf->info[1] . ', ' . $sgf->size . ', \'' . $diff . '\');' . PHP_EOL;
85    }
86
87    public int $tsumegoID;
88    public int $setConnectionID;
89    public int $order;
90    public ?string $status;
91    public ?float $rating;
92    public float $seconds = 0;
93    public string $performance;
94    public bool $isCurrentlyOpened = false;
95    public $sgf = null;
96}