Skip to content

Commit

Permalink
feature: find game sort by times played
Browse files Browse the repository at this point in the history
  • Loading branch information
kuoche1712003 committed Jul 27, 2024
1 parent 333cbc5 commit 240ab47
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ buildNumber.properties
.classpath

# End of https://www.toptal.com/developers/gitignore/api/intellij,maven

.env
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class StartGameUseCase(
val startGameRequest = StartGameRequest(roomId!!.value, players.map { it.toGamePlayer() })
val startGameResponse = gameService.startGame(gameServerHost, jwtToken, startGameRequest)

return StartedGameEvent(GAME_STARTED, Data(startGameResponse.url, roomId!!))
return StartedGameEvent(GAME_STARTED, Data(startGameResponse.url, roomId!!, game.id!!))
}

private fun Room.Player.toGamePlayer(): StartGameRequest.GamePlayer =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tw.waterballsa.gaas.events

import tw.waterballsa.gaas.domain.GameRegistration
import tw.waterballsa.gaas.domain.Room
import tw.waterballsa.gaas.events.enums.EventMessageType

Expand All @@ -10,6 +11,7 @@ class StartedGameEvent(
data class Data(
val gameUrl: String,
val roomId: Room.Id,
val gameId: GameRegistration.Id,
)

override fun getEventData(): Any = data
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tw.waterballsa.gaas.spring.eventbus

import org.springframework.stereotype.Component
import tw.waterballsa.gaas.application.eventbus.EventBus
import tw.waterballsa.gaas.events.DomainEvent
import kotlin.reflect.safeCast

@Component
class DispatcherEventBus(
private val listeners: List<EventListener<*>>,
) : EventBus {

override fun broadcast(events: Collection<DomainEvent>) {
listeners.forEach { listener ->
events.mapNotNull { listener.eventType.safeCast(it) }
.takeIf { it.isNotEmpty() }
?.run { (listener as EventListener<DomainEvent>).onEvents(this) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tw.waterballsa.gaas.spring.eventbus

import tw.waterballsa.gaas.events.DomainEvent
import kotlin.reflect.KClass

interface EventListener<T: DomainEvent> {
val eventType: KClass<T>
fun onEvents(events: List<T>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tw.waterballsa.gaas.spring.eventbus

import com.corundumstudio.socketio.SocketIOServer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import tw.waterballsa.gaas.events.RoomEvent
import kotlin.reflect.KClass

@Component
class RoomEventListener(
override val eventType: KClass<RoomEvent>,
val socketIOServer: SocketIOServer,
): EventListener<RoomEvent> {

@Autowired
constructor(socketIOServer: SocketIOServer): this(RoomEvent::class, socketIOServer)

override fun onEvents(events: List<RoomEvent>) {
events
.forEach {
socketIOServer.getRoomOperations("ROOM_${it.getRoomId().value}")
.sendEvent(it.type.eventName, it.getEventData())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tw.waterballsa.gaas.spring.eventbus

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import tw.waterballsa.gaas.events.StartedGameEvent
import tw.waterballsa.gaas.spring.repositories.dao.GameRegistrationDAO
import kotlin.reflect.KClass

@Component
class StartedGameEventListener(
override val eventType: KClass<StartedGameEvent>,
private val gameRegistrationDAO: GameRegistrationDAO,
) : EventListener<StartedGameEvent> {

@Autowired
constructor(gameRegistrationDAO: GameRegistrationDAO): this(StartedGameEvent::class, gameRegistrationDAO)

override fun onEvents(events: List<StartedGameEvent>) {
events.forEach {
gameRegistrationDAO.incrementTimesPlayedById(it.data.gameId.value)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tw.waterballsa.gaas.spring.repositories

import org.springframework.data.domain.Sort
import org.springframework.data.domain.Sort.Order
import org.springframework.data.mongodb.core.query.Update
import org.springframework.stereotype.Component
import tw.waterballsa.gaas.application.repositories.GameRegistrationRepository
import tw.waterballsa.gaas.domain.GameRegistration
Expand Down Expand Up @@ -43,7 +44,9 @@ class SpringGameRegistrationRepository(
gameRegistrationDAO.save(gameRegistration.toData()).toDomain()

enum class SortBy(val value: String, val orders: List<Order>) {
CREATED_ON("createdOn", listOf(Order.desc("createdOn"), Order.desc("_id")));
CREATED_ON("createdOn", listOf(Order.desc("createdOn"), Order.desc("_id"))),
TIMES_PLAYED("timesPlayed", listOf(Order.desc("timesPlayed"), Order.desc("_id")))
;

companion object {
private val map = SortBy.values().associateBy { it.value }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package tw.waterballsa.gaas.spring.repositories.dao

import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.data.mongodb.repository.Query
import org.springframework.data.mongodb.repository.Update
import org.springframework.stereotype.Repository
import tw.waterballsa.gaas.spring.repositories.data.GameRegistrationData

@Repository
interface GameRegistrationDAO : MongoRepository<GameRegistrationData, String> {
fun findByUniqueName(uniqueName: String): GameRegistrationData?
fun existsByUniqueName(uniqueName: String): Boolean

@Query("{ '_id' : ?0 }")
@Update("{ '\$inc' : { 'timesPlayed' : ?1 } }")
fun incrementTimesPlayedById(id: String, increment: Long = 1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class GameRegistrationData(
var frontEndUrl: String?,
var backEndUrl: String?,
val createdOn: Instant?,
var timesPlayed: Long? = null,
) {
@DBRef
var logs: MutableList<GameDevelopmentLog> = mutableListOf()
Expand Down

0 comments on commit 240ab47

Please sign in to comment.