Мне нужно отобразить статистику игроков за последний день, прошлую неделю, последний месяц и в целом. Статистика хранится в модели PlayerShip. У меня есть таблицы, которые показывают статистику для каждого отдельного игрока, одни и те же 10-12 статистических данных за каждый период.
У меня были миграции, которые сформировали структуру базы данных для таблицы, из которой извлекаются эти данные, Поле «updated_at» — это временная метка, обновляемая каждый раз при запуске кода. Такая вещь не может получить одну и ту же статистику за разные периоды, потому что существует задание cron, которое запускает этот сервисный код каждый день и также обновляет статистику.
Столбцы, помимо идентификатора учетной записи и игрока. name — это вычисляемая статистика. Могу ли я сохранить статистику в том виде, в котором она была на другую дату, а затем продемонстрировать ее, как в последний день, неделю и т. д.? Мне просто нужно найти способ получить статистику между периодами.
Схема таблицы:
('id', 'bigint(20) unsigned'),
('ship_id', 'bigint(20) unsigned'),
('battles_played', 'int(11)'),
('wins_count', 'int(11)'),
('damage_dealt', 'bigint(20)'),
('average_damage', 'bigint(20)'),
('frags', 'int(11)'),
('xp', 'int(11)'),
('survival_rate', 'double'),
('created_at', 'timestamp'),
('updated_at', 'timestamp'),
('account_id', 'bigint(20) unsigned'),
('player_name', 'varchar(255)'),
('distance', 'int(11)'),
('pve_battles', 'int(11)'),
('pve_wins', 'int(11)'),
('pve_frags', 'int(11)'),
('pve_xp', 'int(11)'),
('pve_survived_battles', 'int(11)'),
('pvp_battles', 'int(11)'),
('pvp_wins', 'int(11)'),
('pvp_frags', 'int(11)'),
('pvp_xp', 'int(11)'),
('pvp_survived_battles', 'int(11)'),
('club_frags', 'int(11)'),
('club_xp', 'int(11)'),
('club_battles', 'int(11)'),
('club_wins', 'int(11)'),
('club_survived_battles', 'int(11)'),
('rank_battles', 'int(11)'),
('rank_wins', 'int(11)'),
('rank_frags', 'int(11)'),
('rank_xp', 'int(11)'),
('rank_survived_battles', 'int(11)'),
('wn8', 'int(11)'),
('ship_name', 'varchar(255)'),
('ship_type', 'varchar(255)'),
('ship_tier', 'int(11)'),
('ship_nation', 'varchar(255)'),
('total_player_wins', 'int(11)'),
('last_battle_time', 'int(10) unsigned'),
('capture', 'int(11)'),
('defense', 'int(11)'),
('spotted', 'int(11)')
Код услуги:
public function getTopPlayersLast24Hours()
{
$last24Hours = now()->subDays(1);
return PlayerShip::select('account_id', DB::raw('MAX(player_name) as player_name'), DB::raw('MAX(total_player_wn8) as total_player_wn8'))
->where('ship_tier', '>', 5)
->where('battles_played', '>', 5)
->where('updated_at', '', 5)
->where('battles_played', '>', 30)
->where('updated_at', '', 5)
->where('battles_played', '>', 120)
->where('updated_at', '>=', $lastMonth)
->groupBy('account_id')
->orderByDesc('total_player_wn8')
->limit(10)
->get()
->map(function ($player) {
return [
'name' => $player->player_name,
'wid' => $player->account_id,
'wn8' => $player->total_player_wn8,
];
})
->toArray();
}
public function getTopPlayersOverall()
{
$overall = now()->subDays(29);
return PlayerShip::select('account_id', DB::raw('MAX(player_name) as player_name'), DB::raw('MAX(total_player_wn8) as total_player_wn8'))
->where('ship_tier', '>', 5)
->where('battles_played', '>', 500)
->where('updated_at', '>=', $overall)
->groupBy('account_id')
->orderByDesc('total_player_wn8')
->limit(10)
->get()
->map(function ($player) {
return [
'name' => $player->player_name,
'wid' => $player->account_id,
'wn8' => $player->total_player_wn8,
];
})
->toArray();
}
//get stats for each player, based on a period: 24, 7, 30, overall
public function getPlayerStatsLastDay($account_id)
{
$playerStatistics = PlayerShip::select(
'battles_played as battles',
'wins_count as wins',
'ship_tier as tier',
'survival_rate as survived',
'damage_dealt as damage',
'frags as frags',
'xp as xp',
'spotted as spotted',
'capture as capture',
'defend as defend',
'wn8 as wn8'
)
->where('account_id', $account_id)
->where('updated_at', '>=', now()->subDay())
->first();
Log::info($playerStatistics);
return $playerStatistics ? $playerStatistics->toArray() : [];
}
public function getPlayerStatsLastWeek($account_id)
{
$playerStatistics = PlayerShip::select(
'battles_played as battles',
'wins_count as wins',
'ship_tier as tier',
'survival_rate as survived',
'damage_dealt as damage',
'frags as frags',
'xp as xp',
'spotted as spotted',
'capture as capture',
'defend as defend',
'wn8 as wn8'
)
->where('account_id', $account_id)
->where('updated_at', '>=', now()->subWeek())
->first();
Log::info($playerStatistics);
return $playerStatistics ? $playerStatistics->toArray() : [];
}
public function getPlayerStatsLastMonth($account_id)
{
$playerStatistics = PlayerShip::select(
'battles_played as battles',
'wins_count as wins',
'ship_tier as tier',
'survival_rate as survived',
'damage_dealt as damage',
'frags as frags',
'xp as xp',
'spotted as spotted',
'capture as capture',
'defend as defend',
'wn8 as wn8'
)
->where('account_id', $account_id)
->where('updated_at', '>=', now()->subMonth())
->first();
Log::info($playerStatistics);
return $playerStatistics ? $playerStatistics->toArray() : [];
}
public function getPlayerStatsOverall($account_id)
{
$playerStatistics = PlayerShip::select(
'battles_played as battles',
'wins_count as wins',
'ship_tier as tier',
'survival_rate as survived',
'damage_dealt as damage',
'frags as frags',
'xp as xp',
'spotted as spotted',
'capture as capture',
'defend as defend',
'wn8 as wn8'
)
->where('account_id', $account_id)
->first();
Log::info($playerStatistics);
return $playerStatistics ? $playerStatistics->toArray() : [];
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... -month-etc
Как я могу отобразить статистику за последние 7 дней, последний месяц и т. д.? ⇐ Php
Кемеровские программисты php общаются здесь
1736588285
Anonymous
Мне нужно отобразить статистику игроков за последний день, прошлую неделю, последний месяц и в целом. Статистика хранится в модели PlayerShip. У меня есть таблицы, которые показывают статистику для каждого отдельного игрока, одни и те же 10-12 статистических данных за каждый период.
У меня были миграции, которые сформировали структуру базы данных для таблицы, из которой извлекаются эти данные, Поле «updated_at» — это временная метка, обновляемая каждый раз при запуске кода. Такая вещь не может получить одну и ту же статистику за разные периоды, потому что существует задание cron, которое запускает этот сервисный код каждый день и также обновляет статистику.
Столбцы, помимо идентификатора учетной записи и игрока. name — это вычисляемая статистика. Могу ли я сохранить статистику в том виде, в котором она была на другую дату, а затем продемонстрировать ее, как в последний день, неделю и т. д.? Мне просто нужно найти способ получить статистику между периодами.
Схема таблицы:
('id', 'bigint(20) unsigned'),
('ship_id', 'bigint(20) unsigned'),
('battles_played', 'int(11)'),
('wins_count', 'int(11)'),
('damage_dealt', 'bigint(20)'),
('average_damage', 'bigint(20)'),
('frags', 'int(11)'),
('xp', 'int(11)'),
('survival_rate', 'double'),
('created_at', 'timestamp'),
('updated_at', 'timestamp'),
('account_id', 'bigint(20) unsigned'),
('player_name', 'varchar(255)'),
('distance', 'int(11)'),
('pve_battles', 'int(11)'),
('pve_wins', 'int(11)'),
('pve_frags', 'int(11)'),
('pve_xp', 'int(11)'),
('pve_survived_battles', 'int(11)'),
('pvp_battles', 'int(11)'),
('pvp_wins', 'int(11)'),
('pvp_frags', 'int(11)'),
('pvp_xp', 'int(11)'),
('pvp_survived_battles', 'int(11)'),
('club_frags', 'int(11)'),
('club_xp', 'int(11)'),
('club_battles', 'int(11)'),
('club_wins', 'int(11)'),
('club_survived_battles', 'int(11)'),
('rank_battles', 'int(11)'),
('rank_wins', 'int(11)'),
('rank_frags', 'int(11)'),
('rank_xp', 'int(11)'),
('rank_survived_battles', 'int(11)'),
('wn8', 'int(11)'),
('ship_name', 'varchar(255)'),
('ship_type', 'varchar(255)'),
('ship_tier', 'int(11)'),
('ship_nation', 'varchar(255)'),
('total_player_wins', 'int(11)'),
('last_battle_time', 'int(10) unsigned'),
('capture', 'int(11)'),
('defense', 'int(11)'),
('spotted', 'int(11)')
Код услуги:
public function getTopPlayersLast24Hours()
{
$last24Hours = now()->subDays(1);
return PlayerShip::select('account_id', DB::raw('MAX(player_name) as player_name'), DB::raw('MAX(total_player_wn8) as total_player_wn8'))
->where('ship_tier', '>', 5)
->where('battles_played', '>', 5)
->where('updated_at', '', 5)
->where('battles_played', '>', 30)
->where('updated_at', '', 5)
->where('battles_played', '>', 120)
->where('updated_at', '>=', $lastMonth)
->groupBy('account_id')
->orderByDesc('total_player_wn8')
->limit(10)
->get()
->map(function ($player) {
return [
'name' => $player->player_name,
'wid' => $player->account_id,
'wn8' => $player->total_player_wn8,
];
})
->toArray();
}
public function getTopPlayersOverall()
{
$overall = now()->subDays(29);
return PlayerShip::select('account_id', DB::raw('MAX(player_name) as player_name'), DB::raw('MAX(total_player_wn8) as total_player_wn8'))
->where('ship_tier', '>', 5)
->where('battles_played', '>', 500)
->where('updated_at', '>=', $overall)
->groupBy('account_id')
->orderByDesc('total_player_wn8')
->limit(10)
->get()
->map(function ($player) {
return [
'name' => $player->player_name,
'wid' => $player->account_id,
'wn8' => $player->total_player_wn8,
];
})
->toArray();
}
//get stats for each player, based on a period: 24, 7, 30, overall
public function getPlayerStatsLastDay($account_id)
{
$playerStatistics = PlayerShip::select(
'battles_played as battles',
'wins_count as wins',
'ship_tier as tier',
'survival_rate as survived',
'damage_dealt as damage',
'frags as frags',
'xp as xp',
'spotted as spotted',
'capture as capture',
'defend as defend',
'wn8 as wn8'
)
->where('account_id', $account_id)
->where('updated_at', '>=', now()->subDay())
->first();
Log::info($playerStatistics);
return $playerStatistics ? $playerStatistics->toArray() : [];
}
public function getPlayerStatsLastWeek($account_id)
{
$playerStatistics = PlayerShip::select(
'battles_played as battles',
'wins_count as wins',
'ship_tier as tier',
'survival_rate as survived',
'damage_dealt as damage',
'frags as frags',
'xp as xp',
'spotted as spotted',
'capture as capture',
'defend as defend',
'wn8 as wn8'
)
->where('account_id', $account_id)
->where('updated_at', '>=', now()->subWeek())
->first();
Log::info($playerStatistics);
return $playerStatistics ? $playerStatistics->toArray() : [];
}
public function getPlayerStatsLastMonth($account_id)
{
$playerStatistics = PlayerShip::select(
'battles_played as battles',
'wins_count as wins',
'ship_tier as tier',
'survival_rate as survived',
'damage_dealt as damage',
'frags as frags',
'xp as xp',
'spotted as spotted',
'capture as capture',
'defend as defend',
'wn8 as wn8'
)
->where('account_id', $account_id)
->where('updated_at', '>=', now()->subMonth())
->first();
Log::info($playerStatistics);
return $playerStatistics ? $playerStatistics->toArray() : [];
}
public function getPlayerStatsOverall($account_id)
{
$playerStatistics = PlayerShip::select(
'battles_played as battles',
'wins_count as wins',
'ship_tier as tier',
'survival_rate as survived',
'damage_dealt as damage',
'frags as frags',
'xp as xp',
'spotted as spotted',
'capture as capture',
'defend as defend',
'wn8 as wn8'
)
->where('account_id', $account_id)
->first();
Log::info($playerStatistics);
return $playerStatistics ? $playerStatistics->toArray() : [];
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79346627/how-can-i-display-stats-from-last-7-days-last-month-etc[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия