|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2018 VectorNetworkProject. All rights reserved. MIT license. |
| 4 | + * |
| 5 | + * GitHub: https://github.com/VectorNetworkProject/TheMix |
| 6 | + * Website: https://www.vector-network.tk |
| 7 | + */ |
| 8 | + |
| 9 | +namespace VectorNetworkProject\TheMix\game\level; |
| 10 | + |
| 11 | +use pocketmine\Player; |
| 12 | +use pocketmine\Server; |
| 13 | +use VectorNetworkProject\TheMix\event\game\PlayerLevelChangeEvent; |
| 14 | +use VectorNetworkProject\TheMix\provider\JSON; |
| 15 | + |
| 16 | +class Level |
| 17 | +{ |
| 18 | + /* @var string */ |
| 19 | + public const FILE_NAME = 'Level'; |
| 20 | + |
| 21 | + /* @var string */ |
| 22 | + public const LEVEL = 'level'; |
| 23 | + |
| 24 | + public static function init(): array |
| 25 | + { |
| 26 | + return [ |
| 27 | + 'level' => 1, |
| 28 | + 'xp' => 0, |
| 29 | + 'max' => 15, |
| 30 | + 'prestige' => 0, |
| 31 | + 'complete' => false, |
| 32 | + ]; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * プレイヤーのレベルを設定します。 |
| 37 | + * |
| 38 | + * @param Player $player |
| 39 | + * @param int $level |
| 40 | + * |
| 41 | + * @throws \Error |
| 42 | + * |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public static function setLevel(Player $player, int $level): void |
| 46 | + { |
| 47 | + if (self::CheckLevel($level)) { |
| 48 | + throw new \Error('数値は120以下にして下さい。'); |
| 49 | + } |
| 50 | + $event = new PlayerLevelChangeEvent($player, self::getLevel($player), $level, self::isComplete($level)); |
| 51 | + Server::getInstance()->getPluginManager()->callEvent($event); |
| 52 | + if ($event->isCancelled()) { |
| 53 | + return; |
| 54 | + } |
| 55 | + $db = new JSON($player->getXuid(), self::FILE_NAME); |
| 56 | + $db->set(self::FILE_NAME, $level); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * プレイヤーのレベルを1上げます。 |
| 61 | + * |
| 62 | + * @param Player $player |
| 63 | + * |
| 64 | + * @return void |
| 65 | + */ |
| 66 | + public static function addLevel(Player $player): void |
| 67 | + { |
| 68 | + $level = self::getLevel($player); |
| 69 | + if (!$level >= 120) { |
| 70 | + return; |
| 71 | + } |
| 72 | + $event = new PlayerLevelChangeEvent($player, $level, $level + 1, self::isComplete($level + 1)); |
| 73 | + Server::getInstance()->getPluginManager()->callEvent($event); |
| 74 | + if ($event->isCancelled()) { |
| 75 | + return; |
| 76 | + } |
| 77 | + $db = new JSON($player->getXuid(), self::FILE_NAME); |
| 78 | + $db->set(self::LEVEL, $level + 1); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * プレイヤーのレベルを返します。 |
| 83 | + * |
| 84 | + * @param Player $player |
| 85 | + * |
| 86 | + * @return int |
| 87 | + */ |
| 88 | + public static function getLevel(Player $player): int |
| 89 | + { |
| 90 | + $db = new JSON($player->getXuid(), self::FILE_NAME); |
| 91 | + |
| 92 | + return $db->get(self::LEVEL); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * レベルが120か調べる。 |
| 97 | + * |
| 98 | + * @param int $level |
| 99 | + * |
| 100 | + * @return bool |
| 101 | + */ |
| 102 | + public static function isComplete(int $level): bool |
| 103 | + { |
| 104 | + return $level >= 120 |
| 105 | + ? true |
| 106 | + : false; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * レベルが120を超えていないか調べる。 |
| 111 | + * |
| 112 | + * @param int $level |
| 113 | + * |
| 114 | + * @return bool |
| 115 | + */ |
| 116 | + private static function CheckLevel(int $level): bool |
| 117 | + { |
| 118 | + return $level > 120 |
| 119 | + ? true |
| 120 | + : false; |
| 121 | + } |
| 122 | +} |
0 commit comments