|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chrl\AppBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Chrl\AppBundle\Entity\Game; |
|
7
|
|
|
use Chrl\AppBundle\Entity\User; |
|
8
|
|
|
use Chrl\AppBundle\BuktopuhaBotApi; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Game service |
|
12
|
|
|
* |
|
13
|
|
|
* @category Symfony |
|
14
|
|
|
* @package Chrl_Buktopuha |
|
15
|
|
|
* @author Kirill Kholodilin <[email protected]> |
|
16
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
|
17
|
|
|
* @link https://take2.ru/ |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
class GameService |
|
21
|
|
|
{ |
|
22
|
|
|
public $em; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(BuktopuhaBotApi $telegramBotApi, $config, EntityManagerInterface $entityManager) |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
$this->em = $entityManager; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
public function findGame(array $message) |
|
31
|
|
|
{ |
|
32
|
|
|
$game = $this->em->getRepository('AppBundle:Game')->findOneBy(['chatId'=>$message['chat']['id']]); |
|
33
|
|
|
if (!$game) { |
|
34
|
|
|
// create game |
|
35
|
|
|
$game = new Game(); |
|
36
|
|
|
$game->status = 0; |
|
37
|
|
|
$game->chatId = $message['chat']['id']; |
|
38
|
|
|
$game->title = isset($message['chat']['title']) |
|
39
|
|
|
? $message['chat']['title'] |
|
40
|
|
|
: 'No title'; |
|
41
|
|
|
|
|
42
|
|
|
$this->em->persist($game); |
|
43
|
|
|
$this->em->flush(); |
|
44
|
|
|
} |
|
45
|
|
|
return $game; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
public function getCurrentUser(array $message) |
|
50
|
|
|
{ |
|
51
|
|
|
$user = $this->em->getRepository('AppBundle:User')->findOneBy(['tgId'=>$message['from']['id']]); |
|
52
|
|
|
if (!$user) { |
|
53
|
|
|
$user = new User(); |
|
54
|
|
|
$user->setName($message['from']['first_name'].' '.$message['from']['last_name']); |
|
55
|
|
|
$user->setAlias($message['from']['username']); |
|
56
|
|
|
$user->setTgId($message['from']['id']); |
|
57
|
|
|
$user->setGame($this->findGame($message)); |
|
58
|
|
|
$user->setChatId($message['chat']['id']); |
|
59
|
|
|
$user->setPoints(0); |
|
60
|
|
|
|
|
61
|
|
|
$this->em->persist($user); |
|
62
|
|
|
$this->em->flush(); |
|
63
|
|
|
} |
|
64
|
|
|
return $user; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getActiveGames() |
|
68
|
|
|
{ |
|
69
|
|
|
$games = $this->em->getRepository('AppBundle:Game')->findBy(['status'=>1]); |
|
70
|
|
|
return $games; |
|
71
|
|
|
} |
|
72
|
|
|
public function getInactiveGames() |
|
73
|
|
|
{ |
|
74
|
|
|
$games = $this->em->getRepository('AppBundle:Game')->findBy(['status'=>0]); |
|
75
|
|
|
return $games; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.