-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: extract DataStorage and Game
- Loading branch information
Showing
5 changed files
with
63 additions
and
36 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
web/src/main/kotlin/ru/org/codingteam/hyperspace/server/DataStorage.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 ru.org.codingteam.hyperspace.server | ||
|
||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
|
||
class DataStorage { | ||
private val mutex = Mutex() | ||
private val storage = mutableMapOf<Int, Game>() | ||
private var lastId = 0 | ||
|
||
suspend fun getAllGames(): List<Game> = mutex.withLock { storage.values.toList() } | ||
suspend fun getGame(id: Int): Game? = mutex.withLock { storage[id] } | ||
|
||
suspend fun createGame(fieldSize: FieldSize): Int = mutex.withLock { | ||
val game = Game(fieldSize) | ||
val nextId = ++lastId | ||
storage[nextId] = game | ||
nextId | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
web/src/main/kotlin/ru/org/codingteam/hyperspace/server/Game.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,8 @@ | ||
package ru.org.codingteam.hyperspace.server | ||
|
||
enum class GameStatus { | ||
Waiting, Started, Finished | ||
} | ||
|
||
data class FieldSize(val width: Int, val height: Int) | ||
data class Game(val size: FieldSize, val playerCount: Int = 0, val status: GameStatus = GameStatus.Waiting) |
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
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