· 7 years ago · Oct 13, 2018, 08:26 PM
1package mineplex.core.donation.repository;
2
3import java.sql.ResultSet;
4import java.sql.SQLException;
5import java.util.UUID;
6
7import org.bukkit.Bukkit;
8import org.bukkit.plugin.java.JavaPlugin;
9
10import mineplex.core.common.util.Callback;
11import mineplex.core.database.DBPool;
12import mineplex.core.database.DatabaseRunnable;
13import mineplex.core.database.RepositoryBase;
14import mineplex.core.database.column.ColumnInt;
15import mineplex.core.database.column.ColumnVarChar;
16import mineplex.core.donation.Donor;
17import mineplex.core.donation.repository.token.GemRewardToken;
18import mineplex.core.donation.repository.token.PurchaseToken;
19import mineplex.core.donation.repository.token.UnknownPurchaseToken;
20import mineplex.core.server.remotecall.AsyncJsonWebCall;
21import mineplex.core.server.remotecall.JsonWebCall;
22import mineplex.core.server.util.TransactionResponse;
23
24public class DonationRepository extends RepositoryBase
25{
26 private static String CREATE_COIN_TRANSACTION_TABLE = "CREATE TABLE IF NOT EXISTS accountCoinTransactions (id INT NOT NULL AUTO_INCREMENT, accountId INT, reason VARCHAR(100), coins INT, PRIMARY KEY (id), FOREIGN KEY (accountId) REFERENCES accounts(id));";
27 private static String CREATE_GEM_TRANSACTION_TABLE = "CREATE TABLE IF NOT EXISTS accountGemTransactions (id INT NOT NULL AUTO_INCREMENT, accountId INT, reason VARCHAR(100), gems INT, PRIMARY KEY (id), FOREIGN KEY (accountId) REFERENCES accounts(id));";
28 private static String INSERT_COIN_TRANSACTION = "INSERT INTO accountCoinTransactions(accountId, reason, coins) VALUES(?, ?, ?);";
29 private static String UPDATE_ACCOUNT_COINS = "UPDATE accounts SET coins = coins + ? WHERE id = ?;";
30 private static String UPDATE_ACCOUNT_GOLD = "UPDATE accounts SET gold = gold + ? WHERE id = ?;";
31 private static String UPDATE_NULL_ACCOUNT_GEMS_AND_COINS_ = "UPDATE accounts SET gems = ?, coins = ? WHERE id = ? AND gems IS NULL AND coins IS NULL;";
32
33 private String _webAddress;
34
35 public DonationRepository(JavaPlugin plugin, String webAddress)
36 {
37 super(plugin, DBPool.ACCOUNT);
38
39 _webAddress = webAddress;
40 }
41
42 public void PurchaseKnownSalesPackage(final Callback<TransactionResponse> callback, String name, final String uuid, final int cost, final int salesPackageId)
43 {
44 final PurchaseToken token = new PurchaseToken();
45 token.AccountName = name;
46 token.UsingCredits = false;
47 token.SalesPackageId = salesPackageId;
48
49 final Callback<TransactionResponse> extraCallback = new Callback<TransactionResponse>()
50 {
51 public void run(final TransactionResponse response)
52 {
53 Bukkit.getServer().getScheduler().runTask(Plugin, new Runnable()
54 {
55 @Override
56 public void run()
57 {
58 callback.run(response);
59 }
60 });
61 }
62 };
63
64 handleDatabaseCall(new DatabaseRunnable(new Runnable()
65 {
66 public void run()
67 {
68 new JsonWebCall(_webAddress + "PlayerAccount/PurchaseKnownSalesPackage").Execute(TransactionResponse.class, extraCallback, token);
69 }
70 }), "Error purchasing known sales package in DonationRepository : ");
71 }
72
73 public void PurchaseUnknownSalesPackage(final Callback<TransactionResponse> callback, final String name, final int accountId, final String packageName, final boolean coinPurchase, final int cost)
74 {
75 final UnknownPurchaseToken token = new UnknownPurchaseToken();
76 token.AccountName = name;
77 token.SalesPackageName = packageName;
78 token.CoinPurchase = coinPurchase;
79 token.Cost = cost;
80 token.Premium = false;
81
82 final Callback<TransactionResponse> extraCallback = new Callback<TransactionResponse>()
83 {
84 public void run(final TransactionResponse response)
85 {
86 if (response == TransactionResponse.Success)
87 {
88 if (coinPurchase)
89 {
90 executeUpdate(UPDATE_ACCOUNT_COINS, new ColumnInt("coins", -cost), new ColumnInt("id", accountId));
91 //executeUpdate(INSERT_COIN_TRANSACTION, new ColumnInt("id", accountId), new ColumnVarChar("reason", 100, "Purchased " + packageName), new ColumnInt("coins", -cost));
92 }
93 }
94
95 Bukkit.getServer().getScheduler().runTask(Plugin, new Runnable()
96 {
97 @Override
98 public void run()
99 {
100 callback.run(response);
101 }
102 });
103 }
104 };
105
106 handleDatabaseCall(new DatabaseRunnable(new Runnable()
107 {
108 public void run()
109 {
110 new JsonWebCall(_webAddress + "PlayerAccount/PurchaseUnknownSalesPackage").Execute(TransactionResponse.class, extraCallback, token);
111 }
112 }), "Error purchasing unknown sales package in DonationRepository : ");
113 }
114
115 public void gemReward(final Callback<Boolean> callback, final String giver, String name, final String uuid, final int greenGems)
116 {
117 final GemRewardToken token = new GemRewardToken();
118 token.Source = giver;
119 token.Name = name;
120 token.Amount = greenGems;
121
122 final Callback<Boolean> extraCallback = new Callback<Boolean>()
123 {
124 public void run(final Boolean response)
125 {
126 Bukkit.getServer().getScheduler().runTask(Plugin, new Runnable()
127 {
128 @Override
129 public void run()
130 {
131 callback.run(response);
132 }
133 });
134 }
135 };
136
137 handleDatabaseCall(new DatabaseRunnable(new Runnable()
138 {
139 public void run()
140 {
141 new JsonWebCall(_webAddress + "PlayerAccount/GemReward").Execute(Boolean.class, extraCallback, token);
142 }
143 }), "Error updating player gem amount in DonationRepository : ");
144 }
145
146 public void rewardCoins(final Callback<Boolean> callback, final String giver, String name, final int accountId, final int coins)
147 {
148 final GemRewardToken token = new GemRewardToken();
149 token.Source = giver;
150 token.Name = name;
151 token.Amount = coins;
152
153 final Callback<Boolean> extraCallback = new Callback<Boolean>()
154 {
155 public void run(final Boolean response)
156 {
157 if (response)
158 {
159 executeUpdate(UPDATE_ACCOUNT_COINS, new ColumnInt("coins", coins), new ColumnInt("id", accountId));
160 //executeUpdate(INSERT_COIN_TRANSACTION, new ColumnInt("id", accountId), new ColumnVarChar("reason", 100, "Rewarded by " + giver), new ColumnInt("coins", coins));
161 }
162
163 Bukkit.getServer().getScheduler().runTask(Plugin, new Runnable()
164 {
165 @Override
166 public void run()
167 {
168 callback.run(response);
169 }
170 });
171 }
172 };
173
174 handleDatabaseCall(new DatabaseRunnable(new Runnable()
175 {
176 public void run()
177 {
178 new JsonWebCall(_webAddress + "PlayerAccount/CoinReward").Execute(Boolean.class, extraCallback, token);
179 }
180 }), "Error updating player coin amount in DonationRepository : ");
181 }
182
183 public void rewardGold(final Callback<Boolean> callback, final String giver, final String name, final int accountId, final int gold)
184 {
185 handleDatabaseCall(new DatabaseRunnable(new Runnable()
186 {
187 public void run()
188 {
189 if (executeUpdate(UPDATE_ACCOUNT_GOLD, new ColumnInt("gold", gold), new ColumnInt("id", accountId)) < 1)
190 {
191 callback.run(false);
192 }
193 else
194 callback.run(true);
195 }
196 }), "Error updating player gold amount in DonationRepository : ");
197 }
198
199 @Override
200 protected void initialize()
201 {
202 executeUpdate(CREATE_COIN_TRANSACTION_TABLE);
203 executeUpdate(CREATE_GEM_TRANSACTION_TABLE);
204 }
205
206 @Override
207 protected void update()
208 {
209 }
210
211 public void updateGemsAndCoins(final UUID accountId, final int gems, final int coins)
212 {
213 handleDatabaseCall(new DatabaseRunnable(new Runnable()
214 {
215 public void run()
216 {
217 executeUpdate(UPDATE_NULL_ACCOUNT_GEMS_AND_COINS_, new ColumnInt("gems", gems), new ColumnInt("coins", coins), new ColumnInt("id", accountId));
218 }
219 }), "Error updating player's null gems and coins DonationRepository : ");
220 }
221
222 public void applyKits(String playerName)
223 {
224 new AsyncJsonWebCall(_webAddress + "PlayerAccount/ApplyKits").Execute(playerName);
225 }
226
227 public Donor retrieveDonorInfo(ResultSet resultSet) throws SQLException
228 {
229 Donor donor = new Donor();
230
231 while (resultSet.next())
232 {
233 donor.addGold(resultSet.getInt(1));
234 }
235
236 return donor;
237 }
238}