| 1 |
<?php
|
| 2 |
// $Id: bookmaker.module,v 1.61 2008/10/28 16:17:17 toddy Exp $
|
| 3 |
/*
|
| 4 |
* Copyright (C) 2006-2008 Tobias Quathamer <t.quathamer@gmx.net>
|
| 5 |
*
|
| 6 |
* This file is part of Bookmaker.
|
| 7 |
*
|
| 8 |
* Bookmaker is free software; you can redistribute it and/or modify
|
| 9 |
* it under the terms of the GNU General Public License as published by
|
| 10 |
* the Free Software Foundation; either version 2 of the License, or
|
| 11 |
* (at your option) any later version.
|
| 12 |
*
|
| 13 |
* Bookmaker is distributed in the hope that it will be useful,
|
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 16 |
* GNU General Public License for more details.
|
| 17 |
*
|
| 18 |
* You should have received a copy of the GNU General Public License
|
| 19 |
* along with Bookmaker; if not, write to the Free Software
|
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
| 21 |
*/
|
| 22 |
|
| 23 |
/**
|
| 24 |
* @file
|
| 25 |
* Helper functions for the bookmaker module
|
| 26 |
*/
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Convert a string into an associative array
|
| 30 |
*
|
| 31 |
* This function is used to perform the conversion from the stored
|
| 32 |
* points string in the database to an array, which can be used for
|
| 33 |
* generating the edit form or calculation purposes.
|
| 34 |
*
|
| 35 |
* Format:
|
| 36 |
* string "exact=5\ntendency=15"
|
| 37 |
*
|
| 38 |
* Result:
|
| 39 |
* array('exact' => 5, 'tendency' => 15)
|
| 40 |
*/
|
| 41 |
function _bookmaker_betoffer_points_array($point_string) {
|
| 42 |
$result = array();
|
| 43 |
|
| 44 |
// each key/value pair is expected to be on a single line
|
| 45 |
$lines = split("\n", $point_string);
|
| 46 |
foreach ($lines as $line) {
|
| 47 |
// there can only be one equal sign on a line
|
| 48 |
$equal_sign = strpos($line, '=');
|
| 49 |
if ($equal_sign !== FALSE) {
|
| 50 |
// there is at least one equal sign in the line, so get the key
|
| 51 |
$key = trim(substr($line, 0, $equal_sign));
|
| 52 |
$value = (int) substr($line, $equal_sign + 1);
|
| 53 |
// if the value is 0, skip the assignment
|
| 54 |
if ($value != 0) {
|
| 55 |
$result[$key] = $value;
|
| 56 |
}
|
| 57 |
}
|
| 58 |
}
|
| 59 |
|
| 60 |
return $result;
|
| 61 |
}
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
/**
|
| 66 |
* Convert an associative array into a string
|
| 67 |
*
|
| 68 |
* This function is used to perform the conversion from a submitted
|
| 69 |
* array to a points string, suitable for storing in the database.
|
| 70 |
*
|
| 71 |
* Format:
|
| 72 |
* array('exact' => 5, 'tendency' => 15)
|
| 73 |
*
|
| 74 |
* Result:
|
| 75 |
* string "exact=5\ntendency=15"
|
| 76 |
*/
|
| 77 |
function _bookmaker_betoffer_points_string($point_array) {
|
| 78 |
$result = array();
|
| 79 |
foreach ($point_array as $key => $value) {
|
| 80 |
// don't include normal indexes
|
| 81 |
if (!is_integer($key)) {
|
| 82 |
// if the value is 0, skip the assignment
|
| 83 |
if ((int)$value != 0) {
|
| 84 |
$result[] = $key ."=". (int)$value;
|
| 85 |
}
|
| 86 |
}
|
| 87 |
}
|
| 88 |
return join("\n", $result);
|
| 89 |
}
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
/**
|
| 94 |
* Calculate the points every user gets for their bet on a bet offer
|
| 95 |
*/
|
| 96 |
function bookmaker_calculate_user_points($outcome, $user_bets, $points) {
|
| 97 |
$user_points = array();
|
| 98 |
|
| 99 |
// cycle through every user bet
|
| 100 |
foreach ($user_bets as $uid => $bet) {
|
| 101 |
// make sure the user gets a fresh start with the points
|
| 102 |
$user_points[$uid] = 0;
|
| 103 |
|
| 104 |
// calculate the points for an exact match
|
| 105 |
if ($points['exact'] != 0) {
|
| 106 |
if ($outcome == $bet) {
|
| 107 |
$user_points[$uid] += $points['exact'];
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
| 111 |
// calculate the points for a correct tendency
|
| 112 |
// this is aimed at team sports such as soccer. the current divider
|
| 113 |
// to be used is the colon, e.g. outcome = 4:3, bet = 3:1
|
| 114 |
if ($points['tendency'] != 0) {
|
| 115 |
// don't assign points to a bet equalling the outcome
|
| 116 |
// also, only start calculation if there is a colon
|
| 117 |
if ($outcome != $bet && strpos($outcome, ':') > 0) {
|
| 118 |
// split the outcome by the colon
|
| 119 |
$split = explode(':', $outcome);
|
| 120 |
$outcome_a = $split[0];
|
| 121 |
$outcome_b = $split[1];
|
| 122 |
|
| 123 |
// split the bet by the colon
|
| 124 |
$split = explode(':', $bet);
|
| 125 |
$bet_a = $split[0];
|
| 126 |
$bet_b = $split[1];
|
| 127 |
|
| 128 |
if ($outcome_a > $outcome_b && $bet_a > $bet_b) {
|
| 129 |
// this is a correct tendency like 4:2 -> 6:0
|
| 130 |
$user_points[$uid] += $points['tendency'];
|
| 131 |
}
|
| 132 |
else if ($outcome_a < $outcome_b && $bet_a < $bet_b) {
|
| 133 |
// this is a correct tendency like 2:4 -> 0:6
|
| 134 |
$user_points[$uid] += $points['tendency'];
|
| 135 |
}
|
| 136 |
else if ($outcome_a == $outcome_b && $bet_a == $bet_b) {
|
| 137 |
// this is a correct tendency like 3:3 -> 1:1
|
| 138 |
$user_points[$uid] += $points['tendency'];
|
| 139 |
}
|
| 140 |
}
|
| 141 |
}
|
| 142 |
}
|
| 143 |
|
| 144 |
return $user_points;
|
| 145 |
}
|