排行榜优化

This commit is contained in:
hahwu 2026-03-12 19:23:59 +08:00
parent 9b653d37e6
commit ff3c6468cf
12 changed files with 127 additions and 28 deletions

2
.gitignore vendored
View File

@ -20,3 +20,5 @@ src/server/test/teLog/*
src/server/*.exe*
src/server/msg/Gameapi.pb.go
src/server/gen-go
src/server/GeoLite2-Country
src/server/test/GeoLite2-Country

View File

@ -72,6 +72,7 @@ func (p *Player) ActivityFire(ChargeId int) {
return
}
if Item == nil {
log.Debug("ActivityFire err : %s", err)
return
}
err = p.HandleItem(Item, proto.ITEM_POP_LABEL_ActivityGift.String())

View File

@ -612,9 +612,7 @@ func ReqGmCommand_(player *Player, Command string) error {
BaseMod.AddCode = fmt.Sprintf("MMM-%s-%s", "156", GoUtil.UniqueStringFromInt(int(BaseMod.Uid)))
player.BackUserInfo()
case "debug":
player.AddPlayroomUpvote(100100129)
i := player.GetPlayroomUpvote(100100129)
log.Debug("debug upvote:%d", i)
player.HandleInUserRank()
case "addLimitEvent":
Id, _ := strconv.Atoi(arg[1])
Cd, _ := strconv.Atoi(arg[2])

View File

@ -45,9 +45,15 @@ type Base struct {
AddCode string // 用于添加好友的code
DiviceId string // 设备id
RegisterTime int64 // 注册时间
CountryCode string // 国家码
}
func (b *Base) InitData(Uid int, Ip string) {
if b.CountryCode == "" {
Ip = GoUtil.GetIPFromAddr(Ip)
code, _ := GoUtil.GetCountryByIP(Ip)
b.CountryCode = code
}
if b.AddCode == "" && Ip != "" {
Code, _ := GoUtil.GetCountryByIP(Ip)
CountryCode := conf.Server.CountryCode

View File

@ -1110,6 +1110,9 @@ func (p *Player) HandleInUserRank() {
Uid: int(p.M_DwUin),
Score: float64(Score),
RankType: RANK_TYPE_USER,
Extra: map[string]interface{}{
"country": p.PlayMod.getBaseMod().CountryCode,
},
},
}
G_GameLogicPtr.RankMgrSend(m)

View File

@ -48,6 +48,7 @@ type RankMsg struct {
Uid int
Score float64
RankType int
Extra map[string]interface{}
}
func (r *RankMgr) Init() {
@ -100,13 +101,25 @@ func (r *RankMgr) getRank(RankType int) []*Rank {
// 获取排行榜信息
func (r *RankMgr) getRankInfo(m *msg.Msg) (interface{}, error) {
data := m.Extra.(RankMsg)
data, ok := m.Extra.(map[string]interface{})
if !ok {
return &RankInfo{
List: []*Rank{},
MyRank: 0,
MyScore: 0,
}, nil
}
// 全球排行榜
if data.RankType == RANK_TYPE_GLOBAL {
rank_type := RANK_TYPE_GLOBAL
info, ok := data["rank_type"]
if ok {
rank_type = GoUtil.Int(info)
}
if rank_type == RANK_TYPE_GLOBAL {
return r.getRedisRankInfo(m)
}
// 国家排行榜
if data.RankType == RANK_TYPE_USER {
if rank_type == RANK_TYPE_USER {
return r.getRedisCountryRankInfo(m)
}
return &RankInfo{
@ -117,7 +130,16 @@ func (r *RankMgr) getRankInfo(m *msg.Msg) (interface{}, error) {
}
func (r *RankMgr) getRedisCountryRankInfo(m *msg.Msg) (interface{}, error) {
RedisKey := fmt.Sprintf("%s_%s", RANK_COUNTRY_USER, conf.Server.CountryCode)
data, ok := m.Extra.(map[string]interface{})
if !ok {
return &RankInfo{}, nil
}
country := "000"
info, ok := data["country"]
if ok && info != "" {
country = info.(string)
}
RedisKey := fmt.Sprintf("%s_%s", RANK_COUNTRY_USER, country)
RedisList, err := db.RedisZRevRangeWithScores(RedisKey, 0, 100)
if err != nil {
return &RankInfo{}, nil
@ -167,16 +189,22 @@ func (r *RankMgr) getRedisRankInfo(m *msg.Msg) (interface{}, error) {
// 进入排行榜
func (r *RankMgr) inRank(m *msg.Msg) (interface{}, error) {
data := m.Extra.(RankMsg)
data, ok := m.Extra.(RankMsg)
if !ok {
return nil, fmt.Errorf("invalid rank msg")
}
if data.RankType == RANK_TYPE_USER {
// 全球玩家排行榜
Uid := strconv.Itoa(data.Uid)
TimeSort := fmt.Sprintf("0.%d", RANK_TIME_SORT-GoUtil.Now())
TimeSortF, _ := strconv.ParseFloat(TimeSort, 64)
db.RedisZAdd(RANK_USER, Uid, data.Score+TimeSortF)
info, ok := data.Extra["country"]
if !ok || info == "" {
info = "000"
}
// 地区玩家排行榜
RedisKey := fmt.Sprintf("%s_%s", RANK_COUNTRY_USER, conf.Server.CountryCode)
RedisKey := fmt.Sprintf("%s_%s", RANK_COUNTRY_USER, info)
db.RedisZAdd(RedisKey, Uid, data.Score+TimeSortF)
}

View File

@ -2333,10 +2333,14 @@ func ReqRank(player *Player, buf []byte) error {
if err != nil {
return err
}
BaseMod := player.PlayMod.getBaseMod()
m := &MsqMod.Msg{
From: int(player.M_DwUin),
Type: MsqMod.HANDLE_TYPE_RANK_INFO,
Extra: RankMsg{RankType: int(req.Type)},
From: int(player.M_DwUin),
Type: MsqMod.HANDLE_TYPE_RANK_INFO,
Extra: map[string]interface{}{
"country": BaseMod.CountryCode,
"rank_type": int(req.Type),
},
SendT: GoUtil.Now(),
}
RankInfo := G_GameLogicPtr.RankMgrCall(m).(*RankInfo)

View File

@ -13,6 +13,7 @@ import (
"fmt"
"io"
"math/rand"
"net"
"net/http"
"reflect"
"server/pkg/github.com/name5566/leaf/log"
@ -20,6 +21,8 @@ import (
"strings"
"sync"
"time"
"github.com/oschwald/geoip2-golang"
)
var bufferPool = sync.Pool{
@ -478,15 +481,28 @@ func GenerateShuffledAlphabet() string {
// 根据IP获取国家名称
func GetCountryByIP(ip string) (string, error) {
return "", nil
// resp, err := http.Get("https://ipapi.co/" + ip + "/country_name/")
// if err != nil {
// return "", err
// }
// defer resp.Body.Close()
// buf := new(bytes.Buffer)
// buf.ReadFrom(resp.Body)
// return strings.TrimSpace(buf.String()), nil
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return "", fmt.Errorf("invalid ip: %s", ip)
}
db, err := geoip2.Open("./GeoLite2-Country/GeoLite2-City.mmdb")
if err != nil {
return "", err
}
defer db.Close()
record, err := db.Country(parsedIP)
if err != nil {
return "", err
}
countryName := record.Country.Names["en"]
if countryName == "" {
return "", fmt.Errorf("country name not found for ip %s", ip)
}
return GetISOCodeByCountry(countryName)
}
// 根据国家名称获取ISO 3166-1国家码
@ -569,11 +585,7 @@ func GetISOCodeByCountry(country string) (string, error) {
// 综合函数根据IP获取ISO 3166-1国家码
func GetISOCodeByIP(ip string) (string, error) {
country, err := GetCountryByIP(ip)
if err != nil {
return "", err
}
return GetISOCodeByCountry(country)
return GetCountryByIP(ip)
}
func GetVarKey(Uid int) string {
@ -658,3 +670,35 @@ func ElemNumber(list []int, ele int) int {
}
return count
}
func GetIPFromAddr(addr string) string {
ip, _, err := net.SplitHostPort(addr)
if err != nil {
return ""
}
return ip
}
func GetGeoInfo(ip string) (string, string, error) {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return "", "", fmt.Errorf("invalid ip: %s", ip)
}
db, err := geoip2.Open("./GeoLite2-Country/GeoLite2-City.mmdb")
if err != nil {
return "", "", err
}
defer db.Close()
record, err := db.Country(parsedIP)
if err != nil {
return "", "", err
}
countryCode, err := GetISOCodeByCountry(record.Country.Names["en"])
if err != nil {
return "", "", err
}
return countryCode, record.Country.IsoCode, nil
}

View File

@ -48,6 +48,8 @@ require (
require (
github.com/apache/thrift v0.22.0 // indirect
github.com/oschwald/geoip2-golang v1.13.0 // indirect
github.com/oschwald/maxminddb-golang v1.13.0 // indirect
golang.org/x/sys v0.29.0 // indirect
)

View File

@ -146,6 +146,10 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/oschwald/geoip2-golang v1.13.0 h1:Q44/Ldc703pasJeP5V9+aFSZFmBN7DKHbNsSFzQATJI=
github.com/oschwald/geoip2-golang v1.13.0/go.mod h1:P9zG+54KPEFOliZ29i7SeYZ/GM6tfEL+rgSn03hYuUo=
github.com/oschwald/maxminddb-golang v1.13.0 h1:R8xBorY71s84yO06NgTmQvqvTvlS/bnYZrrWX1MElnU=
github.com/oschwald/maxminddb-golang v1.13.0/go.mod h1:BU0z8BfFVhi1LQaonTwwGQlsHUEu9pWNdMfmq4ztm0o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=

View File

@ -31,5 +31,5 @@ func TestWatchAd(t *testing.T) {
func TestActivityGiftAdd(t *testing.T) {
p1 := new(game.Player)
p1.InitPlayer("3625212")
p1.Charge(76)
p1.Charge(71)
}

View File

@ -285,3 +285,10 @@ func TestChampionShipCreateRobot(t *testing.T) {
func TestRedisZset(t *testing.T) {
db.RedisZAdd("rank_user", "100001", 100)
}
func TestGeoIp(t *testing.T) {
ip := "117.30.95.111:5557"
ip = GoUtil.GetIPFromAddr(ip)
code, _ := GoUtil.GetCountryByIP(ip)
fmt.Printf("IP: %s, Country Code: %s\n", ip, code)
}