forked from Rawrfuls/Uconomy
-
Notifications
You must be signed in to change notification settings - Fork 11
/
UconomyEconomyProvider.cs
134 lines (115 loc) · 4.49 KB
/
UconomyEconomyProvider.cs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using fr34kyn01535.Uconomy.Models;
using Microsoft.EntityFrameworkCore;
using Rocket.API.Commands;
using Rocket.API.DependencyInjection;
using Rocket.API.Economy;
using Rocket.API.Plugins;
using Rocket.API.User;
namespace fr34kyn01535.Uconomy
{
public class UconomyEconomyProvider : IEconomyProvider
{
private readonly IDependencyContainer _container;
private UconomyPlugin Plugin => (UconomyPlugin) _container.Resolve<IPluginLoader>().Plugins.FirstOrDefault(p => p.Name == "Uconomy");
public UconomyEconomyProvider(IDependencyContainer container)
{
_container = container;
}
public async Task AddBalanceAsync(IUser owner, decimal amount, string reason = null)
{
using (var db = new UconomyDbContext(Plugin))
{
var account = await db.Accounts.FirstOrDefaultAsync(a => a.Id == owner.Id);
account.Balance += amount;
account.LastUpdated = DateTime.Now;
await db.SaveChangesAsync();
}
}
public Task<bool> TransferAsync(IEconomyAccount source, IEconomyAccount target, decimal amount, string reason = null)
{
throw new NotImplementedException();
}
public Task<bool> AddBalanceAsync(IEconomyAccount account, decimal amount, string reason = null)
{
throw new NotImplementedException();
}
public async Task<bool> RemoveBalanceAsync(IUser owner, decimal amount, string reason = null)
{
await AddBalanceAsync(owner, -amount);
return true;
}
public Task<bool> RemoveBalanceAsync(IEconomyAccount account, decimal amount, string reason = null)
{
throw new NotImplementedException();
}
public async Task SetBalanceAsync(IUser owner, decimal amount)
{
using (var db = new UconomyDbContext(Plugin))
{
var account = await db.Accounts.FirstOrDefaultAsync(a => a.Id == owner.Id);
account.Balance = amount;
account.LastUpdated = DateTime.Now;
await db.SaveChangesAsync();
}
}
public Task SetBalanceAsync(IEconomyAccount account, decimal amount)
{
throw new NotImplementedException();
}
public Task<bool> SupportsNegativeBalanceAsync(IEconomyAccount account)
{
throw new NotImplementedException();
}
public Task<bool> CreateAccountAsync(IUser owner, string name, out IEconomyAccount account)
{
throw new NotImplementedException();
}
public Task<bool> CreateAccountAsync(IUser owner, string name, IEconomyCurrency currency, out IEconomyAccount account)
{
throw new NotImplementedException();
}
public Task<bool> DeleteAccountAsync(IEconomyAccount account)
{
throw new NotImplementedException();
}
public Task<IEconomyAccount> GetAccountAsync(IUser owner, string accountName = null)
{
throw new NotImplementedException();
}
public Task<IEnumerable<IEconomyAccount>> GetAccountsAsync(IUser owner)
{
throw new NotImplementedException();
}
public bool SupportsUser(IUser user)
{
return !(user is IConsole);
}
public async Task<decimal> GetBalanceAsync(IUser user)
{
using (var db = new UconomyDbContext(Plugin))
{
var account = await db.Accounts.FirstOrDefaultAsync(a => a.Id == user.Id);
return account.Balance;
}
}
public IEnumerable<IEconomyCurrency> Currencies => throw new NotSupportedException();
public IEconomyCurrency DefaultCurrency => new UconomyCurrency(Plugin.ConfigurationInstance.MoneyName);
public bool SupportsMultipleAccounts => false;
public async Task CreateAccountAsync(IUser user)
{
using (var db = new UconomyDbContext(Plugin))
{
if (!await db.Accounts.AnyAsync(a => a.Id == user.Id))
{
var account = new Account(user.Id, Plugin.ConfigurationInstance.InitialBalance);
await db.Accounts.AddAsync(account);
await db.SaveChangesAsync();
}
}
}
}
}