Skip to content

Commit

Permalink
fix: update ws event message
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 8, 2024
1 parent f5b4402 commit cd6554c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hero.alignlab.batch.grouprank.job

import com.hero.alignlab.common.extension.coExecuteOrNull
import com.hero.alignlab.common.extension.coExecute
import com.hero.alignlab.config.database.TransactionTemplates
import com.hero.alignlab.domain.group.application.GroupUserScoreService
import com.hero.alignlab.domain.group.application.GroupUserService
Expand Down Expand Up @@ -34,19 +34,21 @@ class GroupRankRefreshJob(
groupUserScoreService.findAllByUids(uids)
.groupBy { it.groupId }
.forEach { (key, value) ->
val groupUserScore = value.mapNotNull {
val groupUserScores = value.mapNotNull {
val score = counts[it.uid]?.count?.toInt() ?: return@mapNotNull null

it.apply {
it.score = score
}
}

txTemplates.writer.coExecuteOrNull {
groupUserScoreService.saveAllSync(groupUserScore)
val updatedGroupUserScores = txTemplates.writer.coExecute {
groupUserScoreService.saveAllSync(groupUserScores)
}

wsHandler.launchSendEvent(key)
updatedGroupUserScores.forEach { groupUserScore ->
wsHandler.launchSendEvent(groupUserScore.uid, groupUserScore.groupId)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class PoseSnapshotListener(

val groupUserScore = groupUserScoreService.createOrUpdateGroupUserScore(this, score)

wsHandler.launchSendEvent(groupUserScore.groupId)
wsHandler.launchSendEvent(groupUserScore.uid, groupUserScore.groupId)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ReactiveGroupUserWebSocketHandler(
groupUserByMap.forEach { (groupId, sessionByUid) ->
sessionByUid[user.uid] ?: return@forEach

launchSendEvent(groupId, sessionByUid)
launchSendEvent(user.uid, groupId, sessionByUid)
}

session.receive()
Expand Down Expand Up @@ -97,36 +97,38 @@ class ReactiveGroupUserWebSocketHandler(
}

false -> {
launchSendEvent(groupId, uidBySession)
launchSendEvent(uid, groupId, uidBySession)
}
}
}
}
}

fun launchSendEvent(groupId: Long) {
fun launchSendEvent(uid: Long, groupId: Long) {
groupUserByMap[groupId]?.let { groupUsers ->
launchSendEvent(groupId, groupUsers)
launchSendEvent(uid, groupId, groupUsers)
}
}

/** 발송되는 순서가 중요하지 않다. */
private fun launchSendEvent(
uid: Long,
groupId: Long,
sessionByUid: ConcurrentMap<Long, WebSocketSession>
) {
CoroutineScope(Dispatchers.IO + Job()).launch {
sendUpdatedGroupStatus(groupId, sessionByUid)
sendUpdatedGroupStatus(uid, groupId, sessionByUid)
}
}

private suspend fun sendUpdatedGroupStatus(
uid: Long,
groupId: Long,
sessionByUid: MutableMap<Long, WebSocketSession>
) {
val eventMessage = sessionByUid.keys
.toList()
.let { uids -> groupUserWsFacade.generateEventMessage(groupId, uids) }
.let { uids -> groupUserWsFacade.generateEventMessage(uid, groupId, uids) }

sessionByUid.forEach { (_, session) ->
session
Expand Down
37 changes: 22 additions & 15 deletions src/main/kotlin/com/hero/alignlab/ws/model/GroupUserEventMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import java.util.concurrent.atomic.AtomicInteger
data class GroupUserEventMessage(
val timestamp: LocalDateTime = LocalDateTime.now(),
val groupId: Long,
/** 본인 정보, 접속 종료시 본인 정보는 미제공 */
val groupUser: ConcurrentUser?,
/** 그룹 유저 리스 */
val groupUsers: List<ConcurrentUser>
) {
data class ConcurrentUser(
Expand All @@ -22,30 +25,34 @@ data class GroupUserEventMessage(

companion object {
fun of(
uid: Long,
groupId: Long,
userInfoByUid: Map<Long, UserInfo>,
groupUserById: Map<Long, GroupUser>,
scoreByUid: Map<Long, GroupUserScore>
): GroupUserEventMessage {
val rank = AtomicInteger(1)

val groupUsers = userInfoByUid.mapNotNull { (uid, info) ->
val groupUser = groupUserById[uid] ?: return@mapNotNull null

ConcurrentUser(
groupUserId = groupUser.id,
uid = uid,
nickname = info.nickname,
rank = -1,
score = scoreByUid[uid]?.score ?: 0,
)
}.sortedBy { groupScore ->
groupScore.score
}.map { groupScore ->
groupScore.copy(rank = rank.getAndIncrement())
}

return GroupUserEventMessage(
groupId = groupId,
groupUsers = userInfoByUid.mapNotNull { (uid, info) ->
val groupUser = groupUserById[uid] ?: return@mapNotNull null

ConcurrentUser(
groupUserId = groupUser.id,
uid = uid,
nickname = info.nickname,
rank = -1,
score = scoreByUid[uid]?.score ?: 0,
)
}.sortedBy { groupScore ->
groupScore.score
}.map { groupScore ->
groupScore.copy(rank = rank.getAndIncrement())
}.take(5)
groupUser = groupUsers.firstOrNull { users -> users.uid == uid },
groupUsers = groupUsers.take(5)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class GroupUserWsFacade(
private val groupUserService: GroupUserService,
private val groupUserScoreService: GroupUserScoreService,
) {
suspend fun generateEventMessage(groupId: Long, uids: List<Long>): GroupUserEventMessage {
suspend fun generateEventMessage(uid: Long, groupId: Long, uids: List<Long>): GroupUserEventMessage {
return parZip(
{ userInfoService.findAllByIds(uids) },
{ groupUserService.findAllByGroupIdAndUids(groupId, uids) },
{ groupUserScoreService.findAllByGroupIdAndUids(groupId, uids) }
) { userInfoByUid, groupUsers, groupUserScores ->
GroupUserEventMessage.of(
uid = uid,
groupId = groupId,
userInfoByUid = userInfoByUid.associateBy { userInfo -> userInfo.id },
groupUserById = groupUsers.associateBy { groupUser -> groupUser.uid },
Expand Down

0 comments on commit cd6554c

Please sign in to comment.