409 lines
11 KiB
Go
409 lines
11 KiB
Go
package hero_test
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"go_dreamfactory/comm"
|
||
"go_dreamfactory/lego"
|
||
"go_dreamfactory/lego/base/rpcx"
|
||
"go_dreamfactory/lego/core"
|
||
"go_dreamfactory/lego/sys/cron"
|
||
"go_dreamfactory/lego/sys/log"
|
||
"go_dreamfactory/modules/equipment"
|
||
"go_dreamfactory/modules/hero"
|
||
"go_dreamfactory/modules/items"
|
||
"go_dreamfactory/modules/user"
|
||
"go_dreamfactory/pb"
|
||
"go_dreamfactory/services"
|
||
"go_dreamfactory/sys/configure"
|
||
cfg "go_dreamfactory/sys/configure/structs"
|
||
"go_dreamfactory/sys/db"
|
||
"go_dreamfactory/utils"
|
||
"os"
|
||
"reflect"
|
||
"runtime/trace"
|
||
"strconv"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
|
||
"crypto/md5"
|
||
"encoding/hex"
|
||
|
||
"github.com/golang/protobuf/ptypes"
|
||
flag "github.com/spf13/pflag"
|
||
"go.mongodb.org/mongo-driver/bson"
|
||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||
"go.mongodb.org/mongo-driver/mongo/options"
|
||
)
|
||
|
||
var service core.IService
|
||
var s_gateComp comm.ISC_GateRouteComp = services.NewGateRouteComp()
|
||
|
||
var module = new(hero.Hero)
|
||
|
||
type TestService struct {
|
||
rpcx.RPCXService
|
||
}
|
||
|
||
func newService(ops ...rpcx.Option) core.IService {
|
||
s := new(TestService)
|
||
s.Configure(ops...)
|
||
return s
|
||
}
|
||
|
||
//初始化相关系统
|
||
func (this *TestService) InitSys() {
|
||
this.RPCXService.InitSys()
|
||
// if err := log.OnInit(); err != nil {
|
||
// panic(fmt.Sprintf("Sys log Init err:%v", err))
|
||
// } else {
|
||
// log.Infof("Sys log Init success !")
|
||
// }
|
||
if err := db.OnInit(this.GetSettings().Sys["db"]); err != nil {
|
||
panic(fmt.Sprintf("init sys.db err: %s", err.Error()))
|
||
} else {
|
||
log.Infof("init sys.db success!")
|
||
}
|
||
if err := configure.OnInit(this.GetSettings().Sys["configure"], configure.SetConfigPath("F:/work/go/go_dreamfactory/bin/json")); err != nil {
|
||
panic(fmt.Sprintf("init sys.configure err: %s", err.Error()))
|
||
} else {
|
||
log.Infof("init sys.configure success!")
|
||
}
|
||
}
|
||
func GetMonthStartEnd() (int64, int64) {
|
||
t := configure.Now()
|
||
monthStartDay := t.AddDate(0, 0, -t.Day()+1)
|
||
monthStartTime := time.Date(monthStartDay.Year(), monthStartDay.Month(), monthStartDay.Day(), 0, 0, 0, 0, t.Location())
|
||
monthEndDay := monthStartTime.AddDate(0, 1, -1)
|
||
monthEndTime := time.Date(monthEndDay.Year(), monthEndDay.Month(), monthEndDay.Day(), 23, 59, 59, 0, t.Location())
|
||
_d1 := monthStartTime.Unix()
|
||
_d2 := monthEndTime.Unix()
|
||
fmt.Printf("%d,%d", _d1, _d2)
|
||
return _d1, _d2
|
||
}
|
||
|
||
const (
|
||
game_lottery = "game_lottery.json"
|
||
)
|
||
|
||
func TimerStar() {
|
||
|
||
//star := configure.Now()
|
||
if !db.IsCross() {
|
||
conn, err := db.Cross()
|
||
if err == nil {
|
||
model := db.NewDBModel(db.CrossTag(), comm.TablePagodaRecord, conn)
|
||
//model.DB.DeleteMany(comm.TableSeasonPagoda, bson.M{}, options.Delete())
|
||
model.DB.DeleteMany(comm.TablePagodaRecord, bson.M{}, options.Delete())
|
||
for pos := 0; pos < comm.MaxRankNum; pos++ {
|
||
key1 := fmt.Sprintf("%s-pagodaList%d", db.CrossTag(), pos)
|
||
if err := model.Redis.Delete(key1); err != nil {
|
||
log.Errorf("delete failed")
|
||
}
|
||
}
|
||
|
||
if err := model.Redis.Delete("pagodaSeasonRank"); err != nil {
|
||
log.Errorf("delete failed")
|
||
}
|
||
}
|
||
|
||
// DB.DeleteMany(comm.TableSeasonRecord, bson.M{}, options.Delete())
|
||
|
||
}
|
||
//this.module.Debugf("=====%d,", time.Since(star).Milliseconds())
|
||
}
|
||
|
||
// 定义结构体
|
||
type sumy struct {
|
||
a int
|
||
b string
|
||
// 嵌入字段
|
||
float32
|
||
bool
|
||
next *sumy
|
||
}
|
||
|
||
var (
|
||
// 新掉落表
|
||
_group map[int64][]int32 // key 小组ID value cid
|
||
// 类型为1 的数据 该大组中的小组为权重掉落,必定从N个小组中随机出1个小组
|
||
_lotteryType1 map[int32][]int32 // key 大组ID value cid
|
||
// 类型为2 的数据 有多个小组ID
|
||
_lotteryType2 map[int32][]int32 // key 大组ID value 小组ID
|
||
// 小组类型为1
|
||
_groupType1 map[int64][]int32 //value cid
|
||
// 小组类型为2
|
||
_groupType2 map[int64][]int32 //value cid
|
||
//Btype map[int32]int32
|
||
Stype map[int64]int32 // subtype
|
||
SNum map[int64]int32 // 小组产出数量
|
||
)
|
||
|
||
func GetConfigure(name string) (v interface{}, err error) {
|
||
return configure.GetConfigure(name)
|
||
}
|
||
func GetLotterConfById(id int32) (data *cfg.GameLotteryData) {
|
||
if v, err := GetConfigure(game_lottery); err == nil {
|
||
if configure, ok := v.(*cfg.GameLottery); ok {
|
||
return configure.Get(id)
|
||
}
|
||
}
|
||
return
|
||
}
|
||
|
||
type t20190107 struct {
|
||
v int
|
||
}
|
||
|
||
func (t t20190107) F() int {
|
||
return t.v
|
||
}
|
||
|
||
func (t t20190107) A(a int) int {
|
||
return t.v + a
|
||
}
|
||
func Test_Main(t *testing.T) {
|
||
var (
|
||
szByte []byte
|
||
)
|
||
szByte = make([]byte, 0)
|
||
|
||
//szByte = append(szByte, 8)
|
||
fmt.Printf("%v,%s", szByte, string(szByte))
|
||
i := t20190107{678}
|
||
t1 := reflect.TypeOf(i)
|
||
for it := 0; it < t1.NumMethod(); it++ {
|
||
fmt.Println(t1.Method(it).Name)
|
||
}
|
||
|
||
f1, _ := t1.MethodByName("F")
|
||
fmt.Println(f1.Name)
|
||
|
||
r := f1.Func.Call([]reflect.Value{reflect.ValueOf(i), reflect.ValueOf(1)})[0].Int()
|
||
fmt.Println(r)
|
||
|
||
sz := make(map[string]*pb.DBHero)
|
||
fmt.Printf("%v", sz["1"])
|
||
hero := &pb.DBHero{}
|
||
|
||
value, err := strconv.Atoi("944005411")
|
||
fmt.Printf("%v,%v,%d,%v,%v", value, err, utils.WeekIntervalTime(configure.Now().Unix()), sz, hero)
|
||
ids := utils.RandomNumbers(0, 10, 5)
|
||
for _, v := range ids {
|
||
fmt.Printf("%d", v)
|
||
}
|
||
c := map[int32]int32{
|
||
3: 100,
|
||
6: 250,
|
||
9: 100,
|
||
}
|
||
out := make(map[int32]int32)
|
||
last := 1
|
||
for iPos, v := range c {
|
||
for ; int32(last) <= iPos; last++ {
|
||
out[int32(last)] = v
|
||
}
|
||
|
||
}
|
||
// 值包装结构体
|
||
d := reflect.ValueOf(sumy{
|
||
next: &sumy{},
|
||
})
|
||
// 获取字段数量
|
||
fmt.Println("NumField", d.NumField())
|
||
// 获取索引为2的字段(float32字段)
|
||
floatField := d.Field(2)
|
||
// 输出字段类型
|
||
fmt.Println("Field", floatField.Type())
|
||
// 根据名字查找字段
|
||
fmt.Println("FieldByName(\"b\").Type", d.FieldByName("b").Type())
|
||
// 根据索引查找值中, next字段的int字段的值
|
||
fmt.Println("FieldByIndex([]int{4, 0}).Type()", d.FieldByIndex([]int{4, 0}).Type())
|
||
|
||
var a int = 1024
|
||
// 获取变量a的反射值对象
|
||
valueOfA := reflect.ValueOf(a)
|
||
|
||
fmt.Println(reflect.TypeOf(a), a)
|
||
//typeofA := reflect.TypeOf(a).Elem().Kind()
|
||
// 获取interface{}类型的值, 通过类型断言转换
|
||
var getA int = valueOfA.Interface().(int)
|
||
// 获取64位的值, 强制类型转换为int类型
|
||
var getA2 int = int(valueOfA.Int())
|
||
fmt.Println(getA, getA2)
|
||
if err := cron.OnInit(nil); err != nil {
|
||
panic(fmt.Sprintf("init sys.cron err: %s", err.Error()))
|
||
} else {
|
||
//log.Infof("init sys.cron success!")
|
||
}
|
||
cron.AddFunc("0 0 5 /* * ?", TimerStar) //每月第一天5点执行一次
|
||
|
||
//创建trace文件
|
||
f, err := os.Create("trace.out")
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
|
||
defer f.Close()
|
||
|
||
//启动trace goroutine
|
||
err = trace.Start(f)
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
defer trace.Stop()
|
||
|
||
var star1, star2, star3 int32
|
||
var rst int32
|
||
star1 = 1 << 0
|
||
star2 = 1 << 1
|
||
star3 = 1 << 2
|
||
|
||
//rst = star1 ^ star2 ^ star3 // 三星
|
||
//rst = star1 ^ star2 // 2星
|
||
rst ^= star1
|
||
rst ^= star2
|
||
rst ^= star2
|
||
// 校验星
|
||
b1 := (rst&star1 == star1)
|
||
b2 := (rst&star2 == star2)
|
||
b3 := (rst&star3 == star3)
|
||
fmt.Printf("%d,%d,%d,%d", star1, star2, star3, rst)
|
||
fmt.Printf("%v,%v,%v,%d", b1, b2, b3, rst)
|
||
var list []int32
|
||
var list2 []int32
|
||
list = []int32{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||
list2 = []int32{4, 8, 9}
|
||
for _, v := range list2 {
|
||
for i, v1 := range list {
|
||
if v == v1 { // 找到了 就删除
|
||
//conf.PrivilegeId = append(conf.PrivilegeId, v1)
|
||
list = append(list[:i], list[i+1:]...)
|
||
break
|
||
}
|
||
}
|
||
}
|
||
|
||
oldHero := new(pb.DBHero)
|
||
oldHero.Lv = 2
|
||
new := copyPoint(oldHero)
|
||
fmt.Printf("%v", new)
|
||
oldHero.Lv = 8
|
||
currentTime := time.Unix(1637420154, 0)
|
||
startTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentTime.Location())
|
||
fmt.Print(startTime.Unix())
|
||
|
||
service = newService(
|
||
rpcx.SetConfPath("../../bin/conf/worker_1.yaml"),
|
||
)
|
||
service.OnInstallComp( //装备组件
|
||
s_gateComp, //此服务需要接受用户的消息 需要装备网关组件
|
||
)
|
||
go func() {
|
||
lego.Run(service, //运行模块
|
||
module,
|
||
items.NewModule(),
|
||
user.NewModule(),
|
||
equipment.NewModule(),
|
||
)
|
||
}()
|
||
time.Sleep(time.Second * 2)
|
||
|
||
}
|
||
|
||
func GetMap() map[int32]int32 {
|
||
return nil
|
||
}
|
||
|
||
func copyPoint(m *pb.DBHero) *pb.DBHero {
|
||
vt := reflect.TypeOf(m).Elem()
|
||
fmt.Println(vt)
|
||
newoby := reflect.New(vt)
|
||
newoby.Elem().Set(reflect.ValueOf(m).Elem())
|
||
newoby.Interface().(*pb.DBHero).Lv = 1111
|
||
return newoby.Interface().(*pb.DBHero)
|
||
}
|
||
func CloneNewHero(hero *pb.DBHero) (newHero *pb.DBHero) {
|
||
|
||
newHero = new(pb.DBHero)
|
||
|
||
*newHero = *hero
|
||
|
||
newHero.Block = true
|
||
newHero.Lv = 100
|
||
newHero.Id = primitive.NewObjectID().Hex()
|
||
return
|
||
}
|
||
func wordSepNormalizeFunc(f *flag.FlagSet, name string) flag.NormalizedName {
|
||
from := []string{"-", "_"}
|
||
to := "."
|
||
for _, sep := range from {
|
||
name = strings.Replace(name, sep, to, -1)
|
||
}
|
||
return flag.NormalizedName(name)
|
||
}
|
||
|
||
const Salt = "kTlufeTSjD33xSMPXGrkmzITxzFrScob"
|
||
|
||
func EncodeMD5(value, salt string) string {
|
||
m := md5.New()
|
||
m.Write([]byte(value))
|
||
m.Write([]byte(salt))
|
||
return hex.EncodeToString(m.Sum(nil))
|
||
}
|
||
|
||
func GetMd5String(str string) string {
|
||
salt := Salt
|
||
return EncodeMD5(str, salt)
|
||
}
|
||
|
||
func Test_Modules(t *testing.T) {
|
||
var iMax int64
|
||
iMax = 1 << 32
|
||
//fmt.Printf("%d", iMax)
|
||
|
||
salt := GetMd5String("yht116")
|
||
passwordMd5 := EncodeMD5("admin123", salt)
|
||
fmt.Printf("%s", passwordMd5)
|
||
fmt.Printf("%d", (iMax << 30))
|
||
var cliName = flag.StringP("name", "n", "nick", "Input Your Name")
|
||
var cliAge = flag.IntP("age", "a", 22, "Input Your Age")
|
||
var cliGender = flag.StringP("gender", "g", "male", "Input Your Gender")
|
||
var cliOK = flag.BoolP("ok", "o", false, "Input Are You OK")
|
||
var cliDes = flag.StringP("des-detail", "d", "", "Input Description")
|
||
var cliOldFlag = flag.StringP("badflag", "b", "just for test", "Input badflag")
|
||
flag.CommandLine.SetNormalizeFunc(wordSepNormalizeFunc)
|
||
|
||
// 为 age 参数设置 NoOptDefVal 默认值,通过简便的方式为参数设置默认值之外的值
|
||
flag.Lookup("age").NoOptDefVal = "25"
|
||
|
||
// 把 badflag 参数标记为即将废弃的,请用户使用 des-detail 参数
|
||
flag.CommandLine.MarkDeprecated("badflag", "please use --des-detail instead")
|
||
|
||
// 把 badflag 参数的 shorthand 标记为即将废弃的,请用户使用 des-detail 的 shorthand 参数
|
||
flag.CommandLine.MarkShorthandDeprecated("badflag", "please use -d instead")
|
||
|
||
// 在帮助文档中隐藏参数 gender
|
||
flag.CommandLine.MarkHidden("badflag")
|
||
|
||
// 把用户传递的命令行参数解析为对应变量的值
|
||
flag.Parse()
|
||
|
||
fmt.Println("name=", *cliName)
|
||
fmt.Println("age=", *cliAge)
|
||
fmt.Println("gender=", *cliGender)
|
||
fmt.Println("ok=", *cliOK)
|
||
fmt.Println("des=", *cliDes)
|
||
fmt.Println("cliOldFlag=", *cliOldFlag)
|
||
|
||
oldHero := new(pb.DBHero)
|
||
new := copyPoint(oldHero)
|
||
fmt.Printf("%v", new)
|
||
data, _ := ptypes.MarshalAny(&pb.HeroListReq{})
|
||
reply := &pb.RPCMessageReply{}
|
||
s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{UserId: "0_62c79d66acc7aa239b07c21e", MainType: "hero", SubType: "list", Message: data}, reply)
|
||
log.Debugf("reply:%v", reply)
|
||
|
||
}
|