summaryrefslogtreecommitdiff
path: root/sw-server
diff options
context:
space:
mode:
Diffstat (limited to 'sw-server')
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt9
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt24
2 files changed, 10 insertions, 23 deletions
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt
index ff06f549..5036266d 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/LobbyRepository.kt
@@ -5,19 +5,20 @@ import org.luxons.sevenwonders.server.lobby.Lobby
import org.luxons.sevenwonders.server.lobby.Player
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Repository
-import java.util.HashMap
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.atomic.AtomicLong
@Repository
class LobbyRepository @Autowired constructor() {
- private val lobbies = HashMap<Long, Lobby>()
+ private val lobbies = ConcurrentHashMap<Long, Lobby>()
- private var lastGameId: Long = 0
+ private var lastGameId: AtomicLong = AtomicLong(0)
fun list(): Collection<Lobby> = lobbies.values
fun create(gameName: String, owner: Player): Lobby {
- val id = lastGameId++
+ val id = lastGameId.getAndIncrement()
val lobby = Lobby(id, gameName, owner, GameDefinition.load())
lobbies[id] = lobby
return lobby
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt
index 00badf75..b31d2cee 100644
--- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt
+++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt
@@ -3,33 +3,19 @@ package org.luxons.sevenwonders.server.repositories
import org.luxons.sevenwonders.server.ApiMisuseException
import org.luxons.sevenwonders.server.lobby.Player
import org.springframework.stereotype.Repository
-import java.util.HashMap
+import java.util.concurrent.ConcurrentHashMap
@Repository
class PlayerRepository {
- private val players = HashMap<String, Player>()
+ private val players = ConcurrentHashMap<String, Player>()
operator fun contains(username: String): Boolean = players.containsKey(username)
fun createOrUpdate(username: String, displayName: String): Player {
- return if (players.containsKey(username)) {
- update(username, displayName)
- } else {
- create(username, displayName)
- }
- }
-
- private fun create(username: String, displayName: String): Player {
- val player = Player(username, displayName)
- players[username] = player
- return player
- }
-
- private fun update(username: String, displayName: String): Player {
- val player = find(username)
- player.displayName = displayName
- return player
+ val p = players.computeIfAbsent(username) { Player(username, displayName) }
+ p.displayName = displayName
+ return p
}
fun find(username: String): Player = players[username] ?: throw PlayerNotFoundException(username)
bgstack15