Skip to content

Commit

Permalink
feat: implement event gRPC methods on Java service
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Mar 10, 2024
1 parent 1b0c7d2 commit 8432b63
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package dev.emortal.api.service.party;

import dev.emortal.api.grpc.party.EventServiceGrpc;
import dev.emortal.api.grpc.party.PartyProto;
import dev.emortal.api.grpc.party.PartyServiceGrpc;
import dev.emortal.api.model.common.Pageable;
import dev.emortal.api.model.common.PlayerSkin;
import dev.emortal.api.model.party.EventData;
import dev.emortal.api.model.party.Party;
import dev.emortal.api.model.party.PartyInvite;
import dev.emortal.api.utils.ProtoTimestampConverter;
import dev.emortal.api.utils.internal.GrpcErrorHelper;
import io.grpc.Channel;
import io.grpc.Status;
Expand All @@ -13,6 +17,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Instant;
import java.util.List;
import java.util.UUID;

Expand All @@ -22,11 +27,13 @@
@ApiStatus.Internal
public final class DefaultPartyService implements PartyService {

private final PartyServiceGrpc.PartyServiceBlockingStub grpc;
private final PartyServiceGrpc.PartyServiceBlockingStub partyGrpc;
private final EventServiceGrpc.EventServiceBlockingStub eventGrpc;

@ApiStatus.Internal
public DefaultPartyService(@NotNull Channel channel) {
this.grpc = PartyServiceGrpc.newBlockingStub(channel);
this.partyGrpc = PartyServiceGrpc.newBlockingStub(channel);
this.eventGrpc = EventServiceGrpc.newBlockingStub(channel);
}

@Override
Expand All @@ -44,7 +51,7 @@ public DefaultPartyService(@NotNull Channel channel) {
private @Nullable Party getParty(@NotNull PartyProto.GetPartyRequest request) {
PartyProto.GetPartyResponse response;
try {
response = this.grpc.getParty(request);
response = this.partyGrpc.getParty(request);
} catch (StatusRuntimeException exception) {
if (exception.getStatus().getCode() == Status.Code.NOT_FOUND) return null;
throw exception;
Expand All @@ -67,11 +74,12 @@ public DefaultPartyService(@NotNull Channel channel) {

private @NotNull ModifyPartyResult emptyParty(@NotNull PartyProto.EmptyPartyRequest request) {
try {
this.grpc.emptyParty(request);
this.partyGrpc.emptyParty(request);
return ModifyPartyResult.SUCCESS;
} catch (StatusRuntimeException exception) {
var error = GrpcErrorHelper.unpackError(exception, PartyProto.EmptyPartyErrorResponse.class);
if (error.getErrorType() == PartyProto.EmptyPartyErrorResponse.ErrorType.NOT_LEADER) return ModifyPartyResult.NOT_LEADER;
if (error.getErrorType() == PartyProto.EmptyPartyErrorResponse.ErrorType.NOT_LEADER)
return ModifyPartyResult.NOT_LEADER;
throw exception;
}
}
Expand All @@ -84,11 +92,12 @@ public DefaultPartyService(@NotNull Channel channel) {
.build();

try {
this.grpc.setOpenParty(request);
this.partyGrpc.setOpenParty(request);
return ModifyPartyResult.SUCCESS;
} catch (StatusRuntimeException exception) {
var error = GrpcErrorHelper.unpackError(exception, PartyProto.SetOpenPartyErrorResponse.class);
if (error.getErrorType() == PartyProto.SetOpenPartyErrorResponse.ErrorType.NOT_LEADER) return ModifyPartyResult.NOT_LEADER;
if (error.getErrorType() == PartyProto.SetOpenPartyErrorResponse.ErrorType.NOT_LEADER)
return ModifyPartyResult.NOT_LEADER;
throw exception;
}
}
Expand All @@ -114,7 +123,7 @@ public DefaultPartyService(@NotNull Channel channel) {
private @NotNull List<PartyInvite> getInvites(@NotNull PartyProto.GetPartyInvitesRequest request) {
PartyProto.GetPartyInvitesResponse response;
try {
response = this.grpc.getPartyInvites(request);
response = this.partyGrpc.getPartyInvites(request);
} catch (StatusRuntimeException exception) {
if (exception.getStatus().getCode() == Status.Code.NOT_FOUND) return List.of();
throw exception;
Expand All @@ -135,7 +144,7 @@ public DefaultPartyService(@NotNull Channel channel) {

PartyProto.InvitePlayerResponse response;
try {
response = this.grpc.invitePlayer(request);
response = this.partyGrpc.invitePlayer(request);
} catch (StatusRuntimeException exception) {
var error = GrpcErrorHelper.unpackError(exception, PartyProto.InvitePlayerErrorResponse.class);

Expand All @@ -160,7 +169,7 @@ public DefaultPartyService(@NotNull Channel channel) {

PartyProto.JoinPartyResponse response;
try {
response = this.grpc.joinParty(request);
response = this.partyGrpc.joinParty(request);
} catch (StatusRuntimeException exception) {
var error = GrpcErrorHelper.unpackError(exception, PartyProto.JoinPartyErrorResponse.class);

Expand All @@ -179,7 +188,7 @@ public DefaultPartyService(@NotNull Channel channel) {
var request = PartyProto.LeavePartyRequest.newBuilder().setPlayerId(leaverId.toString()).build();

try {
this.grpc.leaveParty(request);
this.partyGrpc.leaveParty(request);
return LeavePartyResult.SUCCESS;
} catch (StatusRuntimeException exception) {
var error = GrpcErrorHelper.unpackError(exception, PartyProto.LeavePartyErrorResponse.class);
Expand All @@ -200,7 +209,7 @@ public DefaultPartyService(@NotNull Channel channel) {
.build();

try {
this.grpc.kickPlayer(request);
this.partyGrpc.kickPlayer(request);
return KickPlayerFromPartyResult.SUCCESS;
} catch (StatusRuntimeException exception) {
var error = GrpcErrorHelper.unpackError(exception, PartyProto.KickPlayerErrorResponse.class);
Expand All @@ -223,7 +232,7 @@ public DefaultPartyService(@NotNull Channel channel) {
.build();

try {
this.grpc.setPartyLeader(request);
this.partyGrpc.setPartyLeader(request);
return SetPartyLeaderResult.SUCCESS;
} catch (StatusRuntimeException exception) {
var error = GrpcErrorHelper.unpackError(exception, PartyProto.SetPartyLeaderErrorResponse.class);
Expand All @@ -235,4 +244,53 @@ public DefaultPartyService(@NotNull Channel channel) {
};
}
}

@Override
public @NotNull EventData createEvent(@NotNull String id, @NotNull UUID ownerId, @NotNull String ownerUsername,
@NotNull PlayerSkin skin, @Nullable Instant displayTime, @Nullable Instant startTime) {

var requestBuilder = PartyProto.CreateEventRequest.newBuilder()
.setEventId(id)
.setOwnerId(ownerId.toString())
.setOwnerUsername(ownerUsername)
.setOwnerSkin(skin);
if (displayTime != null) requestBuilder.setDisplayTime(ProtoTimestampConverter.toProto(displayTime));
if (startTime != null) requestBuilder.setStartTime(ProtoTimestampConverter.toProto(startTime));

var request = requestBuilder.build();

PartyProto.CreateEventResponse response = this.eventGrpc.createEvent(request);
return response.getEvent();
}

@Override
public @NotNull EventData updateEvent(@NotNull String id, @Nullable Instant showTime, @Nullable Instant startTime) {
var requestBuilder = PartyProto.UpdateEventRequest.newBuilder().setEventId(id);
if (showTime != null) requestBuilder.setDisplayTime(ProtoTimestampConverter.toProto(showTime));
if (startTime != null) requestBuilder.setStartTime(ProtoTimestampConverter.toProto(startTime));

var request = requestBuilder.build();

PartyProto.UpdateEventResponse response = this.eventGrpc.updateEvent(request);
return response.getEvent();
}

@Override
public DeleteEventResult deleteEvent(@Nullable String id) {
var request = PartyProto.DeleteEventRequest.newBuilder().setEventId(id).build();

try {
this.eventGrpc.deleteEvent(request);
} catch (StatusRuntimeException exception) {
var error = GrpcErrorHelper.unpackError(exception, PartyProto.DeleteEventErrorResponse.class);

return switch (error.getErrorType()) {
case NOT_FOUND -> DeleteEventResult.NOT_FOUND;
case NO_CURRENT_EVENT -> DeleteEventResult.NO_CURRENT_EVENT;
case UNRECOGNIZED -> throw exception;
};
}

return DeleteEventResult.SUCCESS;
}
}
10 changes: 10 additions & 0 deletions src/main/java/dev/emortal/api/service/party/DeleteEventResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.emortal.api.service.party;

public enum DeleteEventResult {
SUCCESS,
NOT_FOUND,
/**
* Sent if the ID was not provided and there is no current event
*/
NO_CURRENT_EVENT
}
15 changes: 15 additions & 0 deletions src/main/java/dev/emortal/api/service/party/PartyService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package dev.emortal.api.service.party;

import dev.emortal.api.model.common.Pageable;
import dev.emortal.api.model.common.PlayerSkin;
import dev.emortal.api.model.party.EventData;
import dev.emortal.api.model.party.Party;
import dev.emortal.api.model.party.PartyInvite;
import org.jetbrains.annotations.Blocking;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Instant;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -45,4 +48,16 @@ public interface PartyService {
@NotNull KickPlayerFromPartyResult kickPlayer(@NotNull UUID kickerId, @NotNull String kickerName, @NotNull UUID targetId);

@NotNull SetPartyLeaderResult setPartyLeader(@NotNull UUID requesterId, @NotNull String requesterName, @NotNull UUID newLeaderId);

@NotNull EventData createEvent(@NotNull String id, @NotNull UUID ownerId, @NotNull String ownerUsername, @NotNull PlayerSkin skin,
@Nullable Instant showTime, @Nullable Instant startTime);

@NotNull EventData updateEvent(@NotNull String id, @Nullable Instant showTime, @Nullable Instant startTime);

/**
* Deletes an event by its id or if the id is null, deletes the current ongoing event.
*
* @param id the id of the event.
*/
DeleteEventResult deleteEvent(@Nullable String id);
}

0 comments on commit 8432b63

Please sign in to comment.