-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: find game sort by times played
- Loading branch information
1 parent
333cbc5
commit 240ab47
Showing
11 changed files
with
93 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,3 +86,5 @@ buildNumber.properties | |
.classpath | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/intellij,maven | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/DispatcherEventBus.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/EventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>) | ||
} |
25 changes: 25 additions & 0 deletions
25
spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/RoomEventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/StartedGameEventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
spring/src/main/kotlin/tw/waterballsa/gaas/spring/eventbus/WebSocketEventBus.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
spring/src/main/kotlin/tw/waterballsa/gaas/spring/repositories/dao/GameRegistrationDAO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters