improve custom gamemode settings detection
This commit is contained in:
parent
0e47b75e3d
commit
9b4073f456
@ -700,6 +700,35 @@ class MatchManagerCore implements CallbackListener, CommandListener, TimerListen
|
||||
* MARK: Internal Functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reset match variables
|
||||
*
|
||||
* @param Setting $setting
|
||||
*/
|
||||
public function resetMatchVariables() {
|
||||
$this->matchStarted = false;
|
||||
$this->matchrecover = false;
|
||||
$this->pauseon = false;
|
||||
$this->pointstorecover = array();
|
||||
$this->currentscore = array(); // TODO CHECK
|
||||
$this->preendroundscore = null;
|
||||
$this->settingsloaded = false;
|
||||
$this->mapsshuffled = false;
|
||||
$this->mapshidden = false;
|
||||
$this->hidenextmaps = false;
|
||||
$this->maps = array();
|
||||
$this->postmatch = true;
|
||||
$this->matchid = "";
|
||||
|
||||
$this->settings_nbroundsbymap = -1;
|
||||
$this->settings_nbwinners = 2;
|
||||
$this->settings_nbmapsbymatch = 0;
|
||||
$this->settings_pointlimit = 100;
|
||||
|
||||
$this->currentgmbase = "";
|
||||
$this->currentcustomgm = "";
|
||||
$this->currentsettingmode = "";
|
||||
}
|
||||
/**
|
||||
* Add items in AdminUI plugin
|
||||
*/
|
||||
@ -737,6 +766,28 @@ class MatchManagerCore implements CallbackListener, CommandListener, TimerListen
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function called to list matches
|
||||
*/
|
||||
public function getMatchesList(int $limit = 10) {
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
$stmt = $mysqli->prepare("SELECT `gamemodebase`,`started`,`ended` FROM `" . self::DB_MATCHESINDEX . "` ORDER BY `started` DESC LIMIT ?");
|
||||
$stmt->bind_param('i', $limit);
|
||||
|
||||
if (!$stmt->execute()) {
|
||||
Logger::logError('Error executing MySQL query: '. $stmt->error);
|
||||
}
|
||||
|
||||
$result = $stmt->get_result();
|
||||
while($row = $result->fetch_array()) {
|
||||
$array[] = $row;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
/*
|
||||
* MARK: Manage Settings
|
||||
*/
|
||||
/**
|
||||
* Update Widgets on Setting Changes
|
||||
*
|
||||
@ -804,6 +855,7 @@ class MatchManagerCore implements CallbackListener, CommandListener, TimerListen
|
||||
}
|
||||
}
|
||||
|
||||
if ($setting === null || $setting->setting === self::SETTING_MATCH_SETTINGS_MODE || $setting->setting === self::SETTING_MATCH_GAMEMODE_BASE || $setting->setting === self::SETTING_MATCH_CUSTOM_GAMEMODE) {
|
||||
if (defined("\ManiaControl\ManiaControl::ISTRACKMANIACONTROL") && $this->maniaControl->getSettingManager()->getSettingValue($this->maniaControl->getSettingManager(), SettingManager::SETTING_ALLOW_UNLINK_SERVER)) {
|
||||
$deletesettings = !$this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_MATCH_DONT_DELETE_SETTINGS);
|
||||
} else {
|
||||
@ -845,36 +897,6 @@ class MatchManagerCore implements CallbackListener, CommandListener, TimerListen
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reset match variables
|
||||
*
|
||||
* @param Setting $setting
|
||||
*/
|
||||
public function resetMatchVariables() {
|
||||
$this->matchStarted = false;
|
||||
$this->matchrecover = false;
|
||||
$this->pauseon = false;
|
||||
$this->pointstorecover = array();
|
||||
$this->currentscore = array(); // TODO CHECK
|
||||
$this->preendroundscore = null;
|
||||
$this->settingsloaded = false;
|
||||
$this->mapsshuffled = false;
|
||||
$this->mapshidden = false;
|
||||
$this->hidenextmaps = false;
|
||||
$this->maps = array();
|
||||
$this->postmatch = true;
|
||||
$this->matchid = "";
|
||||
|
||||
$this->settings_nbroundsbymap = -1;
|
||||
$this->settings_nbwinners = 2;
|
||||
$this->settings_nbmapsbymatch = 0;
|
||||
$this->settings_pointlimit = 100;
|
||||
|
||||
$this->currentgmbase = "";
|
||||
$this->currentcustomgm = "";
|
||||
$this->currentsettingmode = "";
|
||||
}
|
||||
|
||||
/**
|
||||
@ -893,27 +915,38 @@ class MatchManagerCore implements CallbackListener, CommandListener, TimerListen
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Array with all settings of the Gamemode
|
||||
*
|
||||
* @param String $gamemode
|
||||
* Get recursively Custom gamemodes settings
|
||||
* @param string $customgamemode
|
||||
* @return mixed
|
||||
*/
|
||||
public function getGMSettings(String $gamemodebase, String $customgamemode) {
|
||||
private function getCustomGMSettings(string $customgamemode) {
|
||||
$filename = $this->maniaControl->getServer()->getDirectory()->getUserDataFolder() . DIRECTORY_SEPARATOR . "Scripts" . DIRECTORY_SEPARATOR . $customgamemode;
|
||||
if (!file_exists($filename)) return [];
|
||||
|
||||
$fileContent = file_get_contents($filename);
|
||||
preg_match('/^\s*\#Extends\s+"(.*)"/m', $fileContent, $matches);
|
||||
|
||||
$gamesettings = [];
|
||||
|
||||
foreach (self::GAMEMODES_LIST_SETTINGS as $gamesetting => $info) {
|
||||
if (in_array('Global', $info['gamemode']) || in_array($gamemodebase, $info['gamemode'])) {
|
||||
$gamesettings = array_merge($gamesettings , array($gamesetting => $info));
|
||||
if (count($matches) >= 2) {
|
||||
$gamesettings = $this->getCustomGMSettings($matches[1]);
|
||||
}
|
||||
}
|
||||
if ($customgamemode != "") {
|
||||
$filename = $this->maniaControl->getServer()->getDirectory()->getUserDataFolder() . DIRECTORY_SEPARATOR . "Scripts" . DIRECTORY_SEPARATOR . "Modes" . DIRECTORY_SEPARATOR . $customgamemode;
|
||||
if (file_exists($filename)) {
|
||||
$handle = fopen($filename, "r");
|
||||
if ($handle) {
|
||||
while (($line = fgets($handle)) !== false) {
|
||||
|
||||
// Replace all line breaks and double quote between triple quotes strings.
|
||||
$fileContent = preg_replace_callback('/"""(.*?)"""/s', function($matches) {
|
||||
$processedString = str_replace(["\r\n", "\n", "\r"], " ", $matches[1]);
|
||||
$processedString = str_replace(["\""], "\\\"", $processedString);
|
||||
return '"' . $processedString . '"';
|
||||
}, $fileContent);
|
||||
|
||||
foreach(preg_split("/((\r?\n)|(\r\n?))/", $fileContent) as $line){
|
||||
if (preg_match('/^\*\*\*/', $line)) break;
|
||||
|
||||
if (preg_match('/^(\s*)\#Setting\s+(S_\S+)\s+(\S+)\s*(as\s|)(_\(|)("|\'|)([^\'"]*)("\)|\'\)|"|\'| |)/', $line, $matches)) {
|
||||
$gamesettingname = $matches[2];
|
||||
$defaultvalue = $matches[3];
|
||||
$description = $matches[7];
|
||||
|
||||
if (is_numeric($matches[3]) && is_float($matches[3]+0)) {
|
||||
$type = "float";
|
||||
} else if (is_numeric($matches[3])) {
|
||||
@ -922,38 +955,79 @@ class MatchManagerCore implements CallbackListener, CommandListener, TimerListen
|
||||
$type = "boolean";
|
||||
if (strtolower($matches[3]) == "false") $defaultvalue = "";
|
||||
} else {
|
||||
$defaultvalue = str_replace(["'", '"'], "" , $matches[3]);
|
||||
// Assuming it's a string, and because it can have spaces, we re-do the regexp
|
||||
$type = "string";
|
||||
preg_match_all('/(?<!\\\\)"((?:\\\\"|[^"])*?)(?<!\\\\)"/', $line, $stringMatches);
|
||||
|
||||
if (count($stringMatches) < 2) continue; // just in case it's not a string, and an another setting type
|
||||
|
||||
$defaultvalue = $stringMatches[1][0] ?? "";
|
||||
$defaultvalue = str_replace("\\\"", "\"", $defaultvalue); // replace escaped double quote
|
||||
$description = $stringMatches[1][1] ?? "";
|
||||
}
|
||||
$description = $matches[7];
|
||||
|
||||
settype($defaultvalue, $type);
|
||||
|
||||
$gamesettings = array_merge($gamesettings , array($gamesettingname => [
|
||||
if (array_key_exists($gamesettingname, $gamesettings)) {
|
||||
$gamesettings[$gamesettingname]['default'] = $defaultvalue;
|
||||
} else {
|
||||
$gamesettings[$gamesettingname] = [
|
||||
'type' => $type,
|
||||
'default' => $defaultvalue,
|
||||
'description' => $description
|
||||
]));
|
||||
} else if (preg_match('/^\*\*\*/',$line)) {
|
||||
break;
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $gamesettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Array with all settings of the Gamemode
|
||||
*
|
||||
* @param String $gamemode
|
||||
*/
|
||||
public function getGMSettings(String $gamemodebase, String $customgamemode) {
|
||||
$starttime = hrtime(true);
|
||||
|
||||
$gameSettings = [];
|
||||
|
||||
foreach (self::GAMEMODES_LIST_SETTINGS as $gamesetting => $info) {
|
||||
if (in_array('Global', $info['gamemode']) || in_array($gamemodebase, $info['gamemode'])) {
|
||||
$gameSettings = array_merge($gameSettings , array($gamesetting => $info));
|
||||
}
|
||||
}
|
||||
if ($customgamemode !== "") { // TODO improve detection
|
||||
$filename = $this->maniaControl->getServer()->getDirectory()->getUserDataFolder() . DIRECTORY_SEPARATOR . "Scripts" . DIRECTORY_SEPARATOR . "Modes" . DIRECTORY_SEPARATOR . $customgamemode;
|
||||
if (file_exists($filename)) {
|
||||
$customSettings = $this->getCustomGMSettings("Modes". DIRECTORY_SEPARATOR . $customgamemode);
|
||||
|
||||
foreach ($customSettings as $settingName => $setting) {
|
||||
if (array_key_exists($settingName, $gameSettings)) {
|
||||
$gameSettings[$settingName]['default'] = $setting['default'];
|
||||
} else {
|
||||
$gameSettings[$settingName] = $setting;
|
||||
}
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
Logger::logError("Impossible to read custom gamemode file");
|
||||
$this->maniaControl->getChat()->sendErrorToAdmins($this->chatprefix . " Impossible to read custom gamemode file");
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($gameSettings as $settingname => $info) {
|
||||
$gameSettings[$settingname]['value'] = $this->maniaControl->getSettingManager()->getSettingValue($this, $settingname);
|
||||
if ($gameSettings[$settingname]['value'] == null) {
|
||||
$gameSettings[$settingname]['value'] = $info['default'];
|
||||
}
|
||||
settype($gameSettings[$settingname]['value'], $info['type']);
|
||||
}
|
||||
|
||||
foreach ($gamesettings as $settingname => $info) {
|
||||
$gamesettings[$settingname]['value'] = $this->maniaControl->getSettingManager()->getSettingValue($this, $settingname);
|
||||
if ($gamesettings[$settingname]['value'] == null) {
|
||||
$gamesettings[$settingname]['value'] = $info['default'];
|
||||
}
|
||||
settype($gamesettings[$settingname]['value'], $info['type']);
|
||||
}
|
||||
var_dump('getGMSettings '. $customgamemode . ' duration: '. hrtime(true) - $starttime . '(now: '. hrtime(true) .' - starttime: '. $starttime .')');
|
||||
|
||||
return $gamesettings;
|
||||
return $gameSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1002,25 +1076,6 @@ class MatchManagerCore implements CallbackListener, CommandListener, TimerListen
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function called to list matches
|
||||
*/
|
||||
public function getMatchesList(int $limit = 10) {
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
$stmt = $mysqli->prepare("SELECT `gamemodebase`,`started`,`ended` FROM `" . self::DB_MATCHESINDEX . "` ORDER BY `started` DESC LIMIT ?");
|
||||
$stmt->bind_param('i', $limit);
|
||||
|
||||
if (!$stmt->execute()) {
|
||||
Logger::logError('Error executing MySQL query: '. $stmt->error);
|
||||
}
|
||||
|
||||
$result = $stmt->get_result();
|
||||
while($row = $result->fetch_array()) {
|
||||
$array[] = $row;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
/*
|
||||
* MARK: Can Start Functions
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user