Skip to content

Commit 182ce80

Browse files
committed
Add booth and submission image embeds
1 parent eeabe68 commit 182ce80

File tree

4 files changed

+93
-17
lines changed

4 files changed

+93
-17
lines changed

common/src/main/java/net/modfest/platform/pojo/SubmissionData.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public record SubmissionData(@NonNull String id,
1919
@Nullable String source,
2020
@Nullable BoothData boothData,
2121
@NonNull Awards awards
22-
) implements Data {
22+
) implements Data {
2323

2424
public record Awards(Set<String> theme, Set<String> extra) {
2525
}
@@ -68,8 +68,8 @@ public JsonElement serialize(AssociatedData src,
6868
var jsonObj = context.serialize(src.inner).getAsJsonObject();
6969

7070
var typeKey = switch (src.inner) {
71-
case Modrinth a -> Modrinth.KEY;
72-
case Other a -> Other.KEY;
71+
case Modrinth ignored -> Modrinth.KEY;
72+
case Other ignored -> Other.KEY;
7373
default -> throw new IllegalStateException();
7474
};
7575
jsonObj.addProperty("type", typeKey);
@@ -93,7 +93,11 @@ public enum BoothStatus {
9393
COMPLETE
9494
}
9595

96-
public record Column(int x, int z) {}
96+
public record Column(int x, int z) {
97+
public String toFormattedString() {
98+
return "x: " + x + ", z: " + z;
99+
}
100+
}
97101

98102
public record WarpCoordinates(int x, int y, int z, @NonNull Direction direction) {
99103
@Getter
@@ -121,6 +125,10 @@ public enum Direction {
121125
this.yaw = yaw;
122126
}
123127
}
128+
129+
public String toFormattedString() {
130+
return "x: " + x + ", z: " + z + ", direction: " + direction.name();
131+
}
124132
}
125133
}
126134
}

platform_api/src/main/java/net/modfest/platform/controller/EventController.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.modfest.platform.service.EventService;
1010
import net.modfest.platform.service.ImageService;
1111
import net.modfest.platform.service.SubmissionService;
12+
import net.modfest.platform.service.WebhookService;
1213
import org.apache.shiro.SecurityUtils;
1314
import org.apache.shiro.authz.annotation.RequiresPermissions;
1415
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,6 +34,8 @@ public class EventController {
3334
private UserController userController;
3435
@Autowired
3536
private ImageService imageService;
37+
@Autowired
38+
private WebhookService webhookService;
3639

3740
@GetMapping("/events")
3841
public Collection<EventData> getAllEvents() {
@@ -295,9 +298,10 @@ public SubmissionResponseData editSubmissionImage(HttpServletRequest request, @P
295298
};
296299

297300
imageService.downloadSubmissionImage(url, new SubmissionRepository.SubmissionId(eventId, subId), typeEnum);
301+
webhookService.submissionImageChanged(submission, typeEnum);
298302

299303
return service.addResponseInfo(
300-
request, service.getSubmission(eventId, subId)
304+
request, submission
301305
);
302306
}
303307

platform_api/src/main/java/net/modfest/platform/service/SubmissionService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public SubmissionData editSubmissionBooth(SubmissionData data, SubmissionData.Bo
8181
}
8282
data = data.withBoothData(edit);
8383
submissionRepository.save(data);
84+
85+
webhookService.editSubmissionBooth(data, edit);
86+
8487
return getSubmission(data.event(), data.id());
8588
}
8689

platform_api/src/main/java/net/modfest/platform/service/WebhookService.java

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,44 +31,49 @@ public class WebhookService {
3131

3232
private final RestTemplate restTemplate = new RestTemplate();
3333

34-
protected record Field(String name, String value) {
34+
private record Field(String name, String value) {
3535
}
3636

37-
protected record Footer(String text, @Nullable String icon_url) {
37+
private record Footer(String text, @Nullable String icon_url) {
3838
}
3939

40-
protected record Image(String url) {
40+
private record Image(String url) {
4141
}
4242

43-
protected record Embed(String title, String description, int color, List<Field> fields, Footer footer,
43+
private record Embed(String title, String description, int color, List<Field> fields, Footer footer, Image image,
4444
Image thumbnail) {
4545
}
4646

47-
protected record WebhookPayload(List<Embed> embeds) {
47+
private record WebhookPayload(List<Embed> embeds) {
4848
}
4949

50-
Embed embedForSubmission(SubmissionData data, String event, int eventColor) {
50+
private Embed embedForSubmission(SubmissionData data, String event, int eventColor) {
5151
return embedForSubmission(data, event, eventColor, null, null);
5252
}
5353

54-
Embed embedForSubmission(SubmissionData data, String event, int eventColor, @Nullable List<Field> fields) {
54+
private Embed embedForSubmission(SubmissionData data, String event, int eventColor, @Nullable List<Field> fields) {
5555
return embedForSubmission(data, event, eventColor, fields, null);
5656
}
5757

58-
Embed embedForSubmission(SubmissionData data, String event, int eventColor, @Nullable List<Field> fields, @Nullable Footer footer) {
58+
private Embed embedForSubmission(SubmissionData data, String event, int eventColor, @Nullable List<Field> fields, @Nullable Footer footer) {
59+
return embedForSubmission(data, event, eventColor, fields, footer, null);
60+
}
61+
62+
private Embed embedForSubmission(SubmissionData data, String event, int eventColor, @Nullable List<Field> fields, @Nullable Footer footer, @Nullable Image image) {
5963
return new Embed(
6064
data.name(),
6165
event,
6266
eventColor,
6367
fields,
6468
footer,
65-
getIcon(data)
69+
image,
70+
getImage(data, ImageService.SubmissionImageType.ICON)
6671
);
6772
}
6873

69-
private @Nullable Image getIcon(SubmissionData data) {
74+
private @Nullable Image getImage(SubmissionData data, ImageService.SubmissionImageType type) {
7075
var subKey = new SubmissionRepository.SubmissionId(data.event(), data.id());
71-
var icon = imageService.getImageUrl(null, subKey, ImageService.SubmissionImageType.ICON);
76+
var icon = imageService.getImageUrl(null, subKey, type);
7277

7378
if (icon == null) {
7479
return null;
@@ -77,7 +82,7 @@ Embed embedForSubmission(SubmissionData data, String event, int eventColor, @Nul
7782
return new Image(icon);
7883
}
7984

80-
protected void sendEmbed(Embed embed) {
85+
private void sendEmbed(Embed embed) {
8186
if (discordWebhookUrl == null) {
8287
return;
8388
}
@@ -97,6 +102,12 @@ protected void sendEmbed(Embed embed) {
97102
}
98103
}
99104

105+
private String timestampRelative(int minutes) {
106+
var currentTime = System.currentTimeMillis() / 1000L;
107+
var time = currentTime + minutes * 60L;
108+
109+
return "<t:" + time + ":R>";
110+
}
100111

101112
private String maskedLink(String name, String url) {
102113
return String.format("[%s](%s)", name, url);
@@ -196,6 +207,55 @@ public void deleteSubmission(SubmissionData data) {
196207
sendEmbed(embed);
197208
}
198209

210+
@Async
211+
public void submissionImageChanged(SubmissionData data, ImageService.SubmissionImageType type) {
212+
var typeString = switch (type) {
213+
case TEST -> "Test";
214+
case CLAIM -> "Claim";
215+
case BUILD -> "Build";
216+
default -> null;
217+
};
218+
219+
if (typeString == null) {
220+
return;
221+
}
222+
223+
var embed = embedForSubmission(data, typeString + " image set", 16777048, null, null, getImage(data, type));
224+
sendEmbed(embed);
225+
}
226+
227+
@Async
228+
public void editSubmissionBooth(SubmissionData data, SubmissionData.BoothData edit) {
229+
var fields = new ArrayList<Field>();
230+
231+
if (edit.itemIcon() != null) {
232+
fields.add(new Field("Item icon", edit.itemIcon()));
233+
}
234+
235+
if (edit.markerPos() != null) {
236+
fields.add(new Field("Marker position", edit.markerPos().toFormattedString()));
237+
}
238+
239+
if (edit.minutesToComplete() != null) {
240+
fields.add(new Field("Complete", timestampRelative(edit.minutesToComplete())));
241+
}
242+
243+
if(edit.shards() != null) {
244+
fields.add(new Field("Shards", edit.shards().toString()));
245+
}
246+
247+
if(edit.status() != null) {
248+
fields.add(new Field("Status", edit.status().name()));
249+
}
250+
251+
if(edit.warp() != null) {
252+
fields.add(new Field("Warp", edit.warp().toFormattedString()));
253+
}
254+
255+
var embed = embedForSubmission(data, "Boot data changed", 16777048, fields);
256+
sendEmbed(embed);
257+
}
258+
199259
@Async
200260
public void phaseChanged(String eventName, String phase, int count) {
201261
var embed = new Embed(
@@ -204,6 +264,7 @@ public void phaseChanged(String eventName, String phase, int count) {
204264
16772110,
205265
null,
206266
new Footer(String.format("Currently %d submissions", count), null),
267+
null,
207268
null
208269
);
209270

0 commit comments

Comments
 (0)