Merge pull request #47 from tomvlk/f/refactor-timeelapsed-dbpassword

Reformat time_elapsed_string into camelcase. Database can be used with no password given
This commit is contained in:
Lukas Kremsmayr 2015-11-17 12:58:52 +01:00
commit 0397bd0006
5 changed files with 52 additions and 14 deletions

4
.gitignore vendored
View File

@ -1,4 +1,6 @@
/.idea/ .idea
*.iml
/configs/server.xml /configs/server.xml
/logs/ /logs/
/ManiaControl.log /ManiaControl.log

View File

@ -30,6 +30,30 @@
<option name="ALIGN_CLASS_CONSTANTS" value="true" /> <option name="ALIGN_CLASS_CONSTANTS" value="true" />
<option name="PHPDOC_USE_FQCN" value="true" /> <option name="PHPDOC_USE_FQCN" value="true" />
</PHPCodeStyleSettings> </PHPCodeStyleSettings>
<DBN-PSQL>
<case-options enabled="false">
<option name="KEYWORD_CASE" value="lower" />
<option name="FUNCTION_CASE" value="lower" />
<option name="PARAMETER_CASE" value="lower" />
<option name="DATATYPE_CASE" value="lower" />
<option name="OBJECT_CASE" value="preserve" />
</case-options>
<formatting-settings enabled="false" />
</DBN-PSQL>
<DBN-SQL>
<case-options enabled="false">
<option name="KEYWORD_CASE" value="lower" />
<option name="FUNCTION_CASE" value="lower" />
<option name="PARAMETER_CASE" value="lower" />
<option name="DATATYPE_CASE" value="lower" />
<option name="OBJECT_CASE" value="preserve" />
</case-options>
<formatting-settings enabled="false">
<option name="STATEMENT_SPACING" value="one_line" />
<option name="CLAUSE_CHOP_DOWN" value="chop_down_if_statement_long" />
<option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
</formatting-settings>
</DBN-SQL>
<XML> <XML>
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" /> <option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML> </XML>

View File

@ -42,7 +42,7 @@ class Config {
* @return bool * @return bool
*/ */
public function validate() { public function validate() {
if (!$this->host || !$this->port || !$this->user || !$this->pass || !$this->name) { if (!$this->host || !$this->port || !$this->user || !$this->name) {
return false; return false;
} }
if ($this->user === 'mysql_user' || $this->pass === 'mysql_password') { if ($this->user === 'mysql_user' || $this->pass === 'mysql_password') {

View File

@ -204,7 +204,7 @@ class ManiaExchangeList implements CallbackListener, ManialinkPageAnswerListener
$lineQuad->setZ(0.001); $lineQuad->setZ(0.001);
} }
$time = Formatter::time_elapsed_string(strtotime($map->updated)); $time = Formatter::timeElapsedString(strtotime($map->updated));
$array = array('$s' . $map->id => $posX + 3.5, '$s' . $map->name => $posX + 12.5, '$s' . $map->author => $posX + 59, '$s' . str_replace('Arena', '', $map->maptype) => $posX + 103, '$s' . $map->mood => $posX + 118, '$s' . $time => $posX + 130); $array = array('$s' . $map->id => $posX + 3.5, '$s' . $map->name => $posX + 12.5, '$s' . $map->author => $posX + 59, '$s' . str_replace('Arena', '', $map->maptype) => $posX + 103, '$s' . $map->mood => $posX + 118, '$s' . $time => $posX + 130);
$labels = $this->maniaControl->getManialinkManager()->labelLine($mapFrame, $array); $labels = $this->maniaControl->getManialinkManager()->labelLine($mapFrame, $array);
$authorLabel = $labels[2]; $authorLabel = $labels[2];

View File

@ -48,24 +48,36 @@ abstract class Formatter {
/** /**
* Format an elapsed time String (2 days ago...) by a given timestamp * Format an elapsed time String (2 days ago...) by a given timestamp
* *
* @param int $ptime * @param int $time Input time
* @return string * @param boolean $short Short version
* @return string Formatted elapsed time string
*/ */
public static function time_elapsed_string($ptime) { public static function timeElapsedString($time, $short = false) {
// TODO: refactor code: camelCase! $elapsedTime = time() - $time;
$etime = time() - $ptime;
if ($etime < 1) { $second = $short ? 'sec.' : 'second';
return '0 seconds'; $minute = $short ? 'min.' : 'minute';
$hour = $short ? 'h' : 'hour';
$day = $short ? 'd' : 'day';
$month = $short ? 'm' : 'month';
$year = $short ? 'y' : 'year';
if ($elapsedTime < 1) {
return $short ? '0 sec.' : '0 seconds';
} }
$a = array(12 * 30 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute', 1 => 'second'); $calculateSeconds = array(12 * 30 * 24 * 60 * 60 => $year,
30 * 24 * 60 * 60 => $month,
24 * 60 * 60 => $day,
60 * 60 => $hour,
60 => $minute,
1 => $second);
foreach ($a as $secs => $str) { foreach ($calculateSeconds as $secs => $str) {
$d = $etime / $secs; $d = $elapsedTime / $secs;
if ($d >= 1) { if ($d >= 1) {
$r = round($d); $r = round($d);
return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago'; return $r . ' ' . $str . ($r > 1 ? (!$short ? 's' : '') : '') . ' ago';
} }
} }
return ''; return '';