|
|
|
package serviceGame
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gcharset"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"time"
|
|
|
|
"tyj_admin/api/v1/game"
|
|
|
|
"tyj_admin/internal/serviceGame/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IGameRole interface {
|
|
|
|
GetGameRoleListSearch(ctx context.Context, req *game.RoleSearchReq) (res *game.RoleSearchRes, err error)
|
|
|
|
GetOnlineList(ctx context.Context, req *game.RoleOnlineReq) (res *game.RoleOnlineRes, err error)
|
|
|
|
GetOnlineTopList(ctx context.Context, req *game.RoleOnlineTopReq) (res *game.RoleOnlineTopRes, err error)
|
|
|
|
GetAccount(ctx context.Context, req *game.AccountReq) (res *game.AccountRes, err error)
|
|
|
|
GameRegister(ctx context.Context, req *game.RegisterReq) (res *game.RegisterRes, err error)
|
|
|
|
GameUnitChangeName(ctx context.Context, req *game.UnitChangeNameReq) (res *game.UnitChangeNameRes, err error)
|
|
|
|
GetLoginOut(ctx context.Context, req *game.GetLoginOutReq) (res *game.GetLoginOutRes, err error)
|
|
|
|
GetLoginOutLog(ctx context.Context, req *game.GetLoginOutLogReq) (res *game.GetLoginOutLogRes, err error)
|
|
|
|
GetItemLog(ctx context.Context, req *game.GetItemLogReq) (res *game.GetItemLogRes, err error)
|
|
|
|
GetRegister(ctx context.Context, req *game.GetRegisterReq) (res *game.GetRegisterRes, err error)
|
|
|
|
GameUserDel(ctx context.Context, req *game.UserDelReq) (res *game.UserDelRes, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type gameRoleImpl struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
var gameRoleService = gameRoleImpl{}
|
|
|
|
|
|
|
|
func GameRole() IGameRole {
|
|
|
|
return &gameRoleService
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取玩家角色列表
|
|
|
|
func (s *gameRoleImpl) GetGameRoleListSearch(ctx context.Context, req *game.RoleSearchReq) (res *game.RoleSearchRes, err error) {
|
|
|
|
//res = new(game.RoleSearchRes)
|
|
|
|
g.Try(ctx, func(ctx context.Context) {
|
|
|
|
//fmt.Println("GetGameRoleListSearch: ", req)
|
|
|
|
res, err = internal.GetRoleList(ctx, req)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *gameRoleImpl) GetOnlineList(ctx context.Context, req *game.RoleOnlineReq) (res *game.RoleOnlineRes, err error) {
|
|
|
|
res = new(game.RoleOnlineRes)
|
|
|
|
ip := internal.ServerConfig[fmt.Sprint(req.ServerId)]
|
|
|
|
url := "http://" + ip + "/GetOnline"
|
|
|
|
bytes := g.Client().GetBytes(ctx, url)
|
|
|
|
//fmt.Println("GetOnlineList: ", url, bytes)
|
|
|
|
src := string(bytes)
|
|
|
|
if g.IsEmpty(src) {
|
|
|
|
g.Log().Printf(ctx, "%d GetOnlineList: gerror.New(\"空回调,发送失败\")", req.ServerId)
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
srcCharset := "UTF-8"
|
|
|
|
tmp, _ := gcharset.ToUTF8(srcCharset, src)
|
|
|
|
json, err := gjson.DecodeToJson(tmp)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//fmt.Println("GetOnlineList - json: ", tmp, gjson.MustEncodeString(json.Get("Accounts").Array()))
|
|
|
|
if json.Get("Error").Int() == 200 {
|
|
|
|
res.Total = len(json.Get("Accounts").Array())
|
|
|
|
for _, v := range json.Get("Accounts").Array() {
|
|
|
|
account := map[string]interface{}{}
|
|
|
|
account["id"] = v
|
|
|
|
fmt.Println("GetOnlineList - json: ", v)
|
|
|
|
account["uid"], _ = internal.GetStringIdToUid(ctx, fmt.Sprint(v))
|
|
|
|
res.Onlines = append(res.Onlines, account)
|
|
|
|
if len(res.Onlines) >= 50 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, gerror.New("获取失败")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var DateFormat = "2006-01-02"
|
|
|
|
|
|
|
|
func (s *gameRoleImpl) GetOnlineTopList(ctx context.Context, req *game.RoleOnlineTopReq) (res *game.RoleOnlineTopRes, err error) {
|
|
|
|
res = new(game.RoleOnlineTopRes)
|
|
|
|
fsql := fmt.Sprintf(`select num,server,channel,date from game_online where STR_TO_DATE(date, "%%Y-%%m-%%d")="%s" `, time.Now().Format(DateFormat))
|
|
|
|
if req.ServerId != 0 {
|
|
|
|
fsql += fmt.Sprintf(" and server=%d ", req.ServerId)
|
|
|
|
}
|
|
|
|
data := []game.RoleOnlineTop{}
|
|
|
|
err = g.Model().Raw(fsql).Scan(&data)
|
|
|
|
|
|
|
|
//g.Log().Printf(ctx, "res1: %v", gjson.MustEncodeString(data))
|
|
|
|
res1 := []game.RoleOnlineTop{}
|
|
|
|
for _, v := range data {
|
|
|
|
index := isContain(res1, v.Date, v.Server)
|
|
|
|
if index == -1 {
|
|
|
|
v1 := game.RoleOnlineTop{
|
|
|
|
Server: v.Server,
|
|
|
|
Date: v.Date,
|
|
|
|
Num: v.Num,
|
|
|
|
}
|
|
|
|
res1 = append(res1, v1)
|
|
|
|
} else {
|
|
|
|
res1[index].Num += v.Num
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//g.Log().Printf(ctx, "res1: %v", gjson.MustEncodeString(res1))
|
|
|
|
for _, v := range res1 {
|
|
|
|
index := isContain(res.Onlines, "", v.Server)
|
|
|
|
if index == -1 {
|
|
|
|
v1 := game.RoleOnlineTop{
|
|
|
|
Server: v.Server,
|
|
|
|
Date: "",
|
|
|
|
Num: v.Num,
|
|
|
|
}
|
|
|
|
res.Onlines = append(res.Onlines, v1)
|
|
|
|
} else {
|
|
|
|
if res.Onlines[index].Num < v.Num {
|
|
|
|
res.Onlines[index].Num = v.Num
|
|
|
|
}
|
|
|
|
//g.Log().Printf(ctx, "v1: %v, v: %v", gjson.MustEncodeString(res.Onlines[index]), gjson.MustEncodeString(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.Log().Printf(ctx, "res.Onlines: %v", gjson.MustEncodeString(res.Onlines))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func isContain(data []game.RoleOnlineTop, date string, server int) int {
|
|
|
|
for k, v := range data {
|
|
|
|
if date == v.Date && server == v.Server {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *gameRoleImpl) GetAccount(ctx context.Context, req *game.AccountReq) (res *game.AccountRes, err error) {
|
|
|
|
res, err = internal.GetAccount(ctx, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *gameRoleImpl) GameRegister(ctx context.Context, req *game.RegisterReq) (res *game.RegisterRes, err error) {
|
|
|
|
//g.Log().Info(ctx, "LoginOut - json: ", gjson.MustEncodeString(req))
|
|
|
|
res, err = internal.GameRegister(ctx, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *gameRoleImpl) GameUnitChangeName(ctx context.Context, req *game.UnitChangeNameReq) (res *game.UnitChangeNameRes, err error) {
|
|
|
|
g.Log().Info(ctx, "GameUnitChangeName - json: ", gjson.MustEncodeString(req))
|
|
|
|
res, err = internal.GameUnitChangeName(ctx, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *gameRoleImpl) GetLoginOut(ctx context.Context, req *game.GetLoginOutReq) (res *game.GetLoginOutRes, err error) {
|
|
|
|
res, err = internal.GetLoginOut(ctx, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *gameRoleImpl) GetLoginOutLog(ctx context.Context, req *game.GetLoginOutLogReq) (res *game.GetLoginOutLogRes, err error) {
|
|
|
|
res, err = internal.GetLoginOutLog(ctx, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *gameRoleImpl) GetItemLog(ctx context.Context, req *game.GetItemLogReq) (res *game.GetItemLogRes, err error) {
|
|
|
|
res, err = internal.GetItemLog(ctx, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *gameRoleImpl) GetRegister(ctx context.Context, req *game.GetRegisterReq) (res *game.GetRegisterRes, err error) {
|
|
|
|
res, err = internal.GetRegister(ctx, req)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *gameRoleImpl) GameUserDel(ctx context.Context, req *game.UserDelReq) (res *game.UserDelRes, err error) {
|
|
|
|
res, err = internal.GameUserDel(ctx, req)
|
|
|
|
return res, err
|
|
|
|
}
|