2020-06-13 18:32:09 -04:00
|
|
|
package d2server
|
|
|
|
|
|
|
|
import (
|
2020-08-09 20:32:47 -04:00
|
|
|
"context"
|
2020-06-18 14:11:04 -04:00
|
|
|
"encoding/json"
|
2020-08-09 20:32:47 -04:00
|
|
|
"errors"
|
2020-12-28 16:33:17 -05:00
|
|
|
"io"
|
2020-06-18 14:11:04 -04:00
|
|
|
"net"
|
2020-06-22 20:31:42 -04:00
|
|
|
"sync"
|
2020-06-26 17:12:19 -04:00
|
|
|
"time"
|
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
"github.com/robertkrimen/otto"
|
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-11-21 05:33:22 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-09-12 16:51:30 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-10-10 18:47:51 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2hero"
|
2020-07-17 22:11:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapengine"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapgen"
|
2020-12-28 16:33:17 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2clientconnectiontype"
|
2020-06-26 17:12:19 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
2020-09-12 16:25:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2server/d2tcpclientconnection"
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2script"
|
2020-06-13 18:32:09 -04:00
|
|
|
)
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
const logPrefix = "Game Server"
|
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
const (
|
2020-09-12 16:25:09 -04:00
|
|
|
port = "6669"
|
2020-10-25 21:01:57 -04:00
|
|
|
chunkSize int = 4096 // nolint:deadcode,unused,varcheck // WIP
|
2020-08-09 20:32:47 -04:00
|
|
|
subtilesPerTile = 5
|
|
|
|
middleOfTileOffset = 3
|
2020-07-17 22:11:16 -04:00
|
|
|
)
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
var (
|
2020-09-12 16:25:09 -04:00
|
|
|
errPlayerAlreadyExists = errors.New("player already exists")
|
|
|
|
errServerFull = errors.New("server full") // Server currently at maximum TCP connections
|
2020-08-09 20:32:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// GameServer manages a copy of the map and entities as well as manages packet routing and connections.
|
|
|
|
// It can accept connections from localhost as well remote clients. It can also be started in a standalone mode.
|
2020-06-13 18:32:09 -04:00
|
|
|
type GameServer struct {
|
2020-06-22 20:31:42 -04:00
|
|
|
sync.RWMutex
|
2020-08-09 20:32:47 -04:00
|
|
|
connections map[string]ClientConnection
|
|
|
|
listener net.Listener
|
|
|
|
networkServer bool
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2020-09-12 16:51:30 -04:00
|
|
|
asset *d2asset.AssetManager
|
2020-06-26 17:12:19 -04:00
|
|
|
mapEngines []*d2mapengine.MapEngine
|
2020-06-18 14:11:04 -04:00
|
|
|
scriptEngine *d2script.ScriptEngine
|
|
|
|
seed int64
|
2020-08-09 20:32:47 -04:00
|
|
|
maxConnections int
|
2020-12-28 16:33:17 -05:00
|
|
|
packetManagerChan chan ReceivedPacket
|
2020-10-31 14:30:08 -04:00
|
|
|
heroStateFactory *d2hero.HeroStateFactory
|
2020-11-21 05:33:22 -05:00
|
|
|
|
|
|
|
*d2util.Logger
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-12-28 16:33:17 -05:00
|
|
|
// ReceivedPacket encapsulates the data necessary for the packet manager goroutine to process data from clients.
|
|
|
|
// The packet manager needs to know who sent the data, in addition to the data itself.
|
|
|
|
type ReceivedPacket struct {
|
|
|
|
Client ClientConnection
|
|
|
|
Packet d2netpacket.NetPacket
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// NewGameServer builds a new GameServer that can be started
|
2020-06-29 17:01:26 -04:00
|
|
|
//
|
2020-08-09 20:32:47 -04:00
|
|
|
// ctx: required context item
|
|
|
|
// networkServer: true = 0.0.0.0 | false = 127.0.0.1
|
|
|
|
// maxConnections (default: 8): maximum number of TCP connections allowed open
|
2020-11-21 05:33:22 -05:00
|
|
|
func NewGameServer(asset *d2asset.AssetManager,
|
|
|
|
networkServer bool,
|
|
|
|
l d2util.LogLevel,
|
2020-09-12 16:51:30 -04:00
|
|
|
maxConnections ...int) (*GameServer,
|
|
|
|
error) {
|
2020-08-09 20:32:47 -04:00
|
|
|
if len(maxConnections) == 0 {
|
|
|
|
maxConnections = []int{8}
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
|
|
|
|
2020-10-31 14:30:08 -04:00
|
|
|
heroStateFactory, err := d2hero.NewHeroStateFactory(asset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
gameServer := &GameServer{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
2020-09-12 16:51:30 -04:00
|
|
|
asset: asset,
|
2020-08-09 20:32:47 -04:00
|
|
|
connections: make(map[string]ClientConnection),
|
|
|
|
networkServer: networkServer,
|
|
|
|
maxConnections: maxConnections[0],
|
2020-12-28 16:33:17 -05:00
|
|
|
packetManagerChan: make(chan ReceivedPacket),
|
2020-06-26 17:12:19 -04:00
|
|
|
mapEngines: make([]*d2mapengine.MapEngine, 0),
|
|
|
|
scriptEngine: d2script.CreateScriptEngine(),
|
|
|
|
seed: time.Now().UnixNano(),
|
2020-10-31 14:30:08 -04:00
|
|
|
heroStateFactory: heroStateFactory,
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
gameServer.Logger = d2util.NewLogger()
|
|
|
|
gameServer.Logger.SetPrefix(logPrefix)
|
|
|
|
gameServer.Logger.SetLevel(l)
|
|
|
|
|
|
|
|
mapEngine := d2mapengine.CreateMapEngine(l, asset)
|
2020-08-09 20:32:47 -04:00
|
|
|
mapEngine.SetSeed(gameServer.seed)
|
2020-10-25 10:21:14 -04:00
|
|
|
mapEngine.ResetMap(d2enum.RegionAct1Town, 100, 100)
|
2020-09-20 17:52:01 -04:00
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
mapGen, err := d2mapgen.NewMapGenerator(asset, l, mapEngine)
|
2020-09-20 17:52:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mapGen.GenerateAct1Overworld()
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
gameServer.mapEngines = append(gameServer.mapEngines, mapEngine)
|
|
|
|
|
|
|
|
gameServer.scriptEngine.AddFunction("getMapEngines", func(call otto.FunctionCall) otto.Value {
|
2020-10-31 23:58:55 -04:00
|
|
|
val, err := gameServer.scriptEngine.ToValue(gameServer.mapEngines)
|
2020-06-26 17:12:19 -04:00
|
|
|
if err != nil {
|
2020-11-23 08:18:30 -05:00
|
|
|
gameServer.Error(err.Error())
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
|
|
|
return val
|
|
|
|
})
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
return gameServer, nil
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// Start essentially starts all of the game server go routines as well as begins listening for connection. This will
|
|
|
|
// return an error if it is unable to bind to a socket.
|
|
|
|
func (g *GameServer) Start() error {
|
2020-09-12 16:25:09 -04:00
|
|
|
listenerAddress := "127.0.0.1:" + port
|
2020-08-09 20:32:47 -04:00
|
|
|
if g.networkServer {
|
2020-09-12 16:25:09 -04:00
|
|
|
listenerAddress = "0.0.0.0:" + port
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Infof("Starting Game Server @ %s\n", listenerAddress)
|
2020-09-12 16:25:09 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
l, err := net.Listen("tcp4", listenerAddress)
|
2020-06-26 17:12:19 -04:00
|
|
|
if err != nil {
|
2020-08-09 20:32:47 -04:00
|
|
|
return err
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
g.listener = l
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
go g.packetManager()
|
2020-06-26 17:12:19 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
c, err := g.listener.Accept()
|
|
|
|
if err != nil {
|
2020-12-28 16:33:17 -05:00
|
|
|
select {
|
|
|
|
case <-g.ctx.Done():
|
|
|
|
// this error was just a result of the server closing, don't worry about it
|
|
|
|
default:
|
|
|
|
g.Errorf("Unable to accept connection: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-09-06 01:17:33 -04:00
|
|
|
return
|
2020-08-09 20:32:47 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
go g.handleConnection(c)
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
2020-08-09 20:32:47 -04:00
|
|
|
}()
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-02 16:29:28 -04:00
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
// Stop stops the game server
|
2020-08-09 20:32:47 -04:00
|
|
|
func (g *GameServer) Stop() {
|
|
|
|
g.Lock()
|
|
|
|
g.cancel()
|
2020-12-28 16:33:17 -05:00
|
|
|
g.connections = make(map[string]ClientConnection)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
if err := g.listener.Close(); err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf("failed to close the listener %s, err: %v\n", g.listener.Addr(), err)
|
2020-09-12 16:25:09 -04:00
|
|
|
}
|
2020-08-09 20:32:47 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// packetManager is meant to be started as a Goroutine and is used to manage routing of packets to clients.
|
|
|
|
func (g *GameServer) packetManager() {
|
|
|
|
defer close(g.packetManagerChan)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// If the server is stopped we need to clean up the packet manager goroutine
|
|
|
|
case <-g.ctx.Done():
|
|
|
|
return
|
|
|
|
case p := <-g.packetManagerChan:
|
2020-12-28 16:33:17 -05:00
|
|
|
err := g.OnPacketReceived(p.Client, p.Packet)
|
2020-11-23 08:18:30 -05:00
|
|
|
if err != nil {
|
2020-12-28 16:33:17 -05:00
|
|
|
g.Errorf("failed to handle packet received from client %s: %v", p.Client.GetUniqueID(), err)
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
func (g *GameServer) sendPacketToClients(packet d2netpacket.NetPacket) {
|
|
|
|
for _, c := range g.connections {
|
2020-09-12 16:25:09 -04:00
|
|
|
if err := c.SendPacketToClient(packet); err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf("GameServer: error sending packet: %s to client %s: %s", packet.PacketType, c.GetUniqueID(), err)
|
2020-09-12 16:25:09 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// handleConnection accepts an individual connection and starts pooling for new packets. It is recommended this is called
|
|
|
|
// via Go Routine. Context should be a property of the GameServer Struct.
|
|
|
|
func (g *GameServer) handleConnection(conn net.Conn) {
|
2020-12-28 16:33:17 -05:00
|
|
|
var (
|
|
|
|
connected int
|
|
|
|
client ClientConnection
|
|
|
|
)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Infof("Accepting connection: %s\n", conn.RemoteAddr().String())
|
2020-09-12 16:25:09 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := conn.Close(); err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf("failed to close the connection: %s\n", conn.RemoteAddr())
|
2020-09-12 16:25:09 -04:00
|
|
|
}
|
|
|
|
}()
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
decoder := json.NewDecoder(conn)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
for {
|
2020-12-28 16:33:17 -05:00
|
|
|
var packet d2netpacket.NetPacket
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
err := decoder.Decode(&packet)
|
2020-07-17 22:11:16 -04:00
|
|
|
if err != nil {
|
2020-12-28 16:33:17 -05:00
|
|
|
switch err {
|
|
|
|
case io.EOF:
|
|
|
|
break // the other side closed the connection
|
|
|
|
default:
|
|
|
|
g.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return // allow the connection to close
|
2020-07-17 22:11:16 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// If this is the first packet we are seeing from this specific connection we first need to see if the client
|
|
|
|
// is sending a valid request. If this is a valid request, we will register it and flip the connected switch
|
|
|
|
// to.
|
|
|
|
if connected == 0 {
|
|
|
|
if packet.PacketType != d2netpackettype.PlayerConnectionRequest {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Infof("Closing connection with %s: did not receive new player connection request...", conn.RemoteAddr().String())
|
2020-08-09 20:32:47 -04:00
|
|
|
}
|
2020-07-30 15:04:05 -04:00
|
|
|
|
2020-12-28 16:33:17 -05:00
|
|
|
if client, err = g.registerConnection(packet.PacketData, conn); err != nil {
|
2020-11-01 08:26:15 -05:00
|
|
|
return
|
2020-08-09 20:32:47 -04:00
|
|
|
}
|
2020-07-30 15:04:05 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
connected = 1
|
|
|
|
}
|
2020-07-30 15:04:05 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
select {
|
|
|
|
case <-g.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2020-12-28 16:33:17 -05:00
|
|
|
g.packetManagerChan <- ReceivedPacket{
|
|
|
|
Client: client,
|
|
|
|
Packet: packet,
|
|
|
|
}
|
2020-07-30 15:04:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// registerConnection accepts a PlayerConnectionRequestPacket and thread safely updates the connection pool
|
|
|
|
//
|
|
|
|
// Errors:
|
2020-09-12 16:25:09 -04:00
|
|
|
// - errServerFull
|
|
|
|
// - errPlayerAlreadyExists
|
2020-12-28 16:33:17 -05:00
|
|
|
func (g *GameServer) registerConnection(b []byte, conn net.Conn) (ClientConnection, error) {
|
|
|
|
var client ClientConnection
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
g.Lock()
|
2020-12-28 16:33:17 -05:00
|
|
|
defer g.Unlock()
|
2020-08-09 20:32:47 -04:00
|
|
|
|
|
|
|
// check to see if the server is full
|
|
|
|
if len(g.connections) >= g.maxConnections {
|
2020-12-28 16:33:17 -05:00
|
|
|
sf, serverFullErr := d2netpacket.CreateServerFullPacket()
|
|
|
|
if serverFullErr != nil {
|
|
|
|
g.Errorf("ServerFullPacket: %v", serverFullErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
msf, marshalServerFullErr := d2netpacket.MarshalPacket(sf)
|
|
|
|
if marshalServerFullErr != nil {
|
|
|
|
g.Errorf("MarshalPacket: %v", marshalServerFullErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, errServerFullPacket := conn.Write(msf)
|
|
|
|
g.Warningf("%v", errServerFullPacket)
|
|
|
|
|
|
|
|
return client, errServerFull
|
2020-07-17 22:11:16 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// if it is not full, unmarshal the playerConnectionRequest
|
|
|
|
packet, err := d2netpacket.UnmarshalPlayerConnectionRequest(b)
|
2020-07-17 22:11:16 -04:00
|
|
|
if err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf("Failed to unmarshal PlayerConnectionRequest: %s\n", err)
|
2020-07-17 22:11:16 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// check to see if the player is already registered
|
|
|
|
if _, ok := g.connections[packet.ID]; ok {
|
2020-12-28 16:33:17 -05:00
|
|
|
g.Errorf("%v", errPlayerAlreadyExists)
|
|
|
|
return client, errPlayerAlreadyExists
|
2020-08-09 20:32:47 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// Client a new TCP Client Connection and add it to the connections map
|
2020-12-28 16:33:17 -05:00
|
|
|
client = d2tcpclientconnection.CreateTCPClientConnection(conn, packet.ID)
|
2020-08-09 20:32:47 -04:00
|
|
|
client.SetPlayerState(packet.PlayerState)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-12-28 16:33:17 -05:00
|
|
|
g.OnClientConnected(client)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-12-28 16:33:17 -05:00
|
|
|
return client, nil
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// OnClientConnected initializes the given ClientConnection. It sends the
|
|
|
|
// following packets to the newly connected client: UpdateServerInfoPacket,
|
|
|
|
// GenerateMapPacket, AddPlayerPacket.
|
|
|
|
//
|
|
|
|
// It also sends AddPlayerPackets for each other player entity to the new
|
|
|
|
// player and vice versa, so all player entities exist on all clients.
|
|
|
|
//
|
|
|
|
// For more information, see d2networking.d2netpacket.
|
2020-10-31 23:58:55 -04:00
|
|
|
func (g *GameServer) OnClientConnected(client ClientConnection) {
|
2020-06-26 17:12:19 -04:00
|
|
|
// Temporary position hack --------------------------------------------
|
2020-10-25 18:36:12 -04:00
|
|
|
// https://github.com/OpenDiablo2/OpenDiablo2/issues/829
|
2020-10-31 23:58:55 -04:00
|
|
|
sx, sy := g.mapEngines[0].GetStartPosition()
|
2020-06-26 17:12:19 -04:00
|
|
|
clientPlayerState := client.GetPlayerState()
|
|
|
|
clientPlayerState.X = sx
|
|
|
|
clientPlayerState.Y = sy
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Infof("Client connected with an id of %s", client.GetUniqueID())
|
2020-10-31 23:58:55 -04:00
|
|
|
g.connections[client.GetUniqueID()] = client
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-10-31 23:58:55 -04:00
|
|
|
g.handleClientConnection(client, sx, sy)
|
2020-09-12 16:25:09 -04:00
|
|
|
}
|
|
|
|
|
2020-10-31 23:58:55 -04:00
|
|
|
func (g *GameServer) handleClientConnection(client ClientConnection, x, y float64) {
|
2020-11-23 08:18:30 -05:00
|
|
|
usi, err := d2netpacket.CreateUpdateServerInfoPacket(g.seed, client.GetUniqueID())
|
|
|
|
if err != nil {
|
|
|
|
g.Errorf("UpdateServerInfoPacket: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.SendPacketToClient(usi)
|
2020-06-30 20:01:51 -04:00
|
|
|
if err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf("GameServer: error sending UpdateServerInfoPacket to client %s: %s", client.GetUniqueID(), err)
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-11-23 08:18:30 -05:00
|
|
|
gmp, err := d2netpacket.CreateGenerateMapPacket(d2enum.RegionAct1Town)
|
|
|
|
if err != nil {
|
|
|
|
g.Errorf("GenerateMapPacket: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.SendPacketToClient(gmp)
|
2020-06-30 20:01:51 -04:00
|
|
|
if err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf("GameServer: error sending GenerateMapPacket to client %s: %s", client.GetUniqueID(), err)
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
2020-06-26 17:12:19 -04:00
|
|
|
|
|
|
|
playerState := client.GetPlayerState()
|
2020-07-17 22:11:16 -04:00
|
|
|
|
|
|
|
// these are in subtiles
|
2020-09-12 16:25:09 -04:00
|
|
|
playerX := int(x*subtilesPerTile) + middleOfTileOffset
|
|
|
|
playerY := int(y*subtilesPerTile) + middleOfTileOffset
|
|
|
|
|
2020-10-31 23:58:55 -04:00
|
|
|
d2hero.HydrateSkills(playerState.Skills, g.asset)
|
2020-10-10 18:47:51 -04:00
|
|
|
|
2020-11-23 08:18:30 -05:00
|
|
|
createPlayerPacket, err := d2netpacket.CreateAddPlayerPacket(
|
2020-09-12 16:25:09 -04:00
|
|
|
client.GetUniqueID(),
|
|
|
|
playerState.HeroName,
|
|
|
|
playerX,
|
|
|
|
playerY,
|
|
|
|
playerState.HeroType,
|
|
|
|
playerState.Stats,
|
2020-09-20 11:55:44 -04:00
|
|
|
playerState.Skills,
|
2020-09-12 16:25:09 -04:00
|
|
|
playerState.Equipment,
|
2020-10-31 14:30:08 -04:00
|
|
|
playerState.LeftSkill,
|
|
|
|
playerState.RightSkill,
|
2020-11-22 17:53:33 -05:00
|
|
|
playerState.Gold,
|
2020-09-12 16:25:09 -04:00
|
|
|
)
|
2020-11-23 08:18:30 -05:00
|
|
|
if err != nil {
|
|
|
|
g.Errorf("AddPlayerPacket: %v", err)
|
|
|
|
}
|
2020-09-12 16:25:09 -04:00
|
|
|
|
2020-10-31 23:58:55 -04:00
|
|
|
for _, connection := range g.connections {
|
2020-06-30 20:01:51 -04:00
|
|
|
err := connection.SendPacketToClient(createPlayerPacket)
|
|
|
|
if err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf("GameServer: error sending %T to client %s: %s", createPlayerPacket, connection.GetUniqueID(), err)
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
|
|
|
if connection.GetUniqueID() == client.GetUniqueID() {
|
2020-06-26 17:12:19 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
conPlayerState := connection.GetPlayerState()
|
2020-07-17 22:11:16 -04:00
|
|
|
playerX := int(conPlayerState.X*subtilesPerTile) + middleOfTileOffset
|
|
|
|
playerY := int(conPlayerState.Y*subtilesPerTile) + middleOfTileOffset
|
2020-11-23 08:18:30 -05:00
|
|
|
app, err := d2netpacket.CreateAddPlayerPacket(
|
|
|
|
connection.GetUniqueID(),
|
|
|
|
conPlayerState.HeroName,
|
|
|
|
playerX,
|
|
|
|
playerY,
|
|
|
|
conPlayerState.HeroType,
|
|
|
|
conPlayerState.Stats,
|
|
|
|
conPlayerState.Skills,
|
|
|
|
conPlayerState.Equipment,
|
|
|
|
conPlayerState.LeftSkill,
|
|
|
|
conPlayerState.RightSkill,
|
|
|
|
conPlayerState.Gold,
|
2020-07-17 22:11:16 -04:00
|
|
|
)
|
|
|
|
|
2020-11-23 08:18:30 -05:00
|
|
|
if err != nil {
|
|
|
|
g.Errorf("AddPlayerPacket: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.SendPacketToClient(app)
|
|
|
|
|
2020-06-30 20:01:51 -04:00
|
|
|
if err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf("GameServer: error sending CreateAddPlayerPacket to client %s: %s", connection.GetUniqueID(), err)
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// OnClientDisconnected removes the given client from the list
|
|
|
|
// of client connections.
|
2020-12-28 16:33:17 -05:00
|
|
|
// If this client was the host, disconnects all clients and kills GameServer.
|
2020-10-31 23:58:55 -04:00
|
|
|
func (g *GameServer) OnClientDisconnected(client ClientConnection) {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Infof("Client disconnected with an id of %s", client.GetUniqueID())
|
2020-10-31 23:58:55 -04:00
|
|
|
delete(g.connections, client.GetUniqueID())
|
2020-12-28 16:33:17 -05:00
|
|
|
|
|
|
|
if client.GetConnectionType() == d2clientconnectiontype.Local {
|
|
|
|
g.Info("Host disconnected, game server shuting down")
|
|
|
|
|
|
|
|
serverClosed, err := d2netpacket.CreateServerClosedPacket()
|
|
|
|
if err != nil {
|
|
|
|
g.Errorf("failed to generate ServerClosed packet after host disconnected: %s", err)
|
|
|
|
} else {
|
|
|
|
g.sendPacketToClients(serverClosed)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Stop()
|
|
|
|
}
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
|
|
|
|
2020-12-28 16:33:17 -05:00
|
|
|
// OnPacketReceived is called when a packet has been received from a remote client,
|
|
|
|
// and by the local client to 'send' a packet to the server,
|
2020-10-31 14:30:08 -04:00
|
|
|
// nolint:gocyclo // switch statement on packet type makes sense, no need to change
|
2020-10-31 23:58:55 -04:00
|
|
|
func (g *GameServer) OnPacketReceived(client ClientConnection, packet d2netpacket.NetPacket) error {
|
|
|
|
if g == nil {
|
|
|
|
return errors.New("game server is nil")
|
2020-10-25 18:36:12 -04:00
|
|
|
}
|
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
switch packet.PacketType {
|
|
|
|
case d2netpackettype.MovePlayer:
|
2020-09-23 13:30:54 -04:00
|
|
|
movePacket, err := d2netpacket.UnmarshalMovePlayer(packet.PacketData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-31 23:58:55 -04:00
|
|
|
|
|
|
|
playerState := g.connections[client.GetUniqueID()].GetPlayerState()
|
2020-08-09 20:32:47 -04:00
|
|
|
playerState.X = movePacket.DestX
|
|
|
|
playerState.Y = movePacket.DestY
|
2020-10-31 23:58:55 -04:00
|
|
|
|
|
|
|
g.sendPacketToClients(packet)
|
|
|
|
case d2netpackettype.CastSkill, d2netpackettype.SpawnItem:
|
|
|
|
g.sendPacketToClients(packet)
|
2020-10-31 14:30:08 -04:00
|
|
|
case d2netpackettype.SavePlayer:
|
|
|
|
savePacket, err := d2netpacket.UnmarshalSavePlayer(packet.PacketData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-31 23:58:55 -04:00
|
|
|
playerState := g.connections[client.GetUniqueID()].GetPlayerState()
|
2020-11-18 03:46:03 -05:00
|
|
|
playerState.LeftSkill = savePacket.Player.LeftSkill.Shallow.SkillID
|
|
|
|
playerState.RightSkill = savePacket.Player.RightSkill.Shallow.SkillID
|
|
|
|
playerState.Stats = savePacket.Player.Stats
|
2020-11-25 05:48:23 -05:00
|
|
|
playerState.Act = savePacket.Player.Act
|
|
|
|
playerState.Difficulty = savePacket.Difficulty
|
2020-10-31 14:30:08 -04:00
|
|
|
|
2020-10-31 23:58:55 -04:00
|
|
|
err = g.heroStateFactory.Save(playerState)
|
2020-10-31 14:30:08 -04:00
|
|
|
if err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
g.Errorf("GameServer: error saving saving Player: %s", err)
|
2020-10-31 14:30:08 -04:00
|
|
|
}
|
2020-12-28 16:33:17 -05:00
|
|
|
case d2netpackettype.PlayerConnectionRequest:
|
|
|
|
break // prevent log message. these are handled by handleConnection
|
|
|
|
case d2netpackettype.PlayerDisconnectionNotification:
|
|
|
|
g.sendPacketToClients(packet)
|
|
|
|
g.OnClientDisconnected(client)
|
2020-10-10 18:47:51 -04:00
|
|
|
default:
|
2020-12-28 16:33:17 -05:00
|
|
|
g.Warningf("GameServer: received unknown packet %s", packet.PacketType)
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
return nil
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|