mirvatJ
mirvatJ

Reputation: 376

scoring system calculates sometimes right and in others wrong

I have a scoring system for a online game, that is based on people creating teams out of a list of a players.

once the player score fields are entered, the below script should calculate the points based on predefined points multiplier and update the data across 3 tables

  1. players table (where i update the player score1 and score2 and his points)
  2. score table (where i add the new scores for each player "same fields")
  3. update all teams score1, score2 and points where this player id exists

to have an idea about my db structure here it is briefly

players table

  • id
  • score1
  • score2
  • total

score table

  • id
  • playerid
  • score1
  • score2
  • total
  • time

teams table

  • id
  • userid
  • p1
  • p2
  • b3
  • b4
  • score1 (this is the sum of all score1 scored by all 4 players)
  • score2 (this is the sum of all score2 scored by all 4 players)
  • total (this is the sum of all calculates scores by all 4 players)
  • time

the problem

surprisingly step 1 and step 2 works perfectly, scores are added and players are added correctly but when i get to step3 some of the teams gets updated correctly some of them don't update with the correct scores

my code

//predefined points multiplier
define("points1", 7);
define("points2", 8);

if (isset($_POST['submit'])) {

$id = $_POST['id'];
$score1= $_POST['score1'];
$score2 = $_POST['score2'];

foreach($id as $key=>$player){

//calculate the total points based on their predefined multipliers 
$totalpoints = 0;
$totalpoints += $score1[$key] * points1;
$totalpoints += $score2[$key] * points2;

    //update player table
    #send a request to api to update this player data

    //add scores
    #send a request to api to add the new scores to the score table

    //update teams (updates the teams for users)
    $raw = [
        'id' => $id,
        'score1' => $score1[$key],
        'score2' => $score2[$key],
        'total' => $totalpoints
    ];

    $data = http_build_query ($raw);

    $result = file_get_contents(
        BASE_URL . 'update',
        false,
        stream_context_create(array(
            PROTOCOL => array(
                'method' => 'PUT',
                'header' => array(
                    'Content-Length: ' . strlen($data),
                    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'
                ),
                'content' => $data
            )
        ))
    );
    $response = json_decode($result, false);

    }//end foreach
    echo $response->notice->message;
}

Upvotes: 1

Views: 65

Answers (1)

Peter M
Peter M

Reputation: 1059

The way you're doing it now $totalpoints will always be 0.

the players get updated correctly because the value for the scores is being set inside the foreach loop

$totalpoints = 0;
$totalpoints += $score1[$key] * points1; // <- $key does not exist result will be 0
$totalpoints += $score2[$key] * points2; // <- $key does not exist result will be 0

foreach($id as $key=>$player){
    $raw = [
        'id' => $id,
        'score1' => $score1[$key],  // <- $key has a value
        'score2' => $score2[$key],  // <- $key has a value
        'total' => $totalpoints  // <- result is always 0
    ];
}

try changing it into

foreach($id as $key=>$player){
    $totalpoints = 0;
    $totalpoints += $score1[$key] * points1; // <- $key has a value
    $totalpoints += $score2[$key] * points2; // <- $key has a value

    $raw = [
        'id' => $id,
        'score1' => $score1[$key],  // <- $key has a value
        'score2' => $score2[$key],  // <- $key has a value
        'total' => $totalpoints  // now does have a chance to be other then just 0
    ];
}

Upvotes: 1

Related Questions