DefaultController::characterPageAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
namespace Rottenwood\KingdomBundle\Controller;
4
5
use Rottenwood\KingdomBundle\Entity\Infrastructure\User;
6
use Rottenwood\KingdomBundle\Redis\RedisClientInterface;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
12
13
/** {@inheritDoc} */
14
class DefaultController extends Controller
15
{
16
17
    /**
18
     * @Route("/", name="index")
19
     * @return Response
20
     */
21
    public function indexAction()
22
    {
23
        if ($this->getUser()) {
24
            return $this->redirectToRoute('game_page');
25
        } else {
26
            return $this->redirectToRoute('fos_user_security_login');
27
        }
28
    }
29
30
    /**
31
     * Основная страница игры
32
     * @Security("has_role('ROLE_USER')")
33
     * @Route("/game", name="game_page")
34
     * @param Request $request
35
     * @return Response
36
     */
37
    public function gamePageAction(Request $request)
38
    {
39
        $sessionId = $request->getSession()->getId();
40
        /** @var User $user */
41
        $user = $this->getUser();
42
        $userId = $user->getId();
43
44
        /** @var \Redis $redis */
45
        $redis = $this->container->get('snc_redis.default');
46
47
        $redis->hset(RedisClientInterface::ID_USERNAME_HASH, $userId, $user->getName());
48
        $redis->hset(RedisClientInterface::ID_SESSION_HASH, $userId, $sessionId);
49
        $redis->hset(RedisClientInterface::SESSION_ID_HASH, $sessionId, $userId);
50
        $redis->hset(RedisClientInterface::ID_ROOM_HASH, $userId, $user->getRoom()->getId());
51
52
        return $this->render('RottenwoodKingdomBundle:Default:game.html.twig', ['sessionId' => $sessionId]);
53
    }
54
55
    /**
56
     * Страница информации о персонаже
57
     * @Route("/character/{name}", name="character_page")
58
     * @param Request $request
59
     * @return Response
60
     */
61
    public function characterPageAction(Request $request)
62
    {
63
        $userNameOrId = $request->attributes->get('name');
64
        $humanRepository = $this->get('kingdom.human_repository');
65
66
        $user = $humanRepository->findByNameOrId($userNameOrId);
67
68
        $result = [
69
            'character' => $user,
70
        ];
71
72
        if ($user) {
73
            $result['items'] = $this->get('kingdom.inventory_item_repository')->findByUser($user);
74
        }
75
76
        return $this->render('RottenwoodKingdomBundle:Default:character.html.twig', $result);
77
    }
78
}
79