Selecting one number from the range - php

I have a trivial question for you but I can not deal with the matches myself.
more precisely - I have a problem with writing a condition that will only be fulfilled when specific parameters occur.
say, I have three thresholds: 2000, 15325, 500000
each of them has an appropriate level
user points are, for example: 16,000
I want it to search for the appropriate level and assign it to the user after reaching the threshold
I unfortunately have a problem with the condition of fulfilling the activity, because the condition
user points> = point threshold
shows me all levels, but
user points <= point threshold
it does not show anything, it does not fulfill the assumption of the script
function chooseLevel($dbh) {
$userPoints = getPoints($dbh, 3);
$levels = getLevels($dbh);
foreach($levels as $required) {
if($userPoints >= $required["points"]) {
$level = $required["level"];
}
}
return $level;
}

Related

Keep data from reaching below 0

Im working on a browser game where i need to handle energy. The user spends and gets energy but i have come across an issue that i have tried to fix, When a user has for example 20 Energy and he does something that spends 30 energy it's going through and ofcourse i understand because i am checking the value to not go below 0 like this
if ( 0 < $resultenergy[0]['energy'])
This means that unless the users energy is at 0 he can spend whatever he want. What would be a better way to check the data so it alerts when "trying" to go under 0 ? I appreciate all help and if you have an idea of a different approach im all ears.
Do the calculation once.
$remaining_energy = $current_energy - $energy_cost;
Then check the result, and act accordingly.
if ($remaining_energy >= 0) {
$current_energy = $remaining_energy;
// do whatever required the energy
// send the client a succes indicator
} else {
// don't change current energy
// send the client an error indicator
}
You could subtract and then evaluate. e.g.
if ( 0 < $resultenergy[0]['energy'] - $energyCost) {
$resultenergy[0]['energy'] = $resultenergy[0]['energy'] - $energyCost;
return true
} else {
return false;
}
$energyCost is the cost of whatever action they're trying to do.

How to make an asynchronous self-calling loop non-recursive

I'm writing a function in PHP that loops through an array, and then performs an asynchronous call on it (using a Promise).
The problem is that, the only way I can make this loop happen, is by letting a function call itself asynchronously. I run into the 100-nested functions problem really quick, and I would basically like to change it to not recur.
function myloop($data, $index = 0) {
if (!isset($data[$index])) {
return;
}
$currentItem = $data[$index];
$currentItem()->then(function() use ($data, $index) {
myloop($data, $index + 1);
});
}
For those that want to answer this from a practical perspective (e.g.: rewrite to not be asynchronous), I'm experimenting with functional and asynchronous patterns and I want to know if it is possible to do this with PHP.
I've written a possible solution in pseudo-code. The idea is to limit
the number of items running asynchronous at once by using a database
queue. myloop() is no longer directly recursive, instead being called
whenever an item finishes running. In the sample data, I've limited it
to 4 items concurrently (arbitrary value).
Basically, it's still recursively calling itself, but in a roundabout way,
avoiding the situation you mentioned of many nested called.
Execution Flow:
myloop() ---> queue
^ v
| |
'<-processor <-'
<?php
//----------
// database
//table: config
//columns: setting, value
//items: ACTIVE_COUNT, 0
// ITEM_CONCURRENT_MAX, 4
//table: queue
//columns: id, item, data, index, pid, status(waiting, running, finished), locked
// --- end pseudo-schema ---
<?php
// ---------------
// itemloop.php
// ---------------
//sends an item and associated data produced by myloop() into a database queue,
//to be processed (run asynchronous, but limited to how many can run at once)
function send_item_to_processor($item, $data, $index, $counter) {
//INSERT $item to a queue table, along with $data, $index (if needed), $counter, locked = 0
//status == waiting
}
//original code, slightly modified to remove direct recursion and implement
//the queue.
function myloop($data, $index = 0, $counter = 0) {
if (!isset($data[$index])) {
return;
}
$currentItem = $data[$index];
$currentItem()->then(function() use ($data, $index) {
//instead of directly calling `myloop()`, push item to
//database and let the processor worry about it. see below.
//*if you wanted currentItem to call a specific function after finishing,
//you could create an array of numbered functions and pass the function
//number along with the other data.*
send_item_to_processor($currentItem, $data, $index + 1, $counter + 1);
});
}
// ---------------
// processor.php
// ---------------
//handles the actual running of items. looks for a "waiting" item and
//executes it, updating various statuses along the way.
//*called from `process_queue()`*
function process_new_items() {
//select ACTIVE_COUNT, ITEM_CONCURRENT_MAX
//ITEM_COUNT = total records in the queue. this is done to
//short-circuit the execution of `process_queue()` whenever possible
//(which is called frequently).
if (ITEM_COUNT == 0 || $ACTIVE_COUNT >= $ITEM_CONCURRENT_MAX)
return FALSE;
//select item from queue where status = waiting AND locked = 0 limit 1;
//update item set status = running, pid = programPID
//update config ACTIVE_COUNT = +1
//**** asynchronous run item here ****//
return TRUE;
}
//main processor for the queue. first processes new/waiting items
//if it can (if too many items aren't already running), then processes
//dead/completed items. Upon an item.status == finished, `myloop()` is
//called from this function. Still technically a recursive call, but
//avoids out-of-control situations due to the asynchronous nature.
//this function could be called on a timer of some sort, such as a cronjob
function process_queue() {
if (!process_new_items())
return FALSE; //too many instances running, no need to process
//check queue table for items with status == finished or is_pid_valid(pid) == FALSE
$numComplete = count($rows);
//update all rows to locked = 1, in case process_queue() gets called again before
//we finish, resulting in an item potentially being processed as dead twice.
foreach($rows as $item) {
if (is_invalid(pid) || $status == finished) {
//and here is the call back to myloop(), avoiding a strictly recursive
//function call.
//*Not sure what to do with `$item` here -- might be passed back to `myloop()`?.*
//delete item(s) from queue
myloop(data, index, counter - 1);
//decrease config.ACTIVE_COUNT by $numComplete
}
}
}

Golf league payout to six finishing positions

I have a golf league of 40 individuals. We all throw money in a pot and pay out the first 6 places based on final score.
If there were no ties the pay out would be simple but often we have, for example, 2 people tied for first place, 3 people tied for second, 1 person alone in third, etc. The variations seem endless.
I've been trying to automate the calculated payouts for each place using PHP but have not been successful. Any suggestions, help, or pointing in the right direction would be much appreciated. I noticed that someone else tried to ask a similar question on this site but was not successful in framing the question. I'll try to do a better job.
Here is some data I've been playing around with:
$playerNumber=40;
$totalPoints=100;
Payouts:
$pointsFirst=0.6*$totalPoints;
$pointsSecond=0.2*$totalPoints;
$pointsThird=0.15*$totalPoints;
$pointsFourth=0.03*$totalPoints;
$pointsFifth=0.02*$totalPoints;
$pointsSixth=0.01*$totalPoints;
For the example given above and to pay out six places, we would calculate the payouts as follows:
If two people are tied for first place, we add first and second place points and divide by two.
If three people are tied for second place, we add third, fourth and fifth place points and divide by three.
If one person is alone in third, this person would win sixth place points.
I can count the number of players who are in or tied for a certain place.
$countFirst=2;
$countSecond=3;
$countThird=1;
$countFourth=2;
$countFifth=1;
$countSixth=2;
In this example the player scores would be 72, 72, 73, 73, 73, 74, 75, 75, 76, 77, 77.
At first I thought this was an application for nested arrays. Then I thought perhaps using arrays, array slice, etc, may be a way to go. Each time I end up in the woods. I'm not seeing the logic.
I have used conditional statements for paying out three places but to pay out six places with this method puts me deep in the woods.
Example of payout to three places using conditional statements:
$pointsFirst=0.5*$totalPoints;
$pointsSecond=0.3*$totalPoints;
$pointsThird=0.2*$totalPoints;
if($countFirst>2) {
$ptsA=round($totalPoints/$countFirst,2);
}
elseif($countFirst==2) {
$ptsA=round(($pointsFirst+$pointsSecond)/2,2);
if($countSecond>1) {
$ptsB=round($pointsThird/$countSecond,2);
}
elseif($countSecond==1) {
$ptsB=round($pointsThird,2);
}
}
elseif($countFirst==1) {
$ptsA=round($pointsFirst,2);
if($countSecond>1) {
$ptsB=round(($pointsSecond+$pointsThird)/2,2);
}
elseif($countSecond==1) {
$ptsB=round($pointsSecond,2);
if($countThird>1) {
$ptsC=round($pointsThird/$countThird,2);
}
elseif($countThird==1) {
$ptsC=round($pointsThird,2);
}
}
}
I hope I have been clear in my request. I'll be glad to clarify anything. If anyone has any ideas on how to efficiently automate a payout calculation to six places I will be eternally grateful. Thank-you! Mike
Per request:
$scores=array();
$scores[0]=72;
$scores[1]=72;
$scores[2]=73;
$scores[3]=73;
$scores[4]=73;
$scores[5]=74;
$scores[6]=75;
$scores[7]=75;
$scores[8]=76;
$scores[9]=77;
$scores[10]=77;
$payout=array();
$payout[0]=0.6*$totalPoints;
$payout[1]=0.2*$totalPoints;
$payout[2]=0.15*$totalPoints;
$payout[3]=0.03*$totalPoints;
$payout[4]=0.02*$totalPoints;
$payout[5]=0.01*$totalPoints;
$countScores=array();
$countScores[0]=$countFirst;
$countScores[1]=$countSecond;
$countScores[2]=$countThird;
$countScores[3]=$countFourth;
$countScores[4]=$countFifth;
$countScores[5]=$countSixth;
First, there is a problem with your Payouts. If you add them up you get 1.01 not 1
0.6 (1st) + 0.2 (2nd ) + 0.15 (3rd) + 0.03 (4th) + 0.02 (5th) + 0.01 (6th) = 1.01
Second, it is easier if you make your Payouts and Counts into arrays -
change these -
$pointsFirst=0.6*$totalPoints;
$pointsSecond=0.2*$totalPoints;
$pointsThird=0.15*$totalPoints;
$pointsFourth=0.03*$totalPoints;
$pointsFifth=0.02*$totalPoints;
$pointsSixth=0.01*$totalPoints;
$countFirst=2;
$countSecond=3;
$countThird=1;
$countFourth=2;
$countFifth=1;
$countSixth=2;
to these
$payout=array();
$payout[0]=0.6*$totalPoints;
$payout[1]=0.2*$totalPoints;
$payout[2]=0.15*$totalPoints;
$payout[3]=0.03*$totalPoints;
$payout[4]=0.02*$totalPoints;
$payout[5]=0.01*$totalPoints;
$count=array();
$count[0]=2;
$count[1]=3;
$count[2]=1;
$count[3]=2;
$count[4]=1;
$count[5]=2;
Here is the start of one way to do it. Although I would eventually change this into a function so that I can use it again with different payouts, and number of places (see phpfiddle examples below)
I see this in 4 steps-
Step 1
// Add together the payments if there are ties - ie. 2 tied for first $payout[0]+$payout[1], etc
$payout_groups = array(); // start a payout array
$payout_groups_key = 0; // array key count
$payout_groups_count = 0; // array counter, use to match the $count array values
for($w=0;$w<count($payout);$w++){ //
if(array_key_exists($payout_groups_key,$payout_groups)){
$payout_groups[$payout_groups_key] += $payout[$w]; // if there are ties, add them together
}
else{
$payout_groups[$payout_groups_key] = $payout[$w]; // else set a new payout level
}
$payout_groups_count++; // increase the counter
if($payout_groups_count == $count[$payout_groups_key]){ // if we merged all the ties, set a new array key and restart the counter
$payout_groups_key++;
$payout_groups_count = 0;
}
}
Step 2
// basic counter to get how many placers/winners. This makes it possible to have ties for 6th (last) place
$x = 0;
$y = 0;
while($y < count($payout)){
$y += $count[$x]; // the $count array values until we reach the amount of places/payouts
$x++;
}
Step 3
// Create array for winnings per placing
$winnings = array(); // start an array
$placings_count = 0; //
$placings_counter = 0;
for($z=0;$z<$y;$z++){
$winnings[$z] = $payout_groups[$placings_count]/$count[$placings_count];
$placings_counter++;
if($placings_counter == $count[$placings_count]){
$placings_count++;
$placings_counter = 0;
}
}
Step 4
// Assign winnings to scorecard
$scoreboard = array();
for($t=0;$t<count($winnings);$t++){
$scoreboard[$t]['score'] = $scores[$t];
$scoreboard[$t]['payout'] = $winnings[$t];
}
You can see this using your defined values at - http://phpfiddle.org/main/code/a1g-qu0
Using the same code above, I changed the payout amounts, and increased it to 7th places - http://phpfiddle.org/main/code/uxi-qgt

PHP code reaching execution time limit

I need to go through an array containing points in a map and check their distance from one another. I need to count how many nodes are within 200m and 50m of each one. It works fine for smaller amounts of values. However when I tried to run more values through it (around 4000 for scalability testing) an error occurs saying that I have reached the maximum execution time of 300 seconds. It needs to be able to handle at least this much within 300 seconds if possible.
I have read around and found out that there is a way to disable/change this limit, but I would like to know if there is a simpler way of executing the following code so that the time it takes to run it will decrease.
for($i=0;$i<=count($data)-1;$i++)
{
$amount200a=0;
$amount200p=0;
$amount50a=0;
$amount50p=0;
$distance;
for($_i=0;$_i<=count($data)-1;$_i++)
{
$distance=0;
if($data[$i][0]===$data[$_i][0])
{
}
else
{
//echo "Comparing ".$data[$i][0]." and ".$data[$_i][0]." ";
$lat_a = $data[$i][1] * PI()/180;
$lat_b = $data[$_i][1] * PI()/180;
$long_a = $data[$i][2] * PI()/180;
$long_b = $data[$_i][2] * PI()/180;
$distance =
acos(
sin($lat_a ) * sin($lat_b) +
cos($lat_a) * cos($lat_b) * cos($long_b - $long_a)
) * 6371;
$distance*=1000;
if ($distance<=50)
{
$amount50a++;
$amount200a++;
}
else if ($distance<=200)
{
$amount200a++;
}
}
}
$amount200p=100*number_format($amount200a/count($data),2,'.','');
$amount50p=100*number_format($amount50a/count($data),2,'.','');
/*
$dist[$i][0]=$data[$i][0];
$dist[$i][1]=$amount200a;
$dist[$i][2]=$amount200p;
$dist[$i][3]=$amount50a;
$dist[$i][4]=$amount50p;
//*/
$dist.=$data[$i][0]."&&".$amount200a."&&".$amount200p."&&".$amount50a."&&".$amount50p."%%";
}
Index 0 contains the unique ID of each node, 1 contains the latitude of each node and
index 2 contains the longitude of each node.
The error occurs at the second for loop inside the first loop. This loop is the one comparing the selected map node to other nodes. I am also using the Haversine Formula.
first of all, you are performing in big O notation: O(data^2), which is gonna be slow as hell , and really, either there are 2 possible solutions. Find a proven algorithm that solves the same problem in a better time. Or if you cant, start moving stuff out of the innner for loop, and mathmatically prove if you can convert the inner for loop to mostly simple calculations, which is often something you can do.
after some rewriting, I see some possiblities:
If $data is not a SPLFixedArray (which has a FAR Better access time, ) then make it. since you are accessing that data so many times (4000^2)*2.
secound, write cleaner code. although the optizmier will do its best, if you dont try either to minize the code (which only makes it more readable), then it might not be able to do it as well as possible.
and move intermediate results out of the loops, also something like the size of the array.
Currently you're checking all points against all other points, where in fact you only need to check the current point against all remaining points. The distance from A to B is the same as the distance from B to A, so why calculate it twice?
I would probably make an adjacent array that counts how many nodes are within range of each other, and increment pairs of entries in that array after I've calculated that two nodes are within range of each other.
You should probably come up with a very fast approximation of the distance that can be used to disregard as many nodes as possible before calculating the real distance (which is never going to be super fast).
Generally speaking, beyond algorithmic optimisations, the basic rules of optimisation are:
Don't any processing that you don't have to do: Like not multiplying $distance by 1000. Just change the values you're testing against from 20 and 50 to 0.02 and 0.05, respectively.
Don't call any function more often than you have to: You only need to call count($data) once before any processing starts.
Don't calculate constant values more than once: PI()/180, for example.
Move all possible processing outside of loops. I.e. precalculate as much as possible.
Another minor point which will make your code a little easier to read:
for( $i = 0; $i <= count( $data ) - 1; $i++ ) is the same as:
for( $i = 0; $i < count( $data ); $i++ )
Try this:
$max = count($data);
$CONST_PI = PI() / 180;
for($i=0;$i<$max;$i++)
{
$amount200a=0;
$amount50a=0;
$long_a = $data[$i][2] * $CONST_PI;
$lat_a = $data[$i][1] * $CONST_PI;
for($_i=0;$_i<=$max;$_i++)
//or use for($_i=($i+1);$_i<=$max;$_i++) if you did not need to calculate already calculated in other direction
{
$distance=0;
if($data[$i][0]===$data[$_i][0]) continue;
$lat_b = $data[$_i][1] * $CONST_PI;
$long_b = $data[$_i][2] * $CONST_PI;
$distance =
acos(
sin($lat_a ) * sin($lat_b) +
cos($lat_a) * cos($lat_b) * cos($long_b - $long_a)
) * 6371;
if ($distance<=0.2)
{
$amount200a++;
if ($distance<=0.05)
{
$amount50a++;
}
}
} // for %_i
$amount200p=100*number_format($amount200a/$max,2,'.','');
$amount50p=100*number_format($amount50a/$max,2,'.','');
$dist.=$data[$i][0]."&&".$amount200a."&&".$amount200p."&&".$amount50a."&&".$amount50p."%%";
} // for $i
It will be better to read I think and if you change the commented out line of the for $_i it will be faster at all :)

Implement atomic counter in Memcached without cas

We have a web page want to limit uo to 100 people can access concurrently, so we use a memcached to implement a global counter, e.g.
We are using http://www.php.net/manual/en/class.memcache.php so there is not cas, current code is something like
$count = $memcache_obj->get('count');
if ($count < 100) {
$memcache_obj->set('count', $count+1);
echo "Welcome";
} else {
echo "No luck";
}
As you can see there is race condition in the above code and but if we are not going to replace memcached extension which support cas, it is able to support it using PHP code only?
As answer to "emcconville". This is non-blocking even without CAS.
If your concerned about race conditions, and the count value is completely arbitrary, you can use Memcache::increment directly before any business logic.
The increment method will return the current value after the incrementation takes place; of which, you can compare results. Increment will also return false if the key has yet to be set; allowing for your application to deal with it as needed.
$current = $memcache_obj->increment('count');
if($current === false) {
// NOT_FOUND, so let's create it
// Will return false if has just been created by someone else.
$memcache_obj->add('count',0); // <-- no risk of race-condition
// At this point 'count' key is created by us or someone else (other server/process).
// "increment" will update 0 or whatever it is at the moment by 1.
$current = $memcache_obj->increment('count')
echo "You are the $current!";
}
if ($current < 100) {
echo "Hazah! You are under the limit. Congrats!";
} else {
echo "Ah Snap! No Luck - you reached the limit.";
// If your worried about the value growing _too_ big, just drop the value down.
// $memcache_obj->decrement('count');
}
If your concerned about race conditions, and the count value is completely arbitrary, you can use Memcache::increment directly before any business logic.
The increment method will return the current value after the incrementation takes place; of which, you can compare results. Increment will also return false if the key has yet to be set; allowing for your application to deal with it as needed.
$current = $memcache_obj->increment('count');
if($current === false) {
// NOT_FOUND, so let's create it
$memcache_obj->set('count',1); // <-- still risk of race-condition
echo "Your the first!";
} else if ($current < 100) {
echo "Hazah! Your under the limit.";
} else {
echo "Ah Snap! No Luck";
// If your worried about the value growing _too_ big, just drop the value down.
// $memcache_obj->decrement('count');
}
function memcache_atomic_increment($counter_name, $delta = 1) {
$mc = new Memcache;
$mc->connect('localhost', 11211) or die("Could not connect");
while (($mc->increment($counter_name, $delta) === false) &&
($mc->add($counter_name, ($delta<0)?0:$delta, 0, 0) === false)) {
// loop until one of them succeeds
}
return intval($mc->get($counter_name));
}
The comments in Memcache::add include an example locking function, have you tried it out?

Categories