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-06-26 17:12:19 -04:00
|
|
|
"fmt"
|
2020-08-09 20:32:47 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2server/d2tcpclientconnection"
|
2020-06-13 18:32:09 -04:00
|
|
|
"log"
|
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"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-07-17 22:11:16 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapengine"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapgen"
|
2020-06-26 17:12:19 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2script"
|
2020-06-26 17:12:19 -04:00
|
|
|
"github.com/robertkrimen/otto"
|
2020-06-13 18:32:09 -04:00
|
|
|
)
|
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
const (
|
2020-08-09 20:32:47 -04:00
|
|
|
Port = "6669"
|
|
|
|
ChunkSize int = 4096
|
|
|
|
subtilesPerTile = 5
|
|
|
|
middleOfTileOffset = 3
|
2020-07-17 22:11:16 -04:00
|
|
|
)
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
var (
|
|
|
|
ErrPlayerAlreadyExists = errors.New("player already exists")
|
|
|
|
ErrServerFull = errors.New("server full") // Server currently at maximum TCP connections
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-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
|
|
|
|
packetManagerChan chan []byte
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
//nolint:gochecknoglobals // currently singleton by design
|
2020-06-26 17:12:19 -04:00
|
|
|
var singletonServer *GameServer
|
|
|
|
|
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
|
|
|
|
func NewGameServer(networkServer bool, maxConnections ...int) (*GameServer, error) {
|
|
|
|
if len(maxConnections) == 0 {
|
|
|
|
maxConnections = []int{8}
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
gameServer := &GameServer{
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
connections: make(map[string]ClientConnection),
|
|
|
|
networkServer: networkServer,
|
|
|
|
maxConnections: maxConnections[0],
|
|
|
|
packetManagerChan: make(chan []byte),
|
2020-06-26 17:12:19 -04:00
|
|
|
mapEngines: make([]*d2mapengine.MapEngine, 0),
|
|
|
|
scriptEngine: d2script.CreateScriptEngine(),
|
|
|
|
seed: time.Now().UnixNano(),
|
|
|
|
}
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// TODO: In order to support dedicated mode we need to load the levels txt and files. Revisit this once this we can
|
|
|
|
// load files independent of the app.
|
2020-06-26 17:12:19 -04:00
|
|
|
mapEngine := d2mapengine.CreateMapEngine()
|
2020-08-09 20:32:47 -04:00
|
|
|
mapEngine.SetSeed(gameServer.seed)
|
|
|
|
mapEngine.ResetMap(d2enum.RegionAct1Town, 100, 100) // TODO: Mapgen - Needs levels.txt stuff
|
2020-06-26 17:12:19 -04:00
|
|
|
d2mapgen.GenerateAct1Overworld(mapEngine)
|
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 {
|
|
|
|
val, err := gameServer.scriptEngine.ToValue(singletonServer.mapEngines)
|
2020-06-26 17:12:19 -04:00
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err.Error())
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
})
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// TODO: Temporary hack to work around local connections. Possible that we can move away from the singleton pattern here
|
|
|
|
// but for now this will work.
|
|
|
|
singletonServer = gameServer
|
|
|
|
|
|
|
|
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 {
|
|
|
|
listenerAddress := "127.0.0.1:" + Port
|
|
|
|
if g.networkServer {
|
|
|
|
listenerAddress = "0.0.0.0:" + Port
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
log.Printf("Starting Game Server @ %s\n", listenerAddress)
|
|
|
|
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 {
|
|
|
|
log.Printf("Unable to accept connection: %s\n", err)
|
|
|
|
}
|
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-08-09 20:32:47 -04:00
|
|
|
func (g *GameServer) Stop() {
|
|
|
|
g.Lock()
|
|
|
|
g.cancel()
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
g.listener.Close()
|
|
|
|
}
|
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:
|
|
|
|
switch d2netpacket.InspectPacketType(p) {
|
|
|
|
case d2netpackettype.PlayerConnectionRequest:
|
|
|
|
player, err := d2netpacket.UnmarshalNetPacket(p)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to unmarshal PlayerConnectionRequestPacket: %s\n", err)
|
|
|
|
}
|
|
|
|
g.sendPacketToClients(player)
|
|
|
|
case d2netpackettype.MovePlayer:
|
|
|
|
move, err := d2netpacket.UnmarshalNetPacket(p)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
g.sendPacketToClients(move)
|
|
|
|
case d2netpackettype.SpawnItem:
|
|
|
|
item, err := d2netpacket.UnmarshalNetPacket(p)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
g.sendPacketToClients(item)
|
|
|
|
case d2netpackettype.ServerClosed:
|
|
|
|
g.Stop()
|
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 {
|
|
|
|
c.SendPacketToClient(packet)
|
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) {
|
|
|
|
var connected int
|
|
|
|
var packet d2netpacket.NetPacket
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
log.Printf("Accepting connection: %s", conn.RemoteAddr().String())
|
|
|
|
defer conn.Close()
|
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 {
|
|
|
|
err := decoder.Decode(&packet)
|
2020-07-17 22:11:16 -04:00
|
|
|
if err != nil {
|
2020-08-09 20:32:47 -04:00
|
|
|
log.Println(err)
|
|
|
|
return // exit this connection as we could not read the first packet
|
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 {
|
|
|
|
log.Printf("Closing connection with %s: did not receive new player connection request...\n", conn.RemoteAddr().String())
|
|
|
|
}
|
2020-07-30 15:04:05 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// TODO: I do not think this error check actually works. Need to retrofit with Errors.Is().
|
|
|
|
if err := g.registerConnection(packet.PacketData, conn); err != nil {
|
|
|
|
switch err {
|
|
|
|
case ErrServerFull: // Server is currently full and not accepting new connections.
|
|
|
|
// TODO: Need to create a new Server Full packet to return to clients.
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
case ErrPlayerAlreadyExists: // Player is already registered and did not disconnection correctly.
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
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:
|
|
|
|
g.packetManagerChan <- packet.PacketData
|
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:
|
|
|
|
// - ErrServerFull
|
|
|
|
// - ErrPlayerAlreadyExists
|
|
|
|
func (g *GameServer) registerConnection(b []byte, conn net.Conn) error {
|
|
|
|
g.Lock()
|
|
|
|
|
|
|
|
// check to see if the server is full
|
|
|
|
if len(g.connections) >= g.maxConnections {
|
|
|
|
return 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-08-09 20:32:47 -04:00
|
|
|
log.Printf("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 {
|
|
|
|
return ErrPlayerAlreadyExists
|
|
|
|
}
|
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
|
|
|
|
client := d2tcpclientconnection.CreateTCPClientConnection(conn, packet.ID)
|
|
|
|
client.SetPlayerState(packet.PlayerState)
|
|
|
|
log.Printf("Client connected with an id of %s", client.GetUniqueID())
|
|
|
|
g.connections[client.GetUniqueID()] = client
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// Temporary position hack --------------------------------------------
|
|
|
|
sx, sy := g.mapEngines[0].GetStartPosition() // TODO: Another temporary hack
|
|
|
|
clientPlayerState := client.GetPlayerState()
|
|
|
|
clientPlayerState.X = sx
|
|
|
|
clientPlayerState.Y = sy
|
|
|
|
// ---------
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// This really should be deferred however to much time will be spend holding a lock when we attempt to send a packet
|
|
|
|
g.Unlock()
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
if err := client.SendPacketToClient(d2netpacket.CreateUpdateServerInfoPacket(g.seed, client.GetUniqueID())); err != nil {
|
|
|
|
log.Printf("GameServer: error sending UpdateServerInfoPacket to client %s: %s", client.GetUniqueID(), err)
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
2020-08-09 20:32:47 -04:00
|
|
|
if err := client.SendPacketToClient(d2netpacket.CreateGenerateMapPacket(d2enum.RegionAct1Town)); err != nil {
|
|
|
|
log.Printf("GameServer: error sending GenerateMapPacket to client %s: %s", client.GetUniqueID(), err)
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
playerState := client.GetPlayerState()
|
2020-06-13 18:32:09 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
// these are in subtiles
|
|
|
|
playerX := int(sx*subtilesPerTile) + middleOfTileOffset
|
|
|
|
playerY := int(sy*subtilesPerTile) + middleOfTileOffset
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
createPlayerPacket := d2netpacket.CreateAddPlayerPacket(client.GetUniqueID(),
|
|
|
|
playerState.HeroName, playerX, playerY,
|
|
|
|
playerState.HeroType, playerState.Stats, playerState.Equipment)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
for _, connection := range g.connections {
|
|
|
|
err := connection.SendPacketToClient(createPlayerPacket)
|
2020-06-30 20:01:51 -04:00
|
|
|
if err != nil {
|
2020-08-09 20:32:47 -04:00
|
|
|
log.Printf("GameServer: error sending %T to client %s: %s", createPlayerPacket, connection.GetUniqueID(), err)
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
2020-06-26 17:12:19 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
if connection.GetUniqueID() == client.GetUniqueID() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
conPlayerState := connection.GetPlayerState()
|
|
|
|
playerX := int(conPlayerState.X*subtilesPerTile) + middleOfTileOffset
|
|
|
|
playerY := int(conPlayerState.Y*subtilesPerTile) + middleOfTileOffset
|
|
|
|
err = client.SendPacketToClient(d2netpacket.CreateAddPlayerPacket(
|
|
|
|
connection.GetUniqueID(),
|
|
|
|
conPlayerState.HeroName,
|
|
|
|
playerX, playerY,
|
|
|
|
conPlayerState.HeroType,
|
|
|
|
conPlayerState.Stats, conPlayerState.Equipment),
|
|
|
|
)
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("GameServer: error sending CreateAddPlayerPacket to client %s: %s", connection.GetUniqueID(), err)
|
|
|
|
}
|
|
|
|
}
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
return 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-06-26 17:12:19 -04:00
|
|
|
func OnClientConnected(client ClientConnection) {
|
|
|
|
// Temporary position hack --------------------------------------------
|
|
|
|
sx, sy := singletonServer.mapEngines[0].GetStartPosition() // TODO: Another temporary hack
|
|
|
|
clientPlayerState := client.GetPlayerState()
|
|
|
|
clientPlayerState.X = sx
|
|
|
|
clientPlayerState.Y = sy
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
2020-07-17 22:11:16 -04:00
|
|
|
log.Printf("Client connected with an id of %s", client.GetUniqueID())
|
2020-08-09 20:32:47 -04:00
|
|
|
singletonServer.connections[client.GetUniqueID()] = client
|
2020-07-17 22:11:16 -04:00
|
|
|
err := client.SendPacketToClient(d2netpacket.CreateUpdateServerInfoPacket(singletonServer.seed, client.GetUniqueID()))
|
|
|
|
|
2020-06-30 20:01:51 -04:00
|
|
|
if err != nil {
|
2020-07-17 22:11:16 -04:00
|
|
|
log.Printf("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-06-30 20:01:51 -04:00
|
|
|
err = client.SendPacketToClient(d2netpacket.CreateGenerateMapPacket(d2enum.RegionAct1Town))
|
2020-07-17 22:11:16 -04:00
|
|
|
|
2020-06-30 20:01:51 -04:00
|
|
|
if err != nil {
|
2020-07-17 22:11:16 -04:00
|
|
|
log.Printf("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
|
|
|
|
playerX := int(sx*subtilesPerTile) + middleOfTileOffset
|
|
|
|
playerY := int(sy*subtilesPerTile) + middleOfTileOffset
|
|
|
|
|
|
|
|
createPlayerPacket := d2netpacket.CreateAddPlayerPacket(client.GetUniqueID(),
|
|
|
|
playerState.HeroName, playerX, playerY,
|
|
|
|
playerState.HeroType, playerState.Stats, playerState.Equipment)
|
|
|
|
|
2020-08-09 20:32:47 -04:00
|
|
|
for _, connection := range singletonServer.connections {
|
2020-06-30 20:01:51 -04:00
|
|
|
err := connection.SendPacketToClient(createPlayerPacket)
|
|
|
|
if err != nil {
|
2020-07-17 22:11:16 -04:00
|
|
|
log.Printf("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
|
|
|
|
err = client.SendPacketToClient(d2netpacket.CreateAddPlayerPacket(
|
|
|
|
connection.GetUniqueID(),
|
|
|
|
conPlayerState.HeroName,
|
|
|
|
playerX, playerY,
|
|
|
|
conPlayerState.HeroType,
|
|
|
|
conPlayerState.Stats, conPlayerState.Equipment),
|
|
|
|
)
|
|
|
|
|
2020-06-30 20:01:51 -04:00
|
|
|
if err != nil {
|
2020-07-17 22:11:16 -04:00
|
|
|
log.Printf("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-06-26 17:12:19 -04:00
|
|
|
func OnClientDisconnected(client ClientConnection) {
|
2020-07-17 22:11:16 -04:00
|
|
|
log.Printf("Client disconnected with an id of %s", client.GetUniqueID())
|
2020-08-09 20:32:47 -04:00
|
|
|
delete(singletonServer.connections, client.GetUniqueID())
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// OnPacketReceived is called by the local client to 'send' a packet to the server.
|
2020-06-26 17:12:19 -04:00
|
|
|
func OnPacketReceived(client ClientConnection, packet d2netpacket.NetPacket) error {
|
|
|
|
switch packet.PacketType {
|
|
|
|
case d2netpackettype.MovePlayer:
|
2020-08-09 20:32:47 -04:00
|
|
|
movePacket, _ := d2netpacket.UnmarshalMovePlayer(packet.PacketData)
|
2020-06-26 17:12:19 -04:00
|
|
|
// TODO: This needs to be verified on the server (here) before sending to other clients....
|
|
|
|
// TODO: Hacky, this should be updated in realtime ----------------
|
|
|
|
// TODO: Verify player id
|
2020-08-09 20:32:47 -04:00
|
|
|
playerState := singletonServer.connections[client.GetUniqueID()].GetPlayerState()
|
|
|
|
playerState.X = movePacket.DestX
|
|
|
|
playerState.Y = movePacket.DestY
|
2020-06-26 17:12:19 -04:00
|
|
|
// ----------------------------------------------------------------
|
2020-08-09 20:32:47 -04:00
|
|
|
for _, player := range singletonServer.connections {
|
2020-06-30 20:01:51 -04:00
|
|
|
err := player.SendPacketToClient(packet)
|
|
|
|
if err != nil {
|
2020-07-17 22:11:16 -04:00
|
|
|
log.Printf("GameServer: error sending %T to client %s: %s", packet, player.GetUniqueID(), err)
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
2020-06-26 20:03:00 -04:00
|
|
|
case d2netpackettype.CastSkill:
|
2020-08-09 20:32:47 -04:00
|
|
|
for _, player := range singletonServer.connections {
|
2020-06-30 20:01:51 -04:00
|
|
|
err := player.SendPacketToClient(packet)
|
|
|
|
if err != nil {
|
2020-07-17 22:11:16 -04:00
|
|
|
log.Printf("GameServer: error sending %T to client %s: %s", packet, player.GetUniqueID(), err)
|
2020-06-30 20:01:51 -04:00
|
|
|
}
|
2020-06-26 20:03:00 -04:00
|
|
|
}
|
2020-07-30 15:04:05 -04:00
|
|
|
case d2netpackettype.SpawnItem:
|
2020-08-09 20:32:47 -04:00
|
|
|
for _, player := range singletonServer.connections {
|
2020-07-30 15:04:05 -04:00
|
|
|
err := player.SendPacketToClient(packet)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("GameServer: error sending %T to client %s: %s", packet, player.GetUniqueID(), err)
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|