This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServiceHandler.java
124 lines (101 loc) · 3.77 KB
/
ServiceHandler.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
package me.josscoder.jbridge.service;
import me.josscoder.jbridge.JBridgeCore;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class ServiceHandler {
public Map<String, ServiceInfo> getServiceInfoMapCache() {
return JBridgeCore.getInstance().getServiceInfoCache().asMap();
}
public ServiceInfo getService(String id) {
return filterServices(service -> service.getId().startsWith(id))
.stream()
.findFirst()
.orElse(null);
}
public boolean containsService(String id) {
return getService(id) != null;
}
public List<ServiceInfo> filterServices(Predicate<? super ServiceInfo> predicate) {
return getServiceInfoMapCache().values()
.stream()
.filter(predicate)
.collect(Collectors.toList());
}
public List<ServiceInfo> getGroupServices(String group) {
return filterServices(service -> service.getGroup().equalsIgnoreCase(group));
}
public boolean groupHasServices(String group) {
return getGroupServices(group).size() > 0;
}
public boolean groupHasService(String group, String id) {
return getGroupServices(group).stream()
.anyMatch(service -> service.getId().startsWith(id));
}
public boolean groupHasPlayer(String group, String player) {
return getGroupServices(group).stream()
.anyMatch(service -> service.containsPlayer(player));
}
public int getPlayersOnline(String group) {
return getGroupServices(group)
.stream()
.mapToInt(ServiceInfo::getPlayersOnline)
.sum();
}
public int getPlayersOnline() {
return getServiceInfoMapCache().values()
.stream()
.mapToInt(ServiceInfo::getPlayersOnline)
.sum();
}
public int getMaxPlayers(String group) {
return getGroupServices(group)
.stream()
.mapToInt(ServiceInfo::getMaxPlayers)
.sum();
}
public int getMaxPlayers() {
return getServiceInfoMapCache().values()
.stream()
.mapToInt(ServiceInfo::getMaxPlayers)
.sum();
}
public enum SortMode {
RANDOM,
LOWEST,
FILL
}
public ServiceInfo getSortedServiceFromGroups(List<String> groups, SortMode sortMode) {
List<ServiceInfo> serviceList = new ArrayList<>();
groups.forEach(group -> serviceList.addAll(getGroupServices(group)));
return getSortedServiceFromList(serviceList, sortMode);
}
public ServiceInfo getSortedServiceFromGroup(String group, SortMode sortMode) {
return getSortedServiceFromList(getGroupServices(group), sortMode);
}
public ServiceInfo getSortedServiceFromList(List<ServiceInfo> serviceList, SortMode sortMode) {
ServiceInfo serviceInfo = null;
switch (sortMode) {
case RANDOM:
serviceInfo = serviceList.stream()
.findAny()
.orElse(null);
break;
case LOWEST:
serviceInfo = serviceList.stream()
.min(Comparator.comparingInt(ServiceInfo::getPlayersOnline))
.orElse(null);
break;
case FILL:
serviceInfo = serviceList.stream()
.filter(service -> !service.isFull())
.max(Comparator.comparingInt(ServiceInfo::getPlayersOnline))
.orElse(null);
break;
}
return serviceInfo;
}
}