eos.go 891 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. "github.com/sirupsen/logrus"
  6. "math/rand"
  7. "strings"
  8. "time"
  9. )
  10. func Transfer(player string, starId int) error {
  11. rand.Seed(time.Now().UnixNano())
  12. single := (rand.Intn(2) + 1) * 1000
  13. total := toEOS(single * 10)
  14. num := rand.Intn(23) + 75 // [75, 97]
  15. cmd := fmt.Sprintf("cleos -u %s transfer %s supstargames \"%s\" \"dice|%d|10|%d|%d\" -p %s", NET_HOST, player, total, starId, num, single, player)
  16. logrus.WithField("command", cmd).Infof("execute command")
  17. _, errStr, err := ShellCmdTimeout(5, "/bin/sh", "-c", cmd)
  18. if err != nil {
  19. return err
  20. }
  21. if len(errStr) > 0 && strings.Contains(errStr, "Error ") {
  22. return errors.New(errStr)
  23. }
  24. return nil
  25. }
  26. func toEOS(num int) string {
  27. return fmt.Sprintf("%.4f EOS", float64(num)/10000)
  28. }
  29. type Account struct {
  30. Player string `json:"player"`
  31. StarId int `json:"star_id"`
  32. }