[go游戏开发实践]关于匹配机制的测试

关于游戏匹配机制的测试

上篇博文写过关于匹配的实现算法:[go游戏开发实践]游戏匹配机制实现
文中并没有对该算法进行优化并测试,没有验证该算法的可用性。
在测试的过程中发现了一些问题,并做出了优化的方案。

一、先对原来的算法进行测试,看有什么问题

这边还是贴一下整理后的关于匹配需要的结构代码
主要包括3个结构:匹配参数、匹配玩家信息、匹配池

package main

import (
	"time"
)

//============================匹配参数==========================
type MatchParamModel struct {
   
	SavePoolCheckTime uint32  //蓄池检测时间
	SavePoolTime      uint32  //蓄池时间
	SavePoolNum       uint32  //蓄池目标数量
	MinCount          uint32  //队伍最少人数
	Count             uint32  //队伍人数
	MaxWaitTime       uint32  //最长等待时间
	minRangeParam1    uint32  //参数1的最小值
	maxRangeParam1    uint32  //参数1的最大值
	minRangeParam2    uint32  //参数2的最小值
	maxRangeParam2    uint32  //参数2的最大值
}

//========================匹配玩家信息=========================
type MatchPlayerInfo struct {
   
	playerId  uint64  //玩家id
	param1    uint32  //玩家参数1
	param2    uint32  //玩家参数2
	startTime int64   //玩家开始匹配时间
}

func newMatchPlayerInfo(pId uint64, p1 uint32, p2 uint32) *MatchPlayerInfo {
   
	newMatchPlayer := &MatchPlayerInfo{
   }
	newMatchPlayer.param1 = p1
	newMatchPlayer.param2 = p2
	newMatchPlayer.playerId = pId
	newMatchPlayer.startTime = time.Now().Unix()
	return newMatchPlayer
}

func (this *MatchPlayerInfo) waitTime() uint64 {
   
	var res uint64
	if time.Now().Unix() >= this.startTime {
   
		res = uint64(time.Now().Unix() - this.startTime)
	}
	return res
}

//============================匹配池================================
type MatchPool struct {
   
	playerArr     map[uint64]*MatchPlayerInfo  //玩家map
	size          uint32					   //匹配池玩家数量
	maxWaitPlayer *MatchPlayerInfo       	   //最长等待玩家
}

func newMatchPool() *MatchPool {
   
	newPool := &MatchPool{
   }
	newPool.playerArr = make(map[uint64]*MatchPlayerInfo)
	newPool.size = 0
	return newPool
}

func (this *MatchPool) putPlayerIntoPool(pId uint64, p1 uint32, p2 uint32) bool {
   
	newPlayer := newMatchPlayerInfo(pId, p1, p2)
	_, ok := this.playerArr[pId]
	if ok {
   
		return false
	}
	this.playerArr[pId] = newPlayer
	this.size++
	return true
}

func (this *MatchPool) putPlayerArrIntoPool(pArr map[uint64]*MatchPlayerInfo) bool {
   
	res := true
	for _, p := range pArr {
   
		res = res && this.putPlayerIntoPool(p.playerId, p.param1, p.param2)
	}
	return res
}

func (this *MatchPool) removePlayerOutPool(pId uint64) bool {
   
	_, ok := this.playerArr[pId]
	if ok && this.size > 0 {
   
		delete(this.playerArr, pId)
		this.size--
		return true
	} else {
   
		return false
	}
}

func (this *MatchPool) removePlayerArrOutPool(pArr map[uint64]*MatchPlayerInfo) bool {
   
	res := true
	for _, p := range pArr {
   
		res = res && this.removePlayerOutPool(p.playerId)
	}
	return res
}

func (this *MatchPool) selectPlayerByCount(count uint32, res map[uint64]*MatchPlayerInfo) {
   
	for _, p := range this.playerArr {
   
		res[p.playerId] = p
		this.removePlayerOutPool(p.playerId)
		count--
		if count <= 0 {
   
			break
		}
	}
}

func (this *MatchPool) selectPlayerByParam(p1 uint32, p2 uint32, count uint32, res map[uint64]*MatchPlayerInfo) {
   
	times := 1
	for times <= 3 && count > 0 {
   
		for _, p := range this.playerArr {
   
			flag := true
			if times == 1 {
   
				flag = p1 == p.param1 && p2 == p.param2
			} else if times == 2 {
   
				flag = p1 == p.param1
			}
			if flag {
   
				res[p.playerId] = p
				this.removePlayerOutPool(p.playerId)
				count--
			}
			if count <= 0 {
   
				break
			}
		}
		times++
	}
}

func (this *MatchPool) selectPlayerByWaitTime
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值