123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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) error {
- rand.Seed(time.Now().UnixNano())
- single := (rand.Intn(2) + 1) * 1000
- //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|1|%d|%d\" -p %s -j", NET_HOST, player, total, 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
- }
- if len(errStr) > 0 && strings.Contains(errStr, "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.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"`
- }
|