adjust PlayerList and KeepAlive in go-mc/server

This commit is contained in:
Tnze
2022-06-21 01:12:55 +08:00
parent d94993f34f
commit 59caded2ef
6 changed files with 70 additions and 263 deletions

View File

@ -1,12 +1,8 @@
package server
import (
"context"
"crypto/rsa"
_ "embed"
"sync"
"time"
"github.com/google/uuid"
"github.com/Tnze/go-mc/net"
@ -19,59 +15,3 @@ type GamePlay interface {
// You don't need to close the connection, but to keep not returning while the player is playing.
AcceptPlayer(name string, id uuid.UUID, profilePubKey *rsa.PublicKey, protocol int32, conn *net.Conn)
}
type Game struct {
WorldLocker sync.Mutex
handlers map[int32][]*PacketHandler
components []Component
}
func (g *Game) AcceptPlayer(name string, id uuid.UUID, profilePubKey *rsa.PublicKey, protocol int32, conn *net.Conn) {
conn.Close()
}
type PacketHandler struct {
ID int32
F packetHandlerFunc
}
type packetHandlerFunc func(client *Client, player *Player, packet Packet758) error
type Component interface {
Init(g *Game)
Run(ctx context.Context)
ClientJoin(c *Client, p *Player)
ClientLeft(c *Client, p *Player, reason error)
}
func NewGame(components ...Component) *Game {
g := &Game{
handlers: make(map[int32][]*PacketHandler),
components: components,
}
for _, c := range components {
c.Init(g)
}
return g
}
func (g *Game) AddHandler(ph *PacketHandler) {
g.handlers[ph.ID] = append(g.handlers[ph.ID], ph)
}
func (g *Game) Run(ctx context.Context) {
for _, c := range g.components {
go c.Run(ctx)
}
ticker := time.NewTicker(time.Second / 20)
for {
select {
case <-ticker.C:
g.WorldLocker.Lock()
//g.Dispatcher.Run(g.World)
g.WorldLocker.Unlock()
case <-ctx.Done():
return
}
}
}