Skip to content
Draft
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
1,958 changes: 1,958 additions & 0 deletions app/schemas/io.zenandroid.onlinego.data.db.Database/20.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion app/src/main/java/io/zenandroid/onlinego/data/db/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import io.zenandroid.onlinego.data.model.ogs.PuzzleSolution
PuzzleSolution::class,
VisitedPuzzleCollection::class
],
version = 19,
version = 20,
exportSchema = true,
autoMigrations = [
AutoMigration (from = 16, to = 17),
AutoMigration (from = 17, to = 18),
AutoMigration (from = 18, to = 19),
AutoMigration (from = 19, to = 20),
]
)
@TypeConverters(DbTypeConverters::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package io.zenandroid.onlinego.data.model.local

import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import java.util.Locale
import io.zenandroid.onlinego.data.model.ogs.AnalysisMessage
import io.zenandroid.onlinego.data.model.ogs.Chat
import io.zenandroid.onlinego.data.model.ogs.ChatChannel
import io.zenandroid.onlinego.gamelogic.Util

@Entity
data class Message (
Expand All @@ -15,8 +20,10 @@ data class Message (
val date: Long,
@PrimaryKey val chatId: String,
val text: String,
val reviewUrl: String? = null,
@Embedded(prefix = "variation_") val variation: AnalysisMessage? = null,
val seen: Boolean = false

// TODO: PolymorphicJsonAdapterFactory
) {
enum class Type {
MAIN,
Expand All @@ -27,25 +34,82 @@ data class Message (
}

companion object {
fun fromOGSMessage(chat: Chat, gameId: Long?): Message {
fun fromOGSMessage(chat: Chat, gameId: Long?, gameSize: Int?): Message {
val type = when(chat.channel) {
ChatChannel.MAIN -> Type.MAIN
ChatChannel.SPECTATOR -> Type.SPECTATOR
ChatChannel.MALKOVICH -> Type.MALKOVITCH
ChatChannel.PERSONAL -> Type.PERSONAL
}

val text = chat.line.body as? String ?: "Variation (unsupported)"
return Message(
var message = Message(
type = type,
gameId = gameId,
username = chat.line.username,
playerId = chat.line.player_id,
moveNumber = chat.line.move_number,
date = chat.line.date,
chatId = chat.line.chat_id ?: chat.chat_id!!,
text = text
)
text = ""
).run {
when {
chat.line.body is String? -> {
copy(text = chat.line.body)
}
(chat.line.body as? Map<String, Any>) != null -> {
val content = chat.line.body as Map<String, Any>
when (content["type"] as? String) {
"analysis" -> content.let {
val name = it["name"] as? String
val from = (it["from"] as? Double)?.toInt()
val movestring = (it["moves"] as? String) ?: ""
val moves = movestring.lowercase().run {
if (matches("\\w+".toRegex())) {
chunked(2).map {
Util.getCoordinatesFromSGF(it)
}
}
else null
}
if (moves != null) {
copy(
text = "Variation \"${name ?: ""}\": ",
variation = AnalysisMessage(
name = name,
from = from,
moves = moves,
),
)
}
else {
copy(
text = "Variation \"${name ?: ""}\" from move ${from ?: "?"}: ${movestring}",
)
}
}
"review" -> content.let {
val reviewURL = "https://online-go.com/review/${it["review_id"] as? String}"
copy(
text = "Review: ",
reviewUrl = reviewURL,
)
}
"translated" -> content.let {
val bcp46 = Locale.getDefault().getLanguage()
val translated = it[bcp46.lowercase()] as? String
val original = (it["en"] as? String) ?: ""
copy(text = translated ?: original)
}
else -> (content["type"] as? String)?.capitalize()?.let {
copy(text = "${it} (unsupported)")
} ?: copy(text = "(Unsupported message)")
}
}
else -> copy(text = "(Unsupported message)")
}
}

return message
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.zenandroid.onlinego.data.model.ogs

import androidx.room.Entity
import androidx.room.Ignore
import io.zenandroid.onlinego.data.model.Cell

@Entity
data class AnalysisMessage (
var name: String? = null,
var from: Int? = null,
var moves: List<Cell>? = null,
@Ignore var marks: Map<String, String>? = null,
@Ignore var pen_marks: List<Any>? = null,
@Ignore var branch_move: Long? = null, // deprecated
) {
@Ignore val type: String = "analysis"

constructor(name: String?, from: Int?, moves: List<Cell>?) : this() {
this.name = name
this.from = from
this.moves = moves
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.zenandroid.onlinego.data.model.Cell
import io.zenandroid.onlinego.data.model.local.Message
import io.zenandroid.onlinego.data.model.local.Score
import io.zenandroid.onlinego.data.model.local.Time
import io.zenandroid.onlinego.data.model.ogs.AnalysisMessage
import io.zenandroid.onlinego.data.model.ogs.Chat
import io.zenandroid.onlinego.data.model.ogs.GameData
import io.zenandroid.onlinego.data.model.ogs.OGSPlayer
Expand All @@ -18,6 +19,7 @@ import io.zenandroid.onlinego.data.repositories.ChatRepository
import io.zenandroid.onlinego.gamelogic.Util
import io.zenandroid.onlinego.gamelogic.Util.getCurrentUserId
import io.zenandroid.onlinego.utils.addToDisposable
import io.zenandroid.onlinego.utils.json
import io.zenandroid.onlinego.utils.recordException
import org.koin.core.context.GlobalContext.get
import java.io.Closeable
Expand Down Expand Up @@ -65,13 +67,17 @@ class GameConnection(
val undoAccepted: Observable<Int> = undoAcceptedSubject.hide()

var gameAuth: String? = null
var gameSize: Int? = null

private val subscriptions = CompositeDisposable()

init {
gameDataObservable
.retryOnError("gamedata")
.doOnNext{ gameAuth = it.auth }
.doOnNext{
gameAuth = it.auth
gameSize = it.height
}
.subscribe(gameDataSubject::onNext)
.addToDisposable(subscriptions)

Expand All @@ -97,7 +103,7 @@ class GameConnection(
chatObservable
.retryOnError("chat")
.subscribe {
chatRepository.addMessage(Message.fromOGSMessage(it, gameId))
chatRepository.addMessage(Message.fromOGSMessage(it, gameId, gameSize))
}
.addToDisposable(subscriptions)

Expand Down Expand Up @@ -208,12 +214,33 @@ class GameConnection(
}
}

fun sendMessage(message: String, moveNumber: Int) {
fun sendMessage(message: String, moveNumber: Int, type: String = "main") {
socketService.emit("game/chat") {
"body" - message
"game_id" - gameId
"move_number" - moveNumber
"type" - "main"
"type" - type
}
}

fun sendAnalysisMessage(message: AnalysisMessage, moveNumber: Int, type: String = "main") {
val message_moves = message.moves
?.joinToString(separator = "") {
Util.getSGFCoordinates(it)
}
socketService.emit("game/chat") {
"body" - json {
"type" - message.type
"name" - message.name
"from" - message.from
"moves" - message_moves
"marks" - message.marks
"pen_marks" - message.pen_marks
"branch_move" - message.branch_move
}
"game_id" - gameId
"move_number" - moveNumber
"type" - type
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ class ChatRepository(

fun fetchRecentChatMessages() {
restApi.getMessages(lastRESTFetchedChatId)
.map { it.map { Message.fromOGSMessage(it, it.game_id) } }
.map { it.map {
val game_size = it.game_id?.let(gameDao::getGameMaybe)?.blockingGet()?.height
Message.fromOGSMessage(it, it.game_id, game_size)
} }
.subscribe(
gameDao::insertMessagesFromRest,
{ onError(it, "fetchRecentChatMessages") }
Expand Down Expand Up @@ -94,4 +97,4 @@ class ChatRepository(
Log.e("ChatRepository", message, t)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class GameFragment : Fragment() {
OnlineGoTheme {
GameScreen(
state = state,
analysisMode = viewModel.analyzeMode,
onBack = ::onBackPressed,
onUserAction = viewModel::onUserAction
)
Expand Down
20 changes: 16 additions & 4 deletions app/src/main/java/io/zenandroid/onlinego/ui/screens/game/GameUI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import io.zenandroid.onlinego.data.model.ogs.ChatChannel
import io.zenandroid.onlinego.data.model.Cell
import io.zenandroid.onlinego.data.model.Position
import io.zenandroid.onlinego.data.model.StoneType
Expand Down Expand Up @@ -92,6 +93,7 @@ import io.zenandroid.onlinego.ui.screens.game.UserAction.GameOverDialogNextGame
import io.zenandroid.onlinego.ui.screens.game.UserAction.GameOverDialogQuickReplay
import io.zenandroid.onlinego.ui.screens.game.UserAction.KOMoveDialogDismiss
import io.zenandroid.onlinego.ui.screens.game.UserAction.OpenInBrowser
import io.zenandroid.onlinego.ui.screens.game.UserAction.OpenVariation
import io.zenandroid.onlinego.ui.screens.game.UserAction.OpponentUndoRequestAccepted
import io.zenandroid.onlinego.ui.screens.game.UserAction.OpponentUndoRequestRejected
import io.zenandroid.onlinego.ui.screens.game.UserAction.PassDialogConfirm
Expand All @@ -103,6 +105,7 @@ import io.zenandroid.onlinego.ui.screens.game.UserAction.RetryDialogDismiss
import io.zenandroid.onlinego.ui.screens.game.UserAction.RetryDialogRetry
import io.zenandroid.onlinego.ui.screens.game.UserAction.UserUndoDialogConfirm
import io.zenandroid.onlinego.ui.screens.game.UserAction.UserUndoDialogDismiss
import io.zenandroid.onlinego.ui.screens.game.UserAction.VariationSend
import io.zenandroid.onlinego.ui.screens.game.UserAction.WhitePlayerClicked
import io.zenandroid.onlinego.ui.screens.game.composables.ChatDialog
import io.zenandroid.onlinego.ui.screens.game.composables.PlayerCard
Expand All @@ -111,6 +114,7 @@ import io.zenandroid.onlinego.ui.theme.OnlineGoTheme

@Composable
fun GameScreen(state: GameState,
analysisMode: Boolean,
onUserAction: ((UserAction) -> Unit),
onBack: (() -> Unit),
) {
Expand Down Expand Up @@ -230,8 +234,12 @@ fun GameScreen(state: GameState,
if(state.chatDialogShowing) {
ChatDialog(
messages = state.messages,
game = state.position!!,
inAnalysisMode = analysisMode,
onVariation = { onUserAction(OpenVariation(it)) },
onDialogDismiss = { onUserAction(ChatDialogDismiss) },
onSendMessage = { onUserAction(ChatSend(it)) }
onSendMessage = { m, c -> onUserAction(ChatSend(m, c)) },
onSendVariation = { onUserAction(VariationSend(it)) },
)
}
if (state.retryMoveDialogShowing) {
Expand Down Expand Up @@ -739,7 +747,7 @@ fun Preview() {
blackStartTimer = null,
timeLeft = 1000,
),
), {}, {},
), false, {}, {},
)
}
}
Expand Down Expand Up @@ -782,7 +790,7 @@ fun Preview1() {
blackStartTimer = null,
timeLeft = 1000,
),
), {}, {},
), false, {}, {},
)
}
}
Expand Down Expand Up @@ -826,6 +834,7 @@ fun Preview2() {
),
bottomText = "Submitting move",
),
false,
{}, {},
)
}
Expand Down Expand Up @@ -870,6 +879,7 @@ fun Preview3() {
bottomText = "Submitting move",
retryMoveDialogShowing = true,
),
false,
{}, {},
)
}
Expand Down Expand Up @@ -916,6 +926,7 @@ fun Preview4() {
showPlayers = false,
showAnalysisPanel = true,
),
false,
{}, {},
)
}
Expand Down Expand Up @@ -972,7 +983,8 @@ fun Preview5() {
}
),
),
false,
{}, {},
)
}
}
}
Loading