-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #612 implement GameSimilarityService * #612 add similarity update to create game function * #612 add similar games to game dto * #612 update getFieldValues function * #612 add updateSimilarGamesFields function * #612 add updateSimilarGamesField endpoint * #612 change set to mutableSet
- Loading branch information
Showing
8 changed files
with
101 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
app/backend/src/main/kotlin/com/gamelounge/backend/service/GameSimilarityService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.gamelounge.backend.service | ||
|
||
import com.gamelounge.backend.entity.Game | ||
import com.gamelounge.backend.repository.GameRepository | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.runBlocking | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class GameSimilarityService( | ||
private val gameRepository: GameRepository | ||
) { | ||
|
||
fun updateAllSimilarGamesFields() = runBlocking{ | ||
val allGames = gameRepository.findAll() | ||
|
||
val deferredUpdates = allGames.map { game -> | ||
async(Dispatchers.IO) { | ||
updateSimilarGamesField(game, allGames) | ||
gameRepository.save(game) | ||
} | ||
} | ||
deferredUpdates.awaitAll() | ||
} | ||
|
||
fun updateSimilarGamesField(game: Game){ // This could be wrong | ||
val allGames = gameRepository.findAll() | ||
val similarGames = getSimilarGames(game, allGames) | ||
|
||
game.similarGames = similarGames | ||
} | ||
|
||
fun updateSimilarGamesField(game: Game, allGames: List<Game>){ // This could be wrong | ||
val similarGames = getSimilarGames(game, allGames) | ||
|
||
game.similarGames = similarGames | ||
} | ||
|
||
fun getSimilarGames(referenceGame: Game, allGames: List<Game>): List<Game>{ | ||
val similarityMap = allGames | ||
.filter { it != referenceGame } | ||
.map { game -> game to calculateJaccardSimilarity(referenceGame, game) } | ||
.sortedByDescending { it.second } | ||
|
||
return similarityMap.take(7).map { it.first } | ||
} | ||
|
||
fun calculateJaccardSimilarity(referenceGame: Game, targetGame: Game): Int { | ||
val game1FieldValues = getFieldValuesAsSet(referenceGame) | ||
val game2FieldValues = getFieldValuesAsSet(targetGame) | ||
|
||
return game1FieldValues.intersect(game2FieldValues).size | ||
} | ||
|
||
fun getFieldValuesAsSet(game: Game): HashSet<String>{ | ||
val valuesSet = hashSetOf<String>() | ||
|
||
valuesSet.addAll(game.genres.map { it.name }) | ||
valuesSet.addAll(game.platforms.map { it.name }) | ||
valuesSet.add(game.playerNumber.name) | ||
valuesSet.add(game.universe.name) | ||
valuesSet.add(game.mechanics.name) | ||
|
||
return valuesSet | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters