refact && game reconnection and restore w/o refresh #3 && bugfix bomb explosion on opponent mines #19
This commit is contained in:
@@ -44,7 +44,7 @@ class MineseekerTopic implements TopicInterface
|
||||
* @param ConnectionInterface $connection
|
||||
* @param Topic $topic
|
||||
* @param WampRequest $request
|
||||
* @return void4
|
||||
* @return void
|
||||
*/
|
||||
public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
|
||||
{
|
||||
@@ -56,8 +56,7 @@ class MineseekerTopic implements TopicInterface
|
||||
if ($topic->count() > 2) {
|
||||
$topic->remove($connection);
|
||||
} else {
|
||||
/** @var $users array Save users to database */
|
||||
$users = $this->saveUserToDb($topic, $userName, $user, $topic->count());
|
||||
$users = $this->controlUsers($topic, $userName, $user);
|
||||
|
||||
$topic->broadcast([
|
||||
'userTopicId' => $connection->resourceId,
|
||||
@@ -129,6 +128,9 @@ class MineseekerTopic implements TopicInterface
|
||||
|
||||
/**
|
||||
* Save Resign event to database
|
||||
*
|
||||
* @param $topic
|
||||
* @param $color
|
||||
*/
|
||||
private function saveResignToDb($topic, $color)
|
||||
{
|
||||
@@ -176,6 +178,36 @@ class MineseekerTopic implements TopicInterface
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Control all users in a channel
|
||||
*
|
||||
* @param $topic
|
||||
* @param $userName
|
||||
* @param $user
|
||||
* @return array
|
||||
*/
|
||||
private function controlUsers($topic, $userName, $user)
|
||||
{
|
||||
$gameAssoc = explode('/', $topic->getId())[2];
|
||||
|
||||
$playedGame = $this->em
|
||||
->getRepository('MineSeekerBundle:PlayedGame')
|
||||
->findOneByGameAssoc($gameAssoc);
|
||||
|
||||
$users = $this->getUserCollection($playedGame);
|
||||
|
||||
/** This checks it is a reconnection */
|
||||
if (
|
||||
(null !== $users['red'] || null !== $users['redAnon']) &&
|
||||
(null !== $users['blue'] || null !== $users['blueAnon'])
|
||||
) {
|
||||
/** @var $users array Save users to database */
|
||||
$users = $this->saveUserToDb($topic, $userName, $user, $topic->count());
|
||||
}
|
||||
|
||||
return $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save user data to database
|
||||
*
|
||||
@@ -231,6 +263,17 @@ class MineseekerTopic implements TopicInterface
|
||||
$this->em->persist($playedGame);
|
||||
$this->em->flush();
|
||||
|
||||
return $this->getUserCollection($playedGame);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user collection from PlayedGame entity
|
||||
*
|
||||
* @param $playedGame
|
||||
* @return array
|
||||
*/
|
||||
private function getUserCollection($playedGame)
|
||||
{
|
||||
return array(
|
||||
'red' => null !== $playedGame->getRed() ? $playedGame->getRed()->getUsername() : '',
|
||||
'blue' => null !== $playedGame->getBlue() ? $playedGame->getBlue()->getUsername() : '',
|
||||
@@ -239,7 +282,9 @@ class MineseekerTopic implements TopicInterface
|
||||
);
|
||||
}
|
||||
|
||||
/** Handle prod MySQL timeout */
|
||||
/**
|
||||
* Handle prod MySQL timeout
|
||||
*/
|
||||
private function reConnect()
|
||||
{
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user