Browse Source

Support chat messages

master
Trevor Slocum 2 years ago
parent
commit
6903dad58c
  1. 2
      client.go
  2. 57
      game.go
  3. 6
      gamecommand.go

2
client.go

@ -87,6 +87,8 @@ func (c *Client) processRead() {
c.game.CommandQueue <- GameCommand{Player: c.gameplayer, Command: CommandCut, Value: args}
} else if command_pieces[0] == "throw" || command_pieces[0] == "t" || command_pieces[0] == "th" {
c.game.CommandQueue <- GameCommand{Player: c.gameplayer, Command: CommandThrow, Value: args}
} else if command_pieces[0] == "message" || command_pieces[0] == "msg" || command_pieces[0] == "say" {
c.game.CommandQueue <- GameCommand{Player: c.gameplayer, Command: CommandMessage, Value: args}
} else if command_pieces[0] == "print" && c.game != nil {
c.game.printAll()
} else {

57
game.go

@ -91,6 +91,8 @@ func (g *Game) Reset() {
g.Starter = Card{}
g.ThrowPile = PlayerCards{}
g.DiscardPile = PlayerCards{}
g.ThrownCrib1 = 0
g.ThrownCrib2 = 0
g.Deck = NewDeck(StandardCards, 0)
for i := 0; i < shuffleCount; i++ {
@ -154,7 +156,8 @@ func (g *Game) Cut(cut int) bool {
if g.getClient(msgplayer).ConnType == ClientTelnet {
g.getClient(msgplayer).write("Starter: " + g.Starter.String())
} else {
g.update(msgplayer)
//g.update(msgplayer)
// TODO updateAll is called
}
}
@ -174,7 +177,7 @@ func (g *Game) exportJSON(player int) (string, error) {
fieldtag := gamereflected.Type().Field(i).Tag.Get("json")
if fieldtag == "" || fieldtag == "-" {
continue
} else if g.Phase != PhaseScore && (fieldname == "Hand1" && player != 1) || (fieldname == "Hand2" && player != 2) {
} else if g.Phase != PhaseScore && ((fieldname == "Hand1" && player != 1) || (fieldname == "Hand2" && player != 2)) {
hand := gamereflected.Field(i).Interface().(joker.Cards)
handprinted := ""
for j := 0; j < hand.Len(); j++ {
@ -322,7 +325,7 @@ func (g *Game) scoreHands() (int, int, int) {
var yourscore string
var theirscore string
dealerscoreprinted := fmt.Sprintf("%d (%d in hand + %d in crib)", dealerHandScore+dealerCribScore, dealerHandScore, dealerCribScore)
dealerscoreprinted := fmt.Sprintf("%d (%d hand + %d crib)", dealerHandScore+dealerCribScore, dealerHandScore, dealerCribScore)
opponentscoreprinted := fmt.Sprintf("%d", opponentHandScore)
for msgplayer := 1; msgplayer <= 2; msgplayer++ {
if g.Dealer == msgplayer {
@ -397,6 +400,7 @@ func (g *Game) Throw(player int, cardidentifier string) bool {
g.Crib = append(g.Crib, card)
sort.Sort(g.Crib)
/*if len(g.Crib.Cards) == 4 && player == g.Dealer {
g.updateOpponent(player)
}*/
@ -547,54 +551,52 @@ func (g *Game) updateAll() {
func (g *Game) printScoring() {
for player := 1; player <= 2; player++ {
gameprinted := []string{}
opponentscore, opponentscoreresults := cribbage.Score(cribbage.ShowHand, *g.getHand(g.getOpponent(g.Dealer)), g.Starter)
dealerscore, dealerscoreresults := cribbage.Score(cribbage.ShowHand, *g.getHand(g.Dealer), g.Starter)
dealercribscore, dealercribscoreresults := cribbage.Score(cribbage.ShowCrib, g.Crib, g.Starter)
var gameprinted []string
dealerscoreprinted := fmt.Sprintf("%d (%d in hand + %d in crib)", dealerscore+dealercribscore, dealerscore, dealercribscore)
opponentscoreprinted := fmt.Sprintf("%d", opponentscore)
opponentscore, opponentscoreresults := cribbage.Score(cribbage.ShowHand, *g.getHand(g.getOpponent(player)), g.Starter)
playerscore, dealerscoreresults := cribbage.Score(cribbage.ShowHand, *g.getHand(player), g.Starter)
cribscore, cribscoreresults := cribbage.Score(cribbage.ShowCrib, g.Crib, g.Starter)
var yourscore string
var theirscore string
if g.Dealer == player {
yourscore = dealerscoreprinted
theirscore = opponentscoreprinted
gameprinted = append(gameprinted, fmt.Sprintf("Your hands scored %d (%d hand, %d crib) - Your opponent's hand scored %d", playerscore, cribscore, opponentscore))
} else {
yourscore = opponentscoreprinted
theirscore = dealerscoreprinted
gameprinted = append(gameprinted, fmt.Sprintf("Your hand scored %d - Your opponent's hands scored %d (%d hand, %d crib)", playerscore, opponentscore, cribscore))
}
gameprinted = append(gameprinted, "Your hand(s) scored "+yourscore+" - Your opponent's hand(s) scored "+theirscore)
gameprinted = append(gameprinted, "Starter "+g.Starter.String())
gameprinted = append(gameprinted, fmt.Sprintf("Opponent (%2d) %s", opponentscore, g.getHand(g.getOpponent(g.Dealer))))
gameprinted = append(gameprinted, fmt.Sprintf("Dealer (%2d) %s", dealerscore, g.getHand(g.Dealer)))
gameprinted = append(gameprinted, fmt.Sprintf("Dealer c. (%2d) %s", dealercribscore, g.Crib))
gameprinted = append(gameprinted, "Starter "+g.Starter.String())
gameprinted = append(gameprinted, fmt.Sprintf("Player (%2d) %s", opponentscore, g.getHand(player)))
if g.Dealer == player {
gameprinted = append(gameprinted, fmt.Sprintf("Player c. (%2d) %s", cribscore, g.Crib))
gameprinted = append(gameprinted, fmt.Sprintf("Opponent (%2d) %s", opponentscore, g.getHand(g.getOpponent(player))))
for _, result := range dealerscoreresults {
gameprinted = append(gameprinted, fmt.Sprintf("Your hand scored %s", result))
}
for _, result := range dealercribscoreresults {
for _, result := range cribscoreresults {
gameprinted = append(gameprinted, fmt.Sprintf("Your crib scored %s", result))
}
for _, result := range opponentscoreresults {
gameprinted = append(gameprinted, fmt.Sprintf("Your opponent's hand scored %s", result))
}
} else {
gameprinted = append(gameprinted, fmt.Sprintf("Opponent c. (%2d) %s", cribscore, g.Crib))
gameprinted = append(gameprinted, fmt.Sprintf("Opponent (%2d) %s", opponentscore, g.getHand(g.getOpponent(player))))
for _, result := range opponentscoreresults {
gameprinted = append(gameprinted, fmt.Sprintf("Your hand scored %s", result))
}
for _, result := range dealerscoreresults {
gameprinted = append(gameprinted, fmt.Sprintf("Your opponent's hand scored %s", result))
}
for _, result := range dealercribscoreresults {
for _, result := range cribscoreresults {
gameprinted = append(gameprinted, fmt.Sprintf("Your opponent's crib scored %s", result))
}
}
g.getClient(player).write(strings.Join(gameprinted, "\n"))
for _, msg := range gameprinted {
g.getClient(player).write(msg)
}
}
}
@ -879,6 +881,13 @@ func (g *Game) processGameCommand(command GameCommand) {
g.Throw(command.Player, card)
}
}
} else if command.Command == CommandMessage {
data, err := json.Marshal(command)
if err != nil {
log.Fatal(err)
}
g.getClient(g.getOpponent(command.Player)).write(string(data))
}
g.updateAll()

6
gamecommand.go

@ -7,6 +7,7 @@ const CommandRaw = 0
const CommandContinue = 1
const CommandThrow = 2
const CommandCut = 3
const CommandMessage = 4
type GameCommand struct {
Player int
@ -19,17 +20,16 @@ func (gc GameCommand) ToStr() string {
switch gc.Command {
case CommandEnd:
commandprinted = "End"
break
case CommandRaw:
commandprinted = "Raw"
case CommandContinue:
commandprinted = "Continue"
break
case CommandCut:
commandprinted = "Cut"
break
case CommandThrow:
commandprinted = "Throw"
case CommandMessage:
commandprinted = "Message"
}
return fmt.Sprintf("Player %d - %s - %s", gc.Player, commandprinted, gc.Value)
}

Loading…
Cancel
Save