Преглед на файлове

设置最大投注额度

solupro преди 5 години
родител
ревизия
fd95c9dd35
променени са 2 файла, в които са добавени 43 реда и са изтрити 7 реда
  1. 7 6
      eos.go
  2. 36 1
      runner.go

+ 7 - 6
eos.go

@@ -14,25 +14,26 @@ import (
 	"time"
 )
 
-func Transfer(player string, starId int) error {
+func Transfer(player string, starId int) (int64, error) {
 
 	rand.Seed(time.Now().UnixNano())
 	single := (rand.Intn(2) + 1) * 1000
 	//total := toEOS(single * 10)
-	total := toEOS(single)
+	total := single
+	totalStr := toEOS(total)
 	num := rand.Intn(23) + 75 // [75, 97]
 
-	cmd := fmt.Sprintf("cleos -u %s transfer %s supstargames \"%s\" \"dice|%d|1|%d|%d\" -p %s -j", NET_HOST, player, total, starId, num, single, player)
+	cmd := fmt.Sprintf("cleos -u %s transfer %s supstargames \"%s\" \"dice|%d|1|%d|%d\" -p %s -j", NET_HOST, player, totalStr, starId, num, single, player)
 
 	logrus.WithField("command", cmd).Infof("execute command")
 
 	output, errStr, err := ShellCmdTimeout(5, "/bin/sh", "-c", cmd)
 	if err != nil {
-		return err
+		return 0, err
 	}
 
 	if len(errStr) > 0 && strings.Contains(errStr, "Error ") {
-		return errors.New(errStr)
+		return 0, errors.New(errStr)
 	}
 
 	trx := &TrxResp{}
@@ -41,7 +42,7 @@ func Transfer(player string, starId int) error {
 		gameReport(player, trx.TrxId, trx.Processed.BlockNum)
 	}
 
-	return nil
+	return int64(total), nil
 }
 
 func gameReport(player string, trxId string, blockNum int) {

+ 36 - 1
runner.go

@@ -11,10 +11,16 @@ const (
 	MAX_RUN    = 3
 	MIN_RUN    = 1
 	TIME_CHECK = 10 * 60 * time.Second // 十分钟检查一次
+	//TIME_CHECK = 60 * time.Second // 十分钟检查一次
+
+	TIME_RESET_TOTAL_BET = 24 * 3600 * time.Second
+	//TIME_RESET_TOTAL_BET = 300 * time.Second
 )
 
 var BotQueue *queue
 var Working int64
+var TotalBet int64
+var MaxBet int64
 
 func init() {
 	BotQueue = newQueue()
@@ -31,7 +37,11 @@ func Run(bots []Account) {
 	ticker := time.NewTicker(TIME_CHECK)
 	defer ticker.Stop()
 
+	dayTicker := time.NewTicker(TIME_RESET_TOTAL_BET)
+	defer dayTicker.Stop()
+
 	botCh := make(chan Account)
+	resetBet()
 	master(botCh)
 
 	for {
@@ -39,6 +49,9 @@ func Run(bots []Account) {
 		case <-ticker.C:
 			master(botCh)
 
+		case <-dayTicker.C:
+			resetBet()
+
 		case exitBot := <-botCh:
 			logrus.Warnf("bot: %s exit!", exitBot.Player)
 			atomic.AddInt64(&Working, -1)
@@ -51,6 +64,12 @@ func Run(bots []Account) {
 func master(botCh chan<- Account) {
 	logrus.Info("check running worker")
 
+	totalBet := atomic.LoadInt64(&TotalBet)
+	if totalBet >= MaxBet {
+		logrus.Infof("TotalBet:%d >= MaxBet:%d, stop work!", totalBet, MaxBet)
+		return
+	}
+
 	running := atomic.LoadInt64(&Working)
 	logrus.Infof("running %d bots, queue len: %d", running, BotQueue.getLen())
 
@@ -74,12 +93,17 @@ func worker(bot Account, botCh chan<- Account) {
 	atomic.AddInt64(&Working, 1)
 	for {
 
-		err := Transfer(bot.Player, bot.StarId)
+		bet, err := Transfer(bot.Player, bot.StarId)
 		if err != nil { // 下注失败退出
 			logrus.WithField("error", err).Warnf("bot: %s transfer error", bot.Player)
 			break
 		}
 
+		if atomic.AddInt64(&TotalBet, bet) >= atomic.LoadInt64(&MaxBet) {
+			logrus.Infof("TotalBet >= MaxBet, bot: %s stop", bot.Player)
+			break
+		}
+
 		rand.Seed(time.Now().UnixNano())
 		c := rand.Intn(6) + 15
 		time.Sleep(time.Duration(c) * time.Second)
@@ -87,3 +111,14 @@ func worker(bot Account, botCh chan<- Account) {
 
 	botCh <- bot
 }
+
+func resetBet() {
+	rand.Seed(time.Now().UnixNano())
+	max := rand.Int63n(15000001) + 35000000 // [3500, 5000] EOS
+	//max := rand.Int63n(5000) + 10000 // [3500, 5000] EOS
+
+	atomic.StoreInt64(&MaxBet, max)
+	atomic.StoreInt64(&TotalBet, 0)
+
+	logrus.Infof("Reset MaxBet:%d, TotalBet:%d", max, 0)
+}