Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Challenge browser #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ dependencies {

implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'

implementation 'com.github.sephiroth74:NumberSlidingPicker:v1.0.3'

testImplementation 'com.nhaarman:mockito-kotlin-kt1.1:1.6.0'
testImplementation 'junit:junit:4.13.2'
testImplementation "io.insert-koin:koin-test-junit4:$koin_version"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.zenandroid.onlinego.data.model.ogs

import io.zenandroid.onlinego.data.ogs.TimeControl
import io.zenandroid.onlinego.utils.formatRank

/**
* Created by alex on 08/12/2017.
*/
data class SeekGraphChallenge (
var challenge_id: Int? = null,
var challenge_id: Long? = null,
var game_started: Boolean = false,
var delete: Int? = null,
var name: String = "",
var username: String = "",
Expand All @@ -15,10 +17,10 @@ data class SeekGraphChallenge (
var min_rank: Double = 0.0,
var max_rank: Double = 100.0,
var handicap: Int = 0,
var timePerMove: Int = 0,
var time_per_move: Double? = null,
var width: Int = 0,
var height: Int = 0//,
// var timeControlParameters: JSONObject? = null
var height: Int = 0,
var time_control_parameters: TimeControl? = null
) {
override fun toString(): String {
return "$username (${formatRank(rank)})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ interface OGSRestAPI {
@Query("ended__gt") ended: String,
@Query("page") page: Int = 1): Single<PagedResult<OGSGame>>

@POST("/api/v1/challenges/{challenge_id}/accept")
fun acceptOpenChallenge(@Path("challenge_id") id: Long): Completable

@GET("/api/v1/me/challenges?page_size=100")
fun fetchChallenges(): Single<PagedResult<OGSChallenge>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ class OGSRestService(
}
}

fun acceptOpenChallenge(id: Long): Completable =
restApi.acceptOpenChallenge(id)

fun acceptChallenge(id: Long): Completable =
restApi.acceptChallenge(id)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.zenandroid.onlinego.data.repositories

import android.util.Log
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.BehaviorSubject
import io.zenandroid.onlinego.data.model.ogs.SeekGraphChallenge
import io.zenandroid.onlinego.data.ogs.OGSWebSocketService
import io.zenandroid.onlinego.utils.addToDisposable

class SeekGraphRepository(
private val socketService: OGSWebSocketService
): SocketConnectedRepository {

private val subscriptions = CompositeDisposable()
private var challenges = emptyMap<Long, SeekGraphChallenge>()
val challengesSubject = BehaviorSubject.create<List<SeekGraphChallenge>>()

override fun onSocketConnected() {
socketService.connectToChallenges()
.subscribeOn(Schedulers.io())
.subscribe(this::storeChallenge) { Log.e("SeekGraphRepository", it.toString()) }
.addToDisposable(subscriptions)
}

private fun storeChallenge(challenge: SeekGraphChallenge) {
if (challenge.game_started) {
} else if (challenge.delete != null) {
challenges -= challenge.challenge_id!!
} else {
challenges += Pair(challenge.challenge_id!!, challenge)
}
challengesSubject.onNext(challenges.values.toList())
}

override fun onSocketDisconnected() {
subscriptions.clear()
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/io/zenandroid/onlinego/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import io.zenandroid.onlinego.data.repositories.FinishedGamesRepository
import io.zenandroid.onlinego.data.repositories.JosekiRepository
import io.zenandroid.onlinego.data.repositories.PlayersRepository
import io.zenandroid.onlinego.data.repositories.PuzzleRepository
import io.zenandroid.onlinego.data.repositories.SeekGraphRepository
import io.zenandroid.onlinego.data.repositories.ServerNotificationsRepository
import io.zenandroid.onlinego.data.repositories.SettingsRepository
import io.zenandroid.onlinego.data.repositories.TutorialsRepository
Expand Down Expand Up @@ -88,6 +89,7 @@ private val repositoriesModule = module {
get<ChallengesRepository>(),
get<FinishedGamesRepository>(),
get<ChatRepository>(),
get<SeekGraphRepository>(),
get<ServerNotificationsRepository>(),
get<ClockDriftRepository>(),
get<TutorialsRepository>()
Expand All @@ -103,6 +105,7 @@ private val repositoriesModule = module {
single { JosekiRepository(get(), get()) }
single { PuzzleRepository(get(), get()) }
single { PlayersRepository(get(), get(), get()) }
single { SeekGraphRepository(get()) }
single { ServerNotificationsRepository(get()) }
single { SettingsRepository() }
single { UserSessionRepository() }
Expand Down
Loading