Skip to content

Commit

Permalink
configs: individualize server configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
hex-agon committed Jul 10, 2023
1 parent 172c67f commit 5bc3acd
Show file tree
Hide file tree
Showing 21 changed files with 142 additions and 170 deletions.
11 changes: 0 additions & 11 deletions common/src/main/kotlin/work/fking/pangya/common/Time.kt

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions game-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ port = 20202
flags = []
boosts = ["DOUBLE_PANG"]
icon = "CIEN"

serverChannels = [
{ id = 1, name = "Rookies", capacity = 20, restrictions = ["ROOKIES_ONLY"] },
{ id = 2, name = "Free", capacity = 300 }
]
```

Environment variables:
Expand Down
19 changes: 9 additions & 10 deletions game-server/src/main/kotlin/work/fking/pangya/game/Bootstrap.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package work.fking.pangya.game

import com.fasterxml.jackson.dataformat.toml.TomlMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.lettuce.core.RedisClient
import io.lettuce.core.RedisURI
import org.apache.logging.log4j.LogManager
import work.fking.pangya.common.server.ServerConfigLoader
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 java.nio.file.Files
import java.nio.file.Path


object Bootstrap {
Expand All @@ -16,19 +20,14 @@ object Bootstrap {
@JvmStatic
fun main(args: Array<String>) {
LOGGER.info("Bootstrapping the game server server...")
val serverConfig = ServerConfigLoader.load("config.toml")
val objectMapper = TomlMapper().registerKotlinModule()

val serverConfig = objectMapper.readValue<GameServerConfig>(Files.newInputStream(Path.of("config.toml")))
val redisClient = RedisClient.create(RedisURI.create(System.getenv("REDIS_URI")))
val discoveryClient = DiscoveryClient(redisClient)
val sessionClient = SessionClient(redisClient)

val serverChannels = listOf(
ServerChannel(1, "Rookies", 20, listOf(Restriction.ROOKIES_ONLY)),
ServerChannel(2, "Beginners & Juniors", 20, listOf(Restriction.BEGINNERS_AND_JUNIORS_ONLY)),
ServerChannel(3, "Juniors & Seniors", 20, listOf(Restriction.JUNIORS_AND_SENIORS_ONLY)),
ServerChannel(4, "Free", 20, listOf())
)

val server = GameServer(serverConfig, sessionClient, serverChannels)
val server = GameServer(serverConfig, sessionClient)
HeartbeatPublisher(discoveryClient, GAME, serverConfig) { server.playerCount() }.start()
server.start()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.Channel
import io.netty.channel.ChannelOption
import org.slf4j.LoggerFactory
import work.fking.pangya.common.server.ServerConfig
import work.fking.pangya.game.net.ServerChannelInitializer
import work.fking.pangya.game.player.Item
import work.fking.pangya.game.player.Player
Expand All @@ -20,9 +19,9 @@ import java.util.concurrent.atomic.AtomicInteger
private val LOGGER = LoggerFactory.getLogger(GameServer::class.java)

class GameServer(
private val serverConfig: ServerConfig,
private val serverConfig: GameServerConfig,
private val sessionClient: SessionClient,
private val serverChannels: List<ServerChannel>
private val serverChannels: List<ServerChannel> = serverConfig.serverChannels
) {
private val executorService = Executors.newVirtualThreadPerTaskExecutor()
private val connectionIdSequence = AtomicInteger()
Expand Down Expand Up @@ -138,7 +137,7 @@ class GameServer(
val channel = bootstrap.bind(inetAddress, serverConfig.port)
.sync()
.channel()
LOGGER.info("Successfully bound to port {}, server bootstrap completed!", serverConfig.port)
LOGGER.info("Successfully bound to {}, server bootstrap completed!", channel.localAddress())
channel.closeFuture().sync()
} finally {
bossGroup.shutdownGracefully()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package work.fking.pangya.game

import work.fking.pangya.discovery.ServerBoost
import work.fking.pangya.discovery.ServerConfig
import work.fking.pangya.discovery.ServerFlag
import work.fking.pangya.discovery.ServerIcon

data class GameServerConfig(
override val id: Int,
override val name: String,
override val capacity: Int,
override val advertiseAddress: String,
override val port: Int,
override val flags: List<ServerFlag>?,
override val boosts: List<ServerBoost>?,
override val icon: ServerIcon?,
val bindAddress: String,
val serverChannels: List<ServerChannel> = emptyList()
) : ServerConfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ServerChannel(
val id: Int,
val name: String,
val capacity: Int,
val restrictions: List<Restriction>
val restrictions: List<Restriction> = emptyList()
) {
val roomManager: RoomManager = RoomManager()

Expand Down Expand Up @@ -56,14 +56,10 @@ class ServerChannel(
return id
}

enum class Restriction(private val flag: Int) {
enum class Restriction(val flag: Int) {
JUNIOR_AND_LOWER(1 shl 1),
ROOKIES_ONLY(1 shl 3),
BEGINNERS_AND_JUNIORS_ONLY(1 shl 4),
JUNIORS_AND_SENIORS_ONLY(1 shl 5);

fun flag(): Int {
return flag
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ServerChannelsPacket(
private fun pack(restrictions: List<Restriction>): Int {
var bitFlags = 0
for (restriction in restrictions) {
bitFlags = bitFlags or restriction.flag()
bitFlags = bitFlags or restriction.flag
}
return bitFlags
}
Expand Down
6 changes: 1 addition & 5 deletions login-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ port = 10103
Environment variables:

```
DATABASE_HOST=localhost
DATABASE_NAME=pangya
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=
LOG_LEVEL=trace
LOG_LEVEL=debug
REDIS_URI=redis://localhost
```
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package work.fking.pangya.login

import com.fasterxml.jackson.dataformat.toml.TomlMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.lettuce.core.RedisClient
import io.lettuce.core.RedisURI
import org.apache.logging.log4j.LogManager
import work.fking.pangya.common.server.ServerConfigLoader
import work.fking.pangya.discovery.DiscoveryClient
import work.fking.pangya.discovery.HeartbeatPublisher
import work.fking.pangya.discovery.ServerType.LOGIN
import work.fking.pangya.login.auth.NOOP_AUTHENTICATOR
import work.fking.pangya.login.auth.SessionClient
import java.nio.file.Files
import java.nio.file.Path

object Bootstrap {
private val LOGGER = LogManager.getLogger(Bootstrap::class.java)

@JvmStatic
fun main(args: Array<String>) {
val objectMapper = TomlMapper().registerKotlinModule()

LOGGER.info("Bootstrapping the login server...")
val redisClient = RedisClient.create(RedisURI.create(System.getenv("REDIS_URI")))
val discoveryClient = DiscoveryClient(redisClient)
val sessionClient = SessionClient(redisClient)
val serverConfig = ServerConfigLoader.load("config.toml")
val serverConfig = objectMapper.readValue<LoginServerConfig>(Files.newInputStream(Path.of("config.toml")))
val loginServer = LoginServer(discoveryClient, serverConfig, sessionClient, NOOP_AUTHENTICATOR)
LOGGER.debug("Initializing service discovery...")
HeartbeatPublisher(discoveryClient, LOGIN, serverConfig).start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.Channel
import io.netty.channel.ChannelOption
import org.slf4j.LoggerFactory
import work.fking.pangya.common.server.ServerConfig
import work.fking.pangya.discovery.DiscoveryClient
import work.fking.pangya.login.auth.Authenticator
import work.fking.pangya.login.auth.SessionClient
Expand All @@ -19,7 +18,7 @@ private val LOGGER = LoggerFactory.getLogger(LoginServer::class.java)

class LoginServer(
val discoveryClient: DiscoveryClient,
val serverConfig: ServerConfig,
val serverConfig: LoginServerConfig,
val sessionClient: SessionClient,
val authenticator: Authenticator
) {
Expand Down Expand Up @@ -58,12 +57,11 @@ class LoginServer(
val channel = bootstrap.bind(inetAddress, serverConfig.port)
.sync()
.channel()
LOGGER.info("Successfully bound to port {}, server bootstrap completed!", serverConfig.port)
LOGGER.info("Successfully bound to {}, server bootstrap completed!", channel.localAddress())
channel.closeFuture().sync()
} finally {
bossGroup.shutdownGracefully()
workerGroup.shutdownGracefully()
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package work.fking.pangya.login

import work.fking.pangya.discovery.ServerBoost
import work.fking.pangya.discovery.ServerConfig
import work.fking.pangya.discovery.ServerFlag
import work.fking.pangya.discovery.ServerIcon

data class LoginServerConfig(
override val id: Int,
override val name: String,
override val capacity: Int,
override val advertiseAddress: String,
override val port: Int,
override val flags: List<ServerFlag>?,
override val boosts: List<ServerBoost>?,
override val icon: ServerIcon?,
val bindAddress: String
) : ServerConfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object ServerListReplies {
private fun encodeServerInfo(buffer: ByteBuf, server: ServerInfo) {
var serverFlags = 0
for (flag in server.flags) {
serverFlags = serverFlags or flag.value()
serverFlags = serverFlags or flag.value
}
var serverBoosts = 0
for (boost in server.boosts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands
import org.apache.logging.log4j.LogManager
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock

private val LOGGER = LogManager.getLogger(DiscoveryClient::class.java)
private const val CHANNEL_NAME = "pangya.servers.heartbeat"
Expand All @@ -30,6 +32,7 @@ class DiscoveryClient(
.subscribe()
}

private val serversLock = ReentrantLock()
private val knownServers: MutableList<KnownServer> = ArrayList()

/**
Expand Down Expand Up @@ -88,7 +91,7 @@ class DiscoveryClient(

private fun cleanup() {
val now = LocalDateTime.now()
synchronized(knownServers) {
serversLock.withLock {
knownServers.removeIf { ChronoUnit.MINUTES.between(it.knownSince, now) > 3 }
}
}
Expand Down
Loading

0 comments on commit 5bc3acd

Please sign in to comment.