I'm struggling with some "simple" algorithm for some time now and after searching whole internet for the answer I gave up..... sad, I know. On the beginning I wanted to apologize for my English grammar etc.
Based on this query:
SELECT a.*, b.*
FROM mecze a
JOIN bets b
ON a.mecz_id = b.mecz_id AND a.wynik != 'NULL'
I have an array "punkty" with such values below:
[0] => Array
(
[mecz_id] => 1 //match_id
[druzyna_1_id] => 1 //team_1_id
[druzyna_2_id] => 2 //team_2_id
[wynik] => 1:2 //score - of the match
[wynik_buk] => 2 //bookie score - of the match
[bet_id] => 1 //users bet id
[user_id] => 1 //user id :)
[bet_wynik] => 3:2 //users bet score
[bet_wynik_buk] => 1 //users bookie bet score
[krol] => Mario Gomez (GER)
[laczny] => 5 pkt � Hiszpania ( 2 pkt )
[punkty] => 0 //points - here will be added when calculated
)
[1] => Array
(
[mecz_id] => 2
[druzyna_1_id] => 3
[druzyna_2_id] => 4
[wynik] => 3:2
[wynik_buk] => 1
[bet_id] => 2
[user_id] => 1
[bet_wynik] => 3:2
[bet_wynik_buk] => 1
[krol] => Mario Gomez (GER)
[laczny] => 5 pkt � Hiszpania ( 2 pkt )
[punkty] => 0
)
[2] => Array
(
[mecz_id] => 3
[druzyna_1_id] => 2
[druzyna_2_id] => 4
[wynik] => 1:1
[wynik_buk] => 0
[bet_id] => 3
[user_id] => 1
[bet_wynik] => 1:1
[bet_wynik_buk] => 0
[krol] => Mario Gomez (GER)
[laczny] => 5 pkt � Hiszpania ( 2 pkt )
[punkty] => 0
)
[3] => Array
(
[mecz_id] => 1
[druzyna_1_id] => 1
[druzyna_2_id] => 2
[wynik] => 1:2
[wynik_buk] => 2
[bet_id] => 6
[user_id] => 4
[bet_wynik] => 3:1
[bet_wynik_buk] => 1
[krol] => Mario Gomez (GER)
[laczny] => 5 pkt � Hiszpania ( 2 pkt )
[punkty] => 0
)
[4] => Array
(
[mecz_id] => 2
[druzyna_1_id] => 3
[druzyna_2_id] => 4
[wynik] => 3:2
[wynik_buk] => 1
[bet_id] => 7
[user_id] => 4
[bet_wynik] => 2:3
[bet_wynik_buk] => 2
[krol] => Mario Gomez (GER)
[laczny] => 5 pkt � Hiszpania ( 2 pkt )
[punkty] => 0
)
[5] => Array
(
[mecz_id] => 3
[druzyna_1_id] => 2
[druzyna_2_id] => 4
[wynik] => 1:1
[wynik_buk] => 0
[bet_id] => 8
[user_id] => 4
[bet_wynik] => 0:0
[bet_wynik_buk] => 0
[krol] => Mario Gomez (GER)
[laczny] => 5 pkt � Hiszpania ( 2 pkt )
[punkty] => 0
)
[6] => Array
(
[mecz_id] => 1
[druzyna_1_id] => 1
[druzyna_2_id] => 2
[wynik] => 1:2
[wynik_buk] => 2
[bet_id] => 9
[user_id] => 5
[bet_wynik] => 1:2
[bet_wynik_buk] => 2
[krol] => Mario Gomez (GER)
[laczny] => 5 pkt � Hiszpania ( 2 pkt )
[punkty] => 0
)
[7] => Array
(
[mecz_id] => 2
[druzyna_1_id] => 3
[druzyna_2_id] => 4
[wynik] => 3:2
[wynik_buk] => 1
[bet_id] => 10
[user_id] => 5
[bet_wynik] => 3:3
[bet_wynik_buk] => 0
[krol] => Mario Gomez (GER)
[laczny] => 5 pkt � Hiszpania ( 2 pkt )
[punkty] => 0
)
[8] => Array
(
[mecz_id] => 3
[druzyna_1_id] => 2
[druzyna_2_id] => 4
[wynik] => 1:1
[wynik_buk] => 0
[bet_id] => 11
[user_id] => 5
[bet_wynik] => 1:1
[bet_wynik_buk] => 0
[krol] => Mario Gomez (GER)
[laczny] => 5 pkt � Hiszpania ( 2 pkt )
[punkty] => 0
)
Based on the array above I'm assigning points for getting a good score and exact result:
1 point when a player have correct score (i.e. 1 or X or 2)
4 points when a player have correct exact score (i.e. 1:1 or 3:1 etc.)
foreach ( $punkty as $k => $v ){
$wbuk = $v['wynik_buk']; //match bookie score
$bwbuk = $v['bet_wynik_buk']; //users bookie bet score
$w = $v['wynik']; //match score
$bw = $v['bet_wynik']; //users bet score
if( $wbuk == $bwbuk ) {
if ( $w == $bw ) {
$pkt = 4;
}else{
$pkt = 1;
}
} else {
$pkt = 0;
}
}
The above code is assigning the points correctly but I want to add another thing to it.
I want to add some bonus points for hitting the right score or right exact result if only one user has this score/result:
2 points if user is the only one with right score (i.e. 1 or X or 2)
1 point if user is the only one with right exact result (i.e. 3:1 or 1:1 etc.)
Please help !!! I know that there must be thousand of ways to do it but my mind is in StackOverflow state and crashed :)