Skip to content

Commit

Permalink
Refactor contest status
Browse files Browse the repository at this point in the history
  • Loading branch information
kunyavskiy committed Aug 28, 2024
1 parent 7809746 commit 86f7b58
Show file tree
Hide file tree
Showing 47 changed files with 567 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ data class TeamSpotlightFlowSettings(
val externalScoreScale: Double = 1.0,
) {
fun rankScore(rank: Int, contestStatus: ContestStatus): Double {
if (contestStatus == ContestStatus.BEFORE) {
if (contestStatus is ContestStatus.BEFORE) {
return scoreboardBeforeContestScore
} else if (rank > scoreboardLowestRank) {
return 0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class QueueService : Service {
}

private fun RunInfo.isInProgress(contestInfo: ContestInfo): Boolean {
return result is RunResult.InProgress && time < contestInfo.freezeTime
return result is RunResult.InProgress && contestInfo.freezeTime != null && time < contestInfo.freezeTime!!
}


Expand Down Expand Up @@ -132,7 +132,7 @@ class QueueService : Service {
is AnalyticsUpdate -> {}
is InfoUpdate -> {}
is RunUpdate -> {
val contestInfo = event.state.infoAfterEvent?.takeIf { it.status != ContestStatus.BEFORE } ?: return@collect
val contestInfo = event.state.infoAfterEvent?.takeIf { it.status !is ContestStatus.BEFORE } ?: return@collect
currentContestInfo = contestInfo
val run = update.newInfo
removedRuns[run.id] = run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class TeamSpotlightService(
merge(
loopFlow(settings.scoreboardPushInterval, onError = {}) { ScoreboardPushTrigger },
flow.filter { state ->
state.state.infoAfterEvent?.status == ContestStatus.BEFORE ||
state.state.infoAfterEvent?.status is ContestStatus.BEFORE ||
state.state.lastEvent.let { it is RunUpdate && !it.newInfo.isHidden }
},
addScoreRequests,
Expand Down Expand Up @@ -171,7 +171,7 @@ class TeamSpotlightService(
getTeamInQueue(it.first).addAccent(
TeamScoreboardPlace(
it.second,
lastInfo?.status ?: ContestStatus.BEFORE
lastInfo?.status ?: ContestStatus.BEFORE()
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import io.ktor.server.routing.*
import kotlinx.collections.immutable.persistentMapOf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
import kotlinx.datetime.Instant
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.elementNames
Expand Down Expand Up @@ -123,10 +122,10 @@ object ClicsExporter {
id = "contest",
name = info.name,
formalName = info.name,
startTime = info.startTime.takeIf { it != Instant.fromEpochSeconds(0) },
countdownPauseTime = info.holdBeforeStartTime,
startTime = info.startTime,
countdownPauseTime = (info.status as? ContestStatus.BEFORE)?.holdTime,
duration = info.contestLength,
scoreboardFreezeDuration = info.contestLength - info.freezeTime,
scoreboardFreezeDuration = info.freezeTime?.let { info.contestLength - it },
scoreboardType = "pass-fail",
penaltyTime = info.penaltyPerWrongAttempt.inWholeMinutes,
)
Expand Down Expand Up @@ -171,8 +170,8 @@ object ClicsExporter {
diffRemove(old, new, id, toFinalEvent)
}

private fun getState(info: ContestInfo) = when (info.status) {
ContestStatus.BEFORE -> State(
private fun getState(info: ContestInfo) = when (val status = info.status) {
is ContestStatus.BEFORE -> State(
ended = null,
frozen = null,
started = null,
Expand All @@ -181,30 +180,30 @@ object ClicsExporter {
endOfUpdates = null
)

ContestStatus.RUNNING, ContestStatus.FAKE_RUNNING -> State(
is ContestStatus.RUNNING -> State(
ended = null,
frozen = if (info.currentContestTime >= info.freezeTime) info.startTime + info.freezeTime else null,
started = info.startTime,
frozen = status.frozenAt,
started = status.startedAt,
finalized = null,
thawed = null,
endOfUpdates = null
)

ContestStatus.OVER -> State(
ended = info.startTime + info.contestLength,
frozen = info.startTime + info.freezeTime,
started = info.startTime,
is ContestStatus.OVER -> State(
ended = status.finishedAt,
frozen = status.frozenAt,
started = status.startedAt,
finalized = null,
thawed = null,
endOfUpdates = null
)
ContestStatus.FINALIZED -> State(
ended = info.startTime + info.contestLength,
frozen = info.startTime + info.freezeTime,
started = info.startTime,
finalized = info.startTime + info.contestLength,
thawed = info.startTime + info.contestLength,
endOfUpdates = info.startTime + info.contestLength
is ContestStatus.FINALIZED -> State(
ended = status.finishedAt,
frozen = status.frozenAt,
started = status.startedAt,
finalized = status.finalizedAt,
thawed = null,
endOfUpdates = status.finalizedAt
)
}

Expand All @@ -220,7 +219,7 @@ object ClicsExporter {
languageId = unknownLanguage.id,
problemId = run.problemId.value,
teamId = run.teamId.value,
time = info.startTime + run.time,
time = info.startTimeOrZero + run.time,
contestTime = run.time,
),
::SubmissionEvent
Expand All @@ -233,9 +232,9 @@ object ClicsExporter {
id = run.id.toString(),
submissionId = run.id.toString(),
judgementTypeId = judgmentTypes[result.verdict]?.id,
startTime = info.startTime + run.time,
startTime = info.startTimeOrZero + run.time,
startContestTime = run.time,
endTime = info.startTime + run.time,
endTime = info.startTimeOrZero + run.time,
endContestTime = run.time
),
::JudgementEvent
Expand Down Expand Up @@ -467,7 +466,7 @@ object ClicsExporter {
private fun ContestStateWithScoreboard.toClicsScoreboard(): Scoreboard {
val info = state.infoAfterEvent!!
return Scoreboard(
time = info.startTime + lastSubmissionTime,
time = info.startTimeOrZero + lastSubmissionTime,
contestTime = lastSubmissionTime,
state = getState(info),
rows = rankingAfter.order.zip(rankingAfter.ranks).map { (teamId, rank) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,27 @@ object PCMSExporter {
}

fun ContestStatus.toPcmsStatus() = when (this) {
ContestStatus.FINALIZED, ContestStatus.OVER -> "over"
ContestStatus.RUNNING, ContestStatus.FAKE_RUNNING -> "running"
ContestStatus.BEFORE -> "before"
is ContestStatus.FINALIZED, is ContestStatus.OVER -> "over"
is ContestStatus.RUNNING -> "running"
is ContestStatus.BEFORE -> "before"
}

private fun Element.setAttributeIfNotNull(name: String, value: String?) {
if (value != null) {
setAttribute(name, value)
}
}

private fun Element.buildContestNode(info: ContestInfo) {
setAttribute("name", info.name)
setAttribute("time", info.currentContestTime.inWholeMilliseconds.toString())
setAttribute("start-time", info.startTime.toString())
setAttribute("start-time-millis", info.startTime.toEpochMilliseconds().toString())
setAttributeIfNotNull("start-time", info.startTime?.toString())
setAttributeIfNotNull("start-time-millis", info.startTime?.toEpochMilliseconds().toString())
setAttribute("length", info.contestLength.inWholeMilliseconds.toString())
setAttribute("status", info.status.toPcmsStatus())
setAttribute("frozen", "no")
setAttribute("freeze-time", info.freezeTime.toIsoString())
setAttribute("freeze-time-millis", info.freezeTime.inWholeMilliseconds.toString())
setAttributeIfNotNull("freeze-time", info.freezeTime?.toIsoString())
setAttributeIfNotNull("freeze-time-millis", info.freezeTime?.inWholeMilliseconds.toString())
}
private fun Element.buildChallengeNode(info: ContestInfo) {
info.scoreboardProblems.forEach { problem ->
Expand Down
Loading

0 comments on commit 86f7b58

Please sign in to comment.