summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/data/ExecutionHistory.java
blob: da57c3c50ed181c0ebb0a2e89c1795b23bbaf3fe (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
package com.earth2me.essentials.anticheat.data;

import com.earth2me.essentials.anticheat.actions.Action;
import java.util.HashMap;
import java.util.Map;


/**
 * Store amount of action executions for last 60 seconds for various actions
 *
 */
public class ExecutionHistory
{
	private static class ExecutionHistoryEntry
	{
		private final int executionTimes[];
		private long lastExecution = 0;
		private int totalEntries = 0;
		private long lastClearedTime = 0;

		private ExecutionHistoryEntry(int monitoredTimeFrame)
		{
			this.executionTimes = new int[monitoredTimeFrame];
		}

		/**
		 * Remember an execution at the specific time
		 */
		private void addCounter(long time)
		{
			// clear out now outdated values from the array
			if (time - lastClearedTime > 0)
			{
				// Clear the next few fields of the array
				clearTimes(lastClearedTime + 1, time - lastClearedTime);
				lastClearedTime = time + 1;
			}

			executionTimes[(int)(time % executionTimes.length)]++;
			totalEntries++;
		}

		/**
		 * Clean parts of the array
		 *
		 * @param start
		 * @param length
		 */
		private void clearTimes(long start, long length)
		{

			if (length <= 0)
			{
				return; // nothing to do (yet)
			}
			if (length > executionTimes.length)
			{
				length = executionTimes.length;
			}

			int j = (int)start % executionTimes.length;

			for (int i = 0; i < length; i++)
			{
				if (j == executionTimes.length)
				{
					j = 0;
				}

				totalEntries -= executionTimes[j];
				executionTimes[j] = 0;

				j++;
			}
		}

		public int getCounter()
		{
			return totalEntries;
		}

		public long getLastExecution()
		{
			return lastExecution;
		}

		public void setLastExecution(long time)
		{
			this.lastExecution = time;
		}
	}
	// Store data between Events
	// time + action + action-counter
	private final Map<String, Map<Action, ExecutionHistoryEntry>> executionHistories;

	public ExecutionHistory()
	{
		executionHistories = new HashMap<String, Map<Action, ExecutionHistoryEntry>>();
	}

	/**
	 * Returns true, if the action should be executed, because all time criteria have been met. Will add a entry with
	 * the time to a list which will influence further requests, so only use once and remember the result
	 *
	 * @param check
	 * @param action
	 * @param time a time IN SECONDS
	 * @return
	 */
	public boolean executeAction(String check, Action action, long time)
	{

		Map<Action, ExecutionHistoryEntry> executionHistory = executionHistories.get(check);

		if (executionHistory == null)
		{
			executionHistory = new HashMap<Action, ExecutionHistoryEntry>();
			executionHistories.put(check, executionHistory);
		}

		ExecutionHistoryEntry entry = executionHistory.get(action);

		if (entry == null)
		{
			entry = new ExecutionHistoryEntry(60);
			executionHistory.put(action, entry);
		}

		// update entry
		entry.addCounter(time);

		if (entry.getCounter() > action.delay)
		{
			// Execute action?
			if (entry.getLastExecution() <= time - action.repeat)
			{
				// Execute action!
				entry.setLastExecution(time);
				return true;
			}
		}

		return false;
	}
}