==========================================================================================================================================================================================================================================================================================================================

支持Redcon(RESP)协议,为你的在线游戏提供简单的规则匹配。
1- 简单匹配规则
系统最简单的用法是 "直接匹配规则",在这个规则中,玩家相互匹配,没有任何规则。
package main
import (
"github.com/fatihkahveci/simple-matchmaking/matchmaking"
"github.com/fatihkahveci/simple-matchmaking/server"
"github.com/fatihkahveci/simple-matchmaking/store"
"time"
)
func main() {
inMemory := store.NewInMemoryStore()
dur, _ := time.ParseDuration("10s")
r := matchmaking.NewDirectMatchRule()
respServer := server.NewRespServer(inMemory, ":1234")
matcher := matchmaking.NewMatchmaking("test",respServer,inMemory, r, dur)
matcher.Start()
}
2-得分比赛规则
在这个例子中,用户与给定的分数规则相匹配,这意味着只有用户在15分之间才会发生匹配。
package main
import (
"github.com/fatihkahveci/simple-matchmaking"
"github.com/fatihkahveci/simple-matchmaking/rules"
"github.com/fatihkahveci/simple-matchmaking/server"
"github.com/fatihkahveci/simple-matchmaking/store"
"time"
)
func main() {
inMemory := store.NewInMemoryStore()
dur, _ :=time.ParseDuration("10s")
r := rules.NewScoreMatchRule(10,15)
respServer := server.NewRespServer(inMemory, ":1234")
matcher := simpe_mm.NewMatchmaking("score",respServer,inMemory, r, dur)
matcher.Start()
}
3- 自定义比赛规则
在这个例子中,我们创建自己的规则。我们只需要遵循 "MatchRule "界面。
package main
import (
"github.com/fatihkahveci/simple-matchmaking"
"github.com/fatihkahveci/simple-matchmaking/server"
"github.com/fatihkahveci/simple-matchmaking/store"
"time"
)
type CustomFieldMatchRule struct {
Field string
MinThreshold int
MaxThreshold int
}
func NewCustomFieldMatchRule(field string,minThreshold, maxThreshold int) CustomFieldMatchRule {
return CustomFieldMatchRule{
Field: field,
MinThreshold: minThreshold,
MaxThreshold: maxThreshold,
}
}
func (r CustomFieldMatchRule) Match(user1, user2 store.User) bool {
user1Level := user1.Fields[r.Field].(int)
user2Level := user2.Fields[r.Field].(int)
minLevel := user1Level - r.MinThreshold
maxLevel := user1Level + r.MaxThreshold
if user2Level >= minLevel && user2Level <= maxLevel {
return true
}
return false
}
func (r CustomFieldMatchRule) GetName() string {
return "CustomField"
}
func main() {
inMemory := store.NewInMemoryStore()
dur, _ :=time.ParseDuration("10s")
r := NewCustomFieldMatchRule("level",10,20)
respServer := server.NewRespServer(inMemory, ":1234")
matcher := simpe_mm.NewMatchmaking("custom",respServer,inMemory, r, dur)
matcher.Start()
}
使用方法
你可以使用任何Redis客户端添加新的用户到池中。感谢Redcon ❤️
redis-cli -p 1234 add '{
"id": "55",
"score": 5
}'
同时你需要订阅你的匹配频道。
redis-cli -p 1234
127.0.0.1:1234> subscribe simple
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "simple"
3) (integer) 1
1) "message"
2) "simple"
3) "{\"user_1\":{\"id\":\"12\",\"score\":5,\"join_time\":\"2021-08-31T22:57:27.109609+03:00\",\"fields\":null},\"user_2\":{\"id\":\"55\",\"score\":5,\"join_time\":\"2021-08-31T22:57:27.109614+03:00\",\"fields\":null},\"match_rule_name\":\"Direct\",\"time\":\"2021-08-31T22:57:27.116724+03:00\",\"action_type\":\"match\"}"
TODOS
- 更多的服务器支持(WebSocket等
- 更多存储支持(Redis,Mongo等
- 多用户支持(团队匹配)。
