"PHP" / Говнокод #28873 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
<?php

namespace App\Http\Controllers\api\v1;

use App\Http\Controllers\Controller;
use App\Http\Requests\StandartRequest;
use App\Models\Attractions;
use App\Models\AttractionSessionUpdates;
use App\Models\Locations;
use App\Models\SessionAttractionHistory;
use App\Models\SessionsHistoryAttractions;
use App\Models\SessionsHistoryGames;
use App\Models\Statistic\CountEndAttractionSessions;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class AttractionEndSessionsCount extends Controller
{
    public function endSessions(StandartRequest $request){
        $this->now_time = Carbon::now();

        $locations = Locations::where('user_id', '=', Auth::user()->id)->get();
        if (count($locations) == 0){
            return response()->json(['code' => "404", 'status' => 'error' , 'message'=> "locations not found"]);
        }
        try {
            $request_from = $request->input('date_from');
            $from = Carbon::parse($request_from."00:00:01");

            $date_to = $request->input('date_to');
            $to = Carbon::parse($date_to."11:59:59");
        } catch (\Exception $exception){
            return response()->json(['code' => "422", 'status' => 'error' , 'message'=> "dates have bad format, failed parse"]);
        }

        if ($from->timestamp > $to->timestamp){
            return response()->json(['code' => "422", 'status' => 'error' , 'message'=> "the start date is larger than the end date"]);
        }

        $is_detail = $request->input('detail', 'daily');

        $arr = $this->prepareLocationsAndAttrctions($locations, $from, $to, $is_detail);

        $locations_ids = [];
        foreach ($locations as $loc){
            $locations_ids[] = $loc->id;
        }

        $history = CountEndAttractionSessions::query()->whereIn('location_id', array_values($locations_ids))
            ->whereBetween('day_date', [$from->startOfDay()->format('Y-m-d H:i:s'), $to->endOfDay()->format('Y-m-d H:i:s')])
            ->get();

        $resp = $this->fillResults($arr ,$history, $is_detail);
        if ($this->is_today){
            $resp = $this->fillTodayResult($resp, $locations_ids, $is_detail);
        }
        return response()->json(['code' => "200", 'status' => 'success' , 'data'=> $resp]);

    }

    private function fillResults($arr ,$history, $is_detail){
        foreach ($history as $value){
            try {
                if ($is_detail == 'hourly'){
                    $arr['locations'][$value->location_id]['attractions'][$value->attraction_id]['list'][$value->day_date->format('d-m-Y')][$value->day_date->format('H:00:00')] =
                        [
                            // общее количество игр
                            'total_count' => $value->total_count ?? 0,
                            //общее количество обновлений
                            'total_updates_count' => $value->total_updates_count ?? 0,
                            // количество пролонгейт обновлений
                            'add_time' => $value->add_time ?? 0,
                            // количество лет_гейм обновлений
                            'let_game_end' => $value->let_game_end ?? 0,
                            //средняя длина игры в секундах
                            'avg_duration' => [
                                'sum_seconds' => $value->avg_duration_second_sum,
                                'count_games' => $value->avg_duration_count_games,
                            ],
                            // минимальная игра в секундах
                            'min_duration' => $value->min_duration,
                            // максимальная игра в секундах
                            'max_duration' => $value->max_duration,
                ];
                    for ($i = 1; $i <= 10; $i++){
                        $selector = 'max_players_'.$i;
                        $arr['locations'][$value->location_id]
                        ['attractions'][$value->attraction_id]
                        ['list'][$value->day_date->format('d-m-Y')]
                        [$value->day_date->format('H:00:00')]
                        ['max_players'][$i] = (int)$value->$selector;
                    }
                }elseif ($is_detail == 'daily') {
                    $arr['locations'][$value->location_id]['attractions'][$value->attraction_id]
                    ['list'][$value->day_date->format('d-m-Y')]['total_count'] += $value->total_count;

                    $arr['locations'][$value->location_id]['attractions'][$value->attraction_id]

расчет заранее посчитанной оффлайн и онлайн статистики

Запостил: tlen tlen, (Updated )

Комментарии (1) RSS

Добавить комментарий

Переведи на "PHP", guest!

    А не использовать ли нам bbcode?


    8