Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 2 years ago.
0

Totally possible.

Just to clarify 'biggest number of players' means the player with highest points ?

If so, you can just get the data and store it like :


$players = getPlayersRecordFromApi();

foreach ($players as $player) {
	GameData::create([
		'player_id' => $player->id,
		'points'   => $player->points
	]);
}

You can then retrieve datewise data as eloquent creates timestamps for you.
Last updated 2 years ago.
0

mcraz said:

Totally possible.

Just to clarify 'biggest number of players' means the player with highest points ?

If so, you can just get the data and store it like :


$players = getPlayersRecordFromApi();

foreach ($players as $player) {
  GameData::create([
  	'player_id' => $player->id,
  	'points'   => $player->points
  ]);
}

You can then retrieve datewise data as eloquent creates timestamps for you.

I do not see how it gets the highest number of players. Please note that the api is a live one, and gets me the number of players right now.

Last updated 2 years ago.
0

Create a table for daily maximum online users. Let's say its fields are id,user_count and count_date. Make count_date unique. user_count is for maximum online users count and count_date is for active day. Use your api and get number of online users periodically. Every five minutes, for example. You can use cron or similar service for this. Same time, check user_count by day, compare with newly fetched user count via api with old data. (You can use firstOrCreate for new day if it doesn't exist yet) If new data is bigger than old data, update record with new data. Only thing makes it complicated is my english, it is quite easy task actually.

Last updated 2 years ago.
0

Seems like I misunderstood your question.

So, you are trying to keep a daily record of max players on the day.

Call updatePlayerCount() via cron every x minutes and it will do all your job !

public function updatePlayerCount()
{	
	# ====================================================================
	#	1. Get number of players online now from api.
	# ====================================================================
	$live_count = getLivePlayerCountFromApi();
	
	# ====================================================================
	#   2. We will check if we have created a record today or not.
	# ====================================================================
	$data = GameData::whereRaw('DATE(created_at) = DATE(NOW())')->first();

	// If we already have a record from today, check & update if needed.
	if ( ! is_null( $data ) ) {
		
		// Check if live count us greater
		if ( $data->count < $live_count ) {
			$data->count = $live_count;
			$data->save();
		}

		// If count is just updated the count or it was less, exit.
		return;

	}

	# ====================================================================
	# 3. If we reach this portion of the code, it means we don't have a previous
	# record for today. Let's create a new one.
	# ====================================================================
	GameData::create(
		'count' => $live_count,
	);

	// Task completed. Exit
	return;
}
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Joe96 joe96 Joined 28 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.