Skip to content

Commit

Permalink
advanced message sending
Browse files Browse the repository at this point in the history
  • Loading branch information
bqv committed Oct 20, 2023
1 parent 8b03856 commit 2988ccd
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import java.util.Locale
import io.zenandroid.onlinego.data.model.Cell
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
Expand Down Expand Up @@ -113,21 +113,3 @@ data class Message (
}
}
}

@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
@@ -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 @@ -212,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 @@ -68,6 +68,7 @@ class GameFragment : Fragment() {
OnlineGoTheme {
GameScreen(
state = state,
analysisMode = viewModel.analyzeMode,
onBack = ::onBackPressed,
onUserAction = viewModel::onUserAction
)
Expand Down
17 changes: 13 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 @@ -104,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 @@ -112,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 @@ -232,9 +235,11 @@ fun GameScreen(state: GameState,
ChatDialog(
messages = state.messages,
game = state.position!!,
inAnalysisMode = analysisMode,
onVariation = { onUserAction(OpenVariation(it)) },
onDialogDismiss = { onUserAction(ChatDialogDismiss) },
onSendMessage = { onUserAction(ChatSend(it)) },
onVariation = { onUserAction(OpenVariation(it)) }
onSendMessage = { m, c -> onUserAction(ChatSend(m, c)) },
onSendVariation = { onUserAction(VariationSend(it)) },
)
}
if (state.retryMoveDialogShowing) {
Expand Down Expand Up @@ -742,7 +747,7 @@ fun Preview() {
blackStartTimer = null,
timeLeft = 1000,
),
), {}, {},
), false, {}, {},
)
}
}
Expand Down Expand Up @@ -785,7 +790,7 @@ fun Preview1() {
blackStartTimer = null,
timeLeft = 1000,
),
), {}, {},
), false, {}, {},
)
}
}
Expand Down Expand Up @@ -829,6 +834,7 @@ fun Preview2() {
),
bottomText = "Submitting move",
),
false,
{}, {},
)
}
Expand Down Expand Up @@ -873,6 +879,7 @@ fun Preview3() {
bottomText = "Submitting move",
retryMoveDialogShowing = true,
),
false,
{}, {},
)
}
Expand Down Expand Up @@ -919,6 +926,7 @@ fun Preview4() {
showPlayers = false,
showAnalysisPanel = true,
),
false,
{}, {},
)
}
Expand Down Expand Up @@ -975,6 +983,7 @@ fun Preview5() {
}
),
),
false,
{}, {},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.material.icons.rounded.Undo
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -45,6 +46,8 @@ import io.zenandroid.onlinego.data.model.local.Player
import io.zenandroid.onlinego.data.model.local.Score
import io.zenandroid.onlinego.data.model.local.UserStats
import io.zenandroid.onlinego.data.model.local.isPaused
import io.zenandroid.onlinego.data.model.ogs.AnalysisMessage
import io.zenandroid.onlinego.data.model.ogs.ChatChannel
import io.zenandroid.onlinego.data.model.ogs.Phase
import io.zenandroid.onlinego.data.model.ogs.VersusStats
import io.zenandroid.onlinego.data.ogs.GameConnection
Expand Down Expand Up @@ -104,6 +107,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.usecases.GetUserStatsUseCase
import io.zenandroid.onlinego.usecases.RepoResult
Expand Down Expand Up @@ -156,7 +160,8 @@ class GameViewModel(
private var pendingMove by mutableStateOf<PendingMove?>(null)
private var retrySendMoveDialogShowing by mutableStateOf(false)
private var koMoveDialogShowing by mutableStateOf(false)
private var analyzeMode by mutableStateOf(false)
var analyzeMode by mutableStateOf(false)
private set
private var estimateMode by mutableStateOf(false)
private var analysisShownMoveNumber by mutableStateOf(0)
private var passDialogShowing by mutableStateOf(false)
Expand Down Expand Up @@ -296,7 +301,7 @@ class GameViewModel(
estimateMode -> listOf(ExitEstimate)
game?.phase == Phase.STONE_REMOVAL -> listOf(AcceptStoneRemoval, RejectStoneRemoval)
gameFinished == true -> listOf(chatButton, Estimate(true), Previous, nextButton)
analyzeMode -> listOf(ExitAnalysis, Estimate(!isAnalysisDisabled()), Previous, nextButton)
analyzeMode -> listOf(ExitAnalysis, Estimate(!isAnalysisDisabled()), chatButton, Previous, nextButton)
pendingMove != null -> emptyList()
isMyTurn && candidateMove == null -> listOf(Analyze, Pass, endGameButton, chatButton, nextGameButton)
isMyTurn && candidateMove != null -> listOf(ConfirmMove, DiscardMove)
Expand Down Expand Up @@ -746,7 +751,19 @@ class GameViewModel(
CancelDialogDismiss -> cancelDialogShowing = false
ChatDialogDismiss -> chatDialogShowing = false
KOMoveDialogDismiss -> koMoveDialogShowing = false
is ChatSend -> gameConnection.sendMessage(action.message, gameState?.moves?.size ?: 0)
is ChatSend -> gameConnection.sendMessage(action.message, gameState?.moves?.size ?: 0, when (action.channel) {
ChatChannel.MAIN -> "main"
ChatChannel.MALKOVICH -> "malkovich"
ChatChannel.SPECTATOR -> "spectator"
ChatChannel.PERSONAL -> "personal"
})
is VariationSend -> {
gameConnection.sendAnalysisMessage(AnalysisMessage(
name = action.title,
from = currentVariation!!.rootMoveNo,
moves = currentVariation!!.moves,
), gameState?.moves?.size ?: 0, "main")
}
is OpenVariation -> {
chatDialogShowing = false
analyzeMode = true
Expand Down Expand Up @@ -1034,7 +1051,8 @@ sealed interface UserAction {
object PlayerDetailsDialogDismissed: UserAction
object ChatDialogDismiss: UserAction
object KOMoveDialogDismiss: UserAction
class ChatSend(val message: String): UserAction
class ChatSend(val message: String, val channel: ChatChannel): UserAction
class VariationSend(val title: String): UserAction
class OpenVariation(val variation: Variation): UserAction
object OpenInBrowser: UserAction
object DownloadSGF: UserAction
Expand Down
Loading

0 comments on commit 2988ccd

Please sign in to comment.