Fix lint warnings

This commit is contained in:
Trevor Slocum 2020-10-03 21:26:32 -07:00
parent b6cd5669aa
commit 4046da9073
7 changed files with 62 additions and 70 deletions

View file

@ -27,7 +27,7 @@ type cardStackWidget struct {
sync.RWMutex
}
func NewCardStackWidget() *cardStackWidget {
func newCardStackWidget() *cardStackWidget {
c := cardStackWidget{Box: cview.NewBox()}
return &c
}
@ -60,7 +60,7 @@ func (c *cardStackWidget) SetCards(cards joker.Cards) {
c.Cards = make([]*cardWidget, len(cards))
for i := 0; i < len(cards); i++ {
c.Cards[i] = NewCardWidget(cards[i], throwCard)
c.Cards[i] = newCardWidget(cards[i], throwCard)
}
}

View file

@ -17,7 +17,7 @@ type cardWidget struct {
selectedFunc func(joker.Card)
}
func NewCardWidget(card joker.Card, selectedFunc func(joker.Card)) *cardWidget {
func newCardWidget(card joker.Card, selectedFunc func(joker.Card)) *cardWidget {
c := cardWidget{Box: cview.NewBox().ShowFocus(false), Card: card, selectedFunc: selectedFunc}
c.Box.SetBorder(true)

View file

@ -68,49 +68,47 @@ func handleRead(c *websocket.Conn, done chan struct{}) {
continue
}
var gameState *GameState
gameState = &GameState{}
err = json.Unmarshal(message, gameState)
g := &gameState{}
err = json.Unmarshal(message, g)
if err != nil {
statusMessage(string(message))
continue
}
if gameState.Turn == 0 {
var gameCommand *GameCommand
gameCommand = &GameCommand{}
err = json.Unmarshal(message, gameCommand)
if g.Turn == 0 {
c := &gameCommand{}
err = json.Unmarshal(message, c)
if err != nil {
// TODO currently skipping, should show error
continue
}
if gameCommand.Command == CommandMessage {
statusMessage(fmt.Sprintf("<%s> %s", "Opponent", gameCommand.Value))
if c.Command == CommandMessage {
statusMessage(fmt.Sprintf("<%s> %s", "Opponent", c.Value))
}
}
currentGameState = gameState
currentGameState = g
if gameState.Player > 0 {
playerHand := gameState.Hand1
oppHand := gameState.Hand2
if gameState.Player == 2 {
playerHand = gameState.Hand2
oppHand = gameState.Hand1
if g.Player > 0 {
playerHand := g.Hand1
oppHand := g.Hand2
if g.Player == 2 {
playerHand = g.Hand2
oppHand = g.Hand1
}
playerCrib := gameState.Crib
playerCrib := g.Crib
var oppCrib joker.Cards
if gameState.Dealer != gameState.Player {
if g.Dealer != g.Player {
playerCrib = joker.Cards{}
oppCrib = gameState.Crib
oppCrib = g.Crib
}
if currentGameState.Status == "" {
if currentGameState.Phase == PhasePick {
if len(playerHand) > 4 {
if gameState.Dealer == gameState.Player {
if g.Dealer == g.Player {
currentGameState.Status = "Throw for your crib"
} else {
currentGameState.Status = "Throw for your opponent's crib"
@ -147,7 +145,7 @@ func handleRead(c *websocket.Conn, done chan struct{}) {
}
if currentGameState.Phase == PhasePick {
if gameState.Dealer == gameState.Player {
if g.Dealer == g.Player {
mainCribStack.SetCards(playerCrib)
oppCribStack.SetCards(joker.Cards{})
} else {
@ -188,7 +186,7 @@ func handleRead(c *websocket.Conn, done chan struct{}) {
updateGameText()
if debug {
statusMessage(fmt.Sprintf("read: %+v\n", gameState))
statusMessage(fmt.Sprintf("read: %+v\n", g))
}
app.Draw()

33
game.go
View file

@ -8,6 +8,7 @@ import (
"gitlab.com/tslocum/joker"
)
// Game phases
const (
PhaseEnd = -1
PhaseSetup = 0
@ -16,28 +17,18 @@ const (
PhaseScore = 3
)
const (
PegPhaseNormal = 0
PegPhaseSolo = 1
PegPhaseFinal = 2
)
const WinningScore = 121
const shuffleCount = 7
type PlayerCard struct {
type playerCard struct {
joker.Card
Player int
}
func (c PlayerCard) String() string {
func (c playerCard) String() string {
return fmt.Sprintf("{%d}%s", c.Player, c.Card)
}
type PlayerCards []PlayerCard
type playerCards []playerCard
func (c PlayerCards) String() string {
func (c playerCards) String() string {
var s strings.Builder
for i := range c {
if i > 0 {
@ -48,19 +39,19 @@ func (c PlayerCards) String() string {
return s.String()
}
func (c PlayerCards) Len() int {
func (c playerCards) Len() int {
return len(c)
}
func (c PlayerCards) Less(i, j int) bool {
func (c playerCards) Less(i, j int) bool {
return c[i].Value() < c[j].Value()
}
func (c PlayerCards) Swap(i, j int) {
func (c playerCards) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c PlayerCards) Cards() joker.Cards {
func (c playerCards) Cards() joker.Cards {
var cards = make(joker.Cards, len(c))
for i, card := range c {
cards[i] = card.Card
@ -68,7 +59,7 @@ func (c PlayerCards) Cards() joker.Cards {
return cards
}
func (c PlayerCards) PlayerCards(player int) int {
func (c playerCards) PlayerCards(player int) int {
var i int
for _, card := range c {
if card.Player == player {
@ -78,7 +69,7 @@ func (c PlayerCards) PlayerCards(player int) int {
return i
}
type GameState struct {
type gameState struct {
Phase int `json:"phase"`
Player int `json:"player"`
Dealer int `json:"dealer"`
@ -91,6 +82,6 @@ type GameState struct {
Score1 int `json:"score1"`
Score2 int `json:"score2"`
ThrowPile PlayerCards `json:"throwpile"`
ThrowPile playerCards `json:"throwpile"`
Status string `json:"status"`
}

View file

@ -2,20 +2,23 @@ package main
import "fmt"
const CommandEnd = -1
const CommandRaw = 0
const CommandContinue = 1
const CommandThrow = 2
const CommandCut = 3
const CommandMessage = 4
// Game commands
const (
CommandEnd = -1
CommandRaw = 0
CommandContinue = 1
CommandThrow = 2
CommandCut = 3
CommandMessage = 4
)
type GameCommand struct {
type gameCommand struct {
Player int
Command int
Value string
}
func (gc GameCommand) ToStr() string {
func (gc gameCommand) ToStr() string {
var commandprinted string
switch gc.Command {
case CommandEnd:

24
main.go
View file

@ -16,8 +16,8 @@ import (
)
const (
DefaultStatusText = "Press Enter to chat, 1-6 to throw a card, Space to continue"
LogFormat = "2006-01-02 15:04:05"
defaultStatusText = "Press Enter to chat, 1-6 to throw a card, Space to continue"
logFormat = "2006-01-02 15:04:05"
)
var (
@ -26,7 +26,7 @@ var (
app *cview.Application
inputConfig *cbind.Configuration
statusText *StatusTextView
statusText *statusTextView
gameText *cview.TextView
inputField *cview.InputField
@ -41,7 +41,7 @@ var (
throwStack *cardStackWidget
oppCribStack *cardStackWidget
currentGameState = &GameState{}
currentGameState = &gameState{}
writeBuffer = make(chan string)
@ -198,7 +198,7 @@ func statusMessage(message string) {
message = "* " + message
}
statusBuf.Write([]byte(prefix + time.Now().Format(LogFormat) + " " + message))
statusBuf.Write([]byte(prefix + time.Now().Format(logFormat) + " " + message))
app.Draw()
}
@ -245,11 +245,11 @@ func main() {
app.SetInputCapture(inputConfig.Capture)
starterWidget = NewCardWidget(joker.Card{}, nil)
mainHandStack = NewCardStackWidget()
mainCribStack = NewCardStackWidget()
throwStack = NewCardStackWidget()
oppCribStack = NewCardStackWidget()
starterWidget = newCardWidget(joker.Card{}, nil)
mainHandStack = newCardStackWidget()
mainCribStack = newCardStackWidget()
throwStack = newCardStackWidget()
oppCribStack = newCardStackWidget()
mainHandStack.EverSelectable = true
mainCribStack.EverSelectable = true
@ -274,7 +274,7 @@ func main() {
statusGrid.AddItem(cview.NewTextView(), 1, 0, 1, 1, 0, 0, false)
statusGrid.AddItem(gameText, 2, 1, 1, 1, 0, 0, false)
statusText = NewStatusTextView()
statusText = newStatusTextView()
inputField = cview.NewInputField().
SetText("").
@ -312,7 +312,7 @@ func main() {
focusUpdated()
statusMessage(DefaultStatusText)
statusMessage(defaultStatusText)
go connect(connectAddress)

View file

@ -5,16 +5,16 @@ import (
"gitlab.com/tslocum/cview"
)
type StatusTextView struct {
type statusTextView struct {
*cview.TextView
}
func NewStatusTextView() *StatusTextView {
return &StatusTextView{cview.NewTextView()}
func newStatusTextView() *statusTextView {
return &statusTextView{cview.NewTextView()}
}
// MouseHandler returns the mouse handler for this primitive.
func (t *StatusTextView) MouseHandler() func(action cview.MouseAction, event *tcell.EventMouse, setFocus func(p cview.Primitive)) (consumed bool, capture cview.Primitive) {
func (t *statusTextView) MouseHandler() func(action cview.MouseAction, event *tcell.EventMouse, setFocus func(p cview.Primitive)) (consumed bool, capture cview.Primitive) {
return t.WrapMouseHandler(func(action cview.MouseAction, event *tcell.EventMouse, setFocus func(p cview.Primitive)) (consumed bool, capture cview.Primitive) {
x, y := event.Position()
if !t.InRect(x, y) {