LollipopGo开源游戏服务器框架--匹配优化

栏目: Go · 发布时间: 6年前

内容简介:大家好,我是彬哥,本节给大家讲下LollipopGov1.0.20190114版本游戏服务器1v1匹配功能优化,抛砖引玉了,主要是针对Go语言游戏服务器匹配功能优化功能处理。每天坚持学习1小时Go语言,大家加油,我是彬哥,下期见!如果文章中不同观点、意见请文章下留言或者关注下方订阅号反馈!

大家好,我是彬哥,本节给大家讲下LollipopGov1.0.20190114版本游戏服务器1v1匹配功能优化,抛砖引玉了,主要是针对 Go 语言游戏服务器匹配功能优化功能处理。

LollipopGo开源游戏服务器框架--匹配优化

匹配示意图

package match

import (
    "LollipopGo/LollipopGo/player"
    "LollipopGo/LollipopGo/util"
    "cache2go"
    "fmt"
    "time"
)

//------------------------------------------------------------------------------

var (
    Match_Chan       chan *player.PlayerSt
    MatchData_Chan   chan map[string]interface{}
    Imax             int = 0
    ChanMax          int = 1000
    MatchSpeed           = time.Millisecond * 500
    PlaterMatchSpeed     = time.Second * 1
    // MatchData        map[string]*RoomMatch
    MatchData     map[string]interface{}
    QuitMatchData map[string]string
    cache         *cache2go.CacheTable
    MatchRoomUID  int = 1000
)

type RoomMatchbak struct {
    RoomUID    string           // 房间号
    DataPlayer *player.PlayerSt // 房间的时间限制
}

//------------------------------------------------------------------------------

type RoomMatch struct {
    RoomUID       string                      // 房间号
    PlayerAOpenID string                      // A 阵营的OpenID
    PlayerBOpenID string                      // B 阵营的OpenID
    RoomLimTime   uint64                      // 房间的时间限制
    RoomPlayerMap map[string]*player.PlayerSt // 房间玩家的结构信息
}

//------------------------------------------------------------------------------

func init() {
    Match_Chan = make(chan *player.PlayerSt, ChanMax)
    //  MatchData_Chan = make(chan map[string]*RoomMatch, ChanMax)
    MatchData_Chan = make(chan map[string]interface{}, ChanMax)
    QuitMatchData = make(map[string]string)
    cache = cache2go.Cache("myCache")
    go Sort_timer()
}

func Putdata(data *player.PlayerSt) {
    fmt.Println("加入匹配队列")
    Match_Chan <- data
    return
}

func GetChanLength() int {
    Imax = len(Match_Chan)
    return Imax
}

func DoingMatch() {
    Imax = len(Match_Chan)
    if Imax == 1 {
        fmt.Println(Match_Chan, "等待匹配")
        return
    }

    roomid := ""
    icround := Imax / 2

    MatchData = make(map[string]interface{})
    datamatch := new(RoomMatch)
    datamatch.RoomPlayerMap = make(map[string]*player.PlayerSt)

    for i := 1; i < icround*2+1; i++ {

        if data, ok := <-Match_Chan; ok {
            fmt.Println("3333333333333333333333", data)
            //          if GetMatchPlayer(data.OpenID) {
            //              fmt.Println(data.OpenID, "玩家已经退出!")
            //              continue
            //          }

            datamatch.RoomLimTime = 10
            roomid = util.Int2str_LollipopGo(MatchRoomUID)
            datamatch.RoomUID = roomid
            datamatch.RoomPlayerMap[data.OpenID] = data
        }

        if i%2 == 0 {
            //datamatch.PlayerBOpenID = data.OpenID
            MatchData[roomid] = datamatch
            MatchData_Chan <- MatchData
            fmt.Println("0------------", MatchData_Chan)
            MatchRoomUID++
        }
    }
}

func Sort_timer() {
    for {
        select {
        case <-time.After(MatchSpeed):
            {
                DoingMatch()
            }
        }
    }
}

func SetQuitMatch(OpenID string) {
    cache.Add(OpenID+"QuitMatch", 0, "exit")
}

func DelQuitMatchList(OpenID string) {
    cache.Delete(OpenID + "QuitMatch")
}

func GetMatchPlayer(OpenID string) bool {
    ok := false
    _, err1 := cache.Value(OpenID + "QuitMatch")
    if err1 == nil {
        ok = true
    }
    return ok
}

func GetMatchQueue(OpenID string) bool {
    ok := false
    _, err1 := cache.Value(OpenID + "MatchQueue")
    if err1 == nil {
        ok = true
    }
    return ok
}

func SetMatchQueue(OpenID string) {
    cache.Add(OpenID+"MatchQueue", 0, "exit")
    DelQuitMatchList(OpenID)
}

func DelMatchQueue(OpenID string) {
    cache.Delete(OpenID + "MatchQueue")
}

每天坚持学习1小时Go语言,大家加油,我是彬哥,下期见!如果文章中不同观点、意见请文章下留言或者关注下方订阅号反馈!

社区交流群:221273219

Golang语言社区论坛 :

www.Golang.Ltd

LollipopGo游戏服务器地址:

https://github.com/Golangltd/LollipopGo

社区视频课程课件GIT地址:

https://github.com/Golangltd/codeclass
LollipopGo开源游戏服务器框架--匹配优化

Golang语言社区


以上所述就是小编给大家介绍的《LollipopGo开源游戏服务器框架--匹配优化》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Applications (Hacking Exposed)

Web Applications (Hacking Exposed)

Joel Scambray、Mike Shema / McGraw-Hill Osborne Media / 2002-06-19 / USD 49.99

Get in-depth coverage of Web application platforms and their vulnerabilities, presented the same popular format as the international bestseller, Hacking Exposed. Covering hacking scenarios across diff......一起来看看 《Web Applications (Hacking Exposed)》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具