128 lines
3.8 KiB
Java
Executable file
128 lines
3.8 KiB
Java
Executable file
package nl.iobyte.themepark.api.ridecount;
|
|
|
|
import nl.iobyte.themepark.ThemePark;
|
|
import nl.iobyte.themepark.api.event.ridecount.RideCountTotalTypeChangeEvent;
|
|
import nl.iobyte.themepark.api.ridecount.enums.TotalType;
|
|
import nl.iobyte.themepark.api.ridecount.objects.AttractionCount;
|
|
import nl.iobyte.themepark.api.ridecount.objects.RideCount;
|
|
import nl.iobyte.themepark.api.ridecount.objects.TopData;
|
|
import org.bukkit.Bukkit;
|
|
import java.util.*;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
public class RideCountService {
|
|
|
|
private final Map<String, AttractionCount> counts = new ConcurrentHashMap<>();
|
|
private final TopData topData = new TopData();
|
|
private boolean changed = false;
|
|
private TotalType type = TotalType.DAILY;
|
|
|
|
public TotalType getType() {
|
|
return type;
|
|
}
|
|
|
|
public void setType(TotalType type) {
|
|
if(this.type == type)
|
|
return;
|
|
|
|
TotalType old = this.type;
|
|
this.type = type;
|
|
ThemePark.getInstance().getAPI().getEventDispatcher().call(
|
|
new RideCountTotalTypeChangeEvent(old, type)
|
|
);
|
|
}
|
|
|
|
public AttractionCount getCount(String id) {
|
|
if(id == null || id.isEmpty())
|
|
return null;
|
|
|
|
if(!ThemePark.getInstance().getAPI().getAttractionService().hasAttraction(id))
|
|
return null;
|
|
|
|
return counts.computeIfAbsent(id, k -> new AttractionCount(id));
|
|
}
|
|
|
|
public void setCount(String id, UUID uuid, int amount, int total_amount) {
|
|
if(amount == 0 || total_amount == 0 || id == null || id.isEmpty() || uuid == null)
|
|
return;
|
|
|
|
AttractionCount count = getCount(id);
|
|
if(count == null)
|
|
return;
|
|
|
|
count.setCount(uuid, amount, total_amount);
|
|
}
|
|
|
|
public RideCount getCount(String id, UUID uuid) {
|
|
if(id == null || id.isEmpty() || uuid == null)
|
|
return null;
|
|
|
|
AttractionCount count = getCount(id);
|
|
if(count == null)
|
|
return null;
|
|
|
|
return count.getCount(uuid);
|
|
}
|
|
|
|
public void addCount(String id, UUID uuid, int amount) {
|
|
if(amount == 0 || id == null || id.isEmpty() || uuid == null)
|
|
return;
|
|
|
|
AttractionCount count = getCount(id);
|
|
if(count == null)
|
|
return;
|
|
|
|
count.addCount(uuid, amount);
|
|
changed = true;
|
|
}
|
|
|
|
public void clearCount(UUID uuid) {
|
|
if(uuid == null)
|
|
return;
|
|
|
|
for(AttractionCount attractionCount : counts.values())
|
|
attractionCount.removeCount(uuid);
|
|
}
|
|
|
|
public boolean hasChanged() {
|
|
return changed;
|
|
}
|
|
|
|
public List<RideCount> getChanges(long timestamp) {
|
|
List<RideCount> list = new ArrayList<>();
|
|
for(AttractionCount attractionCount : counts.values())
|
|
for(Map<Integer, Map<UUID, RideCount>> m1 : attractionCount.getCounts().values())
|
|
for(Map<UUID, RideCount> m2 : m1.values())
|
|
for(RideCount count : m2.values())
|
|
if(count.getUpdatedAt() != -1 && count.getUpdatedAt() > timestamp)
|
|
list.add(count);
|
|
|
|
return list;
|
|
}
|
|
|
|
public void clearNonApplicable() {
|
|
changed = false;
|
|
Calendar calendar = new GregorianCalendar();
|
|
calendar.setTime(new Date());
|
|
|
|
int year = calendar.get(Calendar.YEAR);
|
|
int day = calendar.get(Calendar.DAY_OF_YEAR);
|
|
for(AttractionCount attractionCount : counts.values()) {
|
|
attractionCount.removeOld(
|
|
year,
|
|
day
|
|
);
|
|
|
|
Map<UUID, RideCount> map = attractionCount.getCounts(year, day);
|
|
if(map == null)
|
|
return;
|
|
|
|
map.entrySet().removeIf(e -> Bukkit.getPlayer(e.getValue().getUUID()) == null);
|
|
}
|
|
}
|
|
|
|
public TopData getTopData() {
|
|
return topData;
|
|
}
|
|
|
|
}
|