Created
May 24, 2020 15:20
-
-
Save PJZ9n/408555a1d9b378982b364070de1ef4ba to your computer and use it in GitHub Desktop.
スロット
This file contains hidden or 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
<?php | |
/** | |
* @name SlotTest | |
* @version 1.0.0 | |
* @main PJZ9n\SlotTest\Main | |
* @api 3.0.0 | |
*/ | |
declare(strict_types=1); | |
namespace PJZ9n\SlotTest { | |
use pocketmine\command\Command; | |
use pocketmine\command\CommandExecutor; | |
use pocketmine\command\CommandSender; | |
use pocketmine\command\PluginCommand; | |
use pocketmine\event\Listener; | |
use pocketmine\permission\Permission; | |
use pocketmine\permission\PermissionManager; | |
use pocketmine\Player; | |
use pocketmine\plugin\Plugin; | |
use pocketmine\plugin\PluginBase; | |
class Main extends PluginBase | |
{ | |
public function onEnable(): void | |
{ | |
$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this); | |
PermissionManager::getInstance()->addPermission(new Permission( | |
"slottest.command.slot", | |
null, | |
Permission::DEFAULT_TRUE | |
)); | |
$command = new PluginCommand("slot", $this); | |
$command->setDescription("スロットを回す"); | |
$command->setUsage("/slot <number>"); | |
$command->setPermission("slottest.command.slot"); | |
$command->setExecutor(new CommandProcessor()); | |
$this->getServer()->getCommandMap()->register($this->getName(), $command); | |
} | |
} | |
class EventListener implements Listener | |
{ | |
/** @var Plugin */ | |
private $plugin; | |
public function __construct(Plugin $plugin) | |
{ | |
$this->plugin = $plugin; | |
} | |
} | |
class CommandProcessor implements CommandExecutor | |
{ | |
public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool | |
{ | |
if (!$sender instanceof Player) { | |
return true; | |
} | |
$slot = new TestSlot(1); | |
$slot->setPlayer($sender); | |
$slot->slot(); | |
return false; | |
} | |
} | |
class Chance | |
{ | |
/** @var int */ | |
private $all; | |
/** @var int[] */ | |
private $chance; | |
public function __construct(int $all = 65536) | |
{ | |
$this->all = $all; | |
} | |
public function addChance(int $id, int $chance): void | |
{ | |
$this->chance[$id] = $chance; | |
} | |
public function calcChance(): ?int | |
{ | |
$rand = mt_rand(1, $this->all); | |
$last = 1; | |
foreach ($this->chance as $id => $chance) { | |
$cl = $chance + $last; | |
if ($rand >= $last && $rand < $cl) { | |
return $id; | |
} | |
$last += $chance; | |
} | |
return null; | |
} | |
} | |
abstract class Slot | |
{ | |
/** @var int */ | |
private $id; | |
/** @var Player 着席プレイヤー */ | |
private $player; | |
public function __construct(int $id) | |
{ | |
$this->id = $id; | |
} | |
final public function getId(): int | |
{ | |
return $this->id; | |
} | |
public function setPlayer(Player $player): void | |
{ | |
$this->player = $player; | |
} | |
public function getPlayer(): ?Player | |
{ | |
return $this->player; | |
} | |
abstract protected function getChance(): Chance; | |
abstract public function slot(): void; | |
} | |
abstract class SlotOne extends Slot | |
{ | |
public const A = 0; | |
public const B = 1; | |
public const C = 2; | |
protected function getChance(): Chance | |
{ | |
$chance = new Chance(10); | |
$chance->addChance(self::A, 1); | |
$chance->addChance(self::B, 1); | |
$chance->addChance(self::C, 1); | |
return $chance; | |
} | |
} | |
class TestSlot extends SlotOne | |
{ | |
public function slot(): void | |
{ | |
$this->getPlayer()->sendMessage("-----"); | |
$c = $this->getChance()->calcChance(); | |
if ($c === self::A) { | |
$this->getPlayer()->sendMessage("[ <A> <A> <A> ]"); | |
} else if ($c === self::B) { | |
$this->getPlayer()->sendMessage("[ <B> <B> <B> ]"); | |
} else if ($c === self::C) { | |
$this->getPlayer()->sendMessage("[ <C> <C> <C> ]"); | |
} else { | |
$this->getPlayer()->sendMessage("[ <=> <=> <=> ]"); | |
} | |
$this->getPlayer()->sendMessage("-----"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment