Skip to content

Commit

Permalink
Merge pull request #139 from averak/feature/138-friend-send-request-v1
Browse files Browse the repository at this point in the history
Friend.SendRequestV1を実装
  • Loading branch information
averak authored Jan 31, 2024
2 parents 7b61b89 + fc2ce22 commit 5b0bf68
Show file tree
Hide file tree
Showing 24 changed files with 1,551 additions and 176 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package net.averak.gsync.adapter.dao.dto.extend;

import java.time.LocalDateTime;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.jetbrains.annotations.NotNull;

import net.averak.gsync.adapter.dao.dto.base.PlayerDto;
import net.averak.gsync.adapter.dao.dto.base.PlayerLoginDto;
import net.averak.gsync.adapter.dao.dto.base.PlayerProfileDto;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.LocalDateTime;

@SuppressWarnings({"NotNullFieldNotInitialized", "java:S2637"})
public class PlayerILDto extends PlayerDto {
Expand All @@ -20,9 +18,9 @@ public class PlayerILDto extends PlayerDto {
@Nullable
PlayerLoginDto playerLogin;

public PlayerILDto(@NotNull String playerId, @NotNull String friendId, @NotNull Boolean isBanned,
@NotNull LocalDateTime createdAt, @NotNull LocalDateTime updatedAt) {
super(playerId, friendId, isBanned, createdAt, updatedAt);
public PlayerILDto(@NotNull String playerId, @NotNull Boolean isBanned, @NotNull LocalDateTime createdAt,
@NotNull LocalDateTime updatedAt) {
super(playerId, isBanned, createdAt, updatedAt);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package net.averak.gsync.adapter.handler.player_api
import net.averak.gsync.adapter.handler.player_api.pbconv.FriendConverter
import net.averak.gsync.infrastructure.grpc.player_api.Request
import net.averak.gsync.schema.protobuf.player_api.FriendListV1
import net.averak.gsync.schema.protobuf.player_api.FriendSendRequestV1
import net.averak.gsync.schema.protobuf.player_api.IFriendHandler
import net.averak.gsync.usecase.FriendUsecase
import org.springframework.stereotype.Service
import java.util.*

@Service
class FriendHandler(
Expand All @@ -20,4 +22,9 @@ class FriendHandler(
.addAllReceivedFriendRequests(result.receivedFriendRequests.map { FriendConverter.toPb(it) })
.build()
}

override fun sendRequestV1(request: Request<FriendSendRequestV1.Request>): FriendSendRequestV1.Response {
friendUsecase.sendRequest(request.gctx, request.mustPrincipal().playerID, UUID.fromString(request.message.playerId))
return FriendSendRequestV1.Response.newBuilder().build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.google.protobuf.GeneratedMessageV3
import com.google.protobuf.util.JsonFormat
import io.grpc.*
import net.averak.gsync.core.logger.Logger
import net.averak.gsync.infrastructure.grpc.player_api.metadata.OutgoingTrailerKey
import net.averak.gsync.infrastructure.grpc.player_api.metadata.RequestScope
import net.averak.gsync.infrastructure.json.JsonUtils
import org.springframework.core.annotation.Order
Expand Down Expand Up @@ -35,7 +34,7 @@ class AccessLogInterceptor(
next.startCall(
object : ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {

lateinit var end: LocalDateTime
var end: LocalDateTime? = null

@Suppress("kotlin:S108")
override fun sendMessage(message: RespT) {
Expand Down Expand Up @@ -63,14 +62,6 @@ class AccessLogInterceptor(
logger.info("access log", logMessage)
super.sendMessage(message)
}

override fun close(status: Status, trailers: Metadata) {
trailers.put(
Metadata.Key.of(OutgoingTrailerKey.RESPOND_TIMESTAMP.key, Metadata.ASCII_STRING_MARSHALLER),
end.toString(),
)
super.close(status, trailers)
}
},
headers,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ open class PlayerRepository(
// クエリ数が多いので、プロフィールとログイン履歴は別モデルに切り出した方が良いかもしれない
val dto = PlayerDto(
player.id.toString(),
player.friendID.toString(),
player.isBanned,
)
playerMapper.syncOriginal(dto)
Expand Down Expand Up @@ -69,7 +68,6 @@ open class PlayerRepository(
fun convertDtoToModel(dto: PlayerILDto): Player {
return Player(
id = UUID.fromString(dto.playerId),
friendID = UUID.fromString(dto.friendId),
isBanned = dto.isBanned,
profile = convertDtoToModel(dto.playerProfile),
login = dto.playerLogin?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package net.averak.gsync.adapter.handler.player_api

import io.grpc.Status
import net.averak.gsync.core.game_context.GameContext
import net.averak.gsync.domain.model.FriendRequest
import net.averak.gsync.domain.model.Friendship
import net.averak.gsync.schema.protobuf.player_api.FriendListV1
import net.averak.gsync.schema.protobuf.player_api.FriendSendRequestV1
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.api.grpc.Response
import net.averak.gsync.testkit.fixture.builder.master.FriendSettingBuilder
import net.averak.gsync.testkit.fixture.builder.master.MasterDataBuilder
import net.averak.gsync.testkit.fixture.builder.player.PlayerDataBuilder

import java.time.LocalDateTime
Expand Down Expand Up @@ -62,3 +67,91 @@ class FriendHandler_ListV1_IT extends AbstractDatabaseSpec {
}
}
}

class FriendHandler_SendRequestV1_IT extends AbstractDatabaseSpec {

def "フレンドリクエストを送信する"() {
given:
final gctx = Faker.fake(GameContext)
masterUp.setup(
gctx,
new MasterDataBuilder()
.friendSetting(new FriendSettingBuilder().maxFriendRequestCount(2).build())
.build(),
)
playerUp.setup(
gctx,
new PlayerDataBuilder(Faker.uuidv5("p1")).build(),
new PlayerDataBuilder(Faker.uuidv5("p2")).build(),
new PlayerDataBuilder(Faker.uuidv5("p3")).build(),
new PlayerDataBuilder(Faker.uuidv5("p4")).build(),
)
testcase.given()

when:
final response = grpcTester.invoke(
grpcTester.friend.&sendRequestV1,
testcase.when.request,
) {
it.session(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
it.spoofingMasterVersion(gctx.masterVersion)
}

then:
testcase.then(response)

where:
testcase << [
[
name : "フレンドリクエスト送信上限に達していない場合、送信できる",
given: () -> {
playerUp.setup(
new PlayerDataBuilder(Faker.uuidv5("p1"))
.friendRequests(
new FriendRequest(Faker.uuidv5("p1"), Faker.uuidv5("p2"), LocalDateTime.now()),
)
.build(),
)
},
when : [
request: FriendSendRequestV1.Request.newBuilder()
.setPlayerId(Faker.uuidv5("p3").toString())
.build(),
],
then : (Response<FriendSendRequestV1.Response> v) -> {
assert v.status == Status.OK
with(sql.rows("SELECT * FROM gsync_player_friend_request")) {
assert it.size() == 2
assert it*.player_id.sort() == [Faker.uuidv5("p1").toString(), Faker.uuidv5("p1").toString()].sort()
assert it*.receiver_player_id.sort() == [Faker.uuidv5("p2").toString(), Faker.uuidv5("p3").toString()].sort()
}
return true
},
],
[
name : "フレンドリクエスト送信上限に達している場合、送信できない",
given: () -> {
playerUp.setup(
new PlayerDataBuilder(Faker.uuidv5("p1"))
.friendRequests(
new FriendRequest(Faker.uuidv5("p1"), Faker.uuidv5("p2"), LocalDateTime.now()),
new FriendRequest(Faker.uuidv5("p1"), Faker.uuidv5("p3"), LocalDateTime.now()),
)
.build(),
)
},
when : [
request: FriendSendRequestV1.Request.newBuilder()
.setPlayerId(Faker.uuidv5("p4").toString())
.build(),
],
then : (Response<FriendSendRequestV1.Response> v) -> {
assert v.status.code == Status.ABORTED.code
assert v.status.description == "player sent friend requests is over max count."
return true
},
],
]
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.util.*

data class Player(
val id: UUID,
val friendID: UUID,
val isBanned: Boolean,
val profile: PlayerProfile,
var login: PlayerLogin?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.averak.gsync.infrastructure.grpc.player_api

import com.google.protobuf.AbstractMessage
import io.grpc.Status
import io.grpc.stub.StreamObserver
import net.averak.gsync.infrastructure.grpc.player_api.metadata.RequestScope
import org.springframework.stereotype.Component
Expand Down Expand Up @@ -32,10 +33,14 @@ class HandlerWrapper(
try {
val response = method(request)
responseObserver.onNext(response)
} catch (ex: Exception) {
responseObserver.onError(ex)
} finally {
responseObserver.onCompleted()
} catch (ex: Exception) {
responseObserver.onError(
Status.ABORTED
.withDescription(ex.message)
.withCause(ex)
.asRuntimeException(),
)
}
}
}
Loading

0 comments on commit 5b0bf68

Please sign in to comment.