Skip to content

Commit

Permalink
add persistence layer
Browse files Browse the repository at this point in the history
  • Loading branch information
hex-agon committed Jul 22, 2023
1 parent 57f7d97 commit 3ec7b3e
Show file tree
Hide file tree
Showing 26 changed files with 443 additions and 1,634 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.apache.logging.log4j.LogManager
import work.fking.pangya.discovery.DiscoveryClient
import work.fking.pangya.discovery.HeartbeatPublisher
import work.fking.pangya.discovery.ServerType.GAME
import work.fking.pangya.game.ServerChannel.Restriction
import work.fking.pangya.game.persistence.PersistenceContext
import java.nio.file.Files
import java.nio.file.Path

Expand All @@ -27,7 +27,11 @@ object Bootstrap {
val discoveryClient = DiscoveryClient(redisClient)
val sessionClient = SessionClient(redisClient)

val server = GameServer(serverConfig, sessionClient)
val server = GameServer(
serverConfig = serverConfig,
persistenceContext = PersistenceContext(),
sessionClient = sessionClient
)
HeartbeatPublisher(discoveryClient, GAME, serverConfig) { server.players.count() }.start()
server.start()
}
Expand Down
49 changes: 37 additions & 12 deletions game-server/src/main/kotlin/work/fking/pangya/game/GameServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,67 @@ import io.netty.channel.Channel
import io.netty.channel.ChannelOption
import org.slf4j.LoggerFactory
import work.fking.pangya.game.net.ServerChannelInitializer
import work.fking.pangya.game.persistence.PersistenceContext
import work.fking.pangya.game.player.CaddieRoster
import work.fking.pangya.game.player.CharacterRoster
import work.fking.pangya.game.player.Equipment
import work.fking.pangya.game.player.Inventory
import work.fking.pangya.game.player.Player
import work.fking.pangya.game.player.PlayerAchievements
import work.fking.pangya.game.player.PlayerGroup
import work.fking.pangya.game.player.PlayerStatistics
import work.fking.pangya.game.player.PlayerWallet
import work.fking.pangya.networking.selectBestEventLoopAvailable
import work.fking.pangya.networking.selectBestServerChannelAvailable
import java.net.InetAddress
import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.Future
import java.util.concurrent.atomic.AtomicInteger

private val LOGGER = LoggerFactory.getLogger(GameServer::class.java)

class GameServer(
private val serverConfig: GameServerConfig,
val persistenceContext: PersistenceContext,
val sessionClient: SessionClient,
val serverChannels: List<ServerChannel> = serverConfig.serverChannels
) {
private val executorService = Executors.newVirtualThreadPerTaskExecutor()
private val connectionIdSequence = AtomicInteger()
val players = PlayerGroup()

fun serverChannelById(id: Int): ServerChannel? {
return serverChannels.firstOrNull { it.id == id }
}
fun serverChannelById(id: Int): ServerChannel? = serverChannels.firstOrNull { it.id == id }

fun submitTask(runnable: Runnable) {
executorService.execute(runnable)
}
fun <R> submitTask(callable: Callable<R>): Future<R> = executorService.submit(callable)

fun runTask(runnable: Runnable): Future<*> = executorService.submit(runnable)

fun registerPlayer(channel: Channel, uid: Int, username: String, nickname: String): Player {
fun registerPlayer(
channel: Channel,
sessionInfo: SessionClient.SessionInfo,
playerWallet: PlayerWallet,
characterRoster: CharacterRoster,
caddieRoster: CaddieRoster,
inventory: Inventory,
equipment: Equipment,
statistics: PlayerStatistics,
achievements: PlayerAchievements
): Player {
val player = Player(
channel = channel,
uid = uid,
connectionId = connectionIdSequence.incrementAndGet(),
username = username,
nickname = nickname,
uid = sessionInfo.uid,
connectionId = connectionIdSequence.getAndIncrement(),
username = sessionInfo.username,
nickname = sessionInfo.nickname,
wallet = playerWallet,
characterRoster = characterRoster,
caddieRoster = caddieRoster,
inventory = inventory,
equipment = equipment,
statistics = statistics,
achievements = achievements,
)
giveTestStuff(player)

players.add(player)
channel.closeFuture().addListener { onPlayerDisconnect(player) }
Expand Down
Loading

0 comments on commit 3ec7b3e

Please sign in to comment.