Achievements API

This commit is contained in:
Shoghi Cervantes 2013-11-24 00:51:04 +01:00
parent ae8c934b5f
commit 3ff4b9eae0
5 changed files with 180 additions and 8 deletions

141
src/API/AchievementAPI.php Normal file
View File

@ -0,0 +1,141 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class AchievementAPI{
public static $achievements = array(
/*"openInventory" => array(
"name" => "Taking Inventory",
"requires" => array(),
),*/
"mineWood" => array(
"name" => "Getting Wood",
"requires" => array(
//"openInventory",
),
),
"buildWorkBench" => array(
"name" => "Benchmarking",
"requires" => array(
"mineWood",
),
),
"buildPickaxe" => array(
"name" => "Time to Mine!",
"requires" => array(
"buildWorkBench",
),
),
"buildFurnace" => array(
"name" => "Hot Topic",
"requires" => array(
"buildPickaxe",
),
),
"acquireIron" => array(
"name" => "Acquire hardware",
"requires" => array(
"buildFurnace",
),
),
"buildHoe" => array(
"name" => "Time to Farm!",
"requires" => array(
"buildWorkBench",
),
),
"makeBread" => array(
"name" => "Bake Bread",
"requires" => array(
"buildHoe",
),
),
"bakeCake" => array(
"name" => "The Lie",
"requires" => array(
"buildHoe",
),
),
"buildBetterPickaxe" => array(
"name" => "Getting an Upgrade",
"requires" => array(
"buildWorkBench",
),
),
);
function __construct(){
}
public static function broadcastAchievement(Player $player, $achievementId){
if(ServerAPI::request()->api->getProperty("announce-player-achievements") == true){
ServerAPI::request()->api->chat->broadcast($player->username." has just earned the achievement ".self::$achievements[$achievementId]["name"]);
}else{
$player->sendChat("You have just earned the achievement ".self::$achievements[$achievementId]["name"]);
}
}
public static function addAchievement($achievementId, $achievementName, array $requires = array()){
if(!isset(self::$achievements[$achievementId])){
self::$achievements[$achievementId] = array(
"name" => $achievementName,
"requires" => $requires,
);
return true;
}
return false;
}
public static function hasAchievement(Player $player, $achievementId){
if(!isset(self::$achievements[$achievementId]) or !isset($player->achievements)){
$player->achievements = array();
return false;
}
if(!isset($player->achievements[$achievementId]) or $player->achievements[$achievementId] == false){
return false;
}
return true;
}
public static function grantAchievement(Player $player, $achievementId){
if(isset(self::$achievements[$achievementId]) and !self::hasAchievement($player, $achievementId)){
foreach(self::$achievements[$achievementId]["requires"] as $requerimentId){
if(!self::hasAchievement($player, $requerimentId)){
return false;
}
}
$player->achievements[$achievementId] = true;
self::broadcastAchievement($player, $achievementId);
return true;
}
return false;
}
public static function removeAchievement(Player $player, $achievementId){
if(self::hasAchievement($player, $achievementId)){
$player->achievements[$achievementId] = false;
}
}
public function init(){
}
}

View File

@ -449,6 +449,7 @@ class PlayerAPI{
"health" => 20,
"lastIP" => "",
"lastID" => 0,
"achievements" => array(),
);
if(!file_exists(DATA_PATH."players/".$iname.".yml")){

View File

@ -61,6 +61,7 @@ class ServerAPI{
"memory-limit" => "128M",
"last-update" => false,
"white-list" => false,
"announce-player-achievements" => true,
"spawn-protection" => 16,
"view-distance" => 10,
"max-players" => 20,

View File

@ -58,6 +58,7 @@ class Player{
public $windowCnt = 2;
public $windows = array();
public $blocked = true;
public $achievements = array();
public $chunksLoaded = array();
private $chunksOrder = array();
private $lastMeasure = 0;
@ -78,9 +79,9 @@ class Player{
private $received = array();
public $realmsData = array();
public function __get($name){
public function &__get($name){
if(isset($this->{$name})){
return ($this->{$name});
return $this->{$name};
}
return null;
}
@ -216,6 +217,7 @@ class Player{
public function save(){
if($this->entity instanceof Entity){
$this->data->set("achievements", $this->achievements);
$this->data->set("position", array(
"level" => $this->entity->level->getName(),
"x" => $this->entity->x,
@ -534,6 +536,11 @@ class Player{
if(($this->gamemode & 0x01) === 0x00){
$this->addItem($data["entity"]->type, $data["entity"]->meta, $data["entity"]->stack, false);
}
switch($data["entity"]->type){
case WOOD:
AchievementAPI::grantAchievement($this, "mineWood");
break;
}
}elseif($data["entity"]->level === $this->level){
$this->dataPacket(MC_TAKE_ITEM_ENTITY, $data);
}
@ -740,6 +747,19 @@ class Player{
}else{
$this->setSlot($slot, BlockAPI::getItem($item->getID(), $item->getMetadata(), $s->count + $item->count), false);
}
switch($item->getID()){
case WORKBENCH:
AchievementAPI::grantAchievement($this, "buildWorkBench");
break;
case WOODEN_PICKAXE:
AchievementAPI::grantAchievement($this, "buildPickaxe");
break;
case FURNACE:
AchievementAPI::grantAchievement($this, "buildFurnace");
break;
}
}
}
return $res;
@ -1200,6 +1220,7 @@ class Player{
}
$this->data->set("inventory", $inv);
}
$this->achievements = $this->data->get("achievements");
$this->data->set("caseusername", $this->username);
$this->inventory = array();
foreach($this->data->get("inventory") as $slot => $item){
@ -1217,9 +1238,8 @@ class Player{
$this->data->set("lastIP", $this->ip);
$this->data->set("lastID", $this->clientID);
if($this->data instanceof Config){
$this->server->api->player->saveOffline($this->data);
}
$this->server->api->player->saveOffline($this->data);
$this->dataPacket(MC_LOGIN_STATUS, array(
"status" => 0,
));
@ -1940,6 +1960,15 @@ class Player{
));
break;
}
if($tile->class === TILE_FURNACE and $data["slot"] == 2){
switch($slot->getID()){
case IRON_INGOT:
AchievementAPI::grantAchievement($this, "acquireIron");
break;
}
}
if($item->getID() !== AIR and $slot->getID() == $item->getID()){
if($slot->count < $item->count){
if($this->removeItem($item->getID(), $item->getMetadata(), $item->count - $slot->count, false) === false){

View File

@ -150,7 +150,7 @@ class Config{
}
}
public function __get($k){
public function &__get($k){
return $this->get($k);
}
@ -166,11 +166,11 @@ class Config{
return $this->remove($k);
}
public function get($k){
public function &get($k){
if($this->correct === false or !isset($this->config[$k])){
return false;
}
return ($this->config[$k]);
return $this->config[$k];
}
public function set($k, $v = true){