Skip to content

Commit

Permalink
Add new merge command to combine user totals
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCodez committed Feb 6, 2024
1 parent 9f85149 commit 97e5f73
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -35,6 +37,7 @@
import com.bencodez.advancedcore.api.misc.PlayerUtils;
import com.bencodez.advancedcore.api.rewards.RewardOptions;
import com.bencodez.advancedcore.api.updater.Updater;
import com.bencodez.advancedcore.api.user.UserStorage;
import com.bencodez.advancedcore.api.user.userstorage.Column;
import com.bencodez.advancedcore.api.user.userstorage.DataType;
import com.bencodez.advancedcore.api.valuerequest.ValueRequest;
Expand Down Expand Up @@ -1725,6 +1728,51 @@ public void execute(CommandSender sender, String[] args) {
}
});

plugin.getAdminVoteCommand()
.add(new CommandHandler(plugin, new String[] { "MergeDataFrom", "(UserStorage)" },
"VotingPlugin.Commands.AdminVote.MergeDataFrom|" + adminPerm,
"Merge player totals from other storage type", true, true) {

@Override
public void execute(CommandSender sender, String[] args) {
plugin.loadUserAPI(UserStorage.value(args[1]));

if (plugin.getMysql() != null) {
plugin.getMysql().clearCacheBasic();
}

HashMap<UUID, ArrayList<Column>> cols = plugin.getUserManager()
.getAllKeys(UserStorage.value(args[1]));
Queue<Entry<UUID, ArrayList<Column>>> players = new LinkedList<Entry<UUID, ArrayList<Column>>>(
cols.entrySet());
ArrayList<String> uuids = plugin.getUserManager().getAllUUIDs();

while (players.size() > 0) {
Entry<UUID, ArrayList<Column>> entry = players.poll();
VotingPluginUser user = plugin.getVotingPluginUserManager()
.getVotingPluginUser(entry.getKey(), false);
user.dontCache();
if (uuids.contains(entry.getKey().toString())) {
user.mergeData(user.getData().convert(entry.getValue()));
} else {
user.getData().setValues(plugin.getStorageType(),
user.getData().convert(entry.getValue()));
}

sendMessage(sender, "Finished merge for " + user.getUUID() + ", " + players.size()
+ " more left to go!");

if (players.size() % 50 == 0) {
sendMessage(sender,
"Working on converting data, about " + players.size() + " left to go!");
}
}
plugin.getUserManager().getDataManager().clearCache();
sendMessage(sender, "Merge finished");

}
});

plugin.getAdminVoteCommand().add(new CommandHandler(plugin, new String[] {},
"VotingPlugin.Commands.AdminVote|" + adminPerm, "Base command") {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.bencodez.advancedcore.api.rewards.RewardBuilder;
import com.bencodez.advancedcore.api.rewards.RewardOptions;
import com.bencodez.advancedcore.api.user.AdvancedCoreUser;
import com.bencodez.advancedcore.api.user.usercache.value.DataValue;
import com.bencodez.advancedcore.api.user.usercache.value.DataValueInt;
import com.bencodez.votingplugin.VotingPluginMain;
import com.bencodez.votingplugin.bungee.BungeeMessageData;
import com.bencodez.votingplugin.bungee.BungeeMethod;
Expand Down Expand Up @@ -1409,4 +1411,20 @@ public long getLastVoteTimer(VoteSite voteSite) {

}

public void mergeData(HashMap<String, DataValue> toAdd) {
HashMap<String, DataValue> currentData = getData().getValues();
HashMap<String, DataValue> newData = new HashMap<String, DataValue>();

for (TopVoter top : TopVoter.values()) {
if (toAdd.containsKey(top.getColumnName()) && currentData.containsKey(top.getColumnName())) {
newData.put(top.getColumnName(), new DataValueInt(
currentData.get(top.getColumnName()).getInt() + toAdd.get(top.getColumnName()).getInt()));
}
}

if (newData.size() > 0) {
getData().setValues(newData);
}
}

}

0 comments on commit 97e5f73

Please sign in to comment.