Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extrapoints #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
845 changes: 456 additions & 389 deletions api/karlchen.pb.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions api/karlchen.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ message PlayedCard {
Card card = 2;
PlayerValue trick_winner = 3;
PartyValue winner = 4;
repeated TrickEvent trick_event = 5;
}

message Bid {
Expand Down Expand Up @@ -264,6 +265,12 @@ enum Rank {
}


enum TrickEvent {
FOXCAUGHT = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer sepating words by underscores (in line with the style guide):

FOX_CAUGHT
DOPPELKOPF_SCORED
...

DOPPELKOPFSCORED = 1;
CHARLIESCORED = 2;
}

enum Party {
RE = 0;
CONTRA = 1;
Expand Down
34 changes: 24 additions & 10 deletions client/implementations/bot.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package implementations

import (
"fmt"
pb "github.com/supermihi/karlchencloud/api"
"github.com/supermihi/karlchencloud/client"
"github.com/supermihi/karlchencloud/doko/game"
"github.com/supermihi/karlchencloud/doko/match"
"os"
"strings"
"time"
"fmt"
"os"
"strings"
"time"

pb "github.com/supermihi/karlchencloud/api"
"github.com/supermihi/karlchencloud/client"
"github.com/supermihi/karlchencloud/doko/game"
"github.com/supermihi/karlchencloud/doko/match"
)

const superSecretBotPassword = "123"
Expand Down Expand Up @@ -139,7 +140,7 @@ func (h *BotClient) OnMyTurnGame() {
}
}
if cardIndex == -1 {
cardIndex = 0 // no matchnig card -> can play anything
cardIndex = 0 // no matching card -> can play anything
}
time.Sleep(h.delay)
err := h.PlayCard(cardIndex)
Expand All @@ -159,7 +160,20 @@ func (h *BotClient) OnMemberJoin(_ string, _ string) {
}
}

func (h *BotClient) OnPlayedCard(_ *pb.PlayedCard) {
func (h *BotClient) OnPlayedCard(play *pb.PlayedCard) {
if play.TrickWinner != nil && play.TrickWinner.UserId == h.User().Id {
h.Logf("I won the trick!")
for _, extraPoint := range play.TrickEvent {
switch pb.TrickEvent(extraPoint) {
case pb.TrickEvent_FOXCAUGHT:
h.Logf("and caught a fox!")
case pb.TrickEvent_DOPPELKOPFSCORED:
h.Logf("and scored a Doppelkopf!")
case pb.TrickEvent_CHARLIESCORED:
h.Logf("and my Charlie Miller won the last trick of the match!")
}
}
}
if h.Match().Phase == match.MatchFinished && h.isOwner {
h.Logf("I'm the owner, starting next match ...")
time.Sleep(h.delay)
Expand Down
20 changes: 16 additions & 4 deletions client/implementations/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package implementations

import (
"bufio"
"log"
"os"
"strconv"

"github.com/eiannone/keyboard"
pb "github.com/supermihi/karlchencloud/api"
"github.com/supermihi/karlchencloud/client"
"github.com/supermihi/karlchencloud/doko/game"
"github.com/supermihi/karlchencloud/server/pbconv"
"log"
"os"
"strconv"
)

type CliHandler struct {
Expand Down Expand Up @@ -81,7 +82,18 @@ func (h *CliHandler) OnPlayedCard(ev *pb.PlayedCard) {
h.Logf("%v played %v", h.Table().MemberNamesById[ev.UserId], pbconv.ToCard(ev.Card))
}
if len(h.Match().Trick.Cards) == 0 {
h.Logf("trick finished. Winner: %s", h.Table().MemberNamesById[h.Match().Trick.Forehand])
var winnerName = h.Table().MemberNamesById[h.Match().Trick.Forehand]
h.Logf("trick finished. Winner: %s", winnerName)
for _, extraPoint := range ev.TrickEvent {
switch pb.TrickEvent(extraPoint) {
case pb.TrickEvent_FOXCAUGHT:
h.Logf("%s caught a fox!", winnerName)
case pb.TrickEvent_DOPPELKOPFSCORED:
h.Logf("%s scored a Doppelkopf!", winnerName)
case pb.TrickEvent_CHARLIESCORED:
h.Logf("%s won with Charlie Miller the last trick of the match!", winnerName)
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions doko/match/evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func PointsByPlayer(eval *GameEvaluation, mode game.Mode) [game.NumPlayers]int {
}

const (
Karlchen ExtraPointType = iota
Charlie ExtraPointType = iota
Doppelkopf
FuchsGefangen
FoxCaught
)

func countReScoreAndTricks(g *game.Game) (int, int) {
Expand Down
55 changes: 45 additions & 10 deletions doko/match/extrapoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,74 @@ func findExtraPoints(g *game.Game) (ans []ExtraPoint) {
if !extraPointsApply(g.Mode.Type()) {
return
}
ans = append(ans, doppelkoepfe(g)...)
ans = append(ans, fuechse(g)...)
ans = append(ans, karlchen(g)...)
ans = append(ans, doppelkopfs(g)...)
ans = append(ans, foxesCaught(g)...)
ans = append(ans, charlie(g)...)
return
}

func doppelkoepfe(game *game.Game) []ExtraPoint {
func EventsOccured(g *game.Game, trick *game.Trick) (ans []ExtraPointType) {
if !extraPointsApply(g.Mode.Type()) {
return
}
if scoresDoppelkopf(trick) {
ans = append(ans, Doppelkopf)
}
for _, player := range game.Players() {
if maybeFoxCaught(player, trick) {
ans = append(ans, FoxCaught)
}
}
if charlieWinLastTrick(g) {
ans = append(ans, Charlie)
}
return ans
}

func scoresDoppelkopf(trick *game.Trick) bool {
return trick.Score() >= 40
}

func doppelkopfs(game *game.Game) []ExtraPoint {
var ans []ExtraPoint
for i, trick := range game.CompleteTricks {
if trick.Score() >= 40 {
if scoresDoppelkopf(&trick) {
ep := ExtraPoint{Doppelkopf, trick.Winner, i}
ans = append(ans, ep)
}
}
return ans
}

func fuechse(g *game.Game) []ExtraPoint {
//logic without party information
func maybeFoxCaught(player game.Player, trick *game.Trick) bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: in the doppelkopf app I use regularly, party information is consirede public once a player has played a queen of clubs, which is then used to emit the „fox caught“ event more precisely. However in real games it is not uncommon to forget who played a »Dulle«, I think this should also be possibility in the app. ;-)
But at least when contra/re etc. are announced that information could be used (in a separate PR, of course) to add constraints to this function.

return trick.CardsOf[player] == game.DiamondsA && player != trick.Winner
}

func foxesCaught(g *game.Game) []ExtraPoint {
var ans []ExtraPoint
for i, trick := range g.CompleteTricks {
for _, player := range game.Players() {
if trick.CardsOf[player] == game.DiamondsA && g.Mode.PartyOf(player) != g.Mode.PartyOf(trick.Winner) {
ans = append(ans, ExtraPoint{FuchsGefangen, trick.Winner, i})
ans = append(ans, ExtraPoint{FoxCaught, trick.Winner, i})
}
}
}
return ans
}

func karlchen(g *game.Game) []ExtraPoint {
func charlieWinLastTrick(g *game.Game) bool {
if !g.IsFinished() {
return false
}
lastTrick := g.CompleteTricks[game.NumTricks-1]
return lastTrick.CardsOf[lastTrick.Winner] == game.ClubsJ
}

func charlie(g *game.Game) []ExtraPoint {
lastTrick := g.CompleteTricks[game.NumTricks-1]
if lastTrick.CardsOf[lastTrick.Winner] == game.ClubsJ {
ans := [1]ExtraPoint{{Karlchen, lastTrick.Winner, game.NumTricks - 1}}
if charlieWinLastTrick(g) {
ans := [1]ExtraPoint{{Charlie, lastTrick.Winner, game.NumTricks - 1}}
return ans[:]
}
return []ExtraPoint{}
Expand Down
4 changes: 2 additions & 2 deletions doko/match/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestSampleMatch(t *testing.T) {
assert.Equal(t, 134, result.TrickScoreRe)
assert.ElementsMatch(t, [2]GamePoint{{Gewonnen, 1}, {ReAngesagt, 2}}, result.GamePoints)
assert.Equal(t, 5, result.TotalValue)
assert.ElementsMatch(t, [2]ExtraPoint{{Doppelkopf, Player3, 1}, {Karlchen, Player1, 11}}, result.ExtraPoints)
assert.ElementsMatch(t, [2]ExtraPoint{{Doppelkopf, Player3, 1}, {Charlie, Player1, 11}}, result.ExtraPoints)
points := PointsByPlayer(&result, match.Mode())
assert.Equal(t, 5, points[Player1])
assert.Equal(t, 5, points[Player2])
Expand All @@ -148,4 +148,4 @@ func TestMatch_PlayCard_InAuction(t *testing.T) {
match := NewMatch(Player1, cards)
ok := match.PlayCard(Player1, cards[Player1][0])
assert.False(t, ok)
}
}
13 changes: 13 additions & 0 deletions server/pbconv/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pbconv

import (
"fmt"

pb "github.com/supermihi/karlchencloud/api"
"github.com/supermihi/karlchencloud/doko/game"
"github.com/supermihi/karlchencloud/doko/match"
Expand Down Expand Up @@ -110,3 +111,15 @@ func ToMatchPhase(p pb.MatchPhase) match.Phase {
}
panic(fmt.Sprintf("unknown match phase: %v", p))
}

func ToExtraPointType(t pb.TrickEvent) match.ExtraPointType {
switch t {
case pb.TrickEvent_FOXCAUGHT:
return match.FoxCaught;
case pb.TrickEvent_DOPPELKOPFSCORED:
return match.Doppelkopf;
case pb.TrickEvent_CHARLIESCORED:
return match.Charlie;
}
panic(fmt.Sprintf("not a trick event type: %s", t))
}
13 changes: 13 additions & 0 deletions server/pbconv/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pbconv

import (
"fmt"

pb "github.com/supermihi/karlchencloud/api"
"github.com/supermihi/karlchencloud/doko/game"
"github.com/supermihi/karlchencloud/doko/match"
Expand Down Expand Up @@ -249,3 +250,15 @@ func addDetails(state *pb.MatchState, md *t.MatchData) {
panic(fmt.Sprintf("ToPbMatchState called with invalid match phase %v", md.Phase))
}
}

func ToPbTrickEvent(e match.ExtraPointType) pb.TrickEvent {
switch e {
case match.FoxCaught:
return pb.TrickEvent_FOXCAUGHT;
case match.Doppelkopf:
return pb.TrickEvent_DOPPELKOPFSCORED;
case match.Charlie:
return pb.TrickEvent_CHARLIESCORED;
}
panic(fmt.Sprintf("not a extra point type: %v", e))
}
14 changes: 13 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package server

import (
"context"
"log"

pb "github.com/supermihi/karlchencloud/api"
"github.com/supermihi/karlchencloud/doko/game"
"github.com/supermihi/karlchencloud/doko/match"
Expand All @@ -12,7 +14,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
)

type dokoserver struct {
Expand Down Expand Up @@ -256,6 +257,17 @@ func (s *dokoserver) PlayCard(ctx context.Context, req *pb.PlayCardRequest) (*pb
if m.CurrentTrick != nil && m.CurrentTrick.NumCardsPlayed() == 0 {
card.TrickWinner = &pb.PlayerValue{UserId: table.Players[m.PreviousTrick.Winner].String()}
log.Printf("trick finished. winner: %s", table.Players[m.PreviousTrick.Winner].String())
for _, event := range m.TrickEvent {
card.TrickEvent = append(card.TrickEvent, pbconv.ToPbTrickEvent(event))
switch match.ExtraPointType(event) {
case match.FoxCaught:
log.Printf("a fox had been caught!")
case match.Doppelkopf:
log.Printf("Doppelkopf scored!")
case match.Charlie:
log.Printf("Charlie Miller won the last trick of the match!")
}
}
}
if m.Phase == match.MatchFinished {
card.Winner = &pb.PartyValue{}
Expand Down
4 changes: 4 additions & 0 deletions server/tables/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type MatchData struct {
PreviousTrick *game.Trick
Mode game.Mode
Evaluation *match.GameEvaluation
TrickEvent []match.ExtraPointType
}

func NewMatchData(tm *TableMatch) *MatchData {
Expand Down Expand Up @@ -84,6 +85,9 @@ func NewMatchData(tm *TableMatch) *MatchData {
ans.CurrentTrick = &tmp
ans.Mode = g.Mode
ans.Cards = g.HandCards
if ans.CurrentTrick.NumCardsPlayed() == 0 && ans.PreviousTrick != nil {
ans.TrickEvent = match.EventsOccured(g, ans.PreviousTrick)
}
case match.InAuction:
ans.Cards = tm.Match.DealtCards()
case match.MatchFinished:
Expand Down