文档及许可
This commit is contained in:
parent
3f453efef6
commit
f6f8bbebc3
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2023 yuanqi
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
|
@ -0,0 +1,4 @@
|
||||||
|
// Package components 通用组件的内置实现
|
||||||
|
// - lockstep.go 帧同步组件
|
||||||
|
// - lockstep_options.go 帧同步组件可选项
|
||||||
|
package components
|
|
@ -0,0 +1,4 @@
|
||||||
|
// Package component 通用组件的接口定义
|
||||||
|
// - lockstep.go 帧同步组件接口定义
|
||||||
|
// - lockstep_client.go 帧同步组件客户端接口定义
|
||||||
|
package component
|
|
@ -1,5 +1,6 @@
|
||||||
package component
|
package component
|
||||||
|
|
||||||
|
// Lockstep 帧同步组件接口定义
|
||||||
type Lockstep[ClientID comparable, Command any] interface {
|
type Lockstep[ClientID comparable, Command any] interface {
|
||||||
// JoinClient 加入客户端
|
// JoinClient 加入客户端
|
||||||
JoinClient(client LockstepClient[ClientID])
|
JoinClient(client LockstepClient[ClientID])
|
||||||
|
@ -28,5 +29,6 @@ type Lockstep[ClientID comparable, Command any] interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// LockstepStoppedEventHandle 帧同步停止广播事件处理函数
|
||||||
LockstepStoppedEventHandle[ClientID comparable, Command any] func(lockstep Lockstep[ClientID, Command])
|
LockstepStoppedEventHandle[ClientID comparable, Command any] func(lockstep Lockstep[ClientID, Command])
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
package component
|
package component
|
||||||
|
|
||||||
import "github.com/kercylan98/minotaur/game"
|
// LockstepClient 帧同步客户端接口定义
|
||||||
|
// - 客户端应该具备ID及写入数据包的实现
|
||||||
type LockstepClient[ID comparable] interface {
|
type LockstepClient[ID comparable] interface {
|
||||||
game.Player[ID]
|
// GetID 用户玩家ID
|
||||||
|
GetID() ID
|
||||||
|
// Send 发送数据包
|
||||||
|
// - messageType: websocket模式中指定消息类型
|
||||||
|
Send(packet []byte, messageType ...int)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// LoadHandle 配置加载处理函数
|
||||||
type LoadHandle func(handle func(filename string, config any) error)
|
type LoadHandle func(handle func(filename string, config any) error)
|
||||||
|
|
||||||
|
// RefreshHandle 配置刷新处理函数
|
||||||
type RefreshHandle func()
|
type RefreshHandle func()
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -28,12 +31,14 @@ var (
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Init 配置初始化
|
||||||
func Init(loadDir string, loadHandle LoadHandle, refreshHandle RefreshHandle) {
|
func Init(loadDir string, loadHandle LoadHandle, refreshHandle RefreshHandle) {
|
||||||
cLoadDir = loadDir
|
cLoadDir = loadDir
|
||||||
cLoadHandle = loadHandle
|
cLoadHandle = loadHandle
|
||||||
cRefreshHandle = refreshHandle
|
cRefreshHandle = refreshHandle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load 加载配置
|
||||||
func Load() {
|
func Load() {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
if cTicker != nil {
|
if cTicker != nil {
|
||||||
|
@ -53,6 +58,7 @@ func Load() {
|
||||||
mutex.Unlock()
|
mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithTickerLoad 通过定时器加载配置
|
||||||
func WithTickerLoad(ticker *timer.Ticker, interval time.Duration) {
|
func WithTickerLoad(ticker *timer.Ticker, interval time.Duration) {
|
||||||
if ticker != cTicker && cTicker != nil {
|
if ticker != cTicker && cTicker != nil {
|
||||||
cTicker.StopTimer(tickerLoadRefresh)
|
cTicker.StopTimer(tickerLoadRefresh)
|
||||||
|
@ -65,12 +71,14 @@ func WithTickerLoad(ticker *timer.Ticker, interval time.Duration) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StopTickerLoad 停止通过定时器加载配置
|
||||||
func StopTickerLoad() {
|
func StopTickerLoad() {
|
||||||
if cTicker != nil {
|
if cTicker != nil {
|
||||||
cTicker.StopTimer(tickerLoadRefresh)
|
cTicker.StopTimer(tickerLoadRefresh)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Refresh 刷新配置
|
||||||
func Refresh() {
|
func Refresh() {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
cRefreshHandle()
|
cRefreshHandle()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
// Package config 基于配置导表功能实现的配置加载及刷新功能
|
||||||
|
package config
|
|
@ -1,11 +1,12 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
type RefreshEvent func()
|
// RefreshEventHandle 配置刷新事件处理函数
|
||||||
|
type RefreshEventHandle func()
|
||||||
|
|
||||||
var configRefreshEventHandles []func()
|
var configRefreshEventHandles []func()
|
||||||
|
|
||||||
// RegConfigRefreshEvent 当配置刷新时将立即执行被注册的事件处理函数
|
// RegConfigRefreshEvent 当配置刷新时将立即执行被注册的事件处理函数
|
||||||
func RegConfigRefreshEvent(handle RefreshEvent) {
|
func RegConfigRefreshEvent(handle RefreshEventHandle) {
|
||||||
configRefreshEventHandles = append(configRefreshEventHandles, handle)
|
configRefreshEventHandles = append(configRefreshEventHandles, handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
// Package examples 提供了多种实现案例
|
||||||
|
package examples
|
|
@ -4,13 +4,8 @@ import "github.com/kercylan98/minotaur/server"
|
||||||
|
|
||||||
// Player 玩家
|
// Player 玩家
|
||||||
type Player[ID comparable] interface {
|
type Player[ID comparable] interface {
|
||||||
// GetID 用户玩家ID
|
|
||||||
GetID() ID
|
|
||||||
// UseConn 指定连接
|
// UseConn 指定连接
|
||||||
UseConn(conn *server.Conn)
|
UseConn(conn *server.Conn)
|
||||||
// Send 发送数据包
|
|
||||||
// - messageType: websocket模式中指定消息类型
|
|
||||||
Send(packet []byte, messageType ...int)
|
|
||||||
// Close 关闭玩家并且释放其资源
|
// Close 关闭玩家并且释放其资源
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
@echo off
|
||||||
|
echo Open: http://localhost:9998/pkg/github.com/kercylan98/minotaur/
|
||||||
|
godoc -http=:9998 -play
|
|
@ -0,0 +1,2 @@
|
||||||
|
echo Open: http://localhost:9998/pkg/github.com/kercylan98/minotaur/
|
||||||
|
godoc -http=:9998 -play
|
Loading…
Reference in New Issue