summaryrefslogtreecommitdiff
path: root/pkg/ui/views/game_util.go
blob: 8b8c65848b91f2675ad22d9d6e3ae9f19dc19a31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package views

import (
	"fmt"

	"github.com/boozec/rahanna/internal/api/database"
	"github.com/boozec/rahanna/pkg/p2p"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
)

func (m GameModel) handleWindowSizeMsg(msg tea.WindowSizeMsg) (GameModel, tea.Cmd) {
	m.width = msg.Width
	m.height = msg.Height
	listWidth := m.width / 4
	m.availableMovesList.SetSize(listWidth, m.height/2)
	return m, m.updateMovesListCmd()
}

func (m GameModel) buildWindowContent(content string, formWidth int) string {
	return lipgloss.JoinVertical(
		lipgloss.Center,
		windowStyle.Width(formWidth).Render(lipgloss.JoinVertical(
			lipgloss.Center,
			content,
		)),
	)
}

func (m GameModel) isMyTurn() bool {
	if m.game == nil {
		return false
	}

	var totalPlayers int

	switch m.game.Type {
	case database.SingleGameType:
		totalPlayers = 2
	case database.PairGameType:
		totalPlayers = 4
	}

	moves := len(m.chessGame.Moves())
	currentPlayer := (moves % totalPlayers) + 1
	return m.network.Me() == m.playerPeer(currentPlayer)
}

func (m GameModel) playerPeer(n int) p2p.NetworkID {
	if m.game == nil {
		return p2p.EmptyNetworkID
	}
	return p2p.NetworkID(fmt.Sprintf("%s-%d", m.game.Name, n))
}