Skip to content

Commit 44b0790

Browse files
author
Ruslan Molchanov
committed
Merge branch 'change-db' into 'master'
Change db See merge request os-incubator/kicker/kicker-v2!61
2 parents a2f786c + 8ef738b commit 44b0790

21 files changed

Lines changed: 217 additions & 120 deletions

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- History of stats and relations after every game
10+
11+
### Changed
12+
- PageRequest
813

914

1015
## [1.2.2] - 2019-04-10

backend/src/main/kotlin/io/zensoft/kicker/annotation/Contains.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import kotlin.reflect.KClass
1313
@Constraint(validatedBy = [(ContainsValidator::class)])
1414
annotation class Contains(
1515
val value: String,
16-
val set: String,
16+
val map: String,
1717
val message: String = "Collection is not contains value",
1818
val groups: Array<KClass<out Any>> = [],
1919
val payload: Array<KClass<out Payload>> = []

backend/src/main/kotlin/io/zensoft/kicker/annotation/validator/ContainsValidator.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ import javax.validation.ConstraintValidatorContext
1111
class ContainsValidator : ConstraintValidator<Contains, Any> {
1212

1313
private lateinit var valueFieldName: String
14-
private lateinit var setFieldName: String
14+
private lateinit var mapFieldName: String
1515
private lateinit var message: String
1616

1717

1818
override fun initialize(constraintAnnotation: Contains) {
1919
valueFieldName = constraintAnnotation.value
20-
setFieldName = constraintAnnotation.set
20+
mapFieldName = constraintAnnotation.map
2121
message = constraintAnnotation.message
2222
}
2323

2424
override fun isValid(clazz: Any, context: ConstraintValidatorContext): Boolean {
2525
val value = ConstraintValidatorUtils.getValueFromField(clazz, valueFieldName)
26-
val set = ConstraintValidatorUtils.getValueFromField(clazz, setFieldName) as Set<*>
26+
val map = ConstraintValidatorUtils.getValueFromField(clazz, mapFieldName) as Map<*, *>
2727

28-
val condition = set.contains(value)
28+
val condition = map.contains(value)
2929

3030
return when (condition) {
3131
true -> true
3232
false -> {
33-
ConstraintValidatorUtils.buildConstraintViolation(context, message, setFieldName)
33+
ConstraintValidatorUtils.buildConstraintViolation(context, message, mapFieldName)
3434
false
3535
}
3636
}

backend/src/main/kotlin/io/zensoft/kicker/component/listener/PlayerRelationsListener.kt

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.zensoft.kicker.component.listener
22

33
import io.zensoft.kicker.model.Game
4+
import io.zensoft.kicker.model.PlayerRelations
45
import io.zensoft.kicker.service.PlayerRelationsService
56
import org.springframework.context.event.EventListener
67
import org.springframework.stereotype.Component
8+
import org.springframework.transaction.annotation.Transactional
79

810
/**
911
* @author Yauheni Efimenko
@@ -14,18 +16,38 @@ class PlayerRelationsListener(
1416
private val playerRelationsService: PlayerRelationsService
1517
) {
1618

19+
@Transactional
1720
@EventListener
1821
fun handleRegistrationGame(game: Game) {
1922
with(game) {
2023
val winner1ToWinner2 = playerRelationsService.getByPlayerAndPartner(winner1.id, winner2.id)
21-
val winner2ToWinner3 = playerRelationsService.getByPlayerAndPartner(winner2.id, winner1.id)
24+
val winner2ToWinner1 = playerRelationsService.getByPlayerAndPartner(winner2.id, winner1.id)
2225
val loser1ToLoser2 = playerRelationsService.getByPlayerAndPartner(loser1.id, loser2.id)
2326
val loser2ToLoser1 = playerRelationsService.getByPlayerAndPartner(loser2.id, loser1.id)
2427

25-
winner1ToWinner2.addWin()
26-
winner2ToWinner3.addWin()
27-
loser1ToLoser2.addLoss()
28-
loser2ToLoser1.addLoss()
28+
if (null == winner1ToWinner2) {
29+
playerRelationsService.add(PlayerRelations(winner1, winner2, game, true))
30+
} else {
31+
playerRelationsService.add(PlayerRelations(winner1ToWinner2, game, true))
32+
}
33+
34+
if (null == winner2ToWinner1) {
35+
playerRelationsService.add(PlayerRelations(winner2, winner1, game, true))
36+
} else {
37+
playerRelationsService.add(PlayerRelations(winner2ToWinner1, game, true))
38+
}
39+
40+
if (null == loser1ToLoser2) {
41+
playerRelationsService.add(PlayerRelations(loser1, loser2, game, false))
42+
} else {
43+
playerRelationsService.add(PlayerRelations(loser1ToLoser2, game, false))
44+
}
45+
46+
if (null == loser2ToLoser1) {
47+
playerRelationsService.add(PlayerRelations(loser2, loser1, game, false))
48+
} else {
49+
playerRelationsService.add(PlayerRelations(loser2ToLoser1, game, false))
50+
}
2951
}
3052
}
3153

backend/src/main/kotlin/io/zensoft/kicker/component/listener/PlayerStatsListener.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.zensoft.kicker.service.PlayerToGameService
1010
import io.zensoft.kicker.utils.RatingUtils
1111
import org.springframework.context.event.EventListener
1212
import org.springframework.stereotype.Component
13+
import org.springframework.transaction.annotation.Transactional
1314

1415
/**
1516
* @author Yauheni Efimenko
@@ -23,6 +24,7 @@ class PlayerStatsListener(
2324
private val playerSettingsProperties: PlayerSettingsProperties
2425
) {
2526

27+
@Transactional
2628
@EventListener
2729
fun handleRegistrationGame(game: Game) {
2830
with(game) {
@@ -51,14 +53,14 @@ class PlayerStatsListener(
5153

5254
private fun updatePlayerStats(stats: PlayerStats, game: Game, won: Boolean, delta: Double) {
5355
if (won) {
54-
stats.addGame(won, game.losersGoals, gamesSettingsProperties.defaultMaxScore!!, delta)
56+
playerStatsService.add(PlayerStats(stats, game, won, game.losersGoals,
57+
gamesSettingsProperties.defaultMaxScore!!, delta, playerSettingsProperties.countGames!!))
5558
} else {
56-
stats.addGame(won, gamesSettingsProperties.defaultMaxScore!!, game.losersGoals, delta)
59+
playerStatsService.add(PlayerStats(stats, game, won, gamesSettingsProperties.defaultMaxScore!!,
60+
game.losersGoals, delta, playerSettingsProperties.countGames!!))
5761
}
58-
stats.active = stats.rated >= playerSettingsProperties.countGames!!
5962

60-
playerStatsService.save(stats)
61-
playerToGameService.save(PlayerToGame(stats.player, game, won, delta))
63+
playerToGameService.add(PlayerToGame(stats.player, game, won, delta))
6264
}
6365

64-
}
66+
}

backend/src/main/kotlin/io/zensoft/kicker/domain/PageRequest.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import javax.validation.constraints.Min
99
/**
1010
* @author Yauheni Efimenko
1111
*/
12-
@Contains(value = "sortBy", set = "setSortBy", message = "Collection is not contains value for sorting")
12+
@Contains(value = "sortBy", map = "mapSortBy", message = "Collection is not contains value for sorting")
1313
open class PageRequest(
1414
@field:Min(value = 0) var offset: Int = 0,
1515
@field:Min(value = 1) @field:Max(100) var limit: Int = 10,
1616
var sortDirection: Sort.Direction = Sort.Direction.ASC,
17-
var sortBy: String = ID_FIELD,
18-
private val setSortBy: Set<String> = setOf(ID_FIELD)
17+
var sortBy: String = ID_FIELD.first,
18+
private val mapSortBy: Map<String, String> = mapOf(ID_FIELD)
1919
) : Pageable {
2020

2121
companion object {
22-
const val ID_FIELD = "id"
22+
val ID_FIELD = "id" to "id"
2323
}
2424

2525

@@ -28,25 +28,25 @@ open class PageRequest(
2828
override fun hasPrevious(): Boolean = offset > 0
2929

3030
override fun getSort(): Sort {
31-
if (sortBy == ID_FIELD) {
32-
return Sort(sortDirection, ID_FIELD)
31+
if (sortBy == ID_FIELD.first) {
32+
return Sort(sortDirection, ID_FIELD.second)
3333
}
34-
return Sort(sortDirection, sortBy, ID_FIELD)
34+
return Sort(sortDirection, mapSortBy[sortBy], ID_FIELD.second)
3535
}
3636

37-
override fun next(): Pageable = PageRequest(offset + limit, limit, sortDirection, sortBy, setSortBy)
37+
override fun next(): Pageable = PageRequest(offset + limit, limit, sortDirection, sortBy, mapSortBy)
3838

3939
override fun getPageSize(): Int = limit
4040

4141
override fun getOffset(): Long = offset.toLong()
4242

43-
override fun first(): Pageable = PageRequest(0, limit, sortDirection, sortBy, setSortBy)
43+
override fun first(): Pageable = PageRequest(0, limit, sortDirection, sortBy, mapSortBy)
4444

4545
private fun previous(): Pageable {
4646
if (hasPrevious()) {
4747
var newOffset = this.offset - limit
4848
if (newOffset < 0) newOffset = 0
49-
return PageRequest(newOffset, limit, sortDirection, sortBy, setSortBy)
49+
return PageRequest(newOffset, limit, sortDirection, sortBy, mapSortBy)
5050
}
5151
return first()
5252
}
@@ -63,7 +63,7 @@ open class PageRequest(
6363
if (limit != other.limit) return false
6464
if (sortDirection != other.sortDirection) return false
6565
if (sortBy != other.sortBy) return false
66-
if (setSortBy != other.setSortBy) return false
66+
if (mapSortBy != other.mapSortBy) return false
6767

6868
return true
6969
}
@@ -73,7 +73,7 @@ open class PageRequest(
7373
result = 31 * result + limit
7474
result = 31 * result + sortDirection.hashCode()
7575
result = 31 * result + sortBy.hashCode()
76-
result = 31 * result + setSortBy.hashCode()
76+
result = 31 * result + mapSortBy.hashCode()
7777
return result
7878
}
7979

backend/src/main/kotlin/io/zensoft/kicker/domain/model/game/GamePageRequest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package io.zensoft.kicker.domain.model.game
22

33
import io.zensoft.kicker.domain.PageRequest
44

5-
class GamePageRequest : PageRequest(setSortBy = setOf(ID_FIELD, DATE_FIELD)) {
5+
class GamePageRequest : PageRequest(mapSortBy = mapOf(ID_FIELD, DATE_FIELD)) {
66

77
companion object {
8-
const val DATE_FIELD = "date"
8+
val DATE_FIELD = "date" to "date"
99
}
1010

11-
}
11+
}

backend/src/main/kotlin/io/zensoft/kicker/domain/model/playerRelations/PlayerRelationsPageRequest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import io.zensoft.kicker.domain.PageRequest
55
/**
66
* @author Yauheni Efimenko
77
*/
8-
class PlayerRelationsPageRequest : PageRequest(setSortBy = setOf(ID_FIELD, WINNING_PERCENTAGE_FIELD)) {
8+
class PlayerRelationsPageRequest : PageRequest(mapSortBy = mapOf(ID_FIELD, WINNING_PERCENTAGE_FIELD)) {
99

1010
companion object {
11-
const val WINNING_PERCENTAGE_FIELD = "winningPercentage"
11+
val WINNING_PERCENTAGE_FIELD = "winningPercentage" to "winning_percentage"
1212
}
1313

14-
}
14+
}

backend/src/main/kotlin/io/zensoft/kicker/domain/model/playerStats/PlayerStatsPageRequest.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package io.zensoft.kicker.domain.model.playerStats
22

33
import io.zensoft.kicker.domain.PageRequest
44

5-
class PlayerStatsPageRequest : PageRequest(setSortBy = setOf(ID_FIELD, RATING_FIELD, COUNT_GAMES_FIELD, RATED_FIELD,
6-
LONGEST_WINNING_STREAK, LONGEST_LOSSES_STREAK, WINNING_PERCENTAGE)) {
5+
class PlayerStatsPageRequest : PageRequest(mapSortBy = mapOf(ID_FIELD, RATING_FIELD, COUNT_GAMES_FIELD, RATED_FIELD,
6+
LONGEST_WINNING_STREAK_FIELD, LONGEST_LOSSES_STREAK_FIELD, WINNING_PERCENTAGE_FIELD)) {
77

88
companion object {
9-
const val RATING_FIELD = "rating"
10-
const val COUNT_GAMES_FIELD = "countGames"
11-
const val RATED_FIELD = "rated"
12-
const val LONGEST_WINNING_STREAK = "longestWinningStreak"
13-
const val LONGEST_LOSSES_STREAK = "longestLossesStreak"
14-
const val WINNING_PERCENTAGE = "winningPercentage"
9+
val RATING_FIELD = "rating" to "rating"
10+
val COUNT_GAMES_FIELD = "countGames" to "count_games"
11+
val RATED_FIELD = "rated" to "rated"
12+
val LONGEST_WINNING_STREAK_FIELD = "longestWinningStreak" to "longest_winning_streak"
13+
val LONGEST_LOSSES_STREAK_FIELD = "longestLossesStreak" to "longest_losses_streak"
14+
val WINNING_PERCENTAGE_FIELD = "winningPercentage" to "winning_percentage"
1515
}
1616

17-
}
17+
}

backend/src/main/kotlin/io/zensoft/kicker/model/Player.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package io.zensoft.kicker.model
33
import io.zensoft.kicker.model.base.BaseModel
44
import org.springframework.security.core.GrantedAuthority
55
import org.springframework.security.core.userdetails.UserDetails
6-
import javax.persistence.*
6+
import javax.persistence.Column
7+
import javax.persistence.Entity
8+
import javax.persistence.Table
79

810
/**
911
* @author Yauheni Efimenko
@@ -26,9 +28,6 @@ class Player(
2628
@Column(name = "icon_path")
2729
var iconPath: String? = null
2830

29-
@OneToOne(mappedBy = "player", fetch = FetchType.LAZY)
30-
val playerStats: PlayerStats? = null
31-
3231

3332
override fun getAuthorities(): MutableCollection<out GrantedAuthority> = mutableListOf()
3433

0 commit comments

Comments
 (0)