/
SeriousVote.java
607 lines (512 loc) · 22.6 KB
/
SeriousVote.java
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
package net.adamsanchez.seriousvote;
import com.google.inject.Inject;
import com.vexsoftware.votifier.model.Vote;
import com.vexsoftware.votifier.sponge.event.VotifierEvent;
import net.adamsanchez.seriousvote.Data.VoteSpreeSystem;
import net.adamsanchez.seriousvote.Data.OfflineHandler;
import net.adamsanchez.seriousvote.commands.*;
import net.adamsanchez.seriousvote.integration.PlaceHolders;
import net.adamsanchez.seriousvote.loot.LootTable;
import net.adamsanchez.seriousvote.utils.*;
import net.adamsanchez.seriousvote.vote.Status;
import net.adamsanchez.seriousvote.vote.VoteRequest;
import ninja.leaping.configurate.ConfigurationNode;
import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.game.GameReloadEvent;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePostInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.event.game.state.GameStartingServerEvent;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.plugin.Dependency;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.service.user.UserStorageService;
import java.io.*;
import java.nio.file.Path;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
/**
* Created by adam_ on 12/08/16.
*/
@SuppressWarnings("unused")
@Plugin(id = "seriousvote",
name = "SeriousVote",
version = "4.8.9",
description = "This plugin enables server admins to give players rewards for voting for their server.",
dependencies = {@Dependency(id = "nuvotifier", optional = false), @Dependency(id = "placeholderapi", optional = true)})
public class SeriousVote {
@Inject
private Game game;
private Game getGame() {
return this.game;
}
@Inject
private PluginContainer plugin;
public PluginContainer getPlugin() {
return this.plugin;
}
private static SeriousVote instance;
@Inject
Logger logger;
public Logger getLogger() {
return logger;
}
@Inject
@DefaultConfig(sharedRoot = false)
private Path defaultConfig;
private Path offlineVotesPath;
private Path resetDatePath;
@Inject
@DefaultConfig(sharedRoot = false)
private ConfigurationLoader<CommentedConfigurationNode> loader;
@Inject
@ConfigDir(sharedRoot = false)
private Path privateConfigDir;
private CommentedConfigurationNode mainCfgNode;
///////////////////////////////////////////////////////
boolean milestonesEnabled = false, dailiesEnabled = false;
private VoteSpreeSystem voteSpreeSystem;
public List<String> monthlySet, yearlySet, weeklySet;
int[] milestonesUsed;
///////////////////////////////////////////////////////
public String databaseType, databaseName, databaseHostname, databasePort, databasePrefix, databaseUsername, databasePassword, minIdleConnections, maxActiveConnections;
///////////////////////////////////////////////////////
private LinkedList<VoteRequest> processedVoteQueue = new LinkedList<VoteRequest>();
private LinkedList<String> executingQueue = new LinkedList<String>();
private List<VoteRequest> voteQueue = new LinkedList<VoteRequest>();
private ScheduleManager scheduleManager;
LinkedHashMap<Integer, List<Map<String, String>>> lootMap = new LinkedHashMap<Integer, List<Map<String, String>>>();
//Stored Offline Votes
HashMap<String, Integer> offlineVotes = new HashMap<String, Integer>();
int numRandRewards;
int minRandRewards;
int maxRandRewards;
List<String> setCommands;
String publicMessage;
String publicOfflineMessage;
boolean debug = false;
boolean lootTablesAvailable = false;
boolean randomDisabled = false;
boolean processIfOffline = false;
boolean messageOffline = false;
private static Optional<UserStorageService> userStorage;
//////////////////////////////////////////////////////////////////
String[][] mainRewardTables;
private int chanceTotal, chanceMax, chanceMin = 0;
private int[] chanceMap;
@Listener
public void onInitialization(GamePreInitializationEvent event) {
instance = this;
userStorage = Sponge.getServiceManager().provide(UserStorageService.class);
CC.printSVInfo();
getLogger().info(CC.YELLOW + "Trying To setup Config Loader");
offlineVotes = new HashMap<String, Integer>();
offlineVotesPath = Paths.get(privateConfigDir.toString(), "", "offlinevotes.dat");
resetDatePath = Paths.get(privateConfigDir.toString(), "", "lastReset");
OfflineHandler.initOfflineStorage();
CM.initConfig(defaultConfig);
reloadConfigs();
}
@Listener
public void onPostInitialization(GamePostInitializationEvent event) {
instance = this;
}
@Listener
public void onServerStart(GameInitializationEvent event) {
CommandHandler.registerCommands();
getLogger().info(CC.YELLOW + "Serious Vote Has Loaded");
if (milestonesEnabled == true | dailiesEnabled == true) {
voteSpreeSystem = new VoteSpreeSystem(mainCfgNode);
} else {
voteSpreeSystem = null;
}
//begin any scheduled tasks
scheduleManager = new ScheduleManager().run();
}
@Listener
public void onPostInit(GamePostInitializationEvent event) {
}
@Listener
public void onServerStart(GameStartingServerEvent event) {
PlaceHolders.initialize(Sponge.getPluginManager().fromInstance(this).get());
}
@Listener
public void reload(GameReloadEvent event) {
reloadConfigs();
}
///////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////CONFIGURATION METHODS//////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
public boolean reloadConfigs() {
//try loading from file
try {
mainCfgNode = loader.load();
} catch (IOException e) {
U.error(CC.RED + "There was an error while reloading your configs");
U.error(CC.RED_UNDERLINED + "PLEASE CHECK YOUR CONFIG FOR MISSING QUOTES, BRACKETS, OR COMMAS BEFORE ASKING FOR HELP!!");
U.error(CC.YELLOW + CC.LINE);
U.error(CC.RED + e.toString());
return false;
}
//update variables and other instantiations
debug = CM.getDebugMode(mainCfgNode);
publicMessage = CM.getPublicMessage(mainCfgNode);
publicOfflineMessage = CM.getOfflineMessage(mainCfgNode);
processIfOffline = CM.getBypassOffline(mainCfgNode);
messageOffline = CM.getMessageOffline(mainCfgNode);
numRandRewards = updateRewardsNumbers(mainCfgNode);
updateLoot(mainCfgNode);
setCommands = CM.getSetCommands(mainCfgNode);
U.debug("Here's your commands");
for (String ix : CM.getRandomCommands(mainCfgNode)) {
U.debug(ix);
}
//Load Offline votes
U.info(CC.YELLOW + "Trying to load offline player votes from ... " + offlineVotesPath.toString());
try {
offlineVotes = OfflineHandler.loadOffline();
} catch (EOFException e) {
offlineVotes = new HashMap<>();
try {
U.debug("Trying to save corrected Map.");
OfflineHandler.saveOffline();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
U.error(CC.RED + "ahahahahaha We Couldn't load up the stored offline player votes", e);
} catch (ClassNotFoundException e) {
U.error(CC.RED + "Well crap that is noooot a hash map! GO slap the dev!");
}
//Reload DB configuration
databaseType = CM.getDatabaseType(mainCfgNode);
databaseHostname = CM.getDatabaseHostname(mainCfgNode);
databaseName = CM.getDatabaseName(mainCfgNode);
databasePassword = CM.getDatabasePassword(mainCfgNode);
databasePrefix = CM.getDatabasePrefix(mainCfgNode);
databaseUsername = CM.getDatabaseUsername(mainCfgNode);
databasePort = CM.getDatabasePort(mainCfgNode);
minIdleConnections = CM.getMinIdleConnections(mainCfgNode);
maxActiveConnections = CM.getMaxActiveConnections(mainCfgNode);
milestonesEnabled = CM.getMilestonesEnabled(mainCfgNode);
dailiesEnabled = CM.getDailiesEnabled(mainCfgNode);
reloadDB();
/////////Load Up VoteSpreeSystem/////////
monthlySet = CM.getMonthlySetCommands(mainCfgNode);
yearlySet = CM.getYearlySetCommands(mainCfgNode);
weeklySet = CM.getWeeklySetCommands(mainCfgNode);
milestonesUsed = CM.getEnabledMilestones(mainCfgNode);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////
private int updateRewardsNumbers(ConfigurationNode node) {
int number = node.getNode("config", "random-rewards-number").getInt();
randomDisabled = number == 0 ? true : false;
minRandRewards = node.getNode("config", "rewards-min").getInt();
maxRandRewards = node.getNode("config", "rewards-max").getInt() + 1;
return number;
}
/**
* Checks for and imports the random loot settings from the config. It creates chanceMaps of tables.
*
* @param node The base config node used for checking for loot.
*/
public void updateLoot(ConfigurationNode node) {
List<String> nodeStrings = node.getNode("config", "vote-reward", "random").getChildrenList().stream()
.map(ConfigurationNode::getString).collect(Collectors.toList());
if (nodeStrings.size() < 1) {
U.info("There are no random tables to load");
return;
}
if (nodeStrings.size() % 2 != 0) {
U.error("Please check the Config for your main random rewards, to make sure they are formatted correctly");
} else {
lootTablesAvailable = true;
String[] inputLootSource = nodeStrings.stream().toArray(String[]::new);
//Create a new Array of the proper size x*2 to hold the tables for choosing later
String[][] table = new String[2][inputLootSource.length / 2];
chanceMap = new int[inputLootSource.length / 2];
U.info(CC.PURPLE + inputLootSource.length / 2 + CC.YELLOW + " Tables Imported for Rewards");
for (int ix = 0; ix < inputLootSource.length; ix += 2) {
table[0][ix / 2] = inputLootSource[ix];
table[1][ix / 2] = inputLootSource[ix + 1];
//Initialize chanceMap
chanceMap[ix / 2] = Integer.parseInt(table[0][ix / 2]);
if (ix != 0) {
chanceMap[ix / 2] += chanceMap[(ix / 2) - 1];
}
}
mainRewardTables = table;
chanceTotal = chanceMap.length - 1;
chanceMin = chanceMap[0];
chanceMax = chanceMap[chanceTotal];
}
}
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////LISTENERS///////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
@Listener
public synchronized void onVote(VotifierEvent event) {
//Workflow Stage 1
synchronized (voteQueue) {
voteQueue.add(new VoteRequest(event.getVote()));
}
}
public void processVotes() {
//Workflow Stage 2 - Timed Task Main thread
List<VoteRequest> localQueue = new LinkedList<>();
synchronized (voteQueue) {
localQueue.addAll(voteQueue);
voteQueue.clear();
}
//TODO Determine validity of vote -> if offline / online - if found in system
// Separate out the loot gathering code (command making)
// Separate out Online checking and add state
for (VoteRequest vr : localQueue) {
VoteRequest workingRequest = vr;
workingRequest.setVoteStatus(Status.IN_PROCESS);
///NEW CODE ////
//Get Online Status
workingRequest = workflowOnlineState(workingRequest);
//Use online status to decide which route to take
switch (workingRequest.getVoteStatus()) {
case BUILD_ONLINE:
//if online gather rewards
workingRequest.setVoteStatus(Status.GATHER_REWARDS);
workingRequest = processVoteChanceTables(workingRequest);
break;
case BUILD_OFFLINE:
if (processIfOffline) {
workingRequest.setVoteStatus(Status.GATHER_REWARDS);
workingRequest = processVoteChanceTables(workingRequest);
} else {
workingRequest.setVoteStatus(Status.SAVE_OFFLINE);
workingRequest = storeOfflineVote(workingRequest);
if (workingRequest.getVoteStatus() == Status.ERROR) {
U.error("There was an error processing that last vote.");
}
}
break;
default:
U.error("Something was wrong with that vote? Was it offline?");
continue;
}
//Take the state changed request and see whether to broadcast a message or not.
switch (workingRequest.getVoteStatus()) {
case REWARDS_GATHERED:
OutputHelper.broadCastMessage(publicMessage, vr.getUsername(), U.listMaker(workingRequest.getRewardNames()));
break;
case OFFLINE_SAVED:
OutputHelper.broadCastMessage(publicOfflineMessage, vr.getUsername());
break;
default:
U.error("Error with that vote's state...Uh Oh!");
continue;
}
if (voteSpreeSystem != null) {
if (U.isPlayerOnline(vr.getUsername())) {
voteSpreeSystem.addVote(U.getPlayerIdentifier(vr.getUsername()));
} else {
if (userStorage.get().get(vr.getUsername()).isPresent()) {
voteSpreeSystem.addVote(U.getPlayerIdentifier(vr.getUsername()));
}
}
}
workingRequest.setVoteStatus(Status.COMPLETED);
processedVoteQueue.add(workingRequest);
}
executeCommands();
}
public VoteRequest workflowOnlineState(VoteRequest vr) {
if (U.isPlayerOnline(vr.getUsername())) {
vr.setVoteStatus(Status.BUILD_ONLINE);
} else {
vr.setVoteStatus(Status.BUILD_OFFLINE);
}
return vr;
}
public void reloadDB() {
if (dailiesEnabled || milestonesEnabled) {
U.info("Attempting to reload database...");
if (voteSpreeSystem != null) {
voteSpreeSystem.shutdown();
}
voteSpreeSystem = new VoteSpreeSystem(mainCfgNode);
return;
}
U.info("Attempting to reload database, but it is not enabled!");
}
@Listener
public void onPlayerJoin(ClientConnectionEvent.Join event) {
String playerID = event.getTargetEntity().getUniqueId().toString();
String username = event.getTargetEntity().getName();
if (offlineVotes.containsKey(username)) {
U.debug("Offline votes found for player with ID " + playerID);
List<VoteRequest> voteCollection = new LinkedList<VoteRequest>();
for (int ix = 0; ix < offlineVotes.get(username).intValue(); ix++) {
VoteRequest workingRequest = new VoteRequest();
workingRequest.setUsername(username);
voteCollection.add(processVoteChanceTables(workingRequest));
}
//Collect all the reward names into one to prevent spam.
Set<String> rewardNames = new HashSet<String>();
for(VoteRequest vr: voteCollection){
rewardNames.addAll(vr.getRewardNames());
}
OutputHelper.broadCastMessage(publicMessage, username, U.listMaker(rewardNames));
processedVoteQueue.addAll(voteCollection);
offlineVotes.remove(username);
executeCommands();
try {
OfflineHandler.saveOffline();
} catch (IOException e) {
U.error("Error while saving offline votes file", e);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////ACTION METHODS///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
public void executeCommands() {
U.debug(CC.CYAN + "Emptying Queue");
for (VoteRequest vr : processedVoteQueue) {
for (String command : vr.getRewards()) {
game.getCommandManager().process(game.getServer().getConsole(), command);
}
}
processedVoteQueue.clear();
}
public VoteRequest processVoteChanceTables(VoteRequest vr) {
//Workflow Level 4
VoteRequest workingRequest = vr;
U.debug("Adding SetCommands to the process queue");
for (String setCommand : setCommands) {
workingRequest.addReward(setCommand);
}
if (!lootTablesAvailable || randomDisabled) {
workingRequest.setVoteStatus(Status.REWARDS_GATHERED);
return workingRequest;
}
//Setup Loot Table and gather rewards
int maxNumberOfRewards = LootTools.genNumRandRewards(numRandRewards, minRandRewards, maxRandRewards);
for (int i = 0; i < maxNumberOfRewards; i++) {
LootTable mainLoot = new LootTable(LootTools.chooseTable(chanceMap, mainRewardTables), mainCfgNode);
U.debug("Choosing a random reward.");
String chosenReward = mainLoot.chooseReward();
U.debug("Chose: " + chosenReward);
workingRequest.addRewardName(mainCfgNode.getNode("config", "Rewards", chosenReward, "name").getString());
for (String ix : mainCfgNode.getNode("config", "Rewards", chosenReward, "rewards").getChildrenList().stream()
.map(ConfigurationNode::getString).collect(Collectors.toList())) {
workingRequest.addReward(OutputHelper.parseVariables(ix, workingRequest.getUsername()));
U.debug(CC.YELLOW + "QUEUED: " + CC.WHITE + ix);
}
}
workingRequest.setVoteStatus(Status.REWARDS_GATHERED);
return workingRequest;
}
/**
* This processes loot tables and then moves to the command execution workflow
*/
public void forceGiveVote(String username) {
VoteRequest workingRequest = new VoteRequest();
workingRequest.setUsername(username);
workingRequest = processVoteChanceTables(workingRequest);
processedVoteQueue.add(workingRequest);
executeCommands();
}
public VoteRequest storeOfflineVote(VoteRequest vr) {
VoteRequest workingRequest = vr;
if (workingRequest.getUsername() != null) {
//Write to File
if (offlineVotes.containsKey(workingRequest.getUsername())) {
offlineVotes.put(workingRequest.getUsername(), offlineVotes.get(workingRequest.getUsername()).intValue() + 1);
} else {
offlineVotes.put(workingRequest.getUsername(), new Integer(1));
}
try {
OfflineHandler.saveOffline();
workingRequest.setVoteStatus(Status.OFFLINE_SAVED);
} catch (IOException e) {
U.error("Woah did that just happen? I couldn't save that offline player's vote!", e);
workingRequest.setVoteStatus(Status.ERROR);
}
} else {
U.error("That vote didn't have a playername :(");
workingRequest.setVoteStatus(Status.ERROR);
}
return workingRequest;
}
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////Accessors and Modifiers/////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
public static SeriousVote getInstance() {
return instance;
}
public static Optional<UserStorageService> getUserStorage() {
return userStorage;
}
public static Game getPublicGame() {
return getInstance().getGame();
}
public boolean isDailiesEnabled() {
return dailiesEnabled;
}
public boolean usingVoteSpreeSystem() {
if (voteSpreeSystem != null) return true;
return false;
}
public boolean isMilestonesEnabled() {
return milestonesEnabled;
}
public boolean isDebug() {
return debug;
}
public boolean toggleDebug() {
debug = !debug;
return debug;
}
public void setDebug(boolean _debug) {
debug = _debug;
}
public VoteSpreeSystem getVoteSpreeSystem() {
return voteSpreeSystem;
}
public HashMap<String, Integer> getOfflineVotes() {
return offlineVotes;
}
public static boolean isServerOnline() {
return getPublicGame().getServer().getOnlineMode();
}
public Path getOfflineVotesPath() {
return offlineVotesPath;
}
public void triggerSave() {
try {
OfflineHandler.saveOffline();
} catch (IOException e) {
U.debug("Could not save file in a triggered save :(");
e.printStackTrace();
}
}
public Path getResetDatePath() {
return resetDatePath;
}
public Path getSQLDumpPath() {
return Paths.get(privateConfigDir.toString(), "", "sqlExport.csv");
}
public boolean hasUnprocessedVotes() {
return !voteQueue.isEmpty();
}
public CommentedConfigurationNode getMainCfgNode() {
return mainCfgNode;
}
}