Skip to content

Commit

Permalink
Merge pull request #137 from averak/feature/136-friend-get-list-v1
Browse files Browse the repository at this point in the history
Friend.ListV1を実装
  • Loading branch information
averak authored Jan 31, 2024
2 parents e41727e + d482be8 commit 7b61b89
Show file tree
Hide file tree
Showing 33 changed files with 5,536 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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.IFriendHandler
import net.averak.gsync.usecase.FriendUsecase
import org.springframework.stereotype.Service

@Service
class FriendHandler(
private val friendUsecase: FriendUsecase,
) : IFriendHandler {

override fun listV1(request: Request<FriendListV1.Request>): FriendListV1.Response {
val result = friendUsecase.list(request.gctx, request.mustPrincipal().playerID)
return FriendListV1.Response.newBuilder()
.addAllFriends(result.friends.map { FriendConverter.toPb(it) })
.addAllSentFriendRequests(result.sentFriendRequests.map { FriendConverter.toPb(it) })
.addAllReceivedFriendRequests(result.receivedFriendRequests.map { FriendConverter.toPb(it) })
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.averak.gsync.adapter.handler.player_api.pbconv

import net.averak.gsync.domain.model.Friend
import net.averak.gsync.domain.model.FriendRequest

class FriendConverter {

companion object {

@JvmStatic
fun toPb(friend: Friend): net.averak.gsync.schema.protobuf.resource.friend.Friend {
return net.averak.gsync.schema.protobuf.resource.friend.Friend.newBuilder()
.setPlayerId(friend.friendPlayerID.toString())
.setBecomeFriendAt(LocalDateTimeConverter.toPb(friend.becomeFriendAt))
.setProfile(PlayerProfileConverter.toPb(friend.profile)).build()
}

@JvmStatic
fun toPb(friendRequest: FriendRequest): net.averak.gsync.schema.protobuf.resource.friend.FriendRequest {
return net.averak.gsync.schema.protobuf.resource.friend.FriendRequest.newBuilder()
.setSenderPlayerId(friendRequest.playerID.toString())
.setReceiverPlayerId(friendRequest.receiverPlayerID.toString())
.setSentAt(LocalDateTimeConverter.toPb(friendRequest.sentAt)).build()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.averak.gsync.adapter.handler.player_api.pbconv

import net.averak.gsync.domain.model.PlayerProfile

class PlayerProfileConverter {

companion object {

@JvmStatic
fun toPb(profile: PlayerProfile): net.averak.gsync.schema.protobuf.resource.player.PlayerProfile {
return net.averak.gsync.schema.protobuf.resource.player.PlayerProfile.newBuilder()
.setNickname(profile.nickname)
.setIconId(profile.iconID)
.build()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.averak.gsync.adapter.handler.player_api

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.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.fixture.builder.player.PlayerDataBuilder

import java.time.LocalDateTime

class FriendHandler_ListV1_IT extends AbstractDatabaseSpec {

def "正常系"() {
given:
final gctx = Faker.fake(GameContext)
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(),
new PlayerDataBuilder(Faker.uuidv5("p5")).build(),
)
playerUp.setup(
gctx,
new PlayerDataBuilder(Faker.uuidv5("p1"))
.friendships(
new Friendship(Faker.uuidv5("p1"), Faker.uuidv5("p2"), LocalDateTime.now()),
new Friendship(Faker.uuidv5("p1"), Faker.uuidv5("p3"), LocalDateTime.now()),
)
.friendRequests(
new FriendRequest(Faker.uuidv5("p1"), Faker.uuidv5("p4"), LocalDateTime.now()),
)
.build(),
new PlayerDataBuilder(Faker.uuidv5("p5"))
.friendRequests(
new FriendRequest(Faker.uuidv5("p5"), Faker.uuidv5("p1"), LocalDateTime.now()),
)
.build(),
)

when:
final response = grpcTester.invoke(
grpcTester.friend.&listV1,
FriendListV1.Request.newBuilder().build(),
) {
it.session(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
it.spoofingMasterVersion(gctx.masterVersion)
}

then:
with(response.message.friendsList) {
it*.playerId == [Faker.uuidv5("p2").toString(), Faker.uuidv5("p3").toString()]
}
with(response.message.sentFriendRequestsList) {
it*.receiverPlayerId == [Faker.uuidv5("p4").toString()]
}
with(response.message.receivedFriendRequestsList) {
it*.senderPlayerId == [Faker.uuidv5("p5").toString()]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import net.averak.gsync.schema.protobuf.player_api.PlayerStorageSetV1
import net.averak.gsync.schema.protobuf.resource.player_storage.Criteria
import net.averak.gsync.testkit.AbstractDatabaseSpec
import net.averak.gsync.testkit.Faker
import net.averak.gsync.testkit.fixture.builder.player.PlayerBuilder
import net.averak.gsync.testkit.fixture.builder.player.PlayerDataBuilder
import net.averak.gsync.testkit.fixture.builder.player.PlayerStorageBuilder

class PlayerStorageHandler_SearchV1_IT extends AbstractDatabaseSpec {
Expand All @@ -19,14 +19,16 @@ class PlayerStorageHandler_SearchV1_IT extends AbstractDatabaseSpec {
final gctx = Faker.fake(GameContext)
playerUp.setup(
gctx,
new PlayerBuilder(Faker.uuidv5("p1")).build(),
new PlayerStorageBuilder(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
.entries(
new PlayerStorageEntry("group1#key1", "value1".bytes),
new PlayerStorageEntry("group1#key2", "value2".bytes),
new PlayerStorageEntry("group2#key1", "value3".bytes),
)
.build(),
new PlayerDataBuilder(Faker.uuidv5("p1"))
.playerStorage(
new PlayerStorageBuilder(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
.entries(
new PlayerStorageEntry("group1#key1", "value1".bytes),
new PlayerStorageEntry("group1#key2", "value2".bytes),
new PlayerStorageEntry("group2#key1", "value3".bytes),
)
.build(),
).build()
)

when:
Expand Down Expand Up @@ -56,11 +58,13 @@ class PlayerStorageHandler_SetV1_IT extends AbstractDatabaseSpec {
final gctx = Faker.fake(GameContext)
playerUp.setup(
gctx,
new PlayerBuilder(Faker.uuidv5("p1")).build(),
new PlayerStorageBuilder(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
.revision(Faker.uuidv5("r1"))
.entries()
.build(),
new PlayerDataBuilder(Faker.uuidv5("p1"))
.playerStorage(
new PlayerStorageBuilder(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
.revision(Faker.uuidv5("r1"))
.entries()
.build(),
).build()
)

final entry = Faker.fake(PlayerStorageEntry)
Expand Down Expand Up @@ -100,15 +104,17 @@ class PlayerStorageHandler_ClearV1_IT extends AbstractDatabaseSpec {
final gctx = Faker.fake(GameContext)
playerUp.setup(
gctx,
new PlayerBuilder(Faker.uuidv5("p1")).build(),
new PlayerStorageBuilder(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
.revision(Faker.uuidv5("current revision"))
.entries(
new PlayerStorageEntry("group1#key1", "value1".bytes),
new PlayerStorageEntry("group1#key2", "value2".bytes),
new PlayerStorageEntry("group2#key1", "value3".bytes),
)
.build(),
new PlayerDataBuilder(Faker.uuidv5("p1"))
.playerStorage(
new PlayerStorageBuilder(Faker.uuidv5("p1"), Faker.uuidv5("g1"))
.revision(Faker.uuidv5("current revision"))
.entries(
new PlayerStorageEntry("group1#key1", "value1".bytes),
new PlayerStorageEntry("group1#key2", "value2".bytes),
new PlayerStorageEntry("group2#key1", "value3".bytes),
)
.build(),
).build()
)

when:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.averak.gsync.usecase

import net.averak.gsync.core.game_context.GameContext
import net.averak.gsync.domain.model.Friend
import net.averak.gsync.domain.model.FriendRequest
import net.averak.gsync.domain.repository.IFriendRepository
import net.averak.gsync.usecase.transaction.ITransaction
import org.springframework.stereotype.Service
import java.util.*

@Service
class FriendUsecase(
private val transaction: ITransaction,
private val friendRepository: IFriendRepository,
) {

fun list(gctx: GameContext, playerID: UUID): ListResult {
return transaction.roTx {
val friends = friendRepository.getFriends(gctx, playerID)
val sentFriendRequests = friendRepository.getSentFriendRequests(gctx, playerID)
val receivedFriendRequests = friendRepository.getReceivedFriendRequests(gctx, playerID)
return@roTx ListResult(
friends = friends,
sentFriendRequests = sentFriendRequests,
receivedFriendRequests = receivedFriendRequests,
)
}
}

data class ListResult(
val friends: List<Friend>,
val sentFriendRequests: List<FriendRequest>,
val receivedFriendRequests: List<FriendRequest>,
)
}
Loading

0 comments on commit 7b61b89

Please sign in to comment.