summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java
blob: 41f78ad111f118be383debe6b712caa4f6b9905a (plain)
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
package com.earth2me.essentials.commands;

import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import com.earth2me.essentials.textreader.SimpleTextInput;
import com.earth2me.essentials.textreader.TextPager;
import com.earth2me.essentials.utils.NumberUtil;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.bukkit.Server;


public class Commandbalancetop extends EssentialsCommand
{
	public Commandbalancetop()
	{
		super("balancetop");
	}
	private static final int CACHETIME = 2 * 60 * 1000;
	public static final int MINUSERS = 50;
	private static final SimpleTextInput cache = new SimpleTextInput();
	private static long cacheage = 0;
	private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

	@Override
	protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception
	{
		int page = 0;
		boolean force = false;
		if (args.length > 0)
		{
			try
			{
				page = Integer.parseInt(args[0]);
			}
			catch (NumberFormatException ex)
			{
				if (args[0].equalsIgnoreCase("force")
					&& (!sender.isPlayer() || ess.getUser(sender.getPlayer()).isAuthorized("essentials.balancetop.force")))
				{
					force = true;
				}
			}
		}

		if (!force && lock.readLock().tryLock())
		{
			try
			{
				if (cacheage > System.currentTimeMillis() - CACHETIME)
				{
					outputCache(sender, commandLabel, page);
					return;
				}
				if (ess.getUserMap().getUniqueUsers() > MINUSERS)
				{
					sender.sendMessage(tl("orderBalances", ess.getUserMap().getUniqueUsers()));
				}
			}
			finally
			{
				lock.readLock().unlock();
			}
			ess.runTaskAsynchronously(new Viewer(sender, commandLabel, page, force));
		}
		else
		{
			if (ess.getUserMap().getUniqueUsers() > MINUSERS)
			{
				sender.sendMessage(tl("orderBalances", ess.getUserMap().getUniqueUsers()));
			}
			ess.runTaskAsynchronously(new Viewer(sender, commandLabel, page, force));
		}

	}

	private static void outputCache(final CommandSource sender, String command,  int page)
	{
		final Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(cacheage);
		final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
		sender.sendMessage(tl("balanceTop", format.format(cal.getTime())));
		new TextPager(cache).showPage(Integer.toString(page), null, "balancetop", sender);
	}


	private class Calculator implements Runnable
	{
		private final transient Viewer viewer;
		private final boolean force;

		public Calculator(final Viewer viewer, final boolean force)
		{
			this.viewer = viewer;
			this.force = force;
		}

		@Override
		public void run()
		{
			lock.writeLock().lock();
			try
			{
				if (force || cacheage <= System.currentTimeMillis() - CACHETIME)
				{
					cache.getLines().clear();
					final Map<String, BigDecimal> balances = new HashMap<String, BigDecimal>();
					BigDecimal totalMoney = BigDecimal.ZERO;
					if (ess.getSettings().isEcoDisabled())
					{
						if (ess.getSettings().isDebug())
						{
							ess.getLogger().info("Internal economy functions disabled, aborting baltop.");
						}
					}
					else
					{
						for (UUID u : ess.getUserMap().getAllUniqueUsers())
						{
							final User user = ess.getUserMap().getUser(u);
							if (user != null)
							{
								final BigDecimal userMoney = user.getMoney();
								user.updateMoneyCache(userMoney);
								totalMoney = totalMoney.add(userMoney);
								final String name = user.isHidden() ? user.getName() : user.getDisplayName();
								balances.put(name, userMoney);
							}
						}
					}

					final List<Map.Entry<String, BigDecimal>> sortedEntries = new ArrayList<Map.Entry<String, BigDecimal>>(balances.entrySet());
					Collections.sort(sortedEntries, new Comparator<Map.Entry<String, BigDecimal>>()
					{
						@Override
						public int compare(final Entry<String, BigDecimal> entry1, final Entry<String, BigDecimal> entry2)
						{
							return entry2.getValue().compareTo(entry1.getValue());
						}
					});

					cache.getLines().add(tl("serverTotal", NumberUtil.displayCurrency(totalMoney, ess)));
					int pos = 1;
					for (Map.Entry<String, BigDecimal> entry : sortedEntries)
					{
						cache.getLines().add(pos + ". " + entry.getKey() + ", " + NumberUtil.displayCurrency(entry.getValue(), ess));
						pos++;
					}
					cacheage = System.currentTimeMillis();
				}
			}
			finally
			{
				lock.writeLock().unlock();
			}
			ess.runTaskAsynchronously(viewer);
		}
	}


	private class Viewer implements Runnable
	{
		private final transient CommandSource sender;
		private final transient int page;
		private final transient boolean force;
		private final transient String commandLabel;

		public Viewer(final CommandSource sender, final String commandLabel, final int page, final boolean force)
		{
			this.sender = sender;
			this.page = page;
			this.force = force;
			this.commandLabel = commandLabel;
		}

		@Override
		public void run()
		{
			lock.readLock().lock();
			try
			{
				if (!force && cacheage > System.currentTimeMillis() - CACHETIME)
				{
					outputCache(sender, commandLabel, page);
					return;
				}
			}
			finally
			{
				lock.readLock().unlock();
			}
			ess.runTaskAsynchronously(new Calculator(new Viewer(sender, commandLabel, page, false), force));
		}
	}
}