-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplifiedMatchMaker.java
317 lines (225 loc) · 13.7 KB
/
SimplifiedMatchMaker.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package alternativa.test.task1;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
class SimplifiedMatchMaker implements MatchMaker {
private final static int MATCH_PLAYERS_COUNT = 8;
private final static int OTHER_RANKS_ALLOW_TIMEOUT = 5000;
private final static int MIN_RANK = 1;
private final static int MAX_RANK = 30;
private final Deque<WaitingPlayer>[] waitingPlayers;
private final OnMatchCreatedListener matchListener;
private MatchSelectorThread selectorThread;
/**
* @param matchCreatedListener listener to run after each match creation
*/
SimplifiedMatchMaker(OnMatchCreatedListener matchCreatedListener) {
this.matchListener = matchCreatedListener;
//create different rank deque for all ranks
waitingPlayers = new Deque[MAX_RANK - MIN_RANK + 1];
for (int i = 0; i < waitingPlayers.length; i++){
waitingPlayers[i] = new ConcurrentLinkedDeque<>();
}
}
public void startMatchMaking(){
if (selectorThread != null) selectorThread.shutdown();
selectorThread = new MatchSelectorThread();
selectorThread.start();
}
public void stopMatchMaking(){
if (selectorThread != null) selectorThread.shutdown();
}
public void registerPlayer(int uid, int rank){
Deque<WaitingPlayer> d = waitingPlayers[rank-MIN_RANK];
synchronized (d){
d.add(new WaitingPlayer(uid,rank, System.currentTimeMillis()));
}
}
private void createMatch(WaitingPlayer... players){
//emulate match creation delay
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (matchListener != null) matchListener.onMatchCreated(players);
}
private static boolean playersCompat(WaitingPlayer p1, WaitingPlayer p2, long time){
if (p1 == p2) return true;
if (p1.rank == p2.rank) return true;
return (Math.abs(p1.rank - p2.rank)<=(time-p1.enterTime)/ OTHER_RANKS_ALLOW_TIMEOUT
&& Math.abs(p1.rank - p2.rank)<=(time-p2.enterTime)/ OTHER_RANKS_ALLOW_TIMEOUT);
}
final static Comparator<WaitingPlayer> byWaitTimeReverse = (o1, o2) -> {
if (o1.enterTime > o2.enterTime) return 1;
if (o1.enterTime < o2.enterTime) return -1;
return 0;
};
private class MatchSelectorThread extends Thread{
private volatile boolean run = true;
@Override
public void run() {
setName("Match selector");
while (run){
//simple cases processing (i.e. create matches with same rank)
//not matched players added to this list to process hard cases next (different ranks)
List<WaitingPlayer> playersLeft = new LinkedList<>();
long now = System.currentTimeMillis();
for (Deque<WaitingPlayer> rankDeque : waitingPlayers) {
LinkedList<WaitingPlayer> selectedPlayers = new LinkedList<>();
synchronized (rankDeque) {
while (rankDeque.size() >= MATCH_PLAYERS_COUNT) {
for (int i = 0; i < MATCH_PLAYERS_COUNT; i++){
//only select players and release sync lock asap
selectedPlayers.add(rankDeque.pollLast());
}
}
//only players waited enough added to hard case processing
for (WaitingPlayer p : rankDeque){
if (now - p.enterTime >= OTHER_RANKS_ALLOW_TIMEOUT) playersLeft.add(p);
}
}
//create match in separate thread to avoid match creation delay in match selector thread
while (selectedPlayers.size() >= MATCH_PLAYERS_COUNT) {
WaitingPlayer[] matchMembers = new WaitingPlayer[MATCH_PLAYERS_COUNT];
for (int i = 0; i < MATCH_PLAYERS_COUNT; i++){
matchMembers[i] = selectedPlayers.remove();
}
new Thread(() -> createMatch(matchMembers)).start();
}
}
//simple cases done. no more than 7*30 = 210 players left in all rank queues
//work around hard cases (i.e. players with different ranks)
//sorting to make better wait times
playersLeft.sort(byWaitTimeReverse);
//normally do look over only once
boolean doLookOver = true;
while (doLookOver){
doLookOver = false;
//calc compatibility sets
int size = playersLeft.size();
Set<Integer>[] compatSets = new Set[size];
for (int i = 0; i < size; i++) compatSets[i] = new HashSet<>();
for (int i = 0; i < size; i++){
for ( int j = i; j < size; j++){
if (playersCompat(playersLeft.get(i), playersLeft.get(j), now)){
compatSets[i].add(j);
compatSets[j].add(i);
}
}
}
//some big logic, hard to make short explanation
for (int i1 = 0; i1 < size; i1 ++){
Set<Integer> s1 = compatSets[i1];
if (s1.size() < MATCH_PLAYERS_COUNT) continue;
for (Integer i2 : s1){
if (Objects.equals(i2,i1)) continue;
Set<Integer> s2 = compatSets[i2];
if (s2.size() < MATCH_PLAYERS_COUNT) continue;
Set<Integer> intersection1 = new HashSet<>(s1);
intersection1.retainAll(s2);
if (intersection1.size() < MATCH_PLAYERS_COUNT) continue;
for (Integer i3 : intersection1){
if (Objects.equals(i3,i1)
|| Objects.equals(i3,i2)) continue;
Set<Integer> s3 = compatSets[i3];
if (s3.size() < MATCH_PLAYERS_COUNT) continue;
Set<Integer> intersection2 = new HashSet<>(intersection1);
intersection2.retainAll(s3);
if (intersection2.size() < MATCH_PLAYERS_COUNT) continue;
for (Integer i4 : intersection2){
if (Objects.equals(i4,i1)
|| Objects.equals(i4,i2)
|| Objects.equals(i4,i3)) continue;
Set<Integer> s4 = compatSets[i4];
if (s4.size() < MATCH_PLAYERS_COUNT) continue;
Set<Integer> intersection3 = new HashSet<>(intersection2);
intersection3.retainAll(s4);
if (intersection3.size() < MATCH_PLAYERS_COUNT) continue;
for (Integer i5 : intersection3){
if (Objects.equals(i5,i1)
|| Objects.equals(i5,i2)
|| Objects.equals(i5,i3)
|| Objects.equals(i5,i4)) continue;
Set<Integer> s5 = compatSets[i5];
if (s5.size() < MATCH_PLAYERS_COUNT) continue;
Set<Integer> intersection4 = new HashSet<>(intersection3);
intersection4.retainAll(s5);
if (intersection4.size() < MATCH_PLAYERS_COUNT) continue;
for (Integer i6 : intersection4){
if (Objects.equals(i6,i1)
|| Objects.equals(i6,i2)
|| Objects.equals(i6,i3)
|| Objects.equals(i6,i4)
|| Objects.equals(i6,i5)) continue;
Set<Integer> s6 = compatSets[i6];
if (s6.size() < MATCH_PLAYERS_COUNT) continue;
Set<Integer> intersection5 = new HashSet<>(intersection4);
intersection5.retainAll(s6);
if (intersection5.size() < MATCH_PLAYERS_COUNT) continue;
for (Integer i7 : intersection5){
if (Objects.equals(i7,i1)
|| Objects.equals(i7,i2)
|| Objects.equals(i7,i3)
|| Objects.equals(i7,i4)
|| Objects.equals(i7,i5)
|| Objects.equals(i7,i6)) continue;
Set<Integer> s7 = compatSets[i7];
if (s7.size() < MATCH_PLAYERS_COUNT) continue;
Set<Integer> intersection6 = new HashSet<>(intersection5);
intersection6.retainAll(s7);
if (intersection6.size() < MATCH_PLAYERS_COUNT) continue;
for (Integer i8 : intersection6){
if (Objects.equals(i8,i1)
|| Objects.equals(i8,i2)
|| Objects.equals(i8,i3)
|| Objects.equals(i8,i4)
|| Objects.equals(i8,i5)
|| Objects.equals(i8,i6)
|| Objects.equals(i8,i7)) continue;
Set<Integer> s8 = compatSets[i8];
if (s8.size() < MATCH_PLAYERS_COUNT) continue;
Set<Integer> intersection7 = new HashSet<>(intersection6);
intersection7.retainAll(s8);
if (intersection7.size() < MATCH_PLAYERS_COUNT) continue;
WaitingPlayer[] matchMembers = new WaitingPlayer[MATCH_PLAYERS_COUNT];
matchMembers[0] = playersLeft.get(i1);
matchMembers[1] = playersLeft.get(i2);
matchMembers[2] = playersLeft.get(i3);
matchMembers[3] = playersLeft.get(i4);
matchMembers[4] = playersLeft.get(i5);
matchMembers[5] = playersLeft.get(i6);
matchMembers[6] = playersLeft.get(i7);
matchMembers[7] = playersLeft.get(i8);
for (WaitingPlayer player : matchMembers) {
playersLeft.remove(player);
waitingPlayers[player.rank - 1].remove(player);
}
//create match in separate thread to avoid match creation delay in current thread
new Thread(() -> createMatch(matchMembers)).start();
//do it one more time if some matched players found
//and break all look over cycles, they has no sense now because compatibility sets changed
doLookOver = true;
break;
}
if (doLookOver) break;
}
if (doLookOver) break;
}
if (doLookOver) break;
}
if (doLookOver) break;
}
if (doLookOver) break;
}
if (doLookOver) break;
}
if (doLookOver) break;
}
}
}
}
void shutdown(){
run = false;
}
}
}