1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chrl\AppBundle\Service; |
4
|
|
|
|
5
|
|
|
use Chrl\AppBundle\Entity\Question; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Chrl\AppBundle\Entity\Game; |
9
|
|
|
use Chrl\AppBundle\Entity\User; |
10
|
|
|
use Chrl\AppBundle\BuktopuhaBotApi; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Game service |
14
|
|
|
* |
15
|
|
|
* @category Symfony |
16
|
|
|
* @package Chrl_Buktopuha |
17
|
|
|
* @author Kirill Kholodilin <[email protected]> |
18
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License |
19
|
|
|
* @link https://take2.ru/ |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class GameService |
23
|
|
|
{ |
24
|
|
|
/** @var EntityManager */ |
25
|
|
|
public $em; |
26
|
|
|
/** @var BuktopuhaBotApi */ |
27
|
|
|
private $botApi; |
28
|
|
|
|
29
|
|
|
public function __construct(BuktopuhaBotApi $telegramBotApi, $config, EntityManagerInterface $entityManager) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->em = $entityManager; |
|
|
|
|
32
|
|
|
$this->botApi = $telegramBotApi; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return Game |
38
|
|
|
*/ |
39
|
|
|
public function findGame(array $message) |
40
|
|
|
{ |
41
|
|
|
$game = $this->em->getRepository('AppBundle:Game')->findOneBy(['chatId'=>$message['chat']['id']]); |
42
|
|
|
if (!$game) { |
43
|
|
|
// create game |
44
|
|
|
$game = new Game(); |
45
|
|
|
$game->status = 0; |
46
|
|
|
$game->chatId = $message['chat']['id']; |
47
|
|
|
$game->title = isset($message['chat']['title']) |
48
|
|
|
? $message['chat']['title'] |
49
|
|
|
: 'No title'; |
50
|
|
|
|
51
|
|
|
$this->em->persist($game); |
52
|
|
|
$this->em->flush(); |
53
|
|
|
} |
54
|
|
|
return $game; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
public function getCurrentUser(array $message) |
59
|
|
|
{ |
60
|
|
|
$user = $this->em->getRepository('AppBundle:User')->findOneBy(['tgId'=>$message['from']['id']]); |
61
|
|
|
if (!$user) { |
62
|
|
|
$user = new User(); |
63
|
|
|
$user->setName( |
64
|
|
|
( |
65
|
|
|
isset($message['from']['first_name']) |
66
|
|
|
? $message['from']['first_name'] |
67
|
|
|
: '' |
68
|
|
|
) |
69
|
|
|
.' '. |
70
|
|
|
( |
71
|
|
|
isset($message['from']['last_name']) |
72
|
|
|
? $message['from']['last_name'] |
73
|
|
|
: '' |
74
|
|
|
) |
75
|
|
|
); |
76
|
|
|
$user->setAlias($message['from']['username']); |
77
|
|
|
$user->setTgId($message['from']['id']); |
78
|
|
|
$user->setGame($this->findGame($message)); |
|
|
|
|
79
|
|
|
$user->setChatId($message['chat']['id']); |
80
|
|
|
$user->setPoints(0); |
81
|
|
|
|
82
|
|
|
$this->em->persist($user); |
83
|
|
|
$this->em->flush(); |
84
|
|
|
} |
85
|
|
|
return $user; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getActiveGames() |
89
|
|
|
{ |
90
|
|
|
$games = $this->em->getRepository('AppBundle:Game')->findBy(['status'=>1]); |
91
|
|
|
return $games; |
92
|
|
|
} |
93
|
|
|
public function getInactiveGames() |
94
|
|
|
{ |
95
|
|
|
$games = $this->em->getRepository('AppBundle:Game')->findBy(['status'=>0]); |
96
|
|
|
return $games; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function checkAnswer($message) |
100
|
|
|
{ |
101
|
|
|
$game = $this->findGame($message); |
102
|
|
|
$user = $this->getCurrentUser($message); |
103
|
|
|
|
104
|
|
|
if ($game->status == 1) { |
105
|
|
|
/** @var Question $question */ |
106
|
|
|
$question = $this->em->getRepository('AppBundle:Question')->find($game->lastQuestion); |
107
|
|
|
|
108
|
|
|
if (!$question) { |
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (mb_strtoupper($question->a1, 'UTF-8') == mb_strtoupper($message['text'], 'UTF-8')) { |
113
|
|
|
// Correct answer! |
114
|
|
|
|
115
|
|
|
$user->setPoints($user->getPoints()+$question->price); |
116
|
|
|
$this->em->persist($user); |
117
|
|
|
|
118
|
|
|
$this->botApi->sendMessage( |
119
|
|
|
$game->chatId, |
120
|
|
|
'Correct! @'.$user->getAlias().' gets *'. |
121
|
|
|
$question->price.'* and now has *'.$user->getPoints().'* points!', |
122
|
|
|
'markdown', |
123
|
|
|
false, |
124
|
|
|
$message['message_id'] |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$question->correct++; |
128
|
|
|
$this->em->persist($question); |
129
|
|
|
|
130
|
|
|
$question = $this->getRandomQuestion(); |
131
|
|
|
|
132
|
|
|
$game->lastQuestion = $question->getId(); |
133
|
|
|
$game->lastQuestionTime = new \DateTime('now'); |
134
|
|
|
$game->incorrectTries = 0; |
135
|
|
|
|
136
|
|
|
$this->askQuestion($game, $question); |
137
|
|
|
} else { |
138
|
|
|
// Incorrect answer |
139
|
|
|
$game->incorrectTries++; |
140
|
|
|
$this->botApi->sendMessage( |
141
|
|
|
$game->chatId, |
142
|
|
|
'Wrong, @'.$user->getAlias().'. Correct answer: *'.$question->a1.'*', |
143
|
|
|
'markdown' |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->em->persist($game); |
148
|
|
|
$this->em->flush(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function askQuestion(Game $game, Question $question) |
153
|
|
|
{ |
154
|
|
|
$this->botApi->sendMessage($game->chatId, '*[question]* '.$question->text, 'markdown'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return Question |
159
|
|
|
* @throws \Doctrine\ORM\NoResultException |
160
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
161
|
|
|
*/ |
162
|
|
|
public function getRandomQuestion() |
163
|
|
|
{ |
164
|
|
|
$count = $this->em->createQueryBuilder() |
165
|
|
|
->select('COUNT(u)')->from('AppBundle:Question', 'u') |
166
|
|
|
->getQuery() |
167
|
|
|
->getSingleScalarResult(); |
168
|
|
|
|
169
|
|
|
return $this->em->createQuery('SELECT c FROM AppBundle:Question c ORDER BY c.id ASC') |
170
|
|
|
->setFirstResult(rand(0, $count - 1)) |
171
|
|
|
->setMaxResults(1) |
172
|
|
|
->getSingleResult(); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.