ImportManager.php 4.19 KB
Newer Older
arnaudbey's avatar
add app  
arnaudbey committed
1
2
3
4
5
6
<?php

namespace  MagicWordBundle\Manager;

use JMS\DiExtraBundle\Annotation as DI;
use MagicWordBundle\Entity\GameType\Massive;
7
use MagicWordBundle\Entity\Round;
arnaudbey's avatar
add app  
arnaudbey committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

/**
 * @DI\Service("mw_manager.import")
 */
class ImportManager
{
    protected $em;
    protected $gridManager;
    protected $objectiveManager;
    protected $roundManager;
    protected $tokenStorage;

    /**
     * @DI\InjectParams({
     *      "entityManager" = @DI\Inject("doctrine.orm.entity_manager"),
     *      "gridManager"   = @DI\Inject("mw_manager.grid"),
     *      "objectiveManager"   = @DI\Inject("mw_manager.objective"),
     *      "roundManager"   = @DI\Inject("mw_manager.round"),
     *      "tokenStorage"  = @DI\Inject("security.token_storage"),
     * })
     */
    public function __construct($entityManager, $gridManager, $objectiveManager, $roundManager, $tokenStorage)
    {
        $this->em = $entityManager;
        $this->gridManager = $gridManager;
        $this->objectiveManager = $objectiveManager;
        $this->roundManager = $roundManager;
        $this->tokenStorage = $tokenStorage;
    }

    public function importGame($gameJSON)
    {
        $name = $gameJSON['name'];
        $description = $gameJSON['description'];
arnaudbey's avatar
arnaudbey committed
42
        $language = $this->em->getRepository('LexiconBundle:Language')->findOneById($gameJSON['language']);
arnaudbey's avatar
add app  
arnaudbey committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        $accessType = $this->em->getRepository('MagicWordBundle:AccessType')->findOneById($gameJSON['access']);

        $massive = new Massive();
        $massive->setAuthor($this->tokenStorage->getToken()->getUser());
        $massive->setName($name.' (import)');
        $massive->setDescription($description);
        $massive->setLanguage($language);
        $massive->setAccessType($accessType);
        $this->em->persist($massive);
        $this->em->flush();

        $rounds = $gameJSON['rounds'];
        foreach ($rounds as $round) {
            $this->importRound($round, $massive);
        }

        return $massive->getId();
    }

    public function importRound($roundJSON, Massive $massive)
    {
        $letters = $roundJSON['grid']['letters'];
arnaudbey's avatar
arnaudbey committed
65
        $language = $this->em->getRepository('LexiconBundle:Language')->findOneById($roundJSON['language']);
arnaudbey's avatar
add app  
arnaudbey committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        $grid = $letters ? $this->gridManager->generate($language, $letters) : null;

        $displayOrder = $roundJSON['displayOrder'];
        $type = $roundJSON['type'];
        $round = ($type == 'rush')
            ? $this->roundManager->generateRush($massive, $grid, $language, $displayOrder)
            : $this->roundManager->generateConquer($massive, $grid, $language, $displayOrder);

        if ($type == 'conquer') {
            $objectives = $roundJSON['objectives'];
            foreach ($objectives as $objective) {
                $this->importObjective($objective, $round);
            }
        }
        $round->setTitle($roundJSON['title']);
        $round->setDescription($roundJSON['description']);

        $this->em->persist($round);
        $this->em->flush();

        return;
    }

    public function importObjective($objectiveJSON, Round $round)
    {
        switch ($objectiveJSON['type']) {
            case 'findword':
                $lemmaEnough = $objectiveJSON['lemmaEnough'];
                $form = $objectiveJSON['inflection'];
                $hint = $objectiveJSON['hint'];
                $this->objectiveManager->generateFindWord($lemmaEnough, $form, $hint, $round);

                break;

            case 'combo':
                $length = $objectiveJSON['length'];
                $number = $objectiveJSON['number'];

                $this->objectiveManager->generateCombo($number, $length, $round);
                break;

            case 'constraint':
                $properties = [];
                $properties['numberToFind'] = $objectiveJSON['numberToFind'];

Arnaud Bey's avatar
Arnaud Bey committed
111
112
113
114
115
116
                $properties['features'] = [];
                foreach ($objectiveJSON['features'] as $f) {
                    if ($feature = $this->em->getRepository('LexiconBundle:Feature')->findOneById($f)) {
                        $properties['features'][] = $feature;
                    }
                }
arnaudbey's avatar
add app  
arnaudbey committed
117
118
119
120
121
122
123
124
125

                $this->objectiveManager->generateConstraint($round, $properties);

                break;
        }

        return;
    }
}