FastPoint Gaming Posted December 4, 2014 Report Share Posted December 4, 2014 Hi Everyone, As there is not a great deal of option when it comes to managing your redis data (Hence why we are building a custom database management panel - as well as a few other people) I'm going to show you how to both get information from your redis database and manipulate it.This will be helpful for those who wish to build a unique managment system for their servers. Be that very simple or very complex. I will also be taking request and offering help to people who wish to get certain results from their queries.===================================Requirments: PHP capable enviroment (I suggest MAMP/Xampp/Wamp to create a local web server) Redis PHP Client (I will be using TinyRedisClient as it is very lightweight) General knowledge of PHP language as well as good arithmetic Redis Database===================================Example One - Get the total amount of players function uniqueUsers() { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->GET( 'PLAYERS:YourPort' ); $object = json_decode($value, true); $number = count($object); return $number; } Breakdown Declare a fucntion and name it uniqueUsers function uniqueUsers() { } Connect to Redis Database function uniqueUsers() { $client = new TinyRedisClient( 'YourIP:YourPort' ); } Perform a GET request for the Key Called YourIP:Port function uniqueUsers() { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->GET( 'PLAYERS:YourPort' ); } This will return a JSON string. We now need to decode this using the json_decode() function like so function uniqueUsers() { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->GET( 'PLAYERS:YourPort' ); $object = json_decode($value, true); } This will seperate the JSON string into an array called $object. Now we count how many items are in the $objects array to get the total amount of players function uniqueUsers() { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->GET( 'PLAYERS:YourPort' ); $object = json_decode($value, true); $number = count($object); } Now all that is left to do is return the value of $number to a variable that we stored the returned value from the function in. function uniqueUsers() { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->GET( 'PLAYERS:YourPort' ); $object = json_decode($value, true); $number = count($object); return $number; } NOTE: To assign the returned value to a variable you must declare the variable and call the function like so: $uniquePlayers = uniqueUsers(); ===================================Example Two - Get Most Common Vehicle (Class Name) function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { $value = $client->GET( 'Vehicle:YourPort:'.$x ); if ($value != '') { $vehicle = json_decode($value, true); if (isset($vehicle[0])) { $vehicles[$x] = $vehicle[0]; } } } $counted = array_count_values($vehicles); arsort($counted); return(key($counted)); } Breakdown Declare mostPopularVehicle function function mostPopularVehicle () { } Connect to Redis database function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); } Get all KEYS that match the pattern "Vehicle:YourPort:" function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); } Count the result to get the total amount of vehicles function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); } Loop for the number of vehicles function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { } } GET the vehicle number $x (For each number in the total number of cars) function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { $value = $client->GET( 'Vehicle:YourPort:'.$x ); } } Check if the car value is not blank function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { $value = $client->GET( 'Vehicle:YourPort:'.$x ); if ($value != '') { } } } Decode the JSON string function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { $value = $client->GET( 'Vehicle:YourPort:'.$x ); if ($value != '') { $vehicle = json_decode($value, true); } } } Check if the decoded JSON string Index 0 is set function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { $value = $client->GET( 'Vehicle:YourPort:'.$x ); if ($value != '') { $vehicle = json_decode($value, true); if (isset($vehicle[0])) { } } } } If the index is set, vehicles index of $x variables (Each car for the total of cars) = the vehicle class name (Index 0 of the JSON string) function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { $value = $client->GET( 'Vehicle:YourPort:'.$x ); if ($value != '') { $vehicle = json_decode($value, true); if (isset($vehicle[0])) { $vehicles[$x] = $vehicle[0]; } } } } Count all the values in the $vehicles array function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { $value = $client->GET( 'Vehicle:YourPort:'.$x ); if ($value != '') { $vehicle = json_decode($value, true); if (isset($vehicle[0])) { $vehicles[$x] = $vehicle[0]; } } } $counted = array_count_values($vehicles); } Sort the counted array but maintain index association function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { $value = $client->GET( 'Vehicle:YourPort:'.$x ); if ($value != '') { $vehicle = json_decode($value, true); if (isset($vehicle[0])) { $vehicles[$x] = $vehicle[0]; } } } $counted = array_count_values($vehicles); arsort($counted); } Return the value, in this case is the most frequent occurring vehicle in the databse function mostPopularVehicle () { $client = new TinyRedisClient( 'YourIP:YourPort' ); $value = $client->KEYS( 'Vehicle:YourPort*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { $value = $client->GET( 'Vehicle:YourPort:'.$x ); if ($value != '') { $vehicle = json_decode($value, true); if (isset($vehicle[0])) { $vehicles[$x] = $vehicle[0]; } } } $counted = array_count_values($vehicles); arsort($counted); return(key($counted)); } =================================== More coming Soon PAR4NA, uniflare and Skaronator 3 Link to comment Share on other sites More sharing options...
lodac Posted December 4, 2014 Report Share Posted December 4, 2014 Thanks for providing this information. Link to comment Share on other sites More sharing options...
itsatrap Posted December 5, 2014 Report Share Posted December 5, 2014 Still what to know how you get around players DB accout gets renamved if they logout in combat/near others, the server puts an "clone" of you that gets a random name, that name is saved to the database with your steamID, Link to comment Share on other sites More sharing options...
FastPoint Gaming Posted December 5, 2014 Author Report Share Posted December 5, 2014 Updated the second example to include a breakdown. Link to comment Share on other sites More sharing options...
Uebermorgen Posted December 14, 2014 Report Share Posted December 14, 2014 If I use the uniquePlayers-function it says "NO AUTH Authentication" on line 51. Actual error: Fatal error: Uncaught exception 'Exception' with message 'NOAUTH Authentication required.' in C:\xampp\htdocs\TinyRedisClient.php:51 Link to comment Share on other sites More sharing options...
itsatrap Posted December 14, 2014 Report Share Posted December 14, 2014 Just ude the built in redis en php on ubuntu apt-get install php-redis Link to comment Share on other sites More sharing options...
FastPoint Gaming Posted December 14, 2014 Author Report Share Posted December 14, 2014 If I use the uniquePlayers-function it says "NO AUTH Authentication" on line 51. Actual error: Fatal error: Uncaught exception 'Exception' with message 'NOAUTH Authentication required.' in C:\xampp\htdocs\TinyRedisClient.php:51 Send me your code - particualrly the TinyRedis connection line Link to comment Share on other sites More sharing options...
Uebermorgen Posted December 14, 2014 Report Share Posted December 14, 2014 I use Windows Server 2008, not linux. @FastPointGaming.com I did this: <?php function uniqueUsers() { $client = new TinyRedisClient( 'localhost:6379' ); $value = $client->GET( 'PLAYERS:6379' ); $object = json_decode($value, true); $number = count($object); return $number; } $uniquePlayers = uniqueUsers(); echo $uniquePlayers; ?> and thats it. Changed nothing in TinyRedisClient.php Link to comment Share on other sites More sharing options...
Artis Posted December 14, 2014 Report Share Posted December 14, 2014 Thanks this is going to very useful. Link to comment Share on other sites More sharing options...
FastPoint Gaming Posted December 14, 2014 Author Report Share Posted December 14, 2014 I use Windows Server 2008, not linux. @FastPointGaming.com I did this: <?php function uniqueUsers() { $client = new TinyRedisClient( 'localhost:6379' ); $value = $client->GET( 'PLAYERS:6379' ); $object = json_decode($value, true); $number = count($object); return $number; } $uniquePlayers = uniqueUsers(); echo $uniquePlayers; ?> and thats it. Changed nothing in TinyRedisClient.php Can you send me the contents of your redis.conf as well? Link to comment Share on other sites More sharing options...
Uebermorgen Posted December 14, 2014 Report Share Posted December 14, 2014 sure: bind 127.0.0.1 maxmemory 4gb save 900 1 save 300 10 save 60 1000 requirepass MYPASSWORD Link to comment Share on other sites More sharing options...
FastPoint Gaming Posted December 14, 2014 Author Report Share Posted December 14, 2014 sure: bind 127.0.0.1 maxmemory 4gb save 900 1 save 300 10 save 60 1000 requirepass MYPASSWORD Try this: <?php function uniqueUsers() { $client = new TinyRedisClient( 'localhost:6379' ); $auth = $client->AUTH( 'MYPASSWORD' ); $value = $client->GET( 'PLAYERS:6379' ); $object = json_decode($value, true); $number = count($object); return $number; } $uniquePlayers = uniqueUsers(); echo $uniquePlayers; ?> Link to comment Share on other sites More sharing options...
Uebermorgen Posted December 14, 2014 Report Share Posted December 14, 2014 Parse error: syntax error, unexpected '$value' (T_VARIABLE) in C:\xampp\htdocs\index.php on line 51 Link to comment Share on other sites More sharing options...
FastPoint Gaming Posted December 14, 2014 Author Report Share Posted December 14, 2014 Parse error: syntax error, unexpected '$value' (T_VARIABLE) in C:\xampp\htdocs\index.php on line 51 Sorry add ; to the end of the $auth line Link to comment Share on other sites More sharing options...
Uebermorgen Posted December 14, 2014 Report Share Posted December 14, 2014 oups, forgot the ; but it says 0 or is it just the players that are online? Link to comment Share on other sites More sharing options...
FastPoint Gaming Posted December 14, 2014 Author Report Share Posted December 14, 2014 oups, forgot the ; but it says 0 or is it just the players that are online? I believe that the key of PLAYERS:PORT is just online, at least from my experience it's not persistant. Another more accurate method would be to loop and count every key that matches the pattern: Player:* Link to comment Share on other sites More sharing options...
Uebermorgen Posted December 14, 2014 Report Share Posted December 14, 2014 For that I use GameQ. But Player:* says 0 too. I want the total player count that ever played on the server. I think Player is the right way, but it says 0. idk why Link to comment Share on other sites More sharing options...
FastPoint Gaming Posted December 14, 2014 Author Report Share Posted December 14, 2014 For that I use GameQ. But Player:* says 0 too. I want the total player count that ever played on the server. I think Player is the right way, but it says 0. idk why Because you will need to change the code to loop through each Player:* While counting the occurances at the same time Link to comment Share on other sites More sharing options...
FastPoint Gaming Posted December 14, 2014 Author Report Share Posted December 14, 2014 <?php function uniqueUsers() { $client = new TinyRedisClient( 'localhost:6379' ); $auth = $client->AUTH( 'MYPASSWORD' ); $value = $client->KEYS( 'PLAYER:*' ); $number = count($value); for ($x = 0; $x <= $number; $x++) { //Do something in loop } } ?> $x Will be the total amount of players Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now