|
@@ -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"`
|
|
|
+}
|