github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/chat/commands/flip.go (about)

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sync"
     7  
     8  	"github.com/keybase/client/go/chat/globals"
     9  	"github.com/keybase/client/go/chat/utils"
    10  	"github.com/keybase/client/go/protocol/chat1"
    11  	"github.com/keybase/client/go/protocol/gregor1"
    12  )
    13  
    14  type Flip struct {
    15  	*baseCommand
    16  	sync.Mutex
    17  	displayed bool
    18  }
    19  
    20  func NewFlip(g *globals.Context) *Flip {
    21  	return &Flip{
    22  		baseCommand: newBaseCommand(g, "flip", "", "Flip a cryptographic coin", true),
    23  	}
    24  }
    25  
    26  func (s *Flip) Execute(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID,
    27  	tlfName, text string, replyTo *chat1.MessageID) (err error) {
    28  	defer s.Trace(ctx, &err, "Execute")()
    29  	if !s.Match(ctx, text) {
    30  		return ErrInvalidCommand
    31  	}
    32  	return s.G().CoinFlipManager.StartFlip(ctx, uid, convID, tlfName, text, nil)
    33  }
    34  
    35  func (s *Flip) Preview(ctx context.Context, uid gregor1.UID, convID chat1.ConversationID,
    36  	tlfName, text string) {
    37  	s.Lock()
    38  	defer s.Unlock()
    39  	defer s.Trace(ctx, nil, "Preview")()
    40  	if !s.Match(ctx, text) {
    41  		if s.displayed {
    42  			err := s.getChatUI().ChatCommandMarkdown(ctx, convID, nil)
    43  			if err != nil {
    44  				s.Debug(ctx, "Preview: error on markdown: %+v", err)
    45  			}
    46  			s.displayed = false
    47  		}
    48  		return
    49  	}
    50  	cur := s.G().CoinFlipManager.DescribeFlipText(ctx, text)
    51  	var usage string
    52  	if s.G().IsMobileAppType() {
    53  		usage = fmt.Sprintf(flipMobileUsage, "```", "```", cur)
    54  	} else {
    55  		usage = fmt.Sprintf(flipDesktopUsage, "```", "```", cur)
    56  	}
    57  	err := s.getChatUI().ChatCommandMarkdown(ctx, convID, &chat1.UICommandMarkdown{
    58  		Body:  utils.DecorateWithLinks(ctx, utils.EscapeForDecorate(ctx, usage)),
    59  		Title: &flipTitle,
    60  	})
    61  	if err != nil {
    62  		s.Debug(ctx, "Preview: markdown error: %+v", err)
    63  	}
    64  	s.displayed = true
    65  }
    66  
    67  var flipTitle = `*/flip* [options]
    68  Flip a cryptographic coin`
    69  
    70  const flipDesktopUsage = `Variations: %s
    71  /flip 6                      # roll a die [1...6]
    72  /flip 10..20                 # pick a number [10...20]
    73  /flip vegan, keto, soylent   # shuffle some options
    74  /flip cards                  # deal 52 cards
    75  /flip cards 5 Ana, Sam, Kat  # deal 5 cards to 3 friends%s
    76  How it all works: https://keybase.io/coin-flip
    77  Current flip: %s`
    78  
    79  const flipMobileUsage = `Variations: %s
    80  /flip 6
    81  /flip 10..20
    82  /flip coffee, drinks, dinner
    83  /flip cards
    84  /flip cards 5 Ana, Sam, Kat%s
    85  _Current flip_: %s`