You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
6.8 KiB
207 lines
6.8 KiB
package serviceGame |
|
|
|
import ( |
|
"context" |
|
"github.com/gogf/gf/v2/encoding/gjson" |
|
"github.com/gogf/gf/v2/frame/g" |
|
"log" |
|
"strings" |
|
"tyj_admin/api/v1/game" |
|
"tyj_admin/internal/consts" |
|
"tyj_admin/internal/dao" |
|
"tyj_admin/internal/model/do" |
|
"tyj_admin/internal/model/entity" |
|
"tyj_admin/internal/serviceGame/internal" |
|
"tyj_admin/library/libUtils" |
|
"tyj_admin/library/liberr" |
|
"tyj_admin/utils" |
|
) |
|
|
|
type IGameLoginUrl interface { |
|
GetLoginUrl(ctx context.Context, req *game.GetLoginUrlReq) (res *game.GetLoginUrlRes, err error) |
|
GetAllLoginUrl(ctx context.Context, req *game.GetAllLoginUrlReq) (res *game.GetAllLoginUrlRes, err error) |
|
AddLoginUrl(ctx context.Context, req *game.AddLoginUrlReq) (res *game.AddLoginUrlRes, err error) |
|
DelLoginUrl(ctx context.Context, req *game.DelLoginUrlReq) (res *game.DelLoginUrlRes, err error) |
|
GetGameLoginUrl(ctx context.Context, req *game.GetGameLoginUrlReq) (res *game.GetGameLoginUrlRes, err error) |
|
ReConfigServer(ctx context.Context, req *game.ReConfigServerReq) (res *game.ReConfigServerRes, err error) |
|
GetAllChannel(ctx context.Context, req *game.GetAllChannelReq) (res *game.GetAllChannelRes, err error) |
|
GetAllCcdUrl(ctx context.Context, req *game.GetAllCcdUrlReq) (res *game.GetAllCcdUrlRes, err error) |
|
} |
|
|
|
type gameLoginUrlImpl struct { |
|
} |
|
|
|
var gameLoginUrlService = gameLoginUrlImpl{} |
|
|
|
func GameLoginUrl() IGameLoginUrl { |
|
return &gameLoginUrlService |
|
} |
|
func (c *gameLoginUrlImpl) GetLoginUrl(ctx context.Context, req *game.GetLoginUrlReq) (res *game.GetLoginUrlRes, err error) { |
|
g.Try(ctx, func(ctx context.Context) { |
|
res = new(game.GetLoginUrlRes) |
|
model := dao.GameLoginUrl.Ctx(ctx) |
|
res.Total, err = model.Count() |
|
liberr.ErrIsNil(ctx, err, "mysql err") |
|
model = model.Page(req.PageNum, req.PageSize) |
|
err = model.Scan(&res.LoginUrlList) |
|
}) |
|
return |
|
} |
|
func (c *gameLoginUrlImpl) GetAllLoginUrl(ctx context.Context, req *game.GetAllLoginUrlReq) (res *game.GetAllLoginUrlRes, err error) { |
|
g.Try(ctx, func(ctx context.Context) { |
|
res = new(game.GetAllLoginUrlRes) |
|
res.LoginUrlList = internal.ServerList |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) AddLoginUrl(ctx context.Context, req *game.AddLoginUrlReq) (res *game.AddLoginUrlRes, err error) { |
|
res = new(game.AddLoginUrlRes) |
|
g.Try(ctx, func(ctx context.Context) { |
|
server := []entity.GameLoginUrl{} |
|
model := dao.GameLoginUrl.Ctx(ctx) |
|
model.WherePri(req.Id).Scan(&server) |
|
if len(server) > 0 { |
|
model.WherePri(req.Id).Update(g.Map{ |
|
dao.GameLoginUrl.Columns().Id: req.Id, |
|
dao.GameLoginUrl.Columns().Host: req.Host, |
|
dao.GameLoginUrl.Columns().Port: req.Port, |
|
dao.GameLoginUrl.Columns().State: req.State, |
|
dao.GameLoginUrl.Columns().Recommend: req.Recommend, |
|
}) |
|
} else { |
|
_, err = model.Insert(&do.GameLoginUrl{Host: req.Host, Port: req.Port, Id: req.Id, State: req.State, Recommend: req.Recommend, Name: "", Difficulty: 1}) |
|
} |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) DelLoginUrl(ctx context.Context, req *game.DelLoginUrlReq) (res *game.DelLoginUrlRes, err error) { |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
if req.Id == 0 { |
|
liberr.ErrIsNil(ctx, err, "错误的id") |
|
return |
|
} |
|
_, e := dao.GameLoginUrl.Ctx(ctx).Where("id=", req.Id).Delete() |
|
if e != nil { |
|
liberr.ErrIsNil(ctx, e, "删除通知失败") |
|
return |
|
} |
|
}) |
|
|
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) GetGameLoginUrl(ctx context.Context, req *game.GetGameLoginUrlReq) (res *game.GetGameLoginUrlRes, err error) { |
|
res = new(game.GetGameLoginUrlRes) |
|
ip := libUtils.GetClientIp(ctx) |
|
list, _ := internal.GetWhiteList(ctx) |
|
blackList, _ := internal.GetBlackList(ctx) |
|
account := req.Account |
|
if req.Channel == consts.Channel_Editor { |
|
if !strings.Contains(account, req.Channel) { |
|
account = req.Channel + account |
|
} |
|
} |
|
|
|
g.Log().Debugf(ctx, "GetWhiteList -> ", req.Account, gjson.MustEncodeString(list), ip) |
|
err = g.Try(ctx, func(ctx context.Context) { |
|
var LoginUrlList []*entity.GameLoginUrl |
|
err = dao.GameLoginUrl.Ctx(ctx).Where("isShow=?", 1).Scan(&LoginUrlList) |
|
var unitData []*entity.GameUnit |
|
if req.Account != "" { |
|
err = dao.GameUnit.Ctx(ctx).Where("channel=?", req.Channel).Where("account=?", req.Account).Scan(&unitData) |
|
} |
|
|
|
if err != nil || len(LoginUrlList) == 0 { |
|
liberr.ErrIsNil(ctx, err, "获取服务器失败") |
|
return |
|
} |
|
|
|
if req.Channel == "000005000000" && req.VersionName != "" { |
|
req.Version = "1.0.2" |
|
} |
|
|
|
logTime := int64(0) |
|
last := 0 |
|
channel := ";" |
|
if internal.LoginUrlChannelList(ctx, req.Channel+req.Version) == 1 { |
|
channel += req.Channel + req.Version |
|
} else if internal.LoginUrlChannelList(ctx, req.Channel) == 1 { |
|
channel += req.Channel |
|
} else if internal.LoginUrlChannelList(ctx, req.Version) == 1 { |
|
channel += req.Version |
|
} |
|
channel += ";" |
|
for _, v := range LoginUrlList { |
|
if !strings.Contains(v.Channel, channel) { |
|
continue |
|
} |
|
|
|
var data = game.LoginUrlData{Id: v.Id, Host: v.Host, Port: v.Port, Difficulty: v.Difficulty, |
|
Recommend: v.Recommend, Name: v.Name, State: v.State, Area: v.Area} |
|
if data.State == consts.Login_URL_STATE_WHITE { |
|
if utils.ContainsWhiteList(list, ip) || utils.ContainsWhiteList(list, req.Channel) { |
|
data.State = consts.Login_URL_STATE_NORMAL |
|
} else { |
|
data.State = consts.Login_URL_STATE_Maintenance |
|
data.Host = consts.DEFAULT_HOST |
|
} |
|
} |
|
if data.State == consts.Login_URL_STATE_BLACK { |
|
if !utils.ContainsBlackList(blackList, req.Channel) || utils.ContainsWhiteList(list, ip) { |
|
data.State = consts.Login_URL_STATE_NORMAL |
|
} else { |
|
data.State = consts.Login_URL_STATE_Maintenance |
|
data.Host = consts.DEFAULT_HOST |
|
} |
|
} |
|
for _, v1 := range unitData { |
|
if v1.Server == data.Id { |
|
data.AccountName = v1.Name |
|
data.Scale = v1.Scale |
|
if logTime < v1.LogTime { |
|
logTime = v1.LogTime |
|
last = v1.Server |
|
} |
|
} |
|
} |
|
res.UrlStr = append(res.UrlStr, data) |
|
} |
|
for i := 0; i < len(res.UrlStr); i++ { |
|
if res.UrlStr[i].Id == last { |
|
res.UrlStr[i].Last = 1 |
|
break |
|
} |
|
} |
|
|
|
g.Log().Debugf(ctx, "GetGameLoginUrl -> ", req.Account, gjson.MustEncodeString(res), last) |
|
return |
|
}) |
|
|
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) ReConfigServer(ctx context.Context, req *game.ReConfigServerReq) (res *game.ReConfigServerRes, err error) { |
|
internal.InitServer(ctx) |
|
internal.InitRechargeServer(ctx) |
|
internal.InitChannel(ctx) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) GetAllChannel(ctx context.Context, req *game.GetAllChannelReq) (res *game.GetAllChannelRes, err error) { |
|
g.Try(ctx, func(ctx context.Context) { |
|
res = new(game.GetAllChannelRes) |
|
res.List = internal.ChannelList |
|
}) |
|
return |
|
} |
|
|
|
func (c *gameLoginUrlImpl) GetAllCcdUrl(ctx context.Context, req *game.GetAllCcdUrlReq) (res *game.GetAllCcdUrlRes, err error) { |
|
res = new(game.GetAllCcdUrlRes) |
|
g.Try(ctx, func(ctx context.Context) { |
|
err = dao.GameCcdVersion.Ctx(ctx).Scan(&res.List) |
|
log.Printf("ccdBox: %v", res.List) |
|
}) |
|
return |
|
}
|
|
|