<commit_before><?php

namespace Maelstromeous\Mariokart\Controller;

use Maelstromeous\Mariokart\Controller\AbstractController;
use Maelstromeous\Mariokart\Exception\InvalidDataException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class ChampionshipController extends AbstractController
{
    /**
     * Show form to create a new championship
     *
     * @param  Psr\Http\Message\ServerRequestInterface $request
     * @param  Psr\Http\Message\ResponseInterface      $response
     *
     * @return Psr\Http\Message\ResponseInterface
     */
    public function newChampionship(ServerRequestInterface $request, ResponseInterface $response)
    {
        $response->getBody()->write(
            $this->getTemplateDriver()->render(
                'championships/new.html'
            )
        );
    }

    /**
     * AJAX Request which creates a new championship
     *
     * @param  Psr\Http\Message\ServerRequestInterface $request
     * @param  Psr\Http\Message\ResponseInterface      $response
     *
     * @return Psr\Http\Message\ResponseInterface
     */
    public function commitNewChampionship(ServerRequestInterface $request, ResponseInterface $response)
    {
        $json = json_decode($request->getBody()->getContents());

        // Validate that we have the info we need first

        try {
            $this->validateChampionshipPOST($json);
        } catch (InvalidDataException $e) {
            $response->getBody()->write(
                json_encode(['error' => $e->getMessage()])
            );
            return $response->withStatus(400);
        }

        // Valid. Now process.

        // Create new championship
        $query = $this->newInsertQuery();
        $query->into('championships');
        $query->cols([
            'date'  => date('Y-m-d H:i:s'),
            'valid' => 1
        ]);

        $id = $this->executeInsertQuery($query);

        // Add the players and vehicles to the championship
        foreach ($json->players as $player) {
            $query = $this->newInsertQuery();
            $query->into('championships_players_vehicles');
            $query->cols([
                'player'       => $player->player,
                'championship' => $id,
                'vehicle'      => $player->vehicle
            ]);

            $this->executeInsertQuery($query);
        }

        // Return the championship ID to the view to redirect
        $response->getBody()->write(
            json_encode(['id' => $id])
        );
    }

    /**
     * Shows a singular championship
     *
     * @param  ServerRequestInterface $request
     * @param  ResponseInterface      $response
     * @param  array                  $args     URI arguments
     *
     * @return Psr\Http\Message\ResponseInterface
     */
    public function showChampionship(
        ServerRequestInterface $request,
        ResponseInterface $response,
        array $args
    ) {
        // Grab championship details<commit_msg>Corrected missing character info for championships<commit_after><?php

namespace Maelstromeous\Mariokart\Controller;

use Maelstromeous\Mariokart\Controller\AbstractController;
use Maelstromeous\Mariokart\Exception\InvalidDataException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class ChampionshipController extends AbstractController
{
    /**
     * Show form to create a new championship
     *
     * @param  Psr\Http\Message\ServerRequestInterface $request
     * @param  Psr\Http\Message\ResponseInterface      $response
     *
     * @return Psr\Http\Message\ResponseInterface
     */
    public function newChampionship(ServerRequestInterface $request, ResponseInterface $response)
    {
        $response->getBody()->write(
            $this->getTemplateDriver()->render(
                'championships/new.html'
            )
        );
    }

    /**
     * AJAX Request which creates a new championship
     *
     * @param  Psr\Http\Message\ServerRequestInterface $request
     * @param  Psr\Http\Message\ResponseInterface      $response
     *
     * @return Psr\Http\Message\ResponseInterface
     */
    public function commitNewChampionship(ServerRequestInterface $request, ResponseInterface $response)
    {
        $json = json_decode($request->getBody()->getContents());

        // Validate that we have the info we need first

        try {
            $this->validateChampionshipPOST($json);
        } catch (InvalidDataException $e) {
            $response->getBody()->write(
                json_encode(['error' => $e->getMessage()])
            );
            return $response->withStatus(400);
        }

        // Valid. Now process.

        // Create new championship
        $query = $this->newInsertQuery();
        $query->into('championships');
        $query->cols([
            'date'  => date('Y-m-d H:i:s'),
            'valid' => 1
        ]);

        $id = $this->executeInsertQuery($query);

        // Add the players and vehicles to the championship
        foreach ($json->players as $player) {
            $query = $this->newInsertQuery();
            $query->into('championships_players_vehicles');
            $query->cols([
                'player'       => $player->player,
                'championship' => $id,
				'character'    => $player->character,
                'vehicle'      => $player->vehicle
            ]);

            $this->executeInsertQuery($query);
        }

        // Return the championship ID to the view to redirect
        $response->getBody()->write(
            json_encode(['id' => $id])
        );
    }

    /**
     * Shows a singular championship
     *
     * @param  ServerRequestInterface $request
     * @param  ResponseInterface      $response
     * @param  array                  $args     URI arguments
     *
     * @return Psr\Http\Message\ResponseInterface
     */
    public function showChampionship(
        ServerRequestInterface $request,
        ResponseInterface $response,
        array $args
    ) {
        // Grab championship details
{'fraction_non_alphanumeric': 0.11357295256852125, 'fraction_numerical': 0.0013129821106187428, 'mean_word_length': 2.123526396719631, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 67, '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': '239804', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1572, 'n_words': 330}