Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ class MatchService(
if (userId == opponentId) {
throw CustomException(HttpStatus.BAD_REQUEST, "You cannot play against yourself")
}

if (publicOpponent.tier == TierTypeDto.TIER1) {
throw CustomException(
HttpStatus.BAD_REQUEST, "Opponent cannot be a tier 1 player in manual match"
)
}
val (userLanguage, userCode) = lockedCodeService.getLockedCode(userId)
val userMap = lockedMapService.getLockedMap(userId)

Expand Down Expand Up @@ -328,20 +332,21 @@ class MatchService(
val finishedMatch = match.copy(verdict = verdict)
val (newUserRating, newOpponentRating) =
ratingHistoryService.updateRating(match.player1.userId, match.player2.userId, verdict)
if (!(match.mode == MatchModeEnum.MANUAL && (match.player1.tier == TierTypeDto.TIER1))) {

publicUserService.updatePublicRating(
userId = match.player1.userId,
isInitiator = true,
verdict = verdict,
newRating = newUserRating
)
publicUserService.updatePublicRating(
userId = match.player2.userId,
isInitiator = false,
verdict = verdict,
newRating = newOpponentRating
)

publicUserService.updatePublicRating(
userId = match.player1.userId,
isInitiator = true,
verdict = verdict,
newRating = newUserRating
)
publicUserService.updatePublicRating(
userId = match.player2.userId,
isInitiator = false,
verdict = verdict,
newRating = newOpponentRating
)
}
if (match.mode == MatchModeEnum.MANUAL) {
notificationService.sendNotification(
match.player1.userId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
package delta.codecharacter.server.schedulers

import delta.codecharacter.server.user.public_user.PublicUserService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service

@Service class SchedulingService(@Autowired private val publicUserService: PublicUserService)
@Service
class SchedulingService(@Autowired private val publicUserService: PublicUserService) {
private val logger: Logger = LoggerFactory.getLogger(SchedulingService::class.java)

@Scheduled(cron = "\${environment.registration-time}", zone = "GMT")
fun updateTempLeaderboard() {
logger.info("Practice phase ended!!")
publicUserService.resetRatingsAfterPracticePhase()
publicUserService.updateLeaderboardAfterPracticePhase()
}
@Scheduled(cron = "\${environment.update-time}", zone = "GMT")
fun promoteAndDemoteUserTiers() {
logger.info("LeaderBoard Tier Promotion and Demotion started")
publicUserService.promoteTiers()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ class UserService(
.POST(HttpRequest.BodyPublishers.noBody())
.build()
val response = client.send(request, HttpResponse.BodyHandlers.ofString())
val json = JsonObject(response.body())
return json.toBsonDocument().getBoolean("success").value
val json = JsonObject(response.body()).toBsonDocument()
return (
json.getBoolean("success").value &&
(json.getDouble("score").value.compareTo(0.5) >= 0)
)
} catch (e: Exception) {
e.printStackTrace()
return false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package delta.codecharacter.server.user.public_user

import delta.codecharacter.server.leaderboard.LeaderBoardEnum
import delta.codecharacter.dtos.TierTypeDto
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.index.Indexed
import org.springframework.data.mongodb.core.mapping.Document
Expand All @@ -14,7 +14,7 @@ data class PublicUserEntity(
val country: String,
val college: String,
val avatarId: Int,
val tier: LeaderBoardEnum,
val tier: TierTypeDto,
val tutorialLevel: Int,
val rating: Double,
val wins: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package delta.codecharacter.server.user.public_user

import delta.codecharacter.dtos.TierTypeDto
import org.springframework.data.domain.PageRequest
import org.springframework.data.mongodb.repository.MongoRepository
import java.util.Optional
import java.util.UUID

interface PublicUserRepository : MongoRepository<PublicUserEntity, UUID> {
fun findByUsername(username: String): Optional<PublicUserEntity>

fun findAllByTier(tier: TierTypeDto?, pageRequest: PageRequest): List<PublicUserEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import delta.codecharacter.dtos.UpdateCurrentUserProfileDto
import delta.codecharacter.dtos.UserStatsDto
import delta.codecharacter.server.daily_challenge.DailyChallengeEntity
import delta.codecharacter.server.exception.CustomException
import delta.codecharacter.server.leaderboard.LeaderBoardEnum
import delta.codecharacter.server.match.MatchVerdictEnum
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.data.domain.PageRequest
Expand All @@ -25,6 +26,10 @@ import java.util.UUID
class PublicUserService(@Autowired private val publicUserRepository: PublicUserRepository) {

@Value("\${environment.no-of-tutorial-level}") private lateinit var totalTutorialLevels: Number
@Value("\${environment.tier-1-players}") private var tier1Players: Number = 1
@Value("\${environment.top-players}") private var topPlayer: Number = 1
private val logger: Logger = LoggerFactory.getLogger(PublicUserService::class.java)

fun create(
userId: UUID,
username: String,
Expand All @@ -46,16 +51,76 @@ class PublicUserService(@Autowired private val publicUserRepository: PublicUserR
losses = 0,
ties = 0,
score = 0.0,
tier = LeaderBoardEnum.TIER_PRACTICE,
tier = TierTypeDto.TIER_PRACTICE,
tutorialLevel = 1,
dailyChallengeHistory = HashMap()
)
publicUserRepository.save(publicUser)
}

fun updateLeaderboardAfterPracticePhase() {
val publicUsers = publicUserRepository.findAll()
publicUsers.forEachIndexed { index, user ->
if (index < tier1Players.toInt()) {
publicUserRepository.save(user.copy(tier = TierTypeDto.TIER1))
} else {
publicUserRepository.save(user.copy(tier = TierTypeDto.TIER2))
}
}
logger.info("Leaderboard tier set during the start of game phase")
}

fun resetRatingsAfterPracticePhase() {
logger.info("Reset ratings after practice phase starts")
val users = publicUserRepository.findAll()
users.forEach { user ->
publicUserRepository.save(user.copy(rating = 1500.0, wins = 0, ties = 0, losses = 0))
}
logger.info("Reset ratings after practice phase has ended")
}

fun promoteTiers() {
val topPlayersInTier2 =
publicUserRepository.findAllByTier(
TierTypeDto.TIER2,
PageRequest.of(0, topPlayer.toInt(), Sort.by(Sort.Order.desc("rating")))
)
val bottomPlayersInTier1 =
publicUserRepository.findAllByTier(
TierTypeDto.TIER1,
PageRequest.of(0, topPlayer.toInt(), Sort.by(Sort.Order.asc("rating")))
)
topPlayersInTier2.forEach { users ->
val updatedToTier1User = publicUserRepository.save(users.copy(tier = TierTypeDto.TIER1))
if (updatedToTier1User.tier == TierTypeDto.TIER1) {
logger.info("UserName ${updatedToTier1User.username} got promoted to TIER1")
} else {
logger.error(
"Error occurred while updating ${updatedToTier1User.username} (UserName) to TIER1"
)
}
}
bottomPlayersInTier1.forEach { users ->
val updateToTier2User = publicUserRepository.save(users.copy(tier = TierTypeDto.TIER2))
if (updateToTier2User.tier == TierTypeDto.TIER2) {
logger.info("UserName ${updateToTier2User.username} got demoted to TIER2")
} else {
logger.error(
"Error occurred while updating ${updateToTier2User.username} (UserName) to TIER2"
)
}
}
}

fun getLeaderboard(page: Int?, size: Int?, tier: TierTypeDto?): List<LeaderboardEntryDto> {
val pageRequest = PageRequest.of(page ?: 0, size ?: 10, Sort.by(Sort.Direction.DESC, "rating"))
return publicUserRepository.findAll(pageRequest).content.map {
val pageRequest = PageRequest.of(page ?: 0, size ?: 10, Sort.by(Sort.Order.desc("rating")))
val publicUsers =
if (tier == null) {
publicUserRepository.findAll(pageRequest).content
} else {
publicUserRepository.findAllByTier(tier, pageRequest)
}
return publicUsers.map {
LeaderboardEntryDto(
user =
PublicUserDto(
Expand All @@ -72,7 +137,7 @@ class PublicUserService(@Autowired private val publicUserRepository: PublicUserR
wins = it.wins,
losses = it.losses,
ties = it.ties
)
),
)
}
}
Expand Down
5 changes: 5 additions & 0 deletions server/src/main/resources/application.docker.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ environment:
event-start-date: YYYY-MM-DDTHH:MM:SSZ
reCaptcha-key: your-recaptcha-key
no-of-tutorial-level: 0
registration-time: "* * * * * *"
game-start-time: "* * * * * *"
tier-1-players: 20
top-players: 5
update-time: "*/20 * * * * *"

server:
compression:
Expand Down
5 changes: 5 additions & 0 deletions server/src/main/resources/application.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ environment:
event-start-date: YYYY-MM-DDTHH:MM:SSZ
reCaptcha-key: your-recaptcha-key
no-of-tutorial-level: 0
registration-time: "* * * * * *"
game-start-time: "* * * * * *"
Comment thread
Valliammai-SM marked this conversation as resolved.
tier-1-players: 20
top-players: 5
update-time: "*/20 * * * * *"

server:
compression:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package delta.codecharacter.server

import delta.codecharacter.dtos.ChallengeTypeDto
import delta.codecharacter.dtos.DailyChallengeObjectDto
import delta.codecharacter.dtos.TierTypeDto
import delta.codecharacter.server.daily_challenge.DailyChallengeEntity
import delta.codecharacter.server.leaderboard.LeaderBoardEnum
import delta.codecharacter.server.user.LoginType
import delta.codecharacter.server.user.UserEntity
import delta.codecharacter.server.user.public_user.DailyChallengeHistory
Expand Down Expand Up @@ -58,7 +58,7 @@ class TestAttributes {
wins = 4,
losses = 2,
ties = 1,
tier = LeaderBoardEnum.TIER_PRACTICE,
tier = TierTypeDto.TIER_PRACTICE,
score = 0.0,
dailyChallengeHistory = hashMapOf(0 to DailyChallengeHistory(0.0, dailyChallengeCode)),
tutorialLevel = 1
Expand Down
Loading