<commit_before><?php

namespace MatchBundle\RPC;

use Doctrine\ORM\EntityManager;
use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
use Gos\Bundle\WebSocketBundle\Pusher\PusherInterface;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
use Gos\Bundle\WebSocketBundle\RPC\RpcInterface;
use MatchBundle\Entity\Game;
use Ratchet\ConnectionInterface;
use UserBundle\Entity\User;

/**
 * Class WaitingRpc
 * @package MatchBundle\RPC
 */
class WaitingRpc implements RpcInterface
{
    private $clientManipulator;
    private $em;
    private $pusher;

    /**
     * WaitingRpc constructor (DI)
     * @param ClientManipulatorInterface $clientManipulator
     * @param EntityManager              $em
     * @param PusherInterface            $pusher
     */
    public function __construct(
        ClientManipulatorInterface $clientManipulator,
        EntityManager $em,
        PusherInterface $pusher
    ) {
        $this->clientManipulator = $clientManipulator;
        $this->em = $em;
        $this->pusher = $pusher;
    }

    /**
     * Request on a non exist method
     * @param string $name      Method name
     * @param array  $arguments Arguments
     *
     * @return array Error
     */
    public function __call($name, $arguments)
    {
        return ['error' => "Bad Request ($name)"];
    }

    /**
     * Join/Leave the game
     * @param ConnectionInterface $connection
     * @param WampRequest         $request
     * @param array               $params
     *
     * @return array ['success' => bool]
     */
    public function join(ConnectionInterface $connection, WampRequest $request, array $params = [])
    {
        // Get game and user
        try {
            /**
             * @var Game $game
             * @var User $user
             */
            list($game, $user) = $this->getGameAndUser($connection, $params);
        } catch (\Exception $e) {
            return ['success' => false];
        }

        // Refresh user
        $user = $this->em->getRepository('UserBundle:User')->find($user->getId());

        $repo = $this->em->getRepository('MatchBundle:Player');
        if ($this->getParam($params, 'join', false)) {
            // Join
            $ai = ($game->isCreator($user) && isset($params['ai'])) ? $params['ai'] : false;
            $result = $repo->joinGame($game, $user, $ai);
        } else {
            // Leave
            $playerId = ($game->isCreator($user) && isset($params['id'])) ? $params['id'] : null;
            $result = $repo->quitGame($game, $user, $playerId);
        }

        // Msg Error ?
        if (is_string($result)) {
            return [
                'msg' => $result,
                'success' => false,
            ];
        }

        // Success
        if ($result) {
            $this->pusher->push(['_call' => 'updatePlayers'], 'game.wait.topic', ['slug' => $game->getSlug()]);
        }

        return ['success' => $result];
    }

    /**
     * Change the grid size or max player<commit_msg>Update homepage when join/leave game<commit_after><?php

namespace MatchBundle\RPC;

use Doctrine\ORM\EntityManager;
use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
use Gos\Bundle\WebSocketBundle\Pusher\PusherInterface;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
use Gos\Bundle\WebSocketBundle\RPC\RpcInterface;
use MatchBundle\Entity\Game;
use Ratchet\ConnectionInterface;
use UserBundle\Entity\User;

/**
 * Class WaitingRpc
 * @package MatchBundle\RPC
 */
class WaitingRpc implements RpcInterface
{
    private $clientManipulator;
    private $em;
    private $pusher;

    /**
     * WaitingRpc constructor (DI)
     * @param ClientManipulatorInterface $clientManipulator
     * @param EntityManager              $em
     * @param PusherInterface            $pusher
     */
    public function __construct(
        ClientManipulatorInterface $clientManipulator,
        EntityManager $em,
        PusherInterface $pusher
    ) {
        $this->clientManipulator = $clientManipulator;
        $this->em = $em;
        $this->pusher = $pusher;
    }

    /**
     * Request on a non exist method
     * @param string $name      Method name
     * @param array  $arguments Arguments
     *
     * @return array Error
     */
    public function __call($name, $arguments)
    {
        return ['error' => "Bad Request ($name)"];
    }

    /**
     * Join/Leave the game
     * @param ConnectionInterface $connection
     * @param WampRequest         $request
     * @param array               $params
     *
     * @return array ['success' => bool]
     */
    public function join(ConnectionInterface $connection, WampRequest $request, array $params = [])
    {
        // Get game and user
        try {
            /**
             * @var Game $game
             * @var User $user
             */
            list($game, $user) = $this->getGameAndUser($connection, $params);
        } catch (\Exception $e) {
            return ['success' => false];
        }

        // Refresh user
        $user = $this->em->getRepository('UserBundle:User')->find($user->getId());

        $repo = $this->em->getRepository('MatchBundle:Player');
        if ($this->getParam($params, 'join', false)) {
            // Join
            $ai = ($game->isCreator($user) && isset($params['ai'])) ? $params['ai'] : false;
            $result = $repo->joinGame($game, $user, $ai);
        } else {
            // Leave
            $playerId = ($game->isCreator($user) && isset($params['id'])) ? $params['id'] : null;
            $result = $repo->quitGame($game, $user, $playerId);
        }

        // Msg Error ?
        if (is_string($result)) {
            return [
                'msg' => $result,
                'success' => false,
            ];
        }

        // Success
        if ($result) {
            $this->pusher->push(['_call' => 'updatePlayers'], 'game.wait.topic', ['slug' => $game->getSlug()]);

            // Update homepage
            $list = $this->em->getRepository('MatchBundle:Game')->getList();
            $this->pusher->push(['games' => $list], 'homepage.topic');
        }

        return ['success' => $result];
    }

    /**
     * Change the grid size or max player
{'fraction_non_alphanumeric': 0.14281124497991968, 'fraction_numerical': 0.0, 'mean_word_length': 2.0867625185919683, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3849299', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1793, 'n_words': 390}