prevent 'undefined index' error

This commit is contained in:
Steffen Schröder 2014-06-29 17:24:01 +02:00
parent 8d5359bb82
commit 3151ed2bc6

View File

@ -72,6 +72,7 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
private $updateManialink = false; private $updateManialink = false;
/** @var ManiaLink $manialink */ /** @var ManiaLink $manialink */
private $manialink = null; private $manialink = null;
// TODO: use some sort of model class instead of various array keys that you can't remember
private $mxKarma = array(); private $mxKarma = array();
/** /**
@ -370,17 +371,21 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
} }
$content = json_encode($properties); $content = json_encode($properties);
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_GET_MAP_RATING . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data, $error) use (&$player) { $this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_GET_MAP_RATING . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data,
$error) use
(
&$player
) {
if (!$error) { if (!$error) {
$data = json_decode($data); $data = json_decode($data);
if ($data->success) { if ($data->success) {
// Fetch averages if its for the whole server // Fetch averages if its for the whole server
if (!$player) { if (!$player) {
$this->mxKarma["voteCount"] = $data->data->votecount; $this->mxKarma['voteCount'] = $data->data->votecount;
$this->mxKarma["voteAverage"] = $data->data->voteaverage; $this->mxKarma['voteAverage'] = $data->data->voteaverage;
$this->mxKarma["modeVoteCount"] = $data->data->modevotecount; $this->mxKarma['modeVoteCount'] = $data->data->modevotecount;
$this->mxKarma["modeVoteAverage"] = $data->data->modevoteaverage; $this->mxKarma['modeVoteAverage'] = $data->data->modevoteaverage;
} }
foreach ($data->data->votes as $votes) { foreach ($data->data->votes as $votes) {
@ -479,7 +484,8 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$content = json_encode($properties); $content = json_encode($properties);
$this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_SAVE_VOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data, $error) { $this->maniaControl->fileReader->postData(self::MX_KARMA_URL . self::MX_KARMA_SAVE_VOTES . "?sessionKey=" . urlencode($this->mxKarma['session']->sessionKey), function ($data,
$error) {
if (!$error) { if (!$error) {
$data = json_decode($data); $data = json_decode($data);
if ($data->success) { if ($data->success) {
@ -590,23 +596,35 @@ class KarmaPlugin implements CallbackListener, TimerListener, Plugin {
$map = $this->maniaControl->mapManager->getCurrentMap(); $map = $this->maniaControl->mapManager->getCurrentMap();
// Update vote in MX karma array // Update vote in MX karma array
if ($this->maniaControl->settingManager->getSettingValue($this, self::SETTING_MX_KARMA_ACTIVATED) && isset($this->mxKarma["session"])) { if ($this->maniaControl->settingManager->getSettingValue($this, self::SETTING_MX_KARMA_ACTIVATED) && isset($this->mxKarma['session'])) {
if (!isset($this->mxKarma["votes"][$player->login])) { if (!isset($this->mxKarma['votes'][$player->login])) {
$sum = $this->mxKarma["voteCount"] * $this->mxKarma["voteAverage"] + $vote * 100; if (!isset($this->mxKarma['voteCount'])) {
$this->mxKarma["voteCount"]++; $this->mxKarma['voteCount'] = 0;
}
if (!isset($this->mxKarma['voteAverage'])) {
$this->mxKarma['voteAverage'] = 0.;
}
$sum = $this->mxKarma['voteCount'] * $this->mxKarma['voteAverage'] + $vote * 100;
$this->mxKarma['voteCount']++;
$modeSum = $this->mxKarma["modeVoteCount"] * $this->mxKarma["modeVoteAverage"] + $vote * 100; if (!isset($this->mxKarma['modeVoteCount'])) {
$this->mxKarma["modeVoteCount"]++; $this->mxKarma['modeVoteCount'] = 0;
}
if (!isset($this->mxKarma['modeVoteAverage'])) {
$this->mxKarma['modeVoteAverage'] = 0;
}
$modeSum = $this->mxKarma['modeVoteCount'] * $this->mxKarma['modeVoteAverage'] + $vote * 100;
$this->mxKarma['modeVoteCount']++;
} else { } else {
$oldVote = $this->mxKarma["votes"][$player->login]; $oldVote = $this->mxKarma['votes'][$player->login];
$sum = $this->mxKarma["voteCount"] * $this->mxKarma["voteAverage"] - $oldVote + $vote * 100; $sum = $this->mxKarma['voteCount'] * $this->mxKarma['voteAverage'] - $oldVote + $vote * 100;
$modeSum = $this->mxKarma["modeVoteCount"] * $this->mxKarma["modeVoteAverage"] - $oldVote + $vote * 100; $modeSum = $this->mxKarma['modeVoteCount'] * $this->mxKarma['modeVoteAverage'] - $oldVote + $vote * 100;
} }
//FIXME, how can here ever be division by zero?, a voting just happened before, and a vote of a player is set //FIXME, how can here ever be division by zero?, a voting just happened before, and a vote of a player is set
//edit problem is if someone votes on one server (on a map which has no votes yet, joins another server than where same map is running and votes again) //edit problem is if someone votes on one server (on a map which has no votes yet, joins another server than where same map is running and votes again)
$this->mxKarma["voteAverage"] = $sum / $this->mxKarma["voteCount"]; $this->mxKarma['voteAverage'] = $sum / $this->mxKarma['voteCount'];
$this->mxKarma["modeVoteAverage"] = $modeSum / $this->mxKarma["modeVoteCount"]; $this->mxKarma['modeVoteAverage'] = $modeSum / $this->mxKarma['modeVoteCount'];
$this->mxKarma["votes"][$player->login] = $vote * 100; $this->mxKarma['votes'][$player->login] = $vote * 100;
} }
$voted = $this->getPlayerVote($player, $map); $voted = $this->getPlayerVote($player, $map);