Skip to content

Commit 1748148

Browse files
committed
Only send vetoes when player updates them
1 parent cc7f01f commit 1748148

File tree

5 files changed

+44
-35
lines changed

5 files changed

+44
-35
lines changed
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package com.faforever.client.preferences;
22

3+
import com.faforever.client.domain.api.MapPoolAssignment;
4+
35
public record VetoKey(
46
int matchmakerQueueMapPoolId,
57
int mapPoolMapVersionId
6-
) {}
8+
) {
9+
10+
public static VetoKey of(MapPoolAssignment assignment) {
11+
return new VetoKey(assignment.mapPool().mapPool().id(), assignment.id());
12+
}
13+
14+
}

src/main/java/com/faforever/client/teammatchmaking/TeamMatchmakingMapTileController.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import com.faforever.client.domain.api.MapPoolAssignment;
55
import com.faforever.client.domain.api.MapVersion;
66
import com.faforever.client.fx.ImageViewHelper;
7+
import com.faforever.client.fx.NodeController;
78
import com.faforever.client.i18n.I18n;
89
import com.faforever.client.map.MapService;
9-
import com.faforever.client.fx.NodeController;
1010
import com.faforever.client.map.MapService.PreviewSize;
1111
import com.faforever.client.map.generator.MapGeneratorService;
1212
import com.faforever.client.preferences.MatchmakerPrefs;
@@ -25,13 +25,14 @@
2525
import javafx.scene.image.ImageView;
2626
import javafx.scene.layout.HBox;
2727
import javafx.scene.layout.Pane;
28-
import javafx.scene.layout.VBox;
2928
import javafx.scene.layout.Region;
29+
import javafx.scene.layout.VBox;
3030
import lombok.RequiredArgsConstructor;
3131
import lombok.Setter;
3232
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3333
import org.springframework.context.annotation.Scope;
3434
import org.springframework.stereotype.Component;
35+
3536
import java.util.function.Consumer;
3637

3738
/**
@@ -101,9 +102,10 @@ void setVetoModeEnabled(BooleanProperty vetoModeEnabled) {
101102
public void bindVetoModeDependentProperties() {
102103
vetoesBox.mouseTransparentProperty().bind(vetoModeEnabled.not());
103104
vetoesBox.visibleProperty().bind(vetoModeEnabled.or(tokenCount.greaterThan(0)));
104-
root.cursorProperty().bind(Bindings.when(
105-
Bindings.createBooleanBinding(() -> isGeneratedMap.getValue() || vetoModeEnabled.get(), isGeneratedMap, vetoModeEnabled)
106-
).then(Cursor.DEFAULT).otherwise(Cursor.HAND));
105+
root.cursorProperty()
106+
.bind(Bindings.when(
107+
Bindings.createBooleanBinding(() -> isGeneratedMap.getValue() || vetoModeEnabled.get(), isGeneratedMap,
108+
vetoModeEnabled)).then(Cursor.DEFAULT).otherwise(Cursor.HAND));
107109
}
108110

109111
@Override
@@ -112,9 +114,9 @@ protected void onInitialize() {
112114
isGeneratedMap = mapObservable.map(map -> mapGeneratorService.isGeneratedMap(map.displayName())).orElse(false);
113115

114116
thumbnailImageView.imageProperty()
115-
.bind(assignment
116-
.map(assignmentBean -> mapService.loadPreview(assignmentBean.mapVersion(), PreviewSize.SMALL))
117-
.flatMap(imageViewHelper::createPlaceholderImageOnErrorObservable));
117+
.bind(assignment.map(
118+
assignmentBean -> mapService.loadPreview(assignmentBean.mapVersion(), PreviewSize.SMALL))
119+
.flatMap(imageViewHelper::createPlaceholderImageOnErrorObservable));
118120

119121
nameLabel.textProperty().bind(mapObservable.map(map -> {
120122
if (isGeneratedMap.getValue()) {
@@ -123,9 +125,7 @@ protected void onInitialize() {
123125
return map.displayName();
124126
}));
125127

126-
authorBox.visibleProperty()
127-
.bind(mapObservable.map(
128-
map -> (map.author() != null) || isGeneratedMap.getValue()));
128+
authorBox.visibleProperty().bind(mapObservable.map(map -> (map.author() != null) || isGeneratedMap.getValue()));
129129
authorBox.managedProperty().bind(authorBox.visibleProperty());
130130

131131
authorLabel.textProperty().bind(mapObservable.map(map -> {
@@ -165,27 +165,26 @@ protected void onInitialize() {
165165
});
166166

167167
vetoButton.setOnAction(event -> {
168-
if (assignment.getValue() == null) {
168+
MapPoolAssignment value = assignment.getValue();
169+
if (value == null) {
169170
return;
170171
}
171172
int currentTokenCount = tokenCount.get();
172173
int currentMaxPerMap = maxPerMap.get();
173174
if ((isMaxPerMapDynamic.get() || currentTokenCount < currentMaxPerMap) && currentTokenCount < vetoTokensMax.get() && vetoTokensLeft.getValue() > 0) {
174-
teamMatchmakingService.setTokensForMap(
175-
new VetoKey(assignment.getValue().mapPool().mapPool().id(), assignment.getValue().id()),
176-
currentTokenCount + 1);
175+
teamMatchmakingService.setTokensForMap(VetoKey.of(value), currentTokenCount + 1);
177176
}
178177
event.consume();
179178
});
180179

181180
minusButton.setOnAction(event -> {
182-
if (assignment.getValue() == null) {
181+
MapPoolAssignment value = assignment.getValue();
182+
if (value == null) {
183183
return;
184184
}
185185
int current = tokenCount.get();
186186
if (current > 0) {
187-
teamMatchmakingService.setTokensForMap(
188-
new VetoKey(assignment.getValue().mapPool().mapPool().id(), assignment.getValue().id()), current - 1);
187+
teamMatchmakingService.setTokensForMap(VetoKey.of(value), current - 1);
189188
}
190189
event.consume();
191190
});
@@ -213,11 +212,12 @@ private void updateBannedState() {
213212
}
214213

215214
private void updateVetoes() {
216-
if (assignment.getValue() == null) {
215+
MapPoolAssignment value = assignment.getValue();
216+
if (value == null) {
217217
tokenCount.set(0);
218218
return;
219219
}
220-
VetoKey key = new VetoKey(assignment.getValue().mapPool().mapPool().id(), assignment.getValue().id());
220+
VetoKey key = VetoKey.of(value);
221221
int usedTokens = matchmakerPrefs.getAppliedVetoes().getOrDefault(key, 0);
222222
tokenCount.set(usedTokens);
223223
}

src/main/java/com/faforever/client/teammatchmaking/TeamMatchmakingService.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,6 @@ public void afterPropertiesSet() throws Exception {
246246
}
247247
});
248248

249-
matchmakerPrefs.getAppliedVetoes().subscribe(() -> {
250-
if (fafServerAccessor.getConnectionState() == ConnectionState.CONNECTED) {
251-
sendVetoes();
252-
}
253-
});
254-
255249
party.ownerProperty().subscribe((oldValue, newValue) -> {
256250
if (oldValue != null) {
257251
chatService.leaveChannel("#" + oldValue.getUsername() + PARTY_CHANNEL_SUFFIX);
@@ -275,21 +269,31 @@ private void sendVetoes() {
275269
fafServerAccessor.setPlayerVetoes(getVetoesAsList());
276270
}
277271

278-
public void setTokensForMap(VetoKey vetoKey, Integer vetoTokensApplied) {
279-
matchmakerPrefs.getAppliedVetoes().put(vetoKey, vetoTokensApplied);
272+
public void setTokensForMap(VetoKey key, int vetoTokensApplied) {
273+
Integer previousValue;
274+
if (vetoTokensApplied == 0) {
275+
previousValue = matchmakerPrefs.getAppliedVetoes().remove(key);
276+
} else {
277+
previousValue = matchmakerPrefs.getAppliedVetoes().put(key, vetoTokensApplied);
278+
}
279+
280+
if ((previousValue == null && vetoTokensApplied > 0) || (previousValue != null && previousValue != vetoTokensApplied)) {
281+
sendVetoes();
282+
}
280283
}
281284

282-
public void setAllVetoes(List<VetoData> vetoes) {
285+
public void updateVetoes(List<VetoData> vetoes) {
283286
Map<VetoKey, Integer> vetosMap = vetoes.stream()
284287
.collect(Collectors.toMap(
285288
vetoData -> new VetoKey(vetoData.getMatchmakerQueueMapPoolId(),
286289
vetoData.getMapPoolMapVersionId()),
287290
VetoData::getVetoTokensApplied));
291+
matchmakerPrefs.getAppliedVetoes().clear();
288292
matchmakerPrefs.getAppliedVetoes().putAll(vetosMap);
289293
}
290294

291295
private void onVetoesChanged(VetoesChangedInfo vetoesChangedInfo) {
292-
setAllVetoes(vetoesChangedInfo.getVetoes());
296+
updateVetoes(vetoesChangedInfo.getVetoes());
293297

294298
if (vetoesChangedInfo.getForced()) {
295299
notificationService.addNotification(new ImmediateNotification(i18n.get("teammatchmaking.vetoes.forced.title"),

src/test/java/com/faforever/client/teammatchmaking/TeamMatchmakingMapTileControllerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import static org.junit.jupiter.api.Assertions.assertTrue;
3535
import static org.mockito.ArgumentMatchers.any;
3636
import static org.mockito.ArgumentMatchers.anyString;
37-
import static org.mockito.Mockito.doAnswer;
3837
import static org.mockito.Mockito.lenient;
3938
import static org.mockito.Mockito.verify;
4039
import static org.mockito.Mockito.when;

src/test/java/com/faforever/client/teammatchmaking/TeamMatchmakingServiceTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,8 @@ public void testSendVetoesOnConnection() {
638638
public void testSendVetoesOnVetoesChange() {
639639
connectionState.set(ConnectionState.CONNECTED);
640640
when(fafServerAccessor.getConnectionState()).thenReturn(ConnectionState.CONNECTED);
641-
// Clear invocations from connection state change
642-
org.mockito.Mockito.clearInvocations(fafServerAccessor);
643641

644-
matchmakerPrefs.getAppliedVetoes().put(new VetoKey(1, 1), 1);
642+
instance.setTokensForMap(new VetoKey(1, 1), 1);
645643

646644
verify(fafServerAccessor, times(1)).setPlayerVetoes(anyList());
647645
}

0 commit comments

Comments
 (0)