-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First implementation of multiple beatmap mirror support
- Loading branch information
Showing
6 changed files
with
197 additions
and
104 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,71 @@ | ||
package com.reco1l.osu.beatmaplisting | ||
|
||
import org.json.JSONArray | ||
import ru.nsu.ccfit.zuev.osu.RankedStatus | ||
|
||
import androidx.annotation.DrawableRes | ||
import com.reco1l.osu.beatmaplisting.mirrors.OsuDirectDownloadRequestModel | ||
import com.reco1l.osu.beatmaplisting.mirrors.OsuDirectPreviewRequestModel | ||
import com.reco1l.osu.beatmaplisting.mirrors.OsuDirectSearchRequestModel | ||
import com.reco1l.osu.beatmaplisting.mirrors.OsuDirectSearchResponseModel | ||
import ru.nsu.ccfit.zuev.osuplus.R | ||
|
||
/** | ||
* Defines an action to be performed on a mirror API. | ||
* Defines a beatmap mirror API and its actions. | ||
*/ | ||
data class MirrorAction<R, M>( | ||
enum class BeatmapMirror( | ||
|
||
/** | ||
* The action API endpoint. | ||
* The home URL of the beatmap mirror where the user will be redirected to when | ||
* clicking on the logo. | ||
*/ | ||
// TODO replace with a request creation function, some APIs have different query arguments. | ||
val endpoint: String, | ||
val homeUrl: String, | ||
|
||
/** | ||
* A function to map the response into a model. | ||
* The description of the beatmap mirror. | ||
*/ | ||
val mapResponse: (R) -> M | ||
val description: String, | ||
|
||
) | ||
/** | ||
* The resource ID of the logo image to be displayed in the UI. | ||
*/ | ||
@DrawableRes | ||
val logoResource: Int, | ||
|
||
/** | ||
* Defines a beatmap mirror API and its actions. | ||
*/ | ||
enum class BeatmapMirror( | ||
|
||
// Actions / Endpoints | ||
|
||
/** | ||
* The search query action. | ||
*/ | ||
val search: MirrorAction<JSONArray, MutableList<BeatmapSetModel>>, | ||
val search: BeatmapMirrorActionWithResponse<BeatmapMirrorSearchRequestModel, BeatmapMirrorSearchResponseModel>, | ||
|
||
val downloadEndpoint: (Long) -> String, | ||
/** | ||
* The download action. | ||
*/ | ||
val download: BeatmapMirrorAction<BeatmapMirrorDownloadRequestModel>, | ||
|
||
val previewEndpoint: (Long) -> String, | ||
/** | ||
* The music preview action. | ||
*/ | ||
val preview: BeatmapMirrorAction<BeatmapMirrorPreviewRequestModel>, | ||
|
||
) { | ||
) { | ||
|
||
/** | ||
* osu.direct beatmap mirror. | ||
* | ||
* [See documentation](https://osu.direct/api/docs) | ||
*/ | ||
OSU_DIRECT( | ||
search = MirrorAction( | ||
endpoint = "https://osu.direct/api/v2/search", | ||
mapResponse = { array -> | ||
|
||
MutableList(array.length()) { index -> | ||
|
||
val json = array.getJSONObject(index) | ||
|
||
BeatmapSetModel( | ||
id = json.getLong("id"), | ||
title = json.getString("title"), | ||
titleUnicode = json.getString("title_unicode"), | ||
artist = json.getString("artist"), | ||
artistUnicode = json.getString("artist_unicode"), | ||
status = RankedStatus.valueOf(json.getInt("ranked")), | ||
creator = json.getString("creator"), | ||
thumbnail = json.optJSONObject("covers")?.optString("card"), | ||
beatmaps = json.getJSONArray("beatmaps").let { | ||
|
||
MutableList(it.length()) { i -> | ||
|
||
val obj = it.getJSONObject(i) | ||
|
||
BeatmapModel( | ||
id = obj.getLong("id"), | ||
version = obj.getString("version"), | ||
starRating = obj.getDouble("difficulty_rating"), | ||
ar = obj.getDouble("ar"), | ||
cs = obj.getDouble("cs"), | ||
hp = obj.getDouble("drain"), | ||
od = obj.getDouble("accuracy"), | ||
bpm = obj.getDouble("bpm"), | ||
lengthSec = obj.getLong("hit_length"), | ||
circleCount = obj.getInt("count_circles"), | ||
sliderCount = obj.getInt("count_sliders"), | ||
spinnerCount = obj.getInt("count_spinners") | ||
) | ||
|
||
}.sortedBy(BeatmapModel::starRating) | ||
} | ||
) | ||
} | ||
homeUrl = "https://osu.direct", | ||
description = "osu.direct", | ||
logoResource = R.drawable.powered_by_osudirect, | ||
|
||
} | ||
search = BeatmapMirrorActionWithResponse( | ||
request = OsuDirectSearchRequestModel(), | ||
response = OsuDirectSearchResponseModel(), | ||
), | ||
downloadEndpoint = { "https://osu.direct/api/d/$it" }, | ||
previewEndpoint = { "https://osu.direct/api/media/preview/$it" }, | ||
); | ||
download = BeatmapMirrorAction(OsuDirectDownloadRequestModel()), | ||
preview = BeatmapMirrorAction(OsuDirectPreviewRequestModel()), | ||
) | ||
|
||
} | ||
|
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,24 @@ | ||
package com.reco1l.osu.beatmaplisting | ||
|
||
/** | ||
* Defines an action to be performed on a mirror API. | ||
*/ | ||
open class BeatmapMirrorAction<RequestModel>( | ||
|
||
/** | ||
* The action API endpoint. | ||
*/ | ||
val request: RequestModel | ||
|
||
) | ||
|
||
class BeatmapMirrorActionWithResponse<RequestModel, ResponseModel>( | ||
|
||
request: RequestModel, | ||
|
||
/** | ||
* The action response mapping. | ||
*/ | ||
val response: ResponseModel | ||
|
||
) : BeatmapMirrorAction<RequestModel>(request) |
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,38 @@ | ||
package com.reco1l.osu.beatmaplisting | ||
|
||
import okhttp3.HttpUrl | ||
|
||
|
||
// Request | ||
|
||
fun interface BeatmapMirrorSearchRequestModel { | ||
/** | ||
* @param query The search query. | ||
* @param offset The search result offset. | ||
*/ | ||
operator fun invoke(query: String, offset: Int): HttpUrl | ||
} | ||
|
||
fun interface BeatmapMirrorDownloadRequestModel { | ||
/** | ||
* @param beatmapSetId The beatmap set ID. | ||
*/ | ||
operator fun invoke(beatmapSetId: Long): HttpUrl | ||
} | ||
|
||
fun interface BeatmapMirrorPreviewRequestModel { | ||
/** | ||
* @param beatmapSetId The beatmap set ID. | ||
*/ | ||
operator fun invoke(beatmapSetId: Long): HttpUrl | ||
} | ||
|
||
|
||
// Response | ||
|
||
fun interface BeatmapMirrorSearchResponseModel { | ||
/** | ||
* @return The list of beatmap sets. | ||
*/ | ||
operator fun invoke(response: Any): MutableList<BeatmapSetModel> | ||
} |
Oops, something went wrong.