Issues (39)

router/100_guess.php (6 issues)

1
<?php
2
/**
3
 * Create routes for guess game using $app programming style.
4
 */
5
6
7
/**
8
 * Init the game redirect to play the game.
9
 */
10
$app->router->get("guess/init", function () use ($app) {
11
    $_SESSION["res"] = null;
12
    // Init the game
13
    $game = new Bashar\Guess\Guess();
14
    $_SESSION["number"] = $game->number();
15
    $_SESSION["tries"] = $game->tries();
16
17
    return $app->response->redirect("guess/play");
18
});
19
20
21
22
/**
23
 * Play the game. show game status
24
 */
25
$app->router->get("guess/play", function () use ($app) {
26
    $title = "Play the game";
27
28
    
29
    $data = [
30
        "guess" => $_SESSION["guess"] ?? null,
31
        "tries" => $_SESSION["tries"] ?? null,
32
        "number" => $_SESSION["number"] ?? null,
33
        "res" => $_SESSION["res"] ?? null,
34
    ];
35
36
    $_SESSION["res"] = null;
37
38
    $app->page->add("guess/play", $data);
39
    // $app->page->add("guess/debug");
40
41
    return $app->page->render([
42
        "title" => $title,
43
    ]);
44
});
45
46
47
/**
48
 * Play the game. Make a guess (POST method)
49
 */
50
$app->router->post("guess/play", function () use ($app) {
51
52
    // Deal with incoming variables
53
    $guess = $_POST["guess"] ?? null;
54
    $doGuess = $_POST["doGuess"] ?? null;
55
    $doInit = $_POST["doInit"] ?? null;
0 ignored issues
show
The assignment to $doInit is dead and can be removed.
Loading history...
56
    $doCheat = $_POST["doCheat"] ?? null;
57
58
    // Get current settings from the SESSION
59
    $number = $_SESSION["number"] ?? null;
60
    $tries = $_SESSION["tries"] ?? null;
0 ignored issues
show
The assignment to $tries is dead and can be removed.
Loading history...
61
    $res = null;
0 ignored issues
show
The assignment to $res is dead and can be removed.
Loading history...
62
63
    if ($_POST["doInit"]) {
64
        return $app->response->redirect("guess/init");
65
    } elseif ($doGuess) {
66
        $_SESSION["guess"] = $guess;
67
        return $app->response->redirect("guess/make-guess");
68
    } elseif ($doCheat) {
69
        $_SESSION["res"] = "Cheated number is: " . $number;
70
        return $app->response->redirect("guess/play");
71
    } else {
72
        return $app->response->redirect("guess/init");
73
    }
74
});
75
76
77
/**
78
 * Make a guess (make-guess)
79
 */
80
$app->router->get("guess/make-guess", function () use ($app) {
81
    $number = $_SESSION["number"] ?? null;
82
    $tries = $_SESSION["tries"] ?? null;
83
    $guess = $_SESSION["guess"] ?? null;
84
85
    $game = new Bashar\Guess\Guess($number, $tries);
0 ignored issues
show
It seems like $tries can also be of type null; however, parameter $tries of Bashar\Guess\Guess::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
    $game = new Bashar\Guess\Guess($number, /** @scrutinizer ignore-type */ $tries);
Loading history...
It seems like $number can also be of type null; however, parameter $number of Bashar\Guess\Guess::__construct() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
    $game = new Bashar\Guess\Guess(/** @scrutinizer ignore-type */ $number, $tries);
Loading history...
86
87
    try {
88
        $res = $game->makeGuess($guess);
0 ignored issues
show
It seems like $guess can also be of type null; however, parameter $guess of Bashar\Guess\Guess::makeGuess() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        $res = $game->makeGuess(/** @scrutinizer ignore-type */ $guess);
Loading history...
89
    } catch (Bashar\Guess\GuessException $e) {
90
        $res = '<p style="color:red; font-weight: 900;">Warning: </p>' . $e->getMessage();
91
    } catch (TypeError $e) {
92
        $res = `The given number {$guess} is out of range.`;
93
    }
94
95
96
    $_SESSION["tries"] = $game->tries();
97
    $_SESSION["res"] = $res;
98
99
    if ($res == "CORRECT") {
100
        return $app->response->redirect("guess/win");
101
    } elseif ($_SESSION["tries"] < 1) {
102
        return $app->response->redirect("guess/fail");
103
    } else {
104
        return $app->response->redirect("guess/play");
105
    }
106
});
107
108
109
110
/**
111
 * Wining the game
112
 */
113
$app->router->get("guess/win", function () use ($app) {
114
    $title =" You won the game!";
115
116
    $data = [
117
        "number" => $_SESSION["number"] ?? null
118
    ];
119
    
120
121
    $app->page->add("guess/win", $data);
122
123
    return $app->page->render([
124
        "title" => $title,
125
    ]);
126
});
127
128
129
/**
130
 * In case of losing the game
131
 */
132
$app->router->get("guess/fail", function () use ($app) {
133
    $title =" You have lost the game!";
134
135
    $data = [
136
        "tries" => $_SESSION["tries"] ?? null,
137
        "number" => $_SESSION["number"] ?? null
138
    ];
139
140
    $app->page->add("guess/fail", $data);
141
142
    return $app->page->render([
143
        "title" => $title,
144
    ]);
145
});
146