package baccarat import ( "amor/common" redisrepo "amor/repository/redisRepo" "fmt" "strings" "github.com/notnil/joker/pkg/hand" "github.com/redis/rueidis" "github.com/shopspring/decimal" "github.com/spf13/cast" zero "github.com/wdvxdr1123/ZeroBot" ) const ( EntryMsgTemplate = "庄家手牌: %s\n闲家手牌: %s\n%s" ) func Entry(ctx *zero.Ctx) { // bjl 100 庄/闲/和 msg := strings.Split(ctx.Event.Message.String(), " ") if len(msg) != 3 { ctx.Send("命令格式错误") return } if ctx.Event.GroupID == 0 { ctx.Send("请在群聊中使用") return } betAmount := cast.ToInt64(msg[1]) if betAmount <= 0 { ctx.Send("下注金额必须大于0") return } userBalance, err := redisrepo.GetUserBalance(ctx.Event.UserID) if err != nil { if err == rueidis.Nil { userBalance = common.Config.Game.Balance redisrepo.Register(ctx.Event.UserID) } else { ctx.Send("获取用户余额失败") return } } if userBalance < betAmount { ctx.Send("你的余额不足, 请重新下注") return } bet := msg[2] if bet != "庄" && bet != "闲" && bet != "和" { ctx.Send("下注类型错误") return } round := NewRound(betAmount) // 发牌, 顺序: 闲庄闲庄 round.PlayerHand = append(round.PlayerHand, round.Deck.Pop()) round.DealerHand = append(round.DealerHand, round.Deck.Pop()) round.PlayerHand = append(round.PlayerHand, round.Deck.Pop()) round.DealerHand = append(round.DealerHand, round.Deck.Pop()) // 计算点数 playerPoint := CalculateCardPoint(round.PlayerHand) dealerPoint := CalculateCardPoint(round.DealerHand) result := "" playerHaveThirdCard := false if playerPoint == 8 || playerPoint == 9 { result = "闲家例牌, 闲赢" redisrepo.AddUserBalance(ctx.Event.UserID, betAmount) ctx.Send(fmt.Sprintf(EntryMsgTemplate, round.DealerHand, round.PlayerHand, result)) return } if playerPoint == 0 || playerPoint == 1 || playerPoint == 2 || playerPoint == 3 || playerPoint == 4 || playerPoint == 5 { if dealerPoint == 8 || dealerPoint == 9 { result = "庄家例牌, 庄赢" redisrepo.SubUserBalance(ctx.Event.UserID, betAmount) ctx.Send(fmt.Sprintf(EntryMsgTemplate, round.DealerHand, round.PlayerHand, result)) return } if dealerPoint != 8 && dealerPoint != 9 { result = fmt.Sprintf("闲家%d点, 庄家%d点, 闲家补牌\n", playerPoint, dealerPoint) round.PlayerHand = append(round.PlayerHand, round.Deck.Pop()) playerPoint = CalculateCardPoint(round.PlayerHand) playerHaveThirdCard = true } } if dealerPoint == 0 || dealerPoint == 1 || dealerPoint == 2 { result += fmt.Sprintf("庄家%d点, 庄家补牌\n", dealerPoint) round.DealerHand = append(round.DealerHand, round.Deck.Pop()) } if dealerPoint == 3 && playerHaveThirdCard { if round.PlayerHand[2].String()[:1] != "8" { result += fmt.Sprintf("庄家%d点, 庄家补牌\n", dealerPoint) round.DealerHand = append(round.DealerHand, round.Deck.Pop()) } } if dealerPoint == 4 && playerHaveThirdCard { playerThirdCard := round.PlayerHand[2].String()[:1] if playerThirdCard == "2" || playerThirdCard == "3" || playerThirdCard == "4" || playerThirdCard == "5" || playerThirdCard == "6" || playerThirdCard == "7" { result += fmt.Sprintf("庄家%d点, 庄家补牌\n", dealerPoint) round.DealerHand = append(round.DealerHand, round.Deck.Pop()) } } if dealerPoint == 5 && playerHaveThirdCard { playerThirdCard := round.PlayerHand[2].String()[:1] if playerThirdCard == "4" || playerThirdCard == "5" || playerThirdCard == "6" || playerThirdCard == "7" { result += fmt.Sprintf("庄家%d点, 庄家补牌\n", dealerPoint) round.DealerHand = append(round.DealerHand, round.Deck.Pop()) } } if dealerPoint == 6 && playerHaveThirdCard { playerThirdCard := round.PlayerHand[2].String()[:1] if playerThirdCard == "6" || playerThirdCard == "7" { result += fmt.Sprintf("庄家%d点, 庄家补牌\n", dealerPoint) round.DealerHand = append(round.DealerHand, round.Deck.Pop()) } } dealerFinalPoint := CalculateCardPoint(round.DealerHand) playerFinalPoint := CalculateCardPoint(round.PlayerHand) if dealerFinalPoint > playerFinalPoint { result += "庄赢\n" if bet == "庄" { result += fmt.Sprintf("恭喜你赢得了本局游戏, 你赢得了%d", round.Bet) // 庄赢时, 闲家下注金额的5%作为佣金 redisrepo.AddUserBalance(ctx.Event.UserID, decimal.NewFromInt(round.Bet).Mul(decimal.NewFromFloat(0.95)).IntPart()) } else { result += "很遗憾, 你输了本局游戏" redisrepo.SubUserBalance(ctx.Event.UserID, betAmount) } } if dealerFinalPoint < playerFinalPoint { result += "闲赢\n" if bet == "闲" { result += fmt.Sprintf("恭喜你赢得了本局游戏, 你赢得了%d", round.Bet) redisrepo.AddUserBalance(ctx.Event.UserID, betAmount) } else { result += "很遗憾, 你输了本局游戏" redisrepo.SubUserBalance(ctx.Event.UserID, betAmount) } } if dealerFinalPoint == playerFinalPoint { result += "平局\n" if bet == "和" { result += fmt.Sprintf("恭喜你赢得了本局游戏, 你赢得了%d", decimal.NewFromInt(round.Bet).Mul(decimal.NewFromInt(8)).IntPart()) redisrepo.AddUserBalance(ctx.Event.UserID, decimal.NewFromInt(round.Bet).Mul(decimal.NewFromInt(8)).IntPart()) } else { result += "很遗憾, 你输了本局游戏" redisrepo.SubUserBalance(ctx.Event.UserID, betAmount) } } } func CalculateCardPoint(cards []hand.Card) (point int) { for _, card := range cards { cardStr := card.String()[:1] point += ScoreMap()[cardStr] } point %= 10 return } func ScoreMap() map[string]int { return map[string]int{ "T": 0, "J": 0, "Q": 0, "K": 0, "A": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, } }