Private
Public Access
1
0

new: usr: add mineseeker game to the symfony 4 project #3

This commit is contained in:
2019-10-27 13:35:33 +01:00
parent 6caf340302
commit 3bbc96c76c
160 changed files with 49951 additions and 602 deletions

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Application\Sonata\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* This file has been generated by the SonataEasyExtendsBundle.
*
* @link https://sonata-project.org/easy-extends
*
* References:
* @link http://symfony.com/doc/current/book/bundles.html
*/
class ApplicationSonataUserBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function getParent()
{
return 'SonataUserBundle';
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Application\Sonata\UserBundle\Document;
use Sonata\UserBundle\Document\BaseGroup as BaseGroup;
/**
* This file has been generated by the SonataEasyExtendsBundle.
*
* @link https://sonata-project.org/easy-extends
*
* References:
* @link http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/reference/working-with-objects.html
*/
class Group extends BaseGroup
{
/**
* @var int $id
*/
protected $id;
/**
* Get id.
*
* @return int $id
*/
public function getId()
{
return $this->id;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Application\Sonata\UserBundle\Document;
use Sonata\UserBundle\Document\BaseUser as BaseUser;
/**
* This file has been generated by the SonataEasyExtendsBundle.
*
* @link https://sonata-project.org/easy-extends
*
* References:
* @link http://www.doctrine-project.org/docs/mongodb_odm/1.0/en/reference/working-with-objects.html
*/
class User extends BaseUser
{
/**
* @var int $id
*/
protected $id;
/**
* Get id.
*
* @return int $id
*/
public function getId()
{
return $this->id;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Application\Sonata\UserBundle\Entity;
use Sonata\UserBundle\Entity\BaseGroup as BaseGroup;
/**
* This file has been generated by the SonataEasyExtendsBundle.
*
* @link https://sonata-project.org/easy-extends
*
* References:
* @link http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
*/
class Group extends BaseGroup
{
/**
* @var int $id
*/
protected $id;
/**
* Get id.
*
* @return int $id
*/
public function getId()
{
return $this->id;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Application\Sonata\UserBundle\Entity;
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
/**
* This file has been generated by the SonataEasyExtendsBundle.
*
* @link https://sonata-project.org/easy-extends
*
* References:
* @link http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
*/
class User extends BaseUser
{
/**
* @var int $id
*/
protected $id;
/**
* Get id.
*
* @return int $id
*/
public function getId()
{
return $this->id;
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
<document name="App\Application\Sonata\UserBundle\Document\Group" collection="fos_user_group">
<field fieldName="id" id="true" strategy="INCREMENT" />
</document>
</doctrine-mongo-mapping>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="App\Application\Sonata\UserBundle\Entity\Group" table="fos_user_group">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
</entity>
</doctrine-mapping>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
<document name="App\Application\Sonata\UserBundle\Document\User" collection="fos_user_user" customId="true">
<field fieldName="id" id="true" strategy="INCREMENT" />
</document>
</doctrine-mongo-mapping>

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="App\Application\Sonata\UserBundle\Entity\User" table="fos_user_user">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
</entity>
</doctrine-mapping>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<serializer>
<!-- This file has been generated by the SonataEasyExtendsBundle: https://sonata-project.org/bundles/easy-extends -->
<class name="App\Application\Sonata\UserBundle\Document\Group" exclusion-policy="all" xml-root-name="_group">
<property xml-attribute-map="true" name="id" type="integer" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search"/>
</class>
</serializer>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<serializer>
<!-- This file has been generated by the SonataEasyExtendsBundle: https://sonata-project.org/bundles/easy-extends -->
<class name="App\Application\Sonata\UserBundle\Document\User" exclusion-policy="all" xml-root-name="_user">
<property xml-attribute-map="true" name="id" type="integer" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search"/>
</class>
</serializer>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<serializer>
<!-- This file has been generated by the SonataEasyExtendsBundle: https://sonata-project.org/bundles/easy-extends -->
<class name="App\Application\Sonata\UserBundle\Entity\Group" exclusion-policy="all" xml-root-name="_group">
<property xml-attribute-map="true" name="id" type="integer" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search"/>
</class>
</serializer>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<serializer>
<!-- This file has been generated by the SonataEasyExtendsBundle: https://sonata-project.org/bundles/easy-extends -->
<class name="App\Application\Sonata\UserBundle\Entity\User" exclusion-policy="all" xml-root-name="_user">
<property xml-attribute-map="true" name="id" type="integer" expose="true" since-version="1.0" groups="sonata_api_read,sonata_api_write,sonata_search"/>
</class>
</serializer>

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class GameController extends Controller
{
public function index()
{
return $this->render('Game/index.html.twig');
}
public function play(Request $request)
{
return $this->render('Game/play.html.twig', array(
'env' => $this->container->getParameter('kernel.environment'),
'ssl' => $request->isSecure() ? 'true' : 'false'
));
}
public function privacy()
{
return $this->render('Official/privacy.html.twig');
}
public function terms()
{
return $this->render('Official/terms.html.twig');
}
public function contact()
{
return $this->render('Official/contact.html.twig');
}
public function landing()
{
return $this->render('Official/landing.html.twig');
}
}

163
src/Entity/Gamer.php Normal file
View File

@@ -0,0 +1,163 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\GamerRepository")
*/
class Gamer
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string|null
*
* @ORM\Column(name="user_name", type="string", length=100, nullable=false)
*/
private $userName;
/**
* @var string|null
*
* @ORM\Column(name="ip", type="string", length=20, nullable=true)
*/
private $ip;
/**
* TODO
* $ip='0.0.0.0';
* $ip=$_SERVER['REMOTE_ADDR'];
* $clientDetails = json_decode(file_get_contents("http://ipinfo.io/$ip/json"));
* echo "You're logged in from: <b>" . $clientDetails->country . "</b>";
*
* function GetIP()
* {
* if ( getenv("HTTP_CLIENT_IP") ) {
* $ip = getenv("HTTP_CLIENT_IP");
* } elseif ( getenv("HTTP_X_FORWARDED_FOR") ) {
* $ip = getenv("HTTP_X_FORWARDED_FOR");
* if ( strstr($ip, ',') ) {
* $tmp = explode(',', $ip);
* $ip = trim($tmp[0]);
* }
* } else {
* $ip = getenv("REMOTE_ADDR");
* }
* return $ip;
* }
*/
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=100, nullable=true)
*/
private $country;
/**
* @var string
* @see http://symfony.com/doc/current/components/http_foundation.html
*
* @ORM\Column(name="user_agent", type="string", length=255, nullable=true)
*/
private $userAgent;
/**
* @var \DateTime
*
* @ORM\Column(name="conn_timestamp", type="datetime", nullable=false)
*/
private $connTimestamp;
public function getId(): ?int
{
return $this->id;
}
/**
* @return string|null
*/
public function getUserName(): ?string
{
return $this->userName;
}
/**
* @param string|null $userName
*/
public function setUserName(?string $userName): void
{
$this->userName = $userName;
}
/**
* @return string|null
*/
public function getIp(): ?string
{
return $this->ip;
}
/**
* @param string|null $ip
*/
public function setIp(?string $ip): void
{
$this->ip = $ip;
}
/**
* @return string
*/
public function getCountry(): string
{
return $this->country;
}
/**
* @param string $country
*/
public function setCountry(string $country): void
{
$this->country = $country;
}
/**
* @return string
*/
public function getUserAgent(): string
{
return $this->userAgent;
}
/**
* @param string $userAgent
*/
public function setUserAgent(string $userAgent): void
{
$this->userAgent = $userAgent;
}
/**
* @return \DateTime
*/
public function getConnTimestamp(): \DateTime
{
return $this->connTimestamp;
}
/**
* @param \DateTime $connTimestamp
*/
public function setConnTimestamp(\DateTime $connTimestamp): void
{
$this->connTimestamp = $connTimestamp;
}
}

70
src/Entity/Grid.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\GridRepository")
*/
class Grid
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var PlayedGame|null
*
* @ORM\OneToOne(targetEntity="App\Entity\PlayedGame", inversedBy="grid", cascade={"persist"})
*/
private $playedGame;
/**
* @var GridRow|null
*
* @ORM\OneToMany(targetEntity="App\Entity\GridRow", mappedBy="grid", cascade={"persist"})
* @ORM\JoinColumn(name="grid_row", referencedColumnName="id", onDelete="CASCADE")
*/
private $gridRow;
public function getId(): ?int
{
return $this->id;
}
/**
* @return PlayedGame|null
*/
public function getPlayedGame()
{
return $this->playedGame;
}
/**
* @param PlayedGame|null $playedGame
*/
public function setPlayedGame( $playedGame): void
{
$this->playedGame = $playedGame;
}
/**
* @return GridRow|null
*/
public function getGridRow()
{
return $this->gridRow;
}
/**
* @param GridRow|null $gridRow
*/
public function setGridRow( $gridRow): void
{
$this->gridRow = $gridRow;
}
}

70
src/Entity/GridRow.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\GridRowRepository")
*/
class GridRow
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var Grid|null
*
* @ORM\ManyToOne(targetEntity="App\Entity\Grid", inversedBy="gridRow", cascade={"persist"})
* @ORM\JoinColumn(name="grid", referencedColumnName="id", onDelete="CASCADE")
*/
private $grid;
/**
* @var array|null
*
* @ORM\Column(name="grid_col", type="json_array", nullable=false)
*/
private $gridCol;
public function getId(): ?int
{
return $this->id;
}
/**
* @return Grid|null
*/
public function getGrid()
{
return $this->grid;
}
/**
* @param Grid|null $grid
*/
public function setGrid( $grid): void
{
$this->grid = $grid;
}
/**
* @return array|null
*/
public function getGridCol()
{
return $this->gridCol;
}
/**
* @param array|null $gridCol
*/
public function setGridCol($gridCol): void
{
$this->gridCol = $gridCol;
}
}

351
src/Entity/PlayedGame.php Normal file
View File

@@ -0,0 +1,351 @@
<?php
namespace App\Entity;
use App\Application\Sonata\UserBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PlayedGameRepository")
*/
class PlayedGame
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string|null
*
* @ORM\Column(name="game_assoc", type="string", length=50, nullable=false)
*/
private $gameAssoc;
/**
* @var Grid|null
*
* @ORM\OneToOne(targetEntity="App\Entity\Grid", mappedBy="playedGame", cascade={"persist"})
*/
private $grid;
/**
* @var User|null
*
* @ORM\ManyToOne(targetEntity="App\Application\Sonata\UserBundle\Entity\User")
* @ORM\JoinColumn(name="red_id", referencedColumnName="id", nullable=true)
*/
private $red;
/**
* @var Gamer|null
*
* @ORM\ManyToOne(targetEntity="App\Entity\Gamer")
* @ORM\JoinColumn(name="red_anon", referencedColumnName="id", nullable=true)
*/
private $redAnon;
/**
* @var User|null
*
* @ORM\ManyToOne(targetEntity="App\Application\Sonata\UserBundle\Entity\User")
* @ORM\JoinColumn(name="blue_id", referencedColumnName="id", nullable=true)
*/
private $blue;
/**
* @var Gamer|null
*
* @ORM\ManyToOne(targetEntity="App\Entity\Gamer")
* @ORM\JoinColumn(name="blue_anon", referencedColumnName="id", nullable=true)
*/
private $blueAnon;
/**
* @var int|null
*
* @ORM\Column(name="red_points", type="integer", length=5, nullable=true)
*/
private $redPoints;
/**
* @var int|null
*
* @ORM\Column(name="blue_points", type="integer", length=5, nullable=true)
*/
private $bluePoints;
/**
* @var boolean|null
*
* @ORM\Column(name="red_exploded_bomb", type="boolean", nullable=true)
*/
private $redExplodedBomb;
/**
* @var boolean|null
*
* @ORM\Column(name="blue_exploded_bomb", type="boolean", nullable=true)
*/
private $blueExplodedBomb;
/**
* @var string|null
*
* @ORM\Column(name="resign", type="string", length=7, nullable=true)
*/
private $resign;
/**
* @var \DateTime|null
*
* @ORM\Column(name="created", type="datetime", nullable=true)
*/
private $created;
/**
* @var \DateTime|null
*
* @ORM\Column(name="updated", type="datetime", nullable=true)
*/
private $updated;
/**
* @var Step|null
*
* @ORM\OneToMany(targetEntity="App\Entity\Step", mappedBy="playedGame")
* @ORM\JoinColumn(name="step", referencedColumnName="id", nullable=true)
*/
private $step;
public function getId(): ?int
{
return $this->id;
}
/**
* @return string|null
*/
public function getGameAssoc(): ?string
{
return $this->gameAssoc;
}
/**
* @param string|null $gameAssoc
*/
public function setGameAssoc(?string $gameAssoc): void
{
$this->gameAssoc = $gameAssoc;
}
/**
* @return Grid|null
*/
public function getGrid()
{
return $this->grid;
}
/**
* @param Grid|null $grid
*/
public function setGrid( $grid): void
{
$this->grid = $grid;
}
/**
* @return User|null
*/
public function getRed()
{
return $this->red;
}
/**
* @param User|null $red
*/
public function setRed( $red): void
{
$this->red = $red;
}
/**
* @return Gamer|null
*/
public function getRedAnon()
{
return $this->redAnon;
}
/**
* @param Gamer|null $redAnon
*/
public function setRedAnon( $redAnon): void
{
$this->redAnon = $redAnon;
}
/**
* @return User|null
*/
public function getBlue()
{
return $this->blue;
}
/**
* @param User|null $blue
*/
public function setBlue( $blue): void
{
$this->blue = $blue;
}
/**
* @return Gamer|null
*/
public function getBlueAnon()
{
return $this->blueAnon;
}
/**
* @param Gamer|null $blueAnon
*/
public function setBlueAnon( $blueAnon): void
{
$this->blueAnon = $blueAnon;
}
/**
* @return int|null
*/
public function getRedPoints(): ?int
{
return $this->redPoints;
}
/**
* @param int|null $redPoints
*/
public function setRedPoints(?int $redPoints): void
{
$this->redPoints = $redPoints;
}
/**
* @return int|null
*/
public function getBluePoints(): ?int
{
return $this->bluePoints;
}
/**
* @param int|null $bluePoints
*/
public function setBluePoints(?int $bluePoints): void
{
$this->bluePoints = $bluePoints;
}
/**
* @return bool|null
*/
public function getRedExplodedBomb(): ?bool
{
return $this->redExplodedBomb;
}
/**
* @param bool|null $redExplodedBomb
*/
public function setRedExplodedBomb(?bool $redExplodedBomb): void
{
$this->redExplodedBomb = $redExplodedBomb;
}
/**
* @return bool|null
*/
public function getBlueExplodedBomb(): ?bool
{
return $this->blueExplodedBomb;
}
/**
* @param bool|null $blueExplodedBomb
*/
public function setBlueExplodedBomb(?bool $blueExplodedBomb): void
{
$this->blueExplodedBomb = $blueExplodedBomb;
}
/**
* @return string|null
*/
public function getResign(): ?string
{
return $this->resign;
}
/**
* @param string|null $resign
*/
public function setResign(?string $resign): void
{
$this->resign = $resign;
}
/**
* @return \DateTime|null
*/
public function getCreated(): ?\DateTime
{
return $this->created;
}
/**
* @param \DateTime|null $created
*/
public function setCreated(?\DateTime $created): void
{
$this->created = $created;
}
/**
* @return \DateTime|null
*/
public function getUpdated(): ?\DateTime
{
return $this->updated;
}
/**
* @param \DateTime|null $updated
*/
public function setUpdated(?\DateTime $updated): void
{
$this->updated = $updated;
}
/**
* @return Step|null
*/
public function getStep()
{
return $this->step;
}
/**
* @param Step|null $step
*/
public function setStep( $step): void
{
$this->step = $step;
}
}

138
src/Entity/Step.php Normal file
View File

@@ -0,0 +1,138 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\StepRepository")
*/
class Step
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var int|null
*
* @ORM\Column(name="row", length=3, type="integer", nullable=false)
*/
private $row;
/**
* @var int|null
*
* @ORM\Column(name="col", length=3, type="integer", nullable=false)
*/
private $col;
/**
* @var boolean|null
*
* @ORM\Column(name="wbomb", type="boolean", nullable=true)
*/
private $wBomb;
/**
* @var PlayedGame|null
*
* @ORM\ManyToOne(targetEntity="App\Entity\PlayedGame", inversedBy="step")
*/
private $playedGame;
/**
* @var \DateTime|null
*
* @ORM\Column(name="created", type="datetime", nullable=true)
*/
private $created;
public function getId(): ?int
{
return $this->id;
}
/**
* @return int|null
*/
public function getRow(): ?int
{
return $this->row;
}
/**
* @param int|null $row
*/
public function setRow(?int $row): void
{
$this->row = $row;
}
/**
* @return int|null
*/
public function getCol(): ?int
{
return $this->col;
}
/**
* @param int|null $col
*/
public function setCol(?int $col): void
{
$this->col = $col;
}
/**
* @return bool|null
*/
public function getWBomb(): ?bool
{
return $this->wBomb;
}
/**
* @param bool|null $wBomb
*/
public function setWBomb(?bool $wBomb): void
{
$this->wBomb = $wBomb;
}
/**
* @return PlayedGame|null
*/
public function getPlayedGame(): ?PlayedGame
{
return $this->playedGame;
}
/**
* @param PlayedGame|null $playedGame
*/
public function setPlayedGame(?PlayedGame $playedGame): void
{
$this->playedGame = $playedGame;
}
/**
* @return \DateTime|null
*/
public function getCreated(): ?\DateTime
{
return $this->created;
}
/**
* @param \DateTime|null $created
*/
public function setCreated(?\DateTime $created): void
{
$this->created = $created;
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace App\EventListener;
use Gos\Bundle\WebSocketBundle\Event\ClientEvent;
use Gos\Bundle\WebSocketBundle\Event\ClientErrorEvent;
use Gos\Bundle\WebSocketBundle\Event\ServerEvent;
use Gos\Bundle\WebSocketBundle\Event\ClientRejectedEvent;
class MineseekerClientEventListener
{
/**
* Called whenever a client connects
*
* @param ClientEvent $event
*/
public function onClientConnect(ClientEvent $event)
{
$conn = $event->getConnection();
echo $conn->resourceId . ' connected' . PHP_EOL;
}
/**
* Called whenever a client disconnects
*
* @param ClientEvent $event
*/
public function onClientDisconnect(ClientEvent $event)
{
$conn = $event->getConnection();
echo $conn->resourceId . ' disconnected' . PHP_EOL;
}
/**
* Called whenever a client errors
*
* @param ClientErrorEvent $event
*/
public function onClientError(ClientErrorEvent $event)
{
$conn = $event->getConnection();
$e = $event->getException();
echo 'connection error occurred: ' . $e->getMessage() . PHP_EOL;
}
/**
* Called whenever server start
*
* @param ServerEvent $event
*/
public function onServerStart(ServerEvent $event)
{
$event = $event->getEventLoop();
echo 'Server was successfully started !' . PHP_EOL;
}
/**
* Called whenever client is rejected by application
*
* @param ClientRejectedEvent $event
*/
public function onClientRejected(ClientRejectedEvent $event)
{
$origin = $event->getOrigin;
echo 'connection rejected from ' . $origin . PHP_EOL;
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Periodic;
use Gos\Bundle\WebSocketBundle\Periodic\PdoPeriodicPing;
use Gos\Bundle\WebSocketBundle\Periodic\PeriodicInterface;
class MinePeriodic implements PeriodicInterface
{
/** @var PdoPeriodicPing */
private $ping;
public function __construct(PdoPeriodicPing $ping)
{
$this->ping = $ping;
}
/**
* This function is executed every 5 seconds.
*
* For more advanced functionality, try injecting a Topic Service to perform actions on your connections every x seconds.
*/
public function tick()
{
$this->ping->tick();
}
/**
* {@inheritdoc}
*/
public function getTimeout()
{
return 300;
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Gamer;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* @method Gamer|null find($id, $lockMode = null, $lockVersion = null)
* @method Gamer|null findOneBy(array $criteria, array $orderBy = null)
* @method Gamer[] findAll()
* @method Gamer[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class GamerRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Gamer::class);
}
// /**
// * @return Gamer[] Returns an array of Gamer objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('g')
->andWhere('g.exampleField = :val')
->setParameter('val', $value)
->orderBy('g.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Gamer
{
return $this->createQueryBuilder('g')
->andWhere('g.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Grid;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* @method Grid|null find($id, $lockMode = null, $lockVersion = null)
* @method Grid|null findOneBy(array $criteria, array $orderBy = null)
* @method Grid[] findAll()
* @method Grid[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class GridRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Grid::class);
}
// /**
// * @return Grid[] Returns an array of Grid objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('g')
->andWhere('g.exampleField = :val')
->setParameter('val', $value)
->orderBy('g.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Grid
{
return $this->createQueryBuilder('g')
->andWhere('g.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\GridRow;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* @method GridRow|null find($id, $lockMode = null, $lockVersion = null)
* @method GridRow|null findOneBy(array $criteria, array $orderBy = null)
* @method GridRow[] findAll()
* @method GridRow[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class GridRowRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, GridRow::class);
}
// /**
// * @return GridRow[] Returns an array of GridRow objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('g')
->andWhere('g.exampleField = :val')
->setParameter('val', $value)
->orderBy('g.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?GridRow
{
return $this->createQueryBuilder('g')
->andWhere('g.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\PlayedGame;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* @method PlayedGame|null find($id, $lockMode = null, $lockVersion = null)
* @method PlayedGame|null findOneBy(array $criteria, array $orderBy = null)
* @method PlayedGame[] findAll()
* @method PlayedGame[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PlayedGameRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, PlayedGame::class);
}
// /**
// * @return PlayedGame[] Returns an array of PlayedGame objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->orderBy('p.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?PlayedGame
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Step;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
/**
* @method Step|null find($id, $lockMode = null, $lockVersion = null)
* @method Step|null findOneBy(array $criteria, array $orderBy = null)
* @method Step[] findAll()
* @method Step[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class StepRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Step::class);
}
// /**
// * @return Step[] Returns an array of Step objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('s')
->andWhere('s.exampleField = :val')
->setParameter('val', $value)
->orderBy('s.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Step
{
return $this->createQueryBuilder('s')
->andWhere('s.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

180
src/Rpc/MineseekerRpc.php Normal file
View File

@@ -0,0 +1,180 @@
<?php
namespace App\Rpc;
use App\Entity\Grid;
use App\Entity\GridRow;
use App\Entity\PlayedGame;
use Doctrine\DBAL\Driver\PDOException;
use Doctrine\ORM\EntityManager;
use Psr\Log\LoggerInterface;
use Ratchet\ConnectionInterface;
use Gos\Bundle\WebSocketBundle\RPC\RpcInterface;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
class MineseekerRpc implements RpcInterface
{
/** @var EntityManager $em */
protected $em;
protected $grid;
private $logger;
/**
* MineseekerRpc constructor.
* @param EntityManager $entityManager
*/
public function __construct(EntityManager $entityManager, LoggerInterface $logger)
{
$this->em = $entityManager;
$this->logger = $logger;
}
/**
* Name of RPC, use for pubsub router (see step3)
*
* @return string
*/
public function getName()
{
return 'mineseeker.rpc';
}
/**
* @param ConnectionInterface $connection
* @param WampRequest $request
* @param array $params
* @return boolean
*/
public function startGame(ConnectionInterface $connection, WampRequest $request, array $params)
{
return $this->saveGrid($params);
}
/**
* @param ConnectionInterface $connection
* @param WampRequest $request
* @param array $params
* @return string Json string for frontend w/ numbering consideration. (=> a number is not string)
*/
public function connectGame(ConnectionInterface $connection, WampRequest $request, array $params)
{
$grid = $this->getGrid($params);
$users = null !== $grid ? $this->getUsers($params) : null;
return base64_encode(json_encode(
array(
'grid' => $grid,
'users' => $users
)
));
}
/**
* @param $gameAssoc
* @return array
*/
private function getGrid($gameAssoc)
{
$this->reConnect();
$getsee = array();
$this->em->clear();
$grid = $this->em
->getRepository(PlayedGame::class)
->findOneByGameAssoc($gameAssoc);
if (null !== $grid) {
foreach ($grid->getGrid()->getGridRow()->toArray() as $row) {
$getsee[] = $row->getGridCol();
}
return $getsee;
}
return null;
}
/**
* @param $gameAssoc
* @return array
*/
private function getUsers($gameAssoc)
{
$this->reConnect();
return $this->getUserCollection(
$this->em
->getRepository(PlayedGame::class)
->findOneByGameAssoc($gameAssoc)
);
}
/**
* 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() : '',
'redAnon' => null !== $playedGame->getRedAnon() ? $playedGame->getRedAnon()->getUserName() : '',
'blueAnon' => null !== $playedGame->getBlueAnon() ? $playedGame->getBlueAnon()->getUserName() : ''
);
}
/**
* @param $data
* @return boolean
*/
private function saveGrid($data)
{
// $this->reConnect();
$playedGame = new PlayedGame();
$grid = new Grid();
foreach (json_decode(base64_decode($data[0])) as $row) {
$gridRow = new GridRow();
$gridRow->setGridCol($row);
/** Save Row */
$gridRow->setGrid($grid);
$this->em->persist($gridRow);
}
/** Save Grid */
$grid->setPlayedGame($playedGame);
$this->em->persist($grid);
/** Save PlayedGame */
$playedGame->setGameAssoc($data[1]);
$playedGame->setGrid($grid);
$playedGame->setCreated(new \DateTime());
$playedGame->setUpdated(new \DateTime());
$this->em->persist($playedGame);
$this->em->flush();
return true;
}
/** Handle prod MySQL timeout */
private function reConnect()
{
// try {
$connection = $this->em->getConnection();
if (false === $connection->ping()) {
$connection->close();
$connection->connect();
}
// } catch(PDOException $ex) {
// $this->logger->error($ex->getMessage());
// }
}
}

View File

@@ -0,0 +1,321 @@
<?php
namespace App\Topic;
use App\Application\Sonata\UserBundle\Entity\User;
use App\Entity\Gamer;
use App\Entity\PlayedGame;
use App\Entity\Step;
use Doctrine\DBAL\Driver\PDOException;
use Doctrine\ORM\EntityManager;
use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
use Psr\Log\LoggerInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\Topic;
use Symfony\Component\HttpFoundation\RequestStack;
class MineseekerTopic implements TopicInterface
{
/** @var ClientManipulatorInterface $clientManipulator */
protected $clientManipulator;
/** @var EntityManager $em */
protected $em;
/** @var RequestStack $requestStack */
protected $requestStack;
/** @var LoggerInterface $logger */
protected $logger;
/**
* MineseekerTopic constructor.
*
* @param $clientManipulator ClientManipulatorInterface
* @param EntityManager $entityManager
* @param RequestStack $requestStack
* @param LoggerInterface $logger
*/
public function __construct(
ClientManipulatorInterface $clientManipulator,
EntityManager $entityManager,
RequestStack $requestStack,
LoggerInterface $logger
)
{
$this->clientManipulator = $clientManipulator;
$this->em = $entityManager;
$this->requestStack = $requestStack;
$this->logger = $logger;
}
/**
* This will receive any Subscription requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @return void
*/
public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
{
/** this will broadcast the message to ALL subscribers of this topic. */
$user = $this->clientManipulator->getClient($connection);
$userName = is_string($user) ? $user : $user->getUsername();
/** if more user wants to connect than 2 to one channel */
if ($topic->count() > 2) {
$topic->remove($connection);
} else {
$users = $this->controlUsers($topic, $userName, $user);
$topic->broadcast([
'userTopicId' => $connection->resourceId,
'channel' => $topic->getId(),
'user' => $userName,
'userCnt' => $topic->count(),
'users' => $users
]);
}
}
/**
* This will receive any UnSubscription requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @return void
*/
public function onUnSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
{
/** this will broadcast the message to ALL subscribers of this topic. */
$topic->broadcast(['msg' => $connection->resourceId . " has left " . $topic->getId()]);
}
/**
* This will receive any Publish requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @param $event
* @param array $exclude
* @param array $eligible
* @return mixed|void
* @internal param Topic $Topic
* @internal param array $eligibles
*/
public function onPublish(ConnectionInterface $connection, Topic $topic, WampRequest $request, $event, array $exclude, array $eligible)
{
$user = $this->clientManipulator->getClient($connection);
$userName = is_string($user) ? $user : $user->getUsername();
/** Save every step by user to db */
if (null === $event['resign']) {
$this->saveStepToDb($topic, $event);
} else {
$this->saveResignToDb($topic, $event['resign']);
}
$topic->broadcast([
'userTopicId' => $connection->resourceId,
'channel' => $topic->getId(),
'user' => $userName,
'userCnt' => $topic->count(),
'data' => $event
]);
}
/**
* Like RPC is will use to prefix the channel
*
* @return string
*/
public function getName()
{
return 'mineseeker.topic';
}
/**
* Save Resign event to database
*
* @param $topic
* @param $color
*/
private function saveResignToDb($topic, $color)
{
$this->reConnect();
$gameAssoc = explode('/', $topic->getId())[2];
$playedGame = $this->em
->getRepository(PlayedGame::class)
->findOneByGameAssoc($gameAssoc);
$playedGame->setResign($color);
$this->em->persist($playedGame);
$this->em->flush();
}
/**
* Save steps and point information to database
*
* @param $topic
* @param $event
*/
private function saveStepToDb($topic, $event)
{
$this->reConnect();
$gameAssoc = explode('/', $topic->getId())[2];
/** @var PlayedGame $playedGame */
$playedGame = $this->em
->getRepository(PlayedGame::class)
->findOneByGameAssoc($gameAssoc);
$step = new Step();
$step->setRow($event['coords'][0]);
$step->setCol($event['coords'][1]);
$step->setWBomb($event['bomb']);
$step->setPlayedGame($playedGame);
$step->setCreated(new \DateTime());
$this->em->persist($step);
$playedGame->setBluePoints($event['bluePoints']);
$playedGame->setRedPoints($event['redPoints']);
$playedGame->setBlueExplodedBomb($event['blueExplodedBomb'] ? true : null);
$playedGame->setRedExplodedBomb($event['redExplodedBomb'] ? true : null);
$playedGame->setUpdated(new \DateTime());
$this->em->persist($playedGame);
$this->em->flush();
}
/**
* Control all users in a channel
*
* @param $topic
* @param $userName
* @param $user
* @return array
*/
private function controlUsers($topic, $userName, $user)
{
$this->reConnect();
$gameAssoc = explode('/', $topic->getId())[2];
$playedGame = $this->em
->getRepository(PlayedGame::class)
->findOneByGameAssoc($gameAssoc);
/** @var $users {array} */
$users = $this->getUserCollection($playedGame);
$red = "" !== $users['red'] || "" !== $users['redAnon'] ? 1 : 0;
$blue = "" !== $users['blue'] || "" !== $users['blueAnon'] ? 1 : 0;
$one = $topic->count() === 1;
$two = $topic->count() === 2;
/** This checks it is a reconnection */
if (($one && ($red + $blue === 0)) || ($two && ($red + $blue === 1))) {
/** @var $users {array} w/ save users to database */
$users = $this->saveUserToDb($topic, $userName, $user, $topic->count());
}
return $users;
}
/**
* Save user data to database
*
* @param $topic
* @param $userName
* @param $user
* @param $count
* @return array
*/
private function saveUserToDb($topic, $userName, $user, $count)
{
$this->reConnect();
$gameAssoc = explode('/', $topic->getId())[2];
$playedGame = $this->em
->getRepository(PlayedGame::class)
->findOneByGameAssoc($gameAssoc);
/** the user is not anonym */
if (!is_string($user)) {
$FOSUser = $this->em
->getRepository(User::class)
->findOneByUsername($userName);
if ($count == 1) {
/** @var $random {integer} Active player: red: 0, blue: 1 */
$random = rand(0, 1);
!$random ? $playedGame->setRed($FOSUser) : $playedGame->setBlue($FOSUser);
} else {
null === $playedGame->getRed() && null === $playedGame->getRedAnon()
? $playedGame->setRed($FOSUser)
: $playedGame->setBlue($FOSUser);
}
} else {
// $request = $this->requestStack->getCurrentRequest(); // TODO nem megy...
$anon = new Gamer();
$anon->setUsername($userName);
$anon->setConnTimestamp(new \DateTime());
$this->em->persist($anon);
if ($count == 1) {
/** @var $random {integer} Active player: red: 0, blue: 1 */
$random = rand(0, 1);
!$random ? $playedGame->setRedAnon($anon) : $playedGame->setBlueAnon($anon);
} else {
null === $playedGame->getRed() && null === $playedGame->getRedAnon()
? $playedGame->setRedAnon($anon)
: $playedGame->setBlueAnon($anon);
}
}
$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() : '',
'redAnon' => null !== $playedGame->getRedAnon() ? $playedGame->getRedAnon()->getUserName() : '',
'blueAnon' => null !== $playedGame->getBlueAnon() ? $playedGame->getBlueAnon()->getUserName() : ''
);
}
/**
* Handle prod MySQL timeout
*/
private function reConnect()
{
// try {
$connection = $this->em->getConnection();
if (false === $connection->ping()) {
$connection->close();
$connection->connect();
}
// } catch(PDOException $ex) {
// $this->logger->error($ex->getMessage());
// }
}
}