Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.88% covered (warning)
85.88%
73 / 85
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
HeroController
85.88% covered (warning)
85.88%
73 / 85
0.00% covered (danger)
0.00%
0 / 5
17.81
0.00% covered (danger)
0.00%
0 / 1
 refinement
91.43% covered (success)
91.43%
32 / 35
0.00% covered (danger)
0.00%
0 / 1
6.02
 sprint
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
2.06
 intuition
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
2.09
 rejuvenation
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
2.02
 revelation
87.50% covered (warning)
87.50%
21 / 24
0.00% covered (danger)
0.00%
0 / 1
5.05
1<?php
2
3class HeroController extends AppController
4{
5    public function refinement()
6    {
7        if (!Auth::isLoggedIn())
8            return $this->redirect('/users/login');
9        if (Auth::getUser()['used_refinement'])
10            throw new AppException("Refinment is already used up.");
11
12        $queryWithoutRankLimit = "SELECT "
13                . "set_connection.id as set_connection_id, tsumego.id as tsumego_id "
14                . " FROM tsumego"
15                . " JOIN set_connection ON set_connection.tsumego_id=tsumego.id"
16                . " JOIN `set` ON `set`.id=set_connection.set_id"
17                . " WHERE tsumego.deleted is null AND set.public = 1";
18
19        // first we try to select golden tsumego with proper rating relative to user
20        $setConnectionIDs = Util::query($queryWithoutRankLimit . " AND tsumego.rating >= ? AND tsumego.rating <= ?",
21            [
22                Auth::getUser()['rating'] - Constants::$GOLDEN_TSUMEGO_LOWER_RELATIVE_RATING_LIMIT,
23                Auth::getUser()['rating'] + Constants::$GOLDEN_TSUMEGO_UPPER_RELATIVE_RATING_LIMIT
24            ]);
25
26        // if it fails, we try to select "some" tsumego
27        if (empty($setConnectionIDs))
28            $setConnectionIDs = Util::query($queryWithoutRankLimit);
29        if (empty($setConnectionIDs))
30            throw new Exception("No valid tsumego to choose from.");
31        $setConnection = $setConnectionIDs[rand(0, count($setConnectionIDs) - 1)];
32        $tsumegoStatus = ClassRegistry::init('TsumegoStatus')->find('first', ['conditions' => [
33            'tsumego_id' => $setConnection['tsumego_id'],
34            'user_id' => Auth::getUserID()]]);
35        if (!$tsumegoStatus)
36        {
37            ClassRegistry::init('TsumegoStatus')->create();
38            $tsumegoStatus = [];
39            $tsumegoStatus['user_id'] = Auth::getUserID();
40            $tsumegoStatus['tsumego_id'] = $setConnection['tsumego_id'];
41        }
42        else
43            $tsumegoStatus = $tsumegoStatus['TsumegoStatus'];
44        $tsumegoStatus['created'] = date('Y-m-d H:i:s');
45        $tsumegoStatus['status'] = 'G';
46        ClassRegistry::init('TsumegoStatus')->save($tsumegoStatus);
47        Auth::getUser()['used_refinement'] = 1;
48        Auth::saveUser();
49        return $this->redirect('/' . $setConnection['set_connection_id']);
50    }
51
52    public function sprint()
53    {
54        if (!HeroPowers::canUseSprint())
55        {
56            $this->response->statusCode(403);
57            return $this->response;
58        }
59        Auth::getUser()['sprint_start'] = date('Y-m-d H:i:s');
60        Auth::getUser()['used_sprint'] = 1;
61        Auth::saveUser();
62        $this->response->statusCode(200);
63        return $this->response;
64    }
65
66    public function intuition()
67    {
68        if (!HeroPowers::canUseIntuition())
69        {
70            $this->response->statusCode(403);
71            return $this->response;
72        }
73        Auth::getUser()['used_intuition'] = 1;
74        Auth::saveUser();
75        $this->response->statusCode(200);
76        return $this->response;
77    }
78
79    public function rejuvenation()
80    {
81        if (!HeroPowers::canUseRejuvanation())
82        {
83            $this->response->statusCode(403);
84            return $this->response;
85        }
86        Auth::getUser()['used_rejuvenation'] = 1;
87        Auth::getUser()['used_intuition'] = 0;
88        Auth::getUser()['damage'] = 0;
89        Auth::saveUser();
90
91        ClassRegistry::init('TsumegoStatus')->query("UPDATE tsumego_status SET status='V' WHERE status='F' AND user_id=" . Auth::getUserID());
92        ClassRegistry::init('TsumegoStatus')->query("UPDATE tsumego_status SET status='W' WHERE status='X' AND user_id=" . Auth::getUserID());
93
94        $this->response->statusCode(200);
95        return $this->response;
96    }
97
98    public function revelation($tsumegoID)
99    {
100        if (!Auth::isLoggedIn())
101        {
102            $this->response->body('Not logged in.');
103            $this->response->statusCode(403);
104            return $this->response;
105        }
106
107        if (!HeroPowers::canUseRevelation())
108        {
109            $this->response->body('Revelation is used up today.');
110            $this->response->statusCode(403);
111            return $this->response;
112        }
113        $tsumego = ClassRegistry::init('Tsumego')->findById($tsumegoID);
114        if (!$tsumego)
115        {
116            $this->response->statusCode(403);
117            return $this->response;
118        }
119
120        $previousTsumegoStatus = ClassRegistry::init('TsumegoStatus')->find('first', ['conditions' => ['user_id' => Auth::getUserID(), 'tsumego_id' => $tsumegoID]]);
121        if (!$previousTsumegoStatus)
122        {
123            $previousTsumegoStatus = [];
124            $previousTsumegoStatus['user_id'] = Auth::getUserID();
125            $previousTsumegoStatus['tsumego_id'] = $tsumegoID;
126            ClassRegistry::init('TsumegoStatus')->create();
127        }
128        else
129            $previousTsumegoStatus = $previousTsumegoStatus['TsumegoStatus'];
130
131        $previousTsumegoStatus['created'] = date('Y-m-d H:i:s');
132        $previousTsumegoStatus['status'] = 'S';
133        ClassRegistry::init('TsumegoStatus')->save($previousTsumegoStatus);
134
135        Auth::getUser()['used_revelation']++;
136        Auth::saveUser();
137    }
138}