Reset komponenty do výchozího stavu

Fires
Člen | 89
+
0
-

Zdravím, snažím se trochu natrénovat komponenty a dělám alternativu k fifteen. Prostě 5×5 polí a pomocí tlačítek se snažíte dostat křížek na určený čtverec. Nic zajimavého. Každopádně nějak to funguje, ale nevím jak na konci (game over, hra u konce. Začít znovu). Jak vynutit „začít znovu“, takže vyresetovat komponentu do výchozího stavu bez persistentních parametru? V presenteru bych asi věděl ale lze to udělat na úrovní komponenty? Nejaký jednoduchů způsob jak to vyresetovat?

Díky moc.

<?php

namespace App\Components;

use Nette\Application\UI\Control;

class MainComponent extends Control
{


    public $width = 5;
    public $height = 5;

    /** @persistent array*/
    public $position = [];

    /** @persistent */
    public $round = 0;

    /** @persistent array*/
    public $target = [];

    private $gameOver = false;

    public function __construct(){
        $this->position = [rand(1,$this->width),rand(1,$this->height)];
        $this->target = [rand(1,$this->width),rand(1,$this->height)];
    }

    public function render()
    {
        $this->template->width = $this->width;
        $this->template->heigth = $this->height;
        $this->template->round = $this->round;
        $this->template->position = $this->position;
        $this->template->target = $this->target;
        $this->template->gameOver = $this->gameOver;
        $this->template->render(__DIR__ . '/ArrowGame.latte');
    }

    public function handleMove($direction)
    {
        if($direction=="up" && $this->position[1]>1){
            $this->position[1]--;
        }elseif($direction=="down" && $this->position[1]<=$this->height){
            $this->position[1]++;
        }elseif($direction=="left" && $this->position[0]>1){
            $this->position[0]--;
        }elseif($direction=="right" && $this->position[0]<$this->width){
            $this->position[0]++;
        }
        $this->round++;
        if($this->position == $this->target){
            $this->gameOver = true;
        }
    }

    public function handleRestart(){
        $this->redrawControl();
    }


}
{block content}
<div style="background-color: darkseagreen;">
<h2>Arrow game</h2>
{if !$gameOver}
<h4>Round: {$round}</h4>
<a n:href="move! up">UP</a>
<a n:href="move! down">DOWN</a>
<a n:href="move! left">LEFT</a>
<a n:href="move! right">RIGHT</a>
<table>
    <table class="fifteen">
        <tr n:for="$y = 1; $y <= $heigth ; $y++">
            <td style="width:50px;height:50px;background-color: burlywood; text-align: center;" n:for="$x=1; $x <= $width; $x++">
                <h2 n:if="$position[0]==$x && $position[1]==$y">X</h2>
                <h2 n:if="$target[0]==$x && $target[1]==$y">O</h2>
            </td>
        </tr>
</table>
{else}
    <h2>You won! Congratulation !</h2>
    <a n:href="restart!">Restart</a>
{/if}
</div>
m.brecher
Generous Backer | 736
+
0
-

@Fires

Restart provedeme tímto odkazem:

<a n:href="restart!">Restart</a>

Píšu z hlavy, takže bez záruky, musíš si to doladit:

public function handleRestart()
{
    $this->width = 5;
    $this->height = 5;
    $this->position = [];

    .......

    $this->redirect();
}

Fígl je v tom přesměrování.

Komponenty umí přesměrovat stránku stejně jako presentery, dalo by se i volat:

public function handleRestart()
{

    .......

    $this->presenter->redirect();
}

a pravděpodobně by stačilo nastavit persistentní parametry, ty lokální by se po redirectu nastavily asi samy:

public function handleRestart()
{
// reset persistentních parametrů
$this->position = [];

.......

$this->redirect();
}
`

Tak to vyzkoušej a napiš, jestli to takhle funguje ;).

Fires
Člen | 89
+
0
-

m.brecher napsal(a):

@Fires

Restart provedeme tímto odkazem:

<a n:href="restart!">Restart</a>

Píšu z hlavy, takže bez záruky, musíš si to doladit:

public function handleRestart()
{
    $this->width = 5;
    $this->height = 5;
    $this->position = [];

    .......

    $this->redirect();
}

Fígl je v tom přesměrování.

Komponenty umí přesměrovat stránku stejně jako presentery, dalo by se i volat:

public function handleRestart()
{

    .......

    $this->presenter->redirect();
}

a pravděpodobně by stačilo nastavit persistentní parametry, ty lokální by se po redirectu nastavily asi samy:

public function handleRestart()
{
// reset persistentních parametrů
$this->position = [];

.......

$this->redirect();
}
`

Tak to vyzkoušej a napiš, jestli to takhle funguje ;).

    public function handleRestart(){
        $this->redirect('this');
    }

Stále drží persistentní parametry. Můžu je ručně vymazat v tom handle a znova náhodně inicializovat prakticky zkopírovat construct. Ale nepříjde mi to moc elegantní? Duplicita kódu?