【发布时间】:2019-11-02 20:06:01
【问题描述】:
这还需要一个实用的答案来回答。
我整理了一些方法来统计我经常玩的游戏的一些有趣的统计数据。
以下方法将计算所有已玩游戏的总数,将玩家与玩家列表匹配,然后显示总赢/输/平局的总和。
这很棒,而且很实用。
但是,由于大众的需求,我被要求调整查询,以便现在将游戏开始的日期考虑在内。我想将其过滤到最后 30 天的总和。我该怎么做?
在花时间重写整个内容之前,我想问问周围的人。最好一切都保持不变,只需按日期过滤即可。
数据库的日期键是checkSumID,它是一个 UNIX 时间戳。
private function topPlayers() {
$topPlayersList = array();
$playersList = DB::table('pickup_results')
->select(DB::raw("playerID"),
DB::raw("COUNT(CASE WHEN gameResult = 'Win' THEN 1 END) AS wins"),
DB::raw("COUNT(CASE WHEN gameResult = 'Loss' THEN 1 END) AS loss"),
DB::raw("COUNT(CASE WHEN gameResult = 'Tie' THEN 1 END) AS tie")
)
->groupBy('playerID')
->orderBy('wins','DESC')
->get();
$i = 0;
foreach ($playersList as $playerListData) {
if ($playerListData->wins + $playerListData->loss + $playerListData->tie >= 25) {
$avgPick = $this->getPlayerAvgPickCount($playerListData->playerID);
$playerRecordID = $playerListData->playerID;
$playerNameLookup = Players::where([
'player_id' => $playerListData->playerID
])->first();
$playerListData->playerID = $playerNameLookup->player_name;
$topPlayersList[$i] = array(
'name' => $playerNameLookup->player_name,
'total' => +$playerListData->wins + +$playerListData->loss + +$playerListData->tie,
'wins' => +$playerListData->wins,
'loss' => +$playerListData->loss,
'tie' => +$playerListData->tie,
'percent' => +$playerListData->loss == 0 ? 0 : round(
(+$playerListData->wins / (+$playerListData->wins + +$playerListData->loss) * 100),
2
) . ' %',
'avg_pick' => $avgPick[0]->average,
'player_id' => $playerRecordID
);
$i++;
}
}
return $this->sortArray($topPlayersList,'percent','DESC');
}
我写的一种方法可以做类似的事情,但更多的是基于一个人,但不确定如何在不完全重写的情况下将两者拼接在一起。
这是那个方法
private function getTotalGamesPlayed30DayWinLossTies() {
//PickupResults::where('playerID', '=', $this->getPlayerID())->where('checkSumID', '=', Carbon::now()->subDays(30)->timestamp)->count()
$results = PickupResults::get();
//$results = PickupResults::where('playerID', '=', $this->getPlayerID())->get();
$count = 0;
$wins = 0;
$loss = 0;
$tie = 0;
foreach ($results as $result) {
if ($result->playerID === $this->playerID) {
$timeStamp = $result->checkSumID;
$converted = date('m/d/Y', $timeStamp / 1000);
if (strtotime($converted) > strtotime('-30 days')) {
$count = $count + 1;
if ($result->gameResult === 'Win') {
$wins = $wins + 1;
}
if ($result->gameResult === 'Loss') {
$loss = $loss + 1;
}
if ($result->gameResult === 'Tie') {
$tie = $tie + 1;
}
}
}
}
return
array(
'total' => $count,
'wins' => $wins,
'loss' => $loss,
'tie' => $tie,
'percent' => $loss == 0 ? 0 : round(($wins / ( $wins + $loss) * 100 ),2) . ' %'
);
}
任何帮助将不胜感激。
当使用 Arun P 的答案时
$playersList = DB::table('pickup_results')
->select(DB::raw("playerID"),
DB::raw("COUNT(CASE WHEN gameResult = 'Win' THEN 1 END) AS wins"),
DB::raw("COUNT(CASE WHEN gameResult = 'Loss' THEN 1 END) AS loss"),
DB::raw("COUNT(CASE WHEN gameResult = 'Tie' THEN 1 END) AS tie"))
->where('checksumID','<',$now)->where('checksumID','>',$thirty_days_ahead)
->groupBy('playerID')
->orderBy('wins', 'DESC')
->get();
它将返回 0 个结果。这是不正确的;我正在尝试仅收集玩家在过去 30 天内玩过的所有游戏。不多也不少。
您可以访问http://www.Krayvok.com/t1 并查看统计页面以获取工作示例。
我正在尝试使用显示所有玩家总游戏数的当前排行榜。我想对其进行过滤,以仅显示从今天起(滚动 30 天)过去 30 天内玩过游戏的玩家。
【问题讨论】:
-
为什么不添加 where 子句并将时间戳与 date_sub(now(),间隔 30 天)进行比较。这将为您提供最近 30 天的结果
-
@MekjkrhG 我该怎么做?我试着在它上面做一个地方,但由于我的计数,它给了我一个错误。我无法通过查询:/
-
显示你尝试的更新查询
-
@Kray 为了过滤掉过去 30 天,您需要有一个包含数据的日期列 - 在
checkSumID列中吗? (如果是这样,它的名字很奇怪)——或者,最好是,你能显示pickup_results表的表结构吗? -
在您的工作示例中,您在 PHP 中将时间戳转换为日期时执行
$timeStamp / 1000。我相信您也应该能够在 checkSumID 上进行相同的划分,然后在其他示例之一中将其转换为 unix 时间以进行调整。即对于 MekjkrhG 的回答,我认为您可以将一行更改为修复它->where(DB::raw("from_unixtime(checkSumID / 1000) > date_sub(now(), interval 30 day)"))。
标签: php laravel-5 laravel-5.8