@ -16,29 +16,32 @@ class XoiPage extends Page {
$sum["checkouts"][0] += $stat2["checkouts"][0];
$sum["checkouts"][0] += $stat2["checkouts"][0];
$sum["checkoutPoints"] = array_merge($sum["checkoutPoints"], $stat2["checkoutPoints"]);
$sum["checkoutPoints"] = array_merge($sum["checkoutPoints"], $stat2["checkoutPoints"]);
$sum["checkouts"][1] += $stat2["checkouts"][1];
$sum["checkouts"][1] += $stat2["checkouts"][1];
$sum["checkins"][0] += $stat2["checkins"][0];
$sum["checkinPoints"] = array_merge($sum["checkinPoints"], $stat2["checkinPoints"]);
$sum["checkins"][1] += $stat2["checkins"][1];
$sum["180"] += $stat2["180"];
$sum["180"] += $stat2["180"];
$sum["140+"] += $stat2["140+"];
$sum["140+"] += $stat2["140+"];
$sum["100+"] += $stat2["100+"];
$sum["100+"] += $stat2["100+"];
$sum["60+"] += $stat2["60+"];
$sum["60+"] += $stat2["60+"];
return $sum;
return $sum;
}
}
//
/**
// / **
* @kql-allowed
// * @kql-allowed
*/
// */
public function tournamentStats() {
// public function tournamentStats() {
if (count($this->playerUUIDs()) == 2){
// if (count($this->playerUUIDs()) == 2){
return [$this->parent()->getStats($this->playerUUIDs()[0]),$this->parent()->getStats($this->playerUUIDs()[1])];
// return [$this->parent()->getStats($this->playerUUIDs()[0]),$this->parent()->getStats($this->playerUUIDs()[1])];
}
// }
return [];
// return [];
}
// }
//
public function reorderPlayer($order){
// public function reorderPlayer($order){
kirby()->impersonate('kirby');
// kirby()->impersonate('kirby');
return $this->update([
// return $this->update([
"players" => $order
// "players" => $order
]);
// ]);
}
// }
public function playerUUIDs(){
public function playerUUIDs(){
$pp = $this->players()->toPages();
$pp = $this->players()->toPages();
$players = [];
$players = [];
@ -47,58 +50,58 @@ class XoiPage extends Page {
}
}
return $players;
return $players;
}
}
//
public function getPlayerPos($playerUUIDs, $uuid)
// public function getPlayerPos($playerUUIDs, $uuid)
{
// {
$k = 0;
// $k = 0;
foreach ($playerUUIDs as $i => $playeruuid) {
// foreach ($playerUUIDs as $i => $playeruuid) {
if ($playeruuid == $uuid) {
// if ($playeruuid == $uuid) {
break;
// break;
}
// }
$k++;
// $k++;
}
// }
return $k;
// return $k;
}
// }
// entity can be visit, leg or set
// // entity can be visit, leg or set
public function nextPlayer($playerUUIDs, $uuid)
// public function nextPlayer($playerUUIDs, $uuid)
{
// {
$k = $this->getPlayerPos($playerUUIDs, $uuid);
// $k = $this->getPlayerPos($playerUUIDs, $uuid);
return $playerUUIDs[($k+1)%count($playerUUIDs)];
// return $playerUUIDs[($k+1)%count($playerUUIDs)];
}
// }
//
public function updateStats(& $stats, $visit){
// public function updateStats(& $stats, $visit){
$playerUUIDs = $this->playerUUIDs();
// $playerUUIDs = $this->playerUUIDs();
$k = $this->getPlayerPos($playerUUIDs, $visit["player"]);
// $k = $this->getPlayerPos($playerUUIDs, $visit["player"]);
//
$todos = [&$stats["stats"][$k]];
// $todos = [&$stats["stats"][$k]];
$todos[] = &last($stats["sets"])["stats"][$k];
// $todos[] = &last($stats["sets"])["stats"][$k];
$todos[] = &last(last($stats["sets"])["legs"])["stats"][$k];
// $todos[] = &last(last($stats["sets"])["legs"])["stats"][$k];
//
foreach ($todos as $i => $value) {
// foreach ($todos as $i => $value) {
$todos[$i]["average"][0] += $visit["sum"];
// $todos[$i]["average"][0] += $visit["sum"];
$todos[$i]["average"][1] += $visit["numDarts"];
// $todos[$i]["average"][1] += $visit["numDarts"];
if ($visit["visit"] < 4 ) {
// if ($visit["visit"] < 4 ) {
$todos[$i]["first9"][0] += $visit["sum"];
// $todos[$i]["first9"][0] += $visit["sum"];
$todos[$i]["first9"][1] += $visit["numDarts"];
// $todos[$i]["first9"][1] += $visit["numDarts"];
}
// }
if ($visit["toGo"][$k] == 0) {
// if ($visit["toGo"][$k] == 0) {
$todos[$i]["checkouts"][0] += 1;
// $todos[$i]["checkouts"][0] += 1;
$todos[$i]["checkoutPoints"][] += $visit["sum"];
// $todos[$i]["checkoutPoints"][] += $visit["sum"];
}
// }
$todos[$i]["checkouts"][1] += $visit["checkoutTries"];
// $todos[$i]["checkouts"][1] += $visit["checkoutTries"];
if ($visit["sum"] == 180) {
// if ($visit["sum"] == 180) {
$todos[$i]["180"] += 1;
// $todos[$i]["180"] += 1;
} elseif ($visit["sum"] >= 140) {
// } elseif ($visit["sum"] >= 140) {
$todos[$i]["140+"] += 1;
// $todos[$i]["140+"] += 1;
} elseif ($visit["sum"] >= 100) {
// } elseif ($visit["sum"] >= 100) {
$todos[$i]["100+"] += 1;
// $todos[$i]["100+"] += 1;
} elseif ($visit["sum"] >= 60) {
// } elseif ($visit["sum"] >= 60) {
$todos[$i]["60+"] += 1;
// $todos[$i]["60+"] += 1;
}
// }
}
// }
return $stats;
// return $stats;
}
// }
//
private function newVisit($player, $round)
private function newVisit($player, $round)
{
{
return array(
return array(
@ -106,171 +109,172 @@ class XoiPage extends Page {
'throws' => [],
'throws' => [],
'visit' => $round,
'visit' => $round,
'checkoutTries' => 0,
'checkoutTries' => 0,
'checkinTries' => 0,
'numDarts' => 0,
'numDarts' => 0,
);
);
}
}
//
public function calcPoints($throw)
// public function calcPoints($throw)
{
// {
$throw = trim($throw);
// $throw = trim($throw);
if ($throw == "") {
// if ($throw == "") {
return 0;
// return 0;
}
// }
if ($throw == "SB"){
// if ($throw == "SB"){
return 25;
// return 25;
}
// }
if ($throw == "DB"){
// if ($throw == "DB"){
return 50;
// return 50;
}
// }
if ($throw[0] == "S" || $throw[0] == "O" || $throw[0] == "I"){
// if ($throw[0] == "S" || $throw[0] == "O" || $throw[0] == "I"){
return intval(substr($throw, 1));
// return intval(substr($throw, 1));
}
// }
if ($throw[0] == "D"){
// if ($throw[0] == "D"){
return 2*intval(substr($throw, 1));
// return 2*intval(substr($throw, 1));
}
// }
if ($throw[0] == "T"){
// if ($throw[0] == "T"){
return 3*intval(substr($throw, 1));
// return 3*intval(substr($throw, 1));
}
// }
if ($throw[0] == "M"){
// if ($throw[0] == "M"){
return 0;
// return 0;
} else {
// } else {
// TODO: Check for Na
// // TODO: Check for Na
return intval($throw);
// return intval($throw);
}
// }
}
// }
//
public function sumPoints($throws)
// public function sumPoints($throws)
{
// {
$sum = 0;
// $sum = 0;
foreach ($throws as $i => $throw) {
// foreach ($throws as $i => $throw) {
$points = $this->calcPoints($throw);
// $points = $this->calcPoints($throw);
$sum += $points;
// $sum += $points;
}
// }
return $sum;
// return $sum;
}
// }
//
public function getWinner($points, $mode)
// public function getWinner($points, $mode)
{
// {
$sum = 0;
// $sum = 0;
$k = 0;
// $k = 0;
$max = -1;
// $max = -1;
foreach ($points as $i => $point) {
// foreach ($points as $i => $point) {
$sum += $point;
// $sum += $point;
if ($max < $point) {
// if ($max < $point) {
$max = $point;
// $max = $point;
$maxidx = $k;
// $maxidx = $k;
}
// }
$k++;
// $k++;
}
// }
if ($max > $mode/$k) {
// if ($max > $mode/$k) {
return $maxidx;
// return $maxidx;
}
// }
if ($sum == $mode) {
// if ($sum == $mode) {
return -2;
// return -2;
}
// }
return -1;
// return -1;
}
// }
//
public function clearLastVisit()
// public function clearLastVisit()
{
// {
$game = $this->rounds()->parseJSON();
// $game = $this->rounds()->parseJSON();
$set = &$game["sets"][count($game["sets"])-1];
// $set = &$game["sets"][count($game["sets"])-1];
$leg = &$set["legs"][count($set["legs"])-1];
// $leg = &$set["legs"][count($set["legs"])-1];
$visit = &$leg["visits"][count($leg["visits"])-1];
// $visit = &$leg["visits"][count($leg["visits"])-1];
$visit["throws"] = [];
// $visit["throws"] = [];
$visit["checkoutTries"] = 0;
// $visit["checkoutTries"] = 0;
$visit["numDarts"] = 0;
// $visit["numDarts"] = 0;
unset($visit["sum"]);
// unset($visit["sum"]);
unset($visit["toGo"]);
// unset($visit["toGo"]);
return $this->storeGame($game);
// return $this->storeGame($game);
}
// }
//
public function addThrows($throws, $numDarts, $checkoutTries, $done=true)
// public function addThrows($throws, $numDarts, $checkoutTries, $done=true)
{
// {
$game = $this->rounds()->parseJSON();
// $game = $this->rounds()->parseJSON();
$set = &$game["sets"][count($game["sets"])-1];
// $set = &$game["sets"][count($game["sets"])-1];
$leg = &$set["legs"][count($set["legs"])-1];
// $leg = &$set["legs"][count($set["legs"])-1];
$visit = &$leg["visits"][count($leg["visits"])-1];
// $visit = &$leg["visits"][count($leg["visits"])-1];
$current_throws = $visit['throws'];
// $current_throws = $visit['throws'];
if (count($current_throws)+count($throws) > 3) {
// if (count($current_throws)+count($throws) > 3) {
$error = "Two many throws given: ".count($current_throws)." + ".count($throws)." > 3";
// $error = "Two many throws given: ".count($current_throws)." + ".count($throws)." > 3";
throw new \Exception($error, 1);
// throw new \Exception($error, 1);
}
// }
$new_throws = array_merge($current_throws, $throws);
// $new_throws = array_merge($current_throws, $throws);
//
$playerUUIDs = $this->playerUUIDs();
// $playerUUIDs = $this->playerUUIDs();
$k = $this->getPlayerPos($playerUUIDs, $visit["player"]);
// $k = $this->getPlayerPos($playerUUIDs, $visit["player"]);
if (count($leg["visits"])-2 < 0 ) {
// if (count($leg["visits"])-2 < 0 ) {
$toGo = array_fill(0, count($playerUUIDs), $this->max()->toInt());
// $toGo = array_fill(0, count($playerUUIDs), $this->max()->toInt());
} else {
// } else {
$toGo = $leg["visits"][count($leg["visits"])-2]["toGo"];
// $toGo = $leg["visits"][count($leg["visits"])-2]["toGo"];
}
// }
//
$visit["numDarts"] += $numDarts;
// $visit["numDarts"] += $numDarts;
$visit["throws"] = $new_throws;
// $visit["throws"] = $new_throws;
$visit["checkoutTries"] += $checkoutTries;
// $visit["checkoutTries"] += $checkoutTries;
$visit["sum"] = $this->sumPoints($visit["throws"]);
// $visit["sum"] = $this->sumPoints($visit["throws"]);
//
$rest = $toGo[$k] - $visit["sum"];
// $rest = $toGo[$k] - $visit["sum"];
if ($rest < 0 or ( $ this- > out()->toString() == "Double" & & $rest == 1)) {
// if ($rest < 0 or ( $ this- > out()->toString() == "Double" & & $rest == 1)) {
$visit["sum"] = 0;
// $visit["sum"] = 0;
} else {
// } else {
$toGo[$k] = $rest;
// $toGo[$k] = $rest;
}
// }
$visit["toGo"] = $toGo;
// $visit["toGo"] = $toGo;
if (!$done) {
// if (!$done) {
$this->storeGame($game);
// $this->storeGame($game);
return;
// return;
}
// }
//
$stats = $this->stats()->parseJSON();
// $stats = $this->stats()->parseJSON();
$page = $this->updateStats($stats, $visit);
// $page = $this->updateStats($stats, $visit);
$update = [];
// $update = [];
if ($rest != 0) {
// if ($rest != 0) {
// Normal case...next players turn
// // Normal case...next players turn
$nextPlayer = $this->nextPlayer($playerUUIDs, $visit["player"]);
// $nextPlayer = $this->nextPlayer($playerUUIDs, $visit["player"]);
$isNextRound = 0;
// $isNextRound = 0;
if (last(last($game["sets"])["legs"])['visits'][0]["player"] == $nextPlayer) {
// if (last(last($game["sets"])["legs"])['visits'][0]["player"] == $nextPlayer) {
$isNextRound = 1;
// $isNextRound = 1;
}
// }
$newVisit = $this->newVisit($nextPlayer, $visit["visit"]+$isNextRound);
// $newVisit = $this->newVisit($nextPlayer, $visit["visit"]+$isNextRound);
last(last($game["sets"])["legs"])['visits'][] = $newVisit;
// last(last($game["sets"])["legs"])['visits'][] = $newVisit;
} else {
// } else {
// rest == 0 leg finished
// // rest == 0 leg finished
// updates Stats...this is maybe the wrong place to store points
// // updates Stats...this is maybe the wrong place to store points
$newlegp = $leg["points"];
// $newlegp = $leg["points"];
$newlegp[$k] += 1;
// $newlegp[$k] += 1;
$winner = $this->getWinner($newlegp, $this->legs()->toInt());
// $winner = $this->getWinner($newlegp, $this->legs()->toInt());
if ($winner != -1) {
// if ($winner != -1) {
$newsetp = $set["points"];
// $newsetp = $set["points"];
$newsetp[$k] += 1;
// $newsetp[$k] += 1;
$winner = $this->getWinner($newsetp, $this->sets()->toInt());
// $winner = $this->getWinner($newsetp, $this->sets()->toInt());
if ($winner != -1) {
// if ($winner != -1) {
if ($winner == -2) {
// if ($winner == -2) {
$stats["winner"] = "DRAW";
// $stats["winner"] = "DRAW";
} else {
// } else {
$stats["winner"] = $visit["player"];
// $stats["winner"] = $visit["player"];
}
// }
// Game Over
// // Game Over
$update = [
// $update = [
"enddate" => date("Y-m-d H:i:s")
// "enddate" => date("Y-m-d H:i:s")
];
// ];
} else {
// } else {
// new Set
// // new Set
$this->addNewSet($game, $newsetp);
// $this->addNewSet($game, $newsetp);
$stats = $this->addNewSetStats($stats);
// $stats = $this->addNewSetStats($stats);
$stats = $this->addNewLegStats($stats);
// $stats = $this->addNewLegStats($stats);
}
// }
} else {
// } else {
// new Leg
// // new Leg
$this->addNewLeg($game, $newlegp);
// $this->addNewLeg($game, $newlegp);
$stats = $this->addNewLegStats($stats);
// $stats = $this->addNewLegStats($stats);
}
// }
}
// }
$page = $this->storeGame($game);
// $page = $this->storeGame($game);
$page = $page->storeStats($stats);
// $page = $page->storeStats($stats);
$page->update($update);
// $page->update($update);
}
// }
private function newSingleStats($player)
private function newSingleStats($player)
{
{
@ -280,6 +284,8 @@ class XoiPage extends Page {
"first9" => [0, 0],
"first9" => [0, 0],
"checkouts" => [0, 0],
"checkouts" => [0, 0],
"checkoutPoints" => [],
"checkoutPoints" => [],
"checkins" => [0, 0],
"checkinPoints" => [],
"60+" => 0,
"60+" => 0,
"100+" => 0,
"100+" => 0,
"140+" => 0,
"140+" => 0,
@ -298,35 +304,35 @@ class XoiPage extends Page {
}
}
return $stats;
return $stats;
}
}
//
private function addNewSet(& $game, $points){
// private function addNewSet(& $game, $points){
$playerUUIDs = $this->playerUUIDs();
// $playerUUIDs = $this->playerUUIDs();
$set = &$game["sets"][count($game["sets"])-1];
// $set = &$game["sets"][count($game["sets"])-1];
$leg = &$set["legs"][count($set["legs"])-1];
// $leg = &$set["legs"][count($set["legs"])-1];
$p = $this->nextPlayer($playerUUIDs, $set["legs"][0]["visits"][0]["player"]);
// $p = $this->nextPlayer($playerUUIDs, $set["legs"][0]["visits"][0]["player"]);
$game["sets"][] = array(
// $game["sets"][] = array(
'points' => $points,
// 'points' => $points,
'legs' => [
// 'legs' => [
array(
// array(
'points' => array_fill(0, count($playerUUIDs), 0),
// 'points' => array_fill(0, count($playerUUIDs), 0),
'visits' => [$this->newVisit($p, 1)]
// 'visits' => [$this->newVisit($p, 1)]
)
// )
]
// ]
);
// );
return $game;
// return $game;
}
// }
private function addNewLeg(& $game, $points){
// private function addNewLeg(& $game, $points){
$playerUUIDs = $this->playerUUIDs();
// $playerUUIDs = $this->playerUUIDs();
$set = &$game["sets"][count($game["sets"])-1];
// $set = &$game["sets"][count($game["sets"])-1];
$leg = &$set["legs"][count($set["legs"])-1];
// $leg = &$set["legs"][count($set["legs"])-1];
$p = $this->nextPlayer($playerUUIDs, $leg["visits"][0]["player"]);
// $p = $this->nextPlayer($playerUUIDs, $leg["visits"][0]["player"]);
$set["legs"][] = array(
// $set["legs"][] = array(
'points' => $points,
// 'points' => $points,
'visits' => [$this->newVisit($p, 1)]
// 'visits' => [$this->newVisit($p, 1)]
);
// );
return $game;
// return $game;
}
// }
//
private function addNewSetStats(& $stats){
private function addNewSetStats(& $stats){
$stats["sets"][] = $this->newStats();
$stats["sets"][] = $this->newStats();
$stats["sets"][0]["legs"] = [];
$stats["sets"][0]["legs"] = [];
@ -336,32 +342,32 @@ class XoiPage extends Page {
last($stats["sets"])["legs"][] = $this->newStats();
last($stats["sets"])["legs"][] = $this->newStats();
return $stats;
return $stats;
}
}
//
public function recalcStats(){
// public function recalcStats(){
$game = $this->rounds()->parseJSON();
// $game = $this->rounds()->parseJSON();
$page = $this->initStats();
// $page = $this->initStats();
$stats = $page->stats()->parseJSON();
// $stats = $page->stats()->parseJSON();
$lastset = count($game["sets"])-1;
// $lastset = count($game["sets"])-1;
foreach ($game["sets"] as $i => $set) {
// foreach ($game["sets"] as $i => $set) {
$lastleg = count($set["legs"])-1;
// $lastleg = count($set["legs"])-1;
foreach ($set["legs"] as $j => $leg) {
// foreach ($set["legs"] as $j => $leg) {
$lastvisit = count($leg["visits"])-1;
// $lastvisit = count($leg["visits"])-1;
foreach ($leg["visits"] as $k => $visit) {
// foreach ($leg["visits"] as $k => $visit) {
if (!($k == $lastvisit & & $j == $lastleg & & $i == $lastset)) {
// if (!($k == $lastvisit & & $j == $lastleg & & $i == $lastset)) {
$this->updateStats($stats, $visit);
// $this->updateStats($stats, $visit);
}
// }
if ($k == $lastvisit & & $j != $lastleg) {
// if ($k == $lastvisit & & $j != $lastleg) {
$this->addNewLegStats($stats);
// $this->addNewLegStats($stats);
}
// }
}
// }
if ($j == $lastleg & & $i != $lastset) {
// if ($j == $lastleg & & $i != $lastset) {
$this->addNewSetStats($stats);
// $this->addNewSetStats($stats);
}
// }
}
// }
}
// }
return $this->storeStats($stats);
// return $this->storeStats($stats);
}
// }
//
private function initStats()
private function initStats()
{
{
$stats = $this->newStats();
$stats = $this->newStats();
@ -394,20 +400,20 @@ class XoiPage extends Page {
kirby()->impersonate('kirby');
kirby()->impersonate('kirby');
return $page->update($update);
return $page->update($update);
}
}
//
public function storeStats($stats)
// public function storeStats($stats)
{
// {
kirby()->impersonate('kirby');
// kirby()->impersonate('kirby');
return $this->update([
// return $this->update([
"stats" => json_encode($stats)
// "stats" => json_encode($stats)
]);
// ]);
//
}
// }
public function storeGame($game)
// public function storeGame($game)
{
// {
kirby()->impersonate('kirby');
// kirby()->impersonate('kirby');
return $this->update([
// return $this->update([
"rounds" => json_encode($game)
// "rounds" => json_encode($game)
]);
// ]);
}
// }
}
}