-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEconomy.php
More file actions
63 lines (50 loc) · 1.82 KB
/
Economy.php
File metadata and controls
63 lines (50 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace kjBotModule\kj415j45\CoreModule;
use Exception;
use kjBot\Framework\DataStorage;
class Economy{
const NO_MONEY = -1; //余额不足
const UNEXPECTED_NEGATIVE_NUMBER = -2; //未期望的负值
const STR_NO_MONEY_HINT = '金币不足,可通过签到等方式获取';
const BaseDir = 'CoreModule.Economy/';
private $user;
private $balance;
public function __construct($user){
$this->user = $user;
$this->balance = DataStorage::GetData(static::BaseDir.$user);
if($this->balance === false){
$this->balance = 0;
}
}
public function getBalance(){
return $this->balance;
}
protected function save(){
return DataStorage::SetData(static::BaseDir.$this->user, $this->balance);
}
public function addBalance(int $income){
if($income<=0) throw new Exception('入账不能为负或零', static::UNEXPECTED_NEGATIVE_NUMBER);
$this->balance += $income;
$this->save();
Access::LogForMe($this, "{$this->user} + {$income}");
return $this;
}
public function decBalance(int $expend){
if($expend<=0) throw new Exception('支出不能为负或零', static::UNEXPECTED_NEGATIVE_NUMBER);
if($this->balance < $expend) throw new Exception('没有足够的余额', static::NO_MONEY);
$this->balance -= $expend;
$this->save();
Access::LogForMe($this, "{$this->user} - {$expend}");
return $this;
}
public function transfer(Economy $target, int $money){
if($money<=0) throw new Exception('转账金额不能为负或零', static::UNEXPECTED_NEGATIVE_NUMBER);
try{
$this->decBalance($money);
}catch(Exception $e){
throw $e;
}
$target->addBalance($money);
return $this;
}
}