12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- 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"
- )
- func Transfer(player string, starId int) (int64, error) {
- rand.Seed(time.Now().UnixNano())
- single := (rand.Intn(2) + 1) * 1000
- //total := toEOS(single * 10)
- 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, 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 0, err
- }
- if len(errStr) > 0 && strings.Contains(errStr, "Error ") {
- return 0, errors.New(errStr)
- }
- trx := &TrxResp{}
- err = json.Unmarshal([]byte(output), trx)
- if err == nil { // 上报交易id
- gameReport(player, trx.TrxId, trx.Processed.BlockNum)
- }
- return int64(total), 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.WithField("error", err.Error()).Errorf("bot:%s, trxId:%s, blockNum:%d report request error", player, trxId, blockNum)
- }
- 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.WithField("error", respData.Msg).Errorf("bot:%s, trxId:%s, blockNum:%d report report error", player, trxId, blockNum)
- }
- }
- func toEOS(num int) string {
- return fmt.Sprintf("%.4f EOS", float64(num)/10000)
- }
- 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"`
- }
|