<commit_before><?php
namespace TmlpStats\Api;

use App;
use Carbon\Carbon;
use TmlpStats as Models;
use TmlpStats\Api\Base\AuthenticatedApiBase;
use TmlpStats\Domain\Scoreboard;
use TmlpStats\Domain\ScoreboardGame;
use TmlpStats\Reports\Arrangements;

class LiveScoreboard extends AuthenticatedApiBase
{
    /**
     * Get the current scores
     *
     * @param  Models\Center $center
     * @return array
     */
    public function getCurrentScores(Models\Center $center)
    {
        $report = $this->getLatestReport($center);
        $reportData = $this->getOfficialScores($report);
        if (!$reportData) {
            $scoreboard = Scoreboard::blank();
        } else {
            $scoreboard = Scoreboard::fromArray($reportData);
        }

        $tempScores = $this->getTempScoreboardForCenter($center->id, $report->reportingDate);
        $updatedAt = $report->submittedAt;

        // cap, cpc, etc, check for overrides
        foreach ($scoreboard->games() as $gameKey => $game) {
            $key = static::key($game->key, 'actual');
            if (isset($tempScores[$key])) {
                $score = $tempScores[$key];
                if ($updatedAt == null || $score->updatedAt->gt($updatedAt)) {
                    $updatedAt = $score->updatedAt;
                }
                $game->setActual($score->value);
            }
        }
        $scoreboard->meta['updatedAt'] = $updatedAt;
        return $scoreboard;
    }

    /**
     * Set score for the provided center and game
     *
     * @param Models\Center $center
     * @param string        $game   Game name (cap, cpc, etc)
     * @param string        $type   Type (actual is the only value supported now)
     * @param integer       $value  Game value
     *
     * @return array
     */
    public function setScore(Models\Center $center, $game, $type, $value)
    {
        if ($type != 'actual') {
            throw new \Exception("Currently, changing promises is not supported.");
        }

        $this->logChanges([
            'center_id' => $center->id,
            'game' => $game,
            'type' => $type,
            'value' => $value,
        ]);

        $item = $this->getTempScoreboardGame([
            'center_id' => $center->id,
            'routing_key' => static::key($game, $type),
        ]);
        $item->value = $value;
        $item->save();

        $reportData = $this->getCurrentScores($center)->toArray();
        $reportData['success'] = true;

        return $reportData;
    }

    /**
     * Get the scoreboard for given report
     *
     * @param  Models\StatsReport $report
     * @return array
     */
    protected function getOfficialScores(Models\StatsReport $report)
    {
        // Step 1: Determine the week for promises to use (may be same week)
        $quarterFirstWeekDate = $report->quarter->getFirstWeekDate($report->center);
        $quarterEndDate = $report->quarter->getQuarterEndDate($report->center);
        $actualDate = $report->reportingDate;
        $promiseDate = $actualDate->copy();
        while ($promiseDate->lt(Carbon::now())
            && $promiseDate->lt($quarterEndDate)
            && $promiseDate->gt($quarterFirstWeekDate)
        ) {
            $promiseDate->addWeek();
        }

        // Step 2: Fill an input array with both values, faking them into the same week.
        if ($actualDate->ne($promiseDate)) {
            $quarterData = App::make(LocalReport::class)->getQuarterScoreboard($report, [
                'returnUnprocessed' => true,
                'returnUnflattened' => true,
            ]);

            $actualWeekData = $quarterData[$actualDate->toDateString()];
            $promiseWeekData = $quarterData[$promiseDate->toDateString()];
<commit_msg>Set correct timezone for promises in LiveScoreboard<commit_after><?php
namespace TmlpStats\Api;

use App;
use Carbon\Carbon;
use TmlpStats as Models;
use TmlpStats\Api\Base\AuthenticatedApiBase;
use TmlpStats\Domain\Scoreboard;
use TmlpStats\Domain\ScoreboardGame;
use TmlpStats\Reports\Arrangements;

class LiveScoreboard extends AuthenticatedApiBase
{
    /**
     * Get the current scores
     *
     * @param  Models\Center $center
     * @return array
     */
    public function getCurrentScores(Models\Center $center)
    {
        $report = $this->getLatestReport($center);
        $reportData = $this->getOfficialScores($report);
        if (!$reportData) {
            $scoreboard = Scoreboard::blank();
        } else {
            $scoreboard = Scoreboard::fromArray($reportData);
        }

        $tempScores = $this->getTempScoreboardForCenter($center->id, $report->reportingDate);
        $updatedAt = $report->submittedAt;

        // cap, cpc, etc, check for overrides
        foreach ($scoreboard->games() as $gameKey => $game) {
            $key = static::key($game->key, 'actual');
            if (isset($tempScores[$key])) {
                $score = $tempScores[$key];
                if ($updatedAt == null || $score->updatedAt->gt($updatedAt)) {
                    $updatedAt = $score->updatedAt;
                }
                $game->setActual($score->value);
            }
        }
        $scoreboard->meta['updatedAt'] = $updatedAt;
        return $scoreboard;
    }

    /**
     * Set score for the provided center and game
     *
     * @param Models\Center $center
     * @param string        $game   Game name (cap, cpc, etc)
     * @param string        $type   Type (actual is the only value supported now)
     * @param integer       $value  Game value
     *
     * @return array
     */
    public function setScore(Models\Center $center, $game, $type, $value)
    {
        if ($type != 'actual') {
            throw new \Exception("Currently, changing promises is not supported.");
        }

        $this->logChanges([
            'center_id' => $center->id,
            'game' => $game,
            'type' => $type,
            'value' => $value,
        ]);

        $item = $this->getTempScoreboardGame([
            'center_id' => $center->id,
            'routing_key' => static::key($game, $type),
        ]);
        $item->value = $value;
        $item->save();

        $reportData = $this->getCurrentScores($center)->toArray();
        $reportData['success'] = true;

        return $reportData;
    }

    /**
     * Get the scoreboard for given report
     *
     * @param  Models\StatsReport $report
     * @return array
     */
    protected function getOfficialScores(Models\StatsReport $report)
    {
        // Step 1: Determine the week for promises to use (may be same week)
        $quarterFirstWeekDate = $report->quarter->getFirstWeekDate($report->center);
        $quarterEndDate = $report->quarter->getQuarterEndDate($report->center);
        $actualDate = $report->reportingDate;
        $promiseDate = $actualDate->copy();
        while ($promiseDate->lt(Carbon::now($report->center->timezone))
            && $promiseDate->lt($quarterEndDate)
            && $promiseDate->gt($quarterFirstWeekDate)
        ) {
            $promiseDate->addWeek();
        }

        // Step 2: Fill an input array with both values, faking them into the same week.
        if ($actualDate->ne($promiseDate)) {
            $quarterData = App::make(LocalReport::class)->getQuarterScoreboard($report, [
                'returnUnprocessed' => true,
                'returnUnflattened' => true,
            ]);

            $actualWeekData = $quarterData[$actualDate->toDateString()];
            $promiseWeekData = $quarterData[$promiseDate->toDateString()];

{'fraction_non_alphanumeric': 0.13692287339595186, 'fraction_numerical': 0.0005291705252017463, 'mean_word_length': 2.215652913653764, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 99, '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': '902646', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2136, 'n_words': 464}