anpera.net
https://anpera.homeip.net/phpbb3/

Gametime-Objekt
https://anpera.homeip.net/phpbb3/viewtopic.php?f=43&t=5028
Seite 1 von 1

Autor:  Eliwood [ Fr 30 Okt, 2009 00:15 ]
Betreff des Beitrags:  Gametime-Objekt

Mal wieder was neues, das ich heute aus langeweile geschrieben habe. Das ganze ist ein Objekt, das die Spielzeit anhand der wirklichen Zeit berechnet. Bis jetzt kann man nur das Datum und die Zeit bekommen, aber ich plane, auch noch die wichtigen restlichen Möglichkeiten von date() zu implementieren. Das einzige, was ich vermutlich nie implementieren werde, sind Schaltjahre.
Die Einstellmöglichkeiten sind vielfältig:
  • Dauer einer Spielsekunde je Realer Sekunde
  • Anzahl Sekunden je Minute
  • Anzahl Minuten je Stunde
  • Anzahl Tage pro Woche
  • Tage pro Monate (Als Array, die Anzahl Elemente sind die Anzahl Monate, die einzelnen Werte die Anzahl Tage)
  • Anzahl Monate pro Jahr
  • Startjahr des Zeitstempfels (Wann war 1970? Für ein Spieljahr von 1809 muss als Startjahr 1770 angegeben werden)

$this->bbcode_second_pass_code('', '<?php

/* Gametime-Object
* Calculates the ingame date and time based on the real timestamp
*
* Version 1.0, 30. Oktober 2009
* 2009 by Basilius "Wasili" Sauter
* Licensed unter the terms of the GNU GPL 2
*/
class Gametime {
public static $secondsPerMinute = 60;
public static $minutesPerHour = 60;
public static $hoursPerDay = 24;
public static $daysPerWeek = 7;
public static $daysPerMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
public static $monthsPerYear = 12;

public static $firstYear = 1970;
public static $secondsPerRealSecond = 1;

public static $daysPerYear = NULL;
public static $secondsPerDay = NULL;
public static $secondsPerYear = NULL;

protected $timestamp = 0;

public function __construct($timestamp = false) {
self::loadGametimeSettings();

if($timestamp === false) {
$this->timestamp = microtime(true)/self::$secondsPerRealSecond;
}
}

protected static function loadGametimeSettings() {
// Get Basic Settings
self::$secondsPerMinute = GAMETIME_SPM;
self::$minutesPerHour = GAMETIME_MPH;
self::$hoursPerDay = GAMETIME_HPD;
self::$daysPerWeek = GAMETIME_DPW;
self::$daysPerMonth = unserialize(GAMETIME_DPM);
self::$monthsPerYear = GAMETIME_MPY;

self::$firstYear = GAMETIME_FY;
self::$secondsPerRealSecond = GAMETIME_SPR;

// Calculate other values
self::$daysPerYear = array_sum(self::$daysPerMonth);
self::$secondsPerDay = self::$secondsPerMinute * self::$minutesPerHour * self::$hoursPerDay;
self::$secondsPerYear = self::$secondsPerDay * self::$daysPerYear;
}

public function getDate($a) {
// Replace Year
$a = str_replace('Y', $this->getYear(), $a);
$a = str_replace('y', substr($this->getYear(), -2), $a);

// Replace month
$a = str_replace('m', sprintf('%02d', $this->getMonth()), $a);

// Replace day
$a = str_replace('d', sprintf('%02d', $this->getDay()), $a);

// Replace hour
$a = str_replace('H', sprintf('%02d', $this->getHour()), $a);

// Replace minute
$a = str_replace('i', sprintf('%02d', $this->getMinute()), $a);

// Replace second
$a = str_replace('s', sprintf('%02d', $this->getSecond()), $a);
return $a;
}

protected function getYear() {
return self::$firstYear + floor($this->timestamp/self::$secondsPerYear);
}

protected function getMonth() {
// Get the day of the Year.
$d = $this->getDayOfYear();

// Get the month!
$m = 0;
foreach(self::$daysPerMonth as $days) {
$m++;
if($d - $days > 0) {
$d-=$days;
continue;
}
else {
break;
}
}

return $m;
}

protected function getDay() {
// Get the day of the Year.
$d = $this->getDayOfYear();

// Get the month!
$m = 0;
foreach(self::$daysPerMonth as $days) {
$m++;
if($d - $days > 0) {
$d-=$days;
continue;
}
else {
break;
}
}

return $d;
}

protected function getHour() {
$secondsToday = $this->getSecondsToday();

return floor($secondsToday / (self::$minutesPerHour * self::$secondsPerMinute));
}

protected function getMinute() {
$secondsToday = $this->getSecondsToday();

$secondsInHour = $secondsToday - $this->getHour()*(self::$minutesPerHour * self::$secondsPerMinute);
return floor($secondsInHour / (self::$minutesPerHour));
}

protected function getSecond() {
$secondsToday = $this->getSecondsToday();

$secondsLeft = $secondsToday - $this->getHour()*(self::$minutesPerHour * self::$secondsPerMinute);
$secondsLeft = $secondsLeft - $this->getMinute()*self::$secondsPerMinute;
return round($secondsLeft, 0);
}

protected function getDayOfYear() {
return floor(($this->timestamp - self::$secondsPerYear * floor($this->timestamp/self::$secondsPerYear)) / self::$secondsPerDay);
}

protected function getSecondsToday() {
return $this->timestamp - self::$secondsPerDay * floor($this->timestamp/self::$secondsPerDay);
}
}
')

Beispiel:
$this->bbcode_second_pass_code('', '
header('Content-type: text/plain');

// Menschliche Zeitrechnung
define('GAMETIME_SPM', 60);
define('GAMETIME_MPH', 60);
define('GAMETIME_HPD', 24);
define('GAMETIME_DPW', 7);
define('GAMETIME_DPM', serialize(array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)));
define('GAMETIME_MPY', 12);
define('GAMETIME_FY', 1970);
define('GAMETIME_SPR', 1);

$gt = new Gametime();
printf("Die aktuelle Zeit in menschlicher Zeitrechnung ist %s", $gt->getDate('Y-m-d H:i:s'));
')
Gibt für heute (30. Oktober 2009) aus (Schalttage fehlen!):
$this->bbcode_second_pass_code('', 'Die aktuelle Zeit in menschlicher Zeitrechnung ist 2009-11-08 00:17:33')

Autor:  Marja [ Fr 30 Okt, 2009 00:50 ]
Betreff des Beitrags:  Re: Gametime-Objekt

Öhm- ich sag von vorn herein: Marja kein Plan von JS =D
Aber wir habens iwie anders gelöst in der common ;)

$this->bbcode_second_pass_code('', '
if (getsetting("dispnextday",0))
{
$script111 .= <<<JS
<script language='JavaScript'>
setInterval('displayLocalTime()',1000)

function displayLocalTime()
{
var localTime = document.getElementById('localTime');
if (!localTime)
return;

var date = new Date();

var seconds = date.getSeconds();
var minutes = date.getMinutes();
var hours = date.getHours();

var string = (hours < 10 ? '0' + hours : '' + hours);
string = string + ':' + (minutes < 10 ? '0' + minutes : '' + minutes);
string = string + ':' + (seconds < 10 ? '0' + seconds : '' + seconds);

localTime.innerHTML = string;
}
//-->
</script>
JS;

$echtzeit = "`0$script111 <div id='localTime'>".date("G\\h, i\\m, s\\s")."<div>";

$charstat.=appoencode(

templatereplace("statrow",array("title"=>"Aktuelle Uhrzeit","value"=>$echtzeit))
,true);

}
')

Allerdings gibt's kein Datum mit Jahresangabe ;)
Dennoch, meins wirkt iwie mickriger gegen deins, Eliwood o.O Ist des normal?

Autor:  Eliwood [ Fr 30 Okt, 2009 09:41 ]
Betreff des Beitrags:  Re: Gametime-Objekt

Öhm? Du hast schon verstanden, dass a) mein Objekt _PHP_ ist und b) zur Darstellung eines _völlig_ anderen Kalenders? Mit Beispielsweise 1000 Tage pro Jahr, 10 Monate à 100 Tagen, der Tag à 100 Stunden, die Stunde à 100 Minuten und die Minute à 100 Sekunden? Dabei kann sogar die länge der Sekunde variieren - eine Ingamesekunde kann zum Beispiel 3 reale Sekunden dauern, oder aber auch nur ein Bruchteil davon.

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/