@@ -164,18 +164,20 @@ interface FavListItemDao {
164
164
)
165
165
fun leastTrained (favListId : Int ): FavListItem ?
166
166
167
+ // Sort the second match by the closest neighbor, IE abs(difference)
167
168
@Query(
168
169
"""
169
170
SELECT * FROM FavListItem
170
171
WHERE fav_list_id = :favListId
171
- AND id <> :firstFavListItemId
172
- ORDER BY RANDOM()
172
+ AND id <> :firstItemId
173
+ ORDER BY ABS(glicko_rating - :firstGlickoRating), RANDOM()
173
174
LIMIT 1
174
175
""" ,
175
176
)
176
- fun randomAndNot (
177
+ fun closestMatch (
177
178
favListId : Int ,
178
- firstFavListItemId : Int ,
179
+ firstItemId : Int ,
180
+ firstGlickoRating : Float ,
179
181
): FavListItem ?
180
182
181
183
@Insert(entity = FavListItem ::class , onConflict = OnConflictStrategy .IGNORE )
@@ -218,10 +220,11 @@ class FavListItemRepository(
218
220
219
221
fun leastTrained (favListId : Int ) = favListItemDao.leastTrained(favListId)
220
222
221
- fun randomAndNot (
223
+ fun closestMatch (
222
224
favListId : Int ,
223
- favListItemId : Int ,
224
- ) = favListItemDao.randomAndNot(favListId, favListItemId)
225
+ firstItemId : Int ,
226
+ firstGlickoRating : Float ,
227
+ ) = favListItemDao.closestMatch(favListId, firstItemId, firstGlickoRating)
225
228
226
229
fun insert (favListItem : FavListItemInsert ) = favListItemDao.insert(favListItem)
227
230
@@ -249,10 +252,11 @@ class FavListItemViewModel(
249
252
250
253
fun leastTrained (favListId : Int ) = repository.leastTrained(favListId)
251
254
252
- fun randomAndNot (
255
+ fun closestMatch (
253
256
favListId : Int ,
254
- favListItemId : Int ,
255
- ) = repository.randomAndNot(favListId, favListItemId)
257
+ firstItemId : Int ,
258
+ firstGlickoRating : Float ,
259
+ ) = repository.closestMatch(favListId, firstItemId, firstGlickoRating)
256
260
257
261
fun insert (favListItem : FavListItemInsert ) = repository.insert(favListItem)
258
262
0 commit comments