2025-07-14 09:22:49 +08:00

173 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package domain
import (
"admin/apps/game/domain/entity"
"admin/apps/game/domain/projects"
"admin/apps/game/model"
"admin/internal/consts"
"admin/internal/errcode"
"bytes"
"fmt"
"sort"
"strconv"
"strings"
"text/template"
"time"
)
type CdnServerInfo struct {
Properties [8]struct {
Key string
Value string
}
}
type CdnNoticeInfo struct {
Sn int
UpdataMod string
Title string
Content string
}
type CdnServerNoticeInfo struct {
ServerList []*CdnServerInfo
NoticeVersion string
NoticeList []*CdnNoticeInfo
}
// todo 根据项目获取不同的cdn推送内容
func genCdnServerListContent(projectEt *entity.Project, serverList, noticeList []*entity.CommonResource) (string, error) {
info := &CdnServerNoticeInfo{}
hook, _ := projects.GetProjectResourceHook(projectEt, consts.ResourcesName_Server).(projects.IServerInfoHook)
for _, server := range serverList {
serverDbInfo := server.Po.(*model.Server)
if hook != nil {
serverDbInfo.IsServerDown = hook.IsServerDownStatus(projectEt, serverDbInfo)
}
serverInfo := &CdnServerInfo{}
serverInfo.Properties[0].Key = "id"
serverInfo.Properties[0].Value = serverDbInfo.ServerConfID
addr := strings.Split(serverDbInfo.ClientConnAddr, ":")
serverInfo.Properties[1].Key = "ip"
serverInfo.Properties[1].Value = ""
if len(addr) == 2 {
serverInfo.Properties[1].Value = addr[0]
}
serverInfo.Properties[2].Key = "name"
serverInfo.Properties[2].Value = serverDbInfo.Desc
serverInfo.Properties[3].Key = "port"
serverInfo.Properties[3].Value = ""
if len(addr) == 2 {
serverInfo.Properties[3].Value = addr[1]
}
serverInfo.Properties[4].Key = "recommend"
serverInfo.Properties[4].Value = "0"
serverInfo.Properties[5].Key = "status"
serverInfo.Properties[5].Value = fmt.Sprintf("%v", getCdnServerStatus(serverDbInfo))
serverInfo.Properties[6].Key = "area"
serverId, _ := strconv.Atoi(serverDbInfo.ServerConfID)
serverInfo.Properties[6].Value = getCdnServerArea(serverId)
serverInfo.Properties[7].Key = "type"
serverInfo.Properties[7].Value = "1"
info.ServerList = append(info.ServerList, serverInfo)
}
info.NoticeVersion = time.Now().Format("200601021504")
sort.SliceStable(noticeList, func(i, j int) bool {
infoI := noticeList[i].Po.(*model.Notice)
infoJ := noticeList[j].Po.(*model.Notice)
return infoI.SortWeight > infoJ.SortWeight
})
for i, notice := range noticeList {
noticeDbInfo := notice.Po.(*model.Notice)
noticeInfo := &CdnNoticeInfo{}
noticeInfo.Sn = i + 1
noticeInfo.UpdataMod = noticeDbInfo.Mod
noticeInfo.Title = noticeDbInfo.Title
noticeInfo.Content = noticeDbInfo.Content
info.NoticeList = append(info.NoticeList, noticeInfo)
}
bytesBuffer := bytes.NewBufferString("")
tpl, err := template.New("").Parse(cdnServerListContentTemplate)
if err != nil {
return "", errcode.New(errcode.ServerError, "new render template error:%v", err)
}
err = tpl.Execute(bytesBuffer, info)
if err != nil {
return "", errcode.New(errcode.ServerError, "execute render template error:%v", err)
}
return bytesBuffer.String(), nil
}
func getCdnServerArea(serverId int) string {
// 神魔大陆每4个服对应一个区从20001开始
return fmt.Sprintf("%v区", (serverId-20001)/4+1)
}
func getCdnServerStatus(server *model.Server) int {
if server.IsServerDown {
return consts.CdnServerStatus_Maintain
}
switch server.ServerStatus {
case consts.ServerStatus_Normal:
return consts.CdnServerStatus_Normal
case consts.ServerStatus_Busy:
return consts.CdnServerStatus_Busy
case consts.ServerStatus_Full:
return consts.CdnServerStatus_Full
}
return consts.CdnServerStatus_Maintain
}
var cdnServerListContentTemplate = `
local Data = {}
Data.versionCode = "2.2.2"
Data.typenName = "iOS服务器"
--测试服务器
Data.apptest = {}
Data.zhuboAcount ={}
--正式服务器
Data.appOnline = {}
{{ range .ServerList }}
table.insert(Data.appOnline,{
{{- range .Properties }}
{{ .Key }} = "{{ .Value }}",
{{- end }}
})
{{- end }}
Data.notice = {}
Data.notice.version = "{{ .NoticeVersion }}"
Data.notice.info = {
{{- range .NoticeList }}
{sn={{.Sn}},updataMod="{{.UpdataMod}}",title="{{.Title}}",updataTxt="{{.Content}}"},
{{- end }}
}
Data.loginResult = {
Code1 = 99989,
Content1 = "服务器正在维护中",
Code2 = 99988,
Content2 = "服务器正在维护中",
}
return Data
`