Last active
December 21, 2015 05:09
-
-
Save Omattyao/6255107 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GrowthMethods_LifePlugin @omattyao_yk | |
<?php | |
define(GROWTH_INTERVAL, 8400); //8400seconds=7days | |
class GrowthMethods implements Plugin | |
{ | |
private $api; | |
private $path; | |
private $id = (int) 0; | |
private $schedule = array(); | |
private $players = array(); | |
public function __construct(ServerAPI $api, $server = false) | |
{ | |
$this->api = $api; | |
} | |
public function init() | |
{ | |
$this->createConfig(); | |
$this->players = $this->loadConfig(); | |
$this->api->event("player.join", array($this, "handleGrowth")); | |
$this->api->event("player.quit", array($this, "handleGrowth")); | |
} | |
public function __destruct() | |
{ | |
$this->saveConfig(); | |
} | |
public function handleGrowth($data, $event) | |
{ | |
switch ($event) { | |
case "player.join": | |
$user = $data->username; | |
if (!isset($this->players[$user])) { | |
$this->createPlayerData($player); | |
} | |
$this->players[$user]["login"] = time(); | |
$this->manageConfig($data); | |
break; | |
case "player.quit"; | |
$userdata &= $this->players[$data->username]; | |
$userdata["passage"] += time() - $userdata["login"]; | |
if ($userdata["schedule_id"] !== false) { | |
$this->cancelSchedule($userdata["schedule_id"]); | |
} | |
$this->saveConfig(); | |
break; | |
} | |
} | |
public function manageGrowth($player) | |
{ | |
$passage = $this->players[$player->username]["passage"]; | |
$ticks = GROWTH_INTERVAL - ($passage % GROWTH_INTERVAL); | |
if ($ticks < 1) { | |
$this->growth($player); | |
} else { | |
$id = $this->schedule($ticks * 20, "growth", array($player), false); | |
$this->players[$user]["schedule_id"] = $id; | |
} | |
} | |
public function growth($player) | |
{ | |
$userdata &= $this->players[$player->username]; | |
$userdata["age"]++; | |
$userdata["passage"] = GROWTH_INTERVAL * $this->players[$user]["age"]; | |
$userdata["login"] = time(); | |
$this->manageGrowth($player); | |
$this->saveConfig(); | |
$player->sendChat("Fooooo!"); | |
} | |
public function schedule($ticks, $method, $args, $repeat = false) | |
{ | |
$id = $this->id; | |
$this->schedule[$id] = array($tocks, $method, $args, $repeat); | |
$this->api->schedule($ticks, array($this, "callback", array($method, $args), $repeat, $id); | |
$this->id++; | |
return $id; | |
} | |
public function cancelSchedule($id) | |
{ | |
@unset($this->schedule[$id]); | |
} | |
public function getSchedule($id) | |
{ | |
if (!isset($this->schedule[$id])) return false; | |
return $this->schedule[$id]; | |
} | |
public function callback($args, $id) | |
{ | |
$schedule = $this->getSchedule($id); | |
if ($schedule === false) return false; | |
$method = $args[0]; | |
$params = $args[1]; | |
@call_user_func_array(array($this, $method), $params); | |
if ($schedule[3] === false) { | |
unset($this->schedule[$id]); | |
} | |
} | |
private function createPlayerData($player) | |
{ | |
$user = $player->username; | |
$data = array( | |
"age" => (int) 0; | |
"login" => null; | |
"passage" => (int) 0; | |
"schedule_id" => false; | |
); | |
$this->players[$user] = $data; | |
$this->saveConfig(); | |
$player->sendChat("You was born!"); | |
} | |
private function createConfig() | |
{ | |
//omitted | |
} | |
private function loadConfig() | |
{ | |
//omitted | |
} | |
private function saveConfig() | |
{ | |
//omitted | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment