Browse Source

refactor(internal/service): 重构游戏盾后台相关功能

- 更新端口格式化函数,增加函数注释
- 优化旧格式转换函数,提高代码可读性- 在游戏盾后台数据处理中添加最大带宽字段处理逻辑- 移除无效代码,提高代码整洁度
fusu 2 months ago
parent
commit
52cc83bc86
2 changed files with 10 additions and 1 deletions
  1. 3 1
      internal/service/formatter.go
  2. 7 0
      internal/service/gameshieldbackend.go

+ 3 - 1
internal/service/formatter.go

@@ -75,6 +75,7 @@ func (service *formatterService) FormatBackendData(ctx context.Context, req *v1.
 	return string(jsonBytes), nil
 }
 
+// FormatPort 格式化端口
 func (service *formatterService) FormatPort(ctx context.Context, req interface{}) []int {
 	if req == nil {
 		return []int{}
@@ -101,6 +102,7 @@ func (service *formatterService) FormatPort(ctx context.Context, req interface{}
 	return res
 }
 
+// OldFormat 旧格式
 func (service *formatterService) OldFormat(ctx context.Context, req *[]model.GameShieldBackend) (map[string]v1.SendGameShieldBackend, error) {
 	res := make(map[string]v1.SendGameShieldBackend)
 	var UdpSessionTimeout string
@@ -214,7 +216,6 @@ func (service *formatterService) TidyFormatBackendData(ctx context.Context, req
 				}
 				itemMap.SdkPort = sdkPorts[i]
 			}
-
 			output[key] = itemMap
 		}
 	}
@@ -257,6 +258,7 @@ func (service *formatterService) ValidateBackendData(ctx context.Context, data m
 	maxBandwidthCount := int64(0)
 
 	for _, item := range data {
+
 		// 计算规则条目数
 		ruleEntriesCount += int64(len(item.Addr))
 

+ 7 - 0
internal/service/gameshieldbackend.go

@@ -7,6 +7,7 @@ import (
 	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
 	"github.com/go-nunu/nunu-layout-advanced/internal/model"
 	"github.com/go-nunu/nunu-layout-advanced/internal/repository"
+	"github.com/spf13/cast"
 	"github.com/spf13/viper"
 	"strconv"
 	"strings"
@@ -274,6 +275,11 @@ func (s *gameShieldBackendService) SaveGameShieldBackend(ctx context.Context, re
 		if v.Type != "pc" {
 			v.SdkIp = "127.0.0.1"
 		}
+		if v.MaxBandwidth == "100m" {
+			v.MaxBandwidth = "1"
+		} else {
+			v.MaxBandwidth = "0"
+		}
 		if err := s.gameShieldBackendRepository.AddGameShieldBackend(ctx,
 			&model.GameShieldBackend{
 				HostId:          hostId,
@@ -285,6 +291,7 @@ func (s *gameShieldBackendService) SaveGameShieldBackend(ctx context.Context, re
 				SdkIp:           v.SdkIp,
 				SdkPort:         strconv.Itoa(v.SdkPort),
 				Type:            v.Type,
+				MaxBandwidth:    cast.ToInt(v.MaxBandwidth),
 				// 可以添加其他字段
 			}); err != nil {
 			return err