Skip to content

Commit

Permalink
change leaderboard behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
t0lia committed May 20, 2024
1 parent a249e99 commit 1b0bb5d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 12 deletions.
28 changes: 28 additions & 0 deletions backend/api-spec/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Define Hurl scripts
HURL_SCRIPTS = leaderboard_post.hurl leaderboard_get.hurl statistic_post.hurl statistic_get.hurl

# Default target to run all Hurl scripts
all: run_all

# Target to run leaderboard POST
leaderboard_post: leaderboard_post.hurl
hurl leaderboard_post.hurl | jq .

# Target to run leaderboard GET
leaderboard_get: leaderboard_get.hurl
hurl leaderboard_get.hurl | jq .

# Target to run statistic POST
statistic_post: statistic_post.hurl
hurl statistic_post.hurl | jq .

# Target to run statistic GET
statistic_get: statistic_get.hurl
hurl statistic_get.hurl | jq .

# Target to run all Hurl scripts
run_all: leaderboard_post leaderboard_get statistic_post statistic_get

# Clean target to remove .run files
clean:
rm -f *.run
6 changes: 6 additions & 0 deletions backend/api-spec/leaderboard_get.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GET http://localhost:8080/api/statistics/leaderboard?limit=10

HTTP/1.1 200
[Asserts]
jsonpath "$[0].userId" exists
jsonpath "$[0].speed" exists
12 changes: 12 additions & 0 deletions backend/api-spec/leaderboard_post.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
POST http://localhost:8080/api/statistics/leaderboard
Content-Type: application/json

{
"userId": "user-1",
"speed": 235.5
}

HTTP/1.1 201
GET http://localhost:8080/api/statistics/leaderboard?limit=10

HTTP/1.1 200
11 changes: 11 additions & 0 deletions backend/api-spec/statistic_get.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
GET http://localhost:8080/api/statistics/user123

HTTP/1.1 200
[Asserts]
jsonpath "$.userId" equals "user123"
jsonpath "$.completedDuels" greaterThanOrEqual 0
jsonpath "$.averageSpeed" greaterThanOrEqual 0.0
jsonpath "$.totalReward" greaterThanOrEqual 0.0
jsonpath "$.overallWinsInDuels" greaterThanOrEqual 0
jsonpath "$.maxSpeed" greaterThanOrEqual 0.0
jsonpath "$.topSpeeds" exists
14 changes: 14 additions & 0 deletions backend/api-spec/statistic_post.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
POST http://localhost:8080/api/statistics
Content-Type: application/json

{
"userId": "user123",
"completedDuels": 5,
"averageSpeed": 110.2,
"totalReward": 500.0,
"overallWinsInDuels": 3,
"maxSpeed": 120.5,
"topSpeeds": [120.5, 115.3, 110.0, 105.5, 100.0]
}

HTTP/1.1 201
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,29 @@ import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.core.ZSetOperations
import org.springframework.stereotype.Repository


interface LeaderboardRepository {
fun addUserScore(userId: String, score: Double)
fun getTopUsers(limit: Int): List<LeaderboardItemModel>
}

@Repository
class LeaderboardRepositoryImpl(private val redisTemplate: RedisTemplate<String, LeaderboardItemModel>) :
LeaderboardRepository {
class LeaderboardRepositoryImpl(private val redisTemplate: RedisTemplate<String, String>) : LeaderboardRepository {

private val leaderboardKey = "leaderboard"

private val zSetOps: ZSetOperations<String, String> by lazy {
redisTemplate.opsForZSet()
}

override fun addUserScore(userId: String, score: Double) {
val zSetOps: ZSetOperations<String, LeaderboardItemModel> = redisTemplate.opsForZSet()
val existingScore = zSetOps.score(leaderboardKey, userId)

if (existingScore == null || score > existingScore) {
zSetOps.add(leaderboardKey, LeaderboardItemModel(userId, score), score)
zSetOps.add(leaderboardKey, userId, score)
}
}

override fun getTopUsers(limit: Int): List<LeaderboardItemModel> {
val zSetOps: ZSetOperations<String, LeaderboardItemModel> = redisTemplate.opsForZSet()
val topUsers: MutableSet<ZSetOperations.TypedTuple<LeaderboardItemModel>> =
zSetOps.reverseRangeWithScores(leaderboardKey, 0, (limit - 1).toLong()) ?: return emptyList()

return topUsers.map { it.value!! }
val topUsers = zSetOps.reverseRangeWithScores(leaderboardKey, 0, (limit - 1).toLong())
return topUsers?.map { LeaderboardItemModel(it.value ?: "", it.score ?: 0.0) } ?: emptyList()
}
}
}

0 comments on commit 1b0bb5d

Please sign in to comment.