Преглед изворни кода

添加游戏上报
调整为单投

solupro пре 5 година
родитељ
комит
d10217d11d
2 измењених фајлова са 65 додато и 5 уклоњено
  1. 57 3
      eos.go
  2. 8 2
      main.go

+ 57 - 3
eos.go

@@ -1,10 +1,15 @@
 package main
 
 import (
+	"encoding/json"
 	"fmt"
 	"github.com/pkg/errors"
 	"github.com/sirupsen/logrus"
+	"io/ioutil"
 	"math/rand"
+	"net/http"
+	"net/url"
+	"strconv"
 	"strings"
 	"time"
 )
@@ -13,14 +18,15 @@ func Transfer(player string, starId int) error {
 
 	rand.Seed(time.Now().UnixNano())
 	single := (rand.Intn(2) + 1) * 1000
-	total := toEOS(single * 10)
+	//total := toEOS(single * 10)
+	total := toEOS(single)
 	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)
+	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)
 
 	logrus.WithField("command", cmd).Infof("execute command")
 
-	_, errStr, err := ShellCmdTimeout(5, "/bin/sh", "-c", cmd)
+	output, errStr, err := ShellCmdTimeout(5, "/bin/sh", "-c", cmd)
 	if err != nil {
 		return err
 	}
@@ -29,9 +35,41 @@ func Transfer(player string, starId int) error {
 		return errors.New(errStr)
 	}
 
+	trx := &TrxResp{}
+	err = json.Unmarshal([]byte(output), trx)
+	if err == nil { // 上报交易id
+		gameReport(player, trx.TrxId, trx.Processed.BlockNum)
+	}
+
 	return nil
 }
 
+func gameReport(player string, trxId string, blockNum int) {
+	api := fmt.Sprintf("%s%s", API_HOST, "dice/gameReport")
+
+	h := http.Client{}
+	v := url.Values{}
+	v.Add("player", player)
+	v.Add("transaction_id", trxId)
+	v.Add("block_num", strconv.Itoa(blockNum))
+
+	resp, err := h.PostForm(api, v)
+	if err != nil {
+		logrus.Warnf("bot:%s, trxId:%s, blockNum:%d report request error: %s", player, trxId, blockNum, err.Error())
+	}
+	defer resp.Body.Close()
+
+	b, _ := ioutil.ReadAll(resp.Body)
+	respData := &Resp{}
+	json.Unmarshal(b, respData)
+
+	if respData.Result == 1 {
+		logrus.Infof("bot:%s, trxId:%s, blockNum:%d report success", player, trxId, blockNum)
+	} else {
+		logrus.Warnf("bot:%s, trxId:%s, blockNum:%d report error: %s", player, trxId, blockNum, respData.Msg)
+	}
+}
+
 func toEOS(num int) string {
 	return fmt.Sprintf("%.4f EOS", float64(num)/10000)
 }
@@ -40,3 +78,19 @@ type Account struct {
 	Player string `json:"player"`
 	StarId int    `json:"star_id"`
 }
+
+type TrxResp struct {
+	TrxId     string    `json:"transaction_id"`
+	Processed Processed `json:"processed"`
+}
+
+type Processed struct {
+	Id       string `json:"id"`
+	BlockNum int    `json:"block_num"`
+}
+
+type Resp struct {
+	Result int    `json:"result"`
+	Code   int    `json:"code"`
+	Msg    string `json:"msg"`
+}

+ 8 - 2
main.go

@@ -6,24 +6,29 @@ import (
 	"fmt"
 	"github.com/sirupsen/logrus"
 	"io/ioutil"
+	"os"
 )
 
 var ENV string
 var NET_HOST string
+var API_HOST string
 
 func main() {
 	logrus.SetFormatter(&logrus.TextFormatter{
-		DisableColors: true,
 		FullTimestamp: true,
 	})
+	logrus.SetLevel(logrus.InfoLevel)
+	logrus.SetOutput(os.Stdout)
 
 	flag.StringVar(&ENV, "env", "dev", "执行环节dev|prod")
 	flag.Parse()
 
 	if ENV == "dev" {
 		NET_HOST = "https://localnet.eosget.io:443/"
+		API_HOST = "https://test.superstar.team/"
 	} else {
 		NET_HOST = "https://proxy.eosnode.tools:443/"
+		API_HOST = "https://superstar.team/"
 	}
 
 	configPath := fmt.Sprintf("./config/bots.%s.json", ENV)
@@ -33,5 +38,6 @@ func main() {
 	err = json.Unmarshal(f, &bots)
 	CheckError(err)
 
-	Run(bots)
+	//Run(bots)
+	Transfer("kunbiao", 9)
 }