solupro преди 5 години
родител
ревизия
c2216c2c78
променени са 6 файла, в които са добавени 91 реда и са изтрити 12 реда
  1. 32 6
      eos.go
  2. 4 1
      go.mod
  3. 2 0
      go.sum
  4. 7 0
      main.go
  5. 7 5
      runner.go
  6. 39 0
      utils.go

+ 32 - 6
eos.go

@@ -1,13 +1,39 @@
 package main
 
-var NET_HOST string
+import (
+	"fmt"
+	"github.com/pkg/errors"
+	"github.com/sirupsen/logrus"
+	"math/rand"
+	"strings"
+	"time"
+)
 
-func init() {
-	if ENV == "dev" {
-		NET_HOST = "https://localnet.eosget.io:443/"
-	} else {
-		NET_HOST = "https://proxy.eosnode.tools:443/"
+func Transfer(player string, starId int) error {
+
+	rand.Seed(time.Now().UnixNano())
+	single := (rand.Intn(2) + 1) * 1000
+	total := toEOS(single * 10)
+	num := rand.Intn(23) + 75 // [75, 97]
+
+	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)
+
+	logrus.WithField("command", cmd).Infof("execute command")
+
+	_, errStr, err := ShellCmdTimeout(5, "/bin/sh", "-c", cmd)
+	if err != nil {
+		return err
 	}
+
+	if len(errStr) > 0 && strings.Contains(errStr, "Error ") {
+		return errors.New(errStr)
+	}
+
+	return nil
+}
+
+func toEOS(num int) string {
+	return fmt.Sprintf("%.4f EOS", float64(num)/10000)
 }
 
 type Account struct {

+ 4 - 1
go.mod

@@ -2,4 +2,7 @@ module bot
 
 go 1.12
 
-require github.com/sirupsen/logrus v1.4.1
+require (
+	github.com/pkg/errors v0.8.1
+	github.com/sirupsen/logrus v1.4.1
+)

+ 2 - 0
go.sum

@@ -1,5 +1,7 @@
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
+github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
+github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k=
 github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=

+ 7 - 0
main.go

@@ -8,11 +8,18 @@ import (
 )
 
 var ENV string
+var NET_HOST string
 
 func main() {
 	flag.StringVar(&ENV, "env", "dev", "执行环节dev|prod")
 	flag.Parse()
 
+	if ENV == "dev" {
+		NET_HOST = "https://localnet.eosget.io:443/"
+	} else {
+		NET_HOST = "https://proxy.eosnode.tools:443/"
+	}
+
 	configPath := fmt.Sprintf("./config/bots.%s.json", ENV)
 	f, err := ioutil.ReadFile(configPath)
 	CheckError(err)

+ 7 - 5
runner.go

@@ -10,7 +10,7 @@ import (
 const (
 	MAX_RUN    = 3
 	MIN_RUN    = 1
-	TIME_CHECK = 10 * time.Second // 十分钟检查一次
+	TIME_CHECK = 10 * 60 * time.Second // 十分钟检查一次
 )
 
 var BotQueue *queue
@@ -74,13 +74,15 @@ func worker(bot Account, botCh chan<- Account) {
 	atomic.AddInt64(&Working, 1)
 	for {
 		rand.Seed(time.Now().UnixNano())
-		c := rand.Intn(5)
+		c := rand.Intn(11) + 20
 
-		logrus.Infof("bot: %s calc %d", bot.Player, c)
-		if c == 3 {
+		err := Transfer(bot.Player, bot.StarId)
+		if err != nil { // 下注失败退出
+			logrus.WithField("error", err).Warnf("bot: %s transfer error", bot.Player)
 			break
 		}
-		time.Sleep(2 * time.Second)
+
+		time.Sleep(time.Duration(c) * time.Second)
 	}
 
 	botCh <- bot

+ 39 - 0
utils.go

@@ -1,7 +1,12 @@
 package main
 
 import (
+	"bytes"
+	"fmt"
 	"math/rand"
+	"os/exec"
+	"strings"
+	"syscall"
 	"time"
 )
 
@@ -17,3 +22,37 @@ func CheckError(err error) {
 		panic(err)
 	}
 }
+
+// 运行Shell命令,设定超时时间(秒)
+func ShellCmdTimeout(timeout int, cmd string, args ...string) (stdout, stderr string, e error) {
+	if len(cmd) == 0 {
+		e = fmt.Errorf("cannot run a empty command")
+		return
+	}
+	var out, err bytes.Buffer
+	command := exec.Command(cmd, args...)
+	command.Stdout = &out
+	command.Stderr = &err
+	command.Start()
+	// 启动routine等待结束
+	done := make(chan error)
+	go func() { done <- command.Wait() }()
+	// 启动routine持续打印输出
+	// 设定超时时间,并select它
+	after := time.After(time.Duration(timeout) * time.Second)
+	select {
+	case <-after:
+		command.Process.Signal(syscall.SIGINT)
+		time.Sleep(time.Second)
+		command.Process.Kill()
+	case <-done:
+	}
+	stdout = trimOutput(out)
+	stderr = trimOutput(err)
+
+	return
+}
+
+func trimOutput(buffer bytes.Buffer) string {
+	return strings.TrimSpace(string(bytes.TrimRight(buffer.Bytes(), "\x00")))
+}