Skip to content

Commit

Permalink
Refactor codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
timoschwarzer committed Aug 23, 2024
1 parent 6a8ed14 commit 8bc2b1b
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 40 deletions.
3 changes: 0 additions & 3 deletions src/main/kotlin/wotw/server/api/LeagueEndpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.utils.io.core.*
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.exists
import org.jetbrains.exposed.sql.notExists
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
Expand All @@ -24,8 +23,6 @@ import wotw.server.game.handlers.league.LeagueGameHandler
import wotw.server.main.WotwBackendServer
import wotw.server.util.NTuple5
import wotw.server.util.then
import java.time.Instant
import java.time.temporal.ChronoUnit
import kotlin.math.floor

class LeagueEndpoint(server: WotwBackendServer) : Endpoint(server) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/wotw/server/api/StateAggregation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class UberStateSyncStrategy(val aggregation: (old: Double, new: Double) ->
val KEEP = UberStateSyncStrategy({ v, _ -> v })
val AVG = UberStateSyncStrategy({ o, n -> (o + n) / 2 })

fun MAX_THRESHOLD(threshold: Double): UberStateSyncStrategy {
fun maxThreshold(threshold: Double): UberStateSyncStrategy {
return UberStateSyncStrategy({ old, new -> min(MAX.aggregation(old, new), threshold) })
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/kotlin/wotw/server/bingo/GoalGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ fun generatePool() = mutableListOf(
),
nof(
3,
pickupsIn.marsh,
pickupsIn.hollow,
pickupsIn.glades,
pickupsIn.wellspring,
pickupsIn.burrows,
pickupsIn.woods,
pickupsIn.depths,
pickupsIn.pools,
pickupsIn.reach,
pickupsIn.wastes,
pickupsIn.willow,
pickupsIn.houses,
PickupsByArea.marsh,
PickupsByArea.hollow,
PickupsByArea.glades,
PickupsByArea.wellspring,
PickupsByArea.burrows,
PickupsByArea.woods,
PickupsByArea.depths,
PickupsByArea.pools,
PickupsByArea.reach,
PickupsByArea.wastes,
PickupsByArea.willow,
PickupsByArea.houses,
).weighted(300),
bool("Spin the Wheel outside Luma Pools", 945, 12852),

Expand Down Expand Up @@ -734,7 +734,7 @@ fun generatePool() = mutableListOf(
bool("Woods Entrance", 58674, 7071),
bool("Woods Exit", 58674, 1965),
bool("Feeding Grounds", 58674, 10029),
bool("Wastes", 20120, 49994),
bool("Central Wastes", 20120, 49994),
bool("Outer Ruins", 20120, 41398),
bool("Willow's End", 16155, 41465),
bool("Inner Ruins", 10289, 4928, countOnly = true),
Expand Down Expand Up @@ -881,7 +881,7 @@ fun generatePool() = mutableListOf(
),
)

object pickupsIn {
object PickupsByArea {
val marsh = group(
"Collect # Pickups In Inkwater Marsh",
threshold("MarshPastOpher.SpiritTrial", 44964, 45951, threshold = 2, hideValue = true),
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/wotw/server/database/EntityCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ open class EntityCache<KEY : Any, VALUE : Any>(
return null
}

suspend fun put(key: KEY, value: VALUE?) = CacheEntry(value, currentTimeMillis()).let {
fun put(key: KEY, value: VALUE?) = CacheEntry(value, currentTimeMillis()).let {
cache[key] = it
it.value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import wotw.server.database.StringIdTable

object BingothonTokens : StringIdTable("bingothon_tokens") {
val owner = reference("owner_id", Users, ReferenceOption.CASCADE, ReferenceOption.CASCADE).uniqueIndex()
val multiverseId = BingothonTokens.reference("multiverse_id", Multiverses, ReferenceOption.CASCADE)
val multiverseId = reference("multiverse_id", Multiverses, ReferenceOption.CASCADE)
val created = datetime("created_at").defaultExpression(CurrentDateTime)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object SeedSpoilerDownloads : LongIdTable("seed_spoiler_downloads") {
val playerId = reference("user_id", Users, ReferenceOption.CASCADE)

init {
uniqueIndex(SeedSpoilerDownloads.seedId, SeedSpoilerDownloads.playerId)
uniqueIndex(seedId, playerId)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/wotw/server/database/model/Spectator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Spectators : LongIdTable("spectators") {
val playerId = reference("user_id", Users, ReferenceOption.CASCADE)

init {
uniqueIndex(Spectators.gameId, Spectators.playerId)
uniqueIndex(gameId, playerId)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/wotw/server/database/model/Universe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class World(id: EntityID<Long>) : LongEntity(id) {
val gameState = GameState.new {
this.multiverse = universe.multiverse
this.universe = universe
val world = World.new {

val world = new {
this.universe = universe
this.name = name
this.seed = seed
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/wotw/server/game/GameHandlerRegistry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class GameHandlerRegistry(val server: WotwBackendServer) {
return handler
}

public fun purgeFromCache(multiverseId: Long) {
fun purgeFromCache(multiverseId: Long) {
handlers.remove(multiverseId)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransacti
import wotw.io.messages.json
import wotw.io.messages.protobuf.*
import wotw.server.api.*
import wotw.server.bingo.Point
import wotw.server.bingo.UberStateMap
import wotw.server.database.model.*
import wotw.server.exception.ConflictException
Expand Down Expand Up @@ -336,13 +337,13 @@ class NormalGameHandler(multiverseId: Long, server: WotwBackendServer) : GameHan
this.finishedTime = finishedTime
}

multiverse.universes.forEach() { universe ->
multiverse.universes.forEach { universe ->
val team = RaceTeam.new {
this.race = race
this.finishedTime = state.universeFinishedTimes[universe.id.value]
}

universe.memberships.forEach() { worldMembership ->
universe.memberships.forEach { worldMembership ->
RaceTeamMember.new {
this.raceTeam = team
this.user = worldMembership.user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import dev.kord.core.Kord
import dev.kord.rest.builder.component.ActionRowBuilder
import dev.kord.rest.builder.message.AttachmentBuilder
import dev.kord.rest.builder.message.addFile
import dev.kord.rest.builder.message.allowedMentions
import dev.kord.rest.builder.message.embed
Expand All @@ -31,7 +30,6 @@ import wotw.server.util.logger
import java.time.Duration
import java.time.Instant
import java.util.concurrent.TimeUnit
import kotlin.io.path.Path


/**
Expand Down Expand Up @@ -177,7 +175,7 @@ class LeagueManager(val server: WotwBackendServer) {
}
}

suspend fun setup() {
fun setup() {
scheduler.scheduleExecution(Every(60, TimeUnit.SECONDS))

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class WorldInventory(private val gameState: GameState) {
gameState.uberStateData[target.uberId] = target.value
}

var value = request.amount.toDouble();
var value = request.amount.toDouble()
if (request.relative) {
value += gameState.uberStateData[request.resourceUberId] ?: 0.0
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/wotw/server/io/ClientConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.ktor.network.sockets.*
import io.ktor.server.auth.jwt.*
import io.ktor.server.websocket.*
import io.ktor.utils.io.core.*
import io.ktor.utils.io.errors.*
import io.ktor.utils.io.errors.IOException
import io.ktor.websocket.*
import kotlinx.coroutines.channels.ClosedReceiveChannelException
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
Expand All @@ -31,7 +31,7 @@ fun DecodedJWT.parsePayload(): Payload {

class ClientConnectionUDPRegistry() {
companion object {
private val availableUdpIDs = HashSet((0..65535).toList());
private val availableUdpIDs = HashSet((0..65535).toList())
private val connections: HashMap<Int, ClientConnection> = hashMapOf()

fun register(connection: ClientConnection): Int {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/wotw/server/main/WotwBackendServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class WotwBackendServer {
if (System.getenv("SENTRY_DSN") != null) {
Sentry.init { options ->
options.dsn = System.getenv("SENTRY_DSN")
options.setEnableUncaughtExceptionHandler(false)
options.isEnableUncaughtExceptionHandler = false
}

Thread.setDefaultUncaughtExceptionHandler { _, throwable ->
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/wotw/server/services/InfoMessagesService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import wotw.server.game.handlers.league.LeagueGameHandler
import wotw.server.main.WotwBackendServer

class InfoMessagesService(private val server: WotwBackendServer) {
val COLORS = arrayOf(
val colors = arrayOf(
"#1565c0",
"#388e3c",
"#ad1457",
Expand Down Expand Up @@ -46,7 +46,7 @@ class InfoMessagesService(private val server: WotwBackendServer) {
suspend fun generateMultiverseInfoMessage(multiverse: Multiverse) = MultiverseInfoMessage(
multiverse.id.value,
multiverse.universes.sortedBy { it.id }
.mapIndexed { index, universe -> generateUniverseInfo(universe, COLORS[index % COLORS.size]) },
.mapIndexed { index, universe -> generateUniverseInfo(universe, colors[index % colors.size]) },
multiverse.cachedBoard != null,
multiverse.spectators.map(::generateUserInfo),
multiverse.seed?.takeIf { it.allowDownload }?.id?.value,
Expand All @@ -63,9 +63,9 @@ class InfoMessagesService(private val server: WotwBackendServer) {
fun generateUniverseInfo(universe: Universe, color: String? = null) = UniverseInfo(
universe.id.value,
universe.name,
color ?: COLORS[universe.multiverse.universes.sortedBy { it.id }.indexOf(universe) % COLORS.size],
color ?: colors[universe.multiverse.universes.sortedBy { it.id }.indexOf(universe) % colors.size],
universe.worlds.sortedBy { it.id }.mapIndexed { index, world ->
generateWorldInfo(world, COLORS[index % COLORS.size])
generateWorldInfo(world, colors[index % colors.size])
}
)

Expand All @@ -79,7 +79,7 @@ class InfoMessagesService(private val server: WotwBackendServer) {
WorldInfo(
world.id.value,
world.name,
color ?: COLORS[world.universe.worlds.sortedBy { it.id }.indexOf(world) % COLORS.size],
color ?: colors[world.universe.worlds.sortedBy { it.id }.indexOf(world) % colors.size],
world.memberships.map(::generateWorldMembershipInfo),
world.seed?.id?.value,
)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/wotw/server/sync/CoopStates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1762,7 +1762,7 @@ val normalWorldSyncAggregationStrategy by lazy {

/// Quests
// TODO: Add all quests here
sync(14019, 33776).with(UberStateSyncStrategy.MAX_THRESHOLD(3.0)), // Acorn Quest
sync(14019, 33776).with(UberStateSyncStrategy.maxThreshold(3.0)), // Acorn Quest
)
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/wotw/server/util/MultiverseUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class MultiverseUtil(val server: WotwBackendServer) {
server.connections.broadcastMultiverseInfoMessage(worldMembership.multiverse)
}

suspend fun movePlayerToWorld(player: User, world: World): WorldMembership {
fun movePlayerToWorld(player: User, world: World): WorldMembership {
var worldMembership = WorldMembership.find {
(WorldMemberships.userId eq player.id) and (WorldMemberships.multiverseId eq world.universe.multiverse.id)
}.firstOrNull()
Expand Down

0 comments on commit 8bc2b1b

Please sign in to comment.