Bounty::setBounty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\bounty;
10
11
use pocketmine\Player;
12
use VectorNetworkProject\TheMix\game\event\player\PlayerBountyEvent;
13
use VectorNetworkProject\TheMix\game\event\player\PlayerBountyLostEvent;
14
use VectorNetworkProject\TheMix\provider\JSON;
15
16
class Bounty
0 ignored issues
show
Coding Style introduced by
Bounty does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
17
{
18
    /* @var string */
19
    public const FILE_NAME = 'Bounty';
20
21
    /* @var int */
22
    public const MAX_GOLD = 500;
23
24
    /* @var string */
25
    public const CONFIG_GOLD = 'gold';
26
27
    /* @var string */
28
    public const CONFIG_BOUNTY = 'bounty';
29
30
    public static function init(Player $player): void
31
    {
32
        $db = new JSON($player->getXuid(), self::FILE_NAME);
33
        $db->init([
34
            'gold'   => 0,
35
            'bounty' => false,
36
        ]);
37
    }
38
39
    /**
40
     * プレイヤーに賭ける賞金を設定します。
41
     *
42
     * @param Player $player
43
     * @param int    $gold
44
     *
45
     * @throws \Error
46
     */
47
    public static function setGold(Player $player, int $gold): void
48
    {
49
        if ($gold >= self::MAX_GOLD) {
50
            throw new \Error('Goldは500以下に設定して下さい。');
0 ignored issues
show
Unused Code introduced by
The call to Error::__construct() has too many arguments starting with 'Goldは500以下に設定して下さい。'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
51
        }
52
        $db = new JSON($player->getXuid(), self::FILE_NAME);
53
        $db->set(self::CONFIG_GOLD, $gold);
54
    }
55
56
    /**
57
     * プレイヤーに賭けられている賞金を返します。
58
     *
59
     * @param Player $player
60
     *
61
     * @return int
62
     */
63
    public static function getGold(Player $player): int
64
    {
65
        $db = new JSON($player->getXuid(), self::FILE_NAME);
66
67
        return $db->get(self::CONFIG_GOLD);
68
    }
69
70
    /**
71
     * @param Player $player
72
     * @param bool   $bounty
73
     */
74
    public static function setBounty(Player $player, bool $bounty): void
75
    {
76
        $db = new JSON($player, self::FILE_NAME);
77
        $db->set(self::CONFIG_BOUNTY, $bounty);
78
    }
79
80
    /**
81
     * プレイヤーに賞金が賭けられているかどうかを返します。
82
     *
83
     * @param Player $player
84
     *
85
     * @return bool
86
     */
87
    public static function isBounty(Player $player): bool
88
    {
89
        $db = new JSON($player, self::FILE_NAME);
90
91
        return $db->get(self::CONFIG_BOUNTY);
92
    }
93
94
    /**
95
     * @param Player $player
96
     *
97
     * @throws \ReflectionException
98
     */
99
    public static function setPlayerBounty(Player $player): void
100
    {
101
        if (self::isBounty($player)) {
102
            $gold = self::getGold($player) + 100;
103
            $event = new PlayerBountyEvent($player, $gold, PlayerBountyEvent::PLUS_GOLD);
104
            $event->call();
105
            if (!$event->isCancelled()) {
106
                self::setGold($player, $gold);
107
            }
108
        } else {
109
            $event = new PlayerBountyEvent($player, 100, PlayerBountyEvent::ENABLE_BOUNTY);
110
            $event->call();
111
            if (!$event->isCancelled()) {
112
                self::setBounty($player, true);
113
                self::setGold($player, 100);
114
            }
115
        }
116
    }
117
118
    /**
119
     * @param Player      $player
120
     * @param Player|null $killer
121
     *
122
     * @throws \ReflectionException
123
     */
124
    public static function PlayerBountyLost(Player $player, Player $killer = null): void
0 ignored issues
show
Coding Style introduced by
function PlayerBountyLost() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
125
    {
126
        if (!self::isBounty($player)) {
127
            return;
128
        }
129
        if ($killer) {
130
            $event = new PlayerBountyLostEvent($player, self::getGold($player));
131
            $event->call();
132
            if (!$event->isCancelled()) {
133
                self::setBounty($player, false);
134
                self::setGold($player, 0);
135
            }
136
        } else {
137
            $event = new PlayerBountyLostEvent($player, self::getGold($player), $killer, PlayerBountyLostEvent::TYPE_KILLED);
138
            $event->call();
139
            if (!$event->isCancelled()) {
140
                self::setBounty($player, false);
141
                self::setGold($player, 0);
142
            }
143
        }
144
    }
145
}
146