2020-06-13 18:32:09 -04:00
|
|
|
package d2server
|
|
|
|
|
|
|
|
import (
|
2020-06-26 17:12:19 -04:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2020-06-18 14:11:04 -04:00
|
|
|
"encoding/json"
|
2020-06-26 17:12:19 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-06-13 18:32:09 -04:00
|
|
|
"log"
|
2020-06-18 14:11:04 -04:00
|
|
|
"net"
|
2020-06-26 17:12:19 -04:00
|
|
|
"strings"
|
2020-06-22 20:31:42 -04:00
|
|
|
"sync"
|
2020-06-26 17:12:19 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapgen"
|
2020-06-21 18:40:37 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapengine"
|
2020-06-26 17:12:19 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2server/d2udpclientconnection"
|
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-06-29 17:01:26 -04:00
|
|
|
// GameServer owns the authoritative copy of the map and entities
|
|
|
|
// It accepts incoming connections from local (host) and remote
|
|
|
|
// clients.
|
2020-06-13 18:32:09 -04:00
|
|
|
type GameServer struct {
|
2020-06-22 20:31:42 -04:00
|
|
|
sync.RWMutex
|
2020-06-13 18:32:09 -04:00
|
|
|
clientConnections map[string]ClientConnection
|
2020-06-22 20:31:42 -04:00
|
|
|
manager *ConnectionManager
|
2020-06-26 17:12:19 -04:00
|
|
|
mapEngines []*d2mapengine.MapEngine
|
2020-06-18 14:11:04 -04:00
|
|
|
scriptEngine *d2script.ScriptEngine
|
|
|
|
udpConnection *net.UDPConn
|
|
|
|
seed int64
|
|
|
|
running bool
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
var singletonServer *GameServer
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Create constructs a new GameServer and assigns it as a singleton. It
|
|
|
|
// also generates the initial map and entities for the server.
|
|
|
|
//
|
|
|
|
// If openNetworkServer is true, the GameServer starts listening for UDP
|
|
|
|
// packets.
|
2020-06-26 17:12:19 -04:00
|
|
|
func Create(openNetworkServer bool) {
|
|
|
|
log.Print("Creating GameServer")
|
|
|
|
if singletonServer != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
singletonServer = &GameServer{
|
|
|
|
clientConnections: make(map[string]ClientConnection),
|
|
|
|
mapEngines: make([]*d2mapengine.MapEngine, 0),
|
|
|
|
scriptEngine: d2script.CreateScriptEngine(),
|
|
|
|
seed: time.Now().UnixNano(),
|
|
|
|
}
|
|
|
|
|
|
|
|
singletonServer.manager = CreateConnectionManager(singletonServer)
|
|
|
|
|
|
|
|
mapEngine := d2mapengine.CreateMapEngine()
|
|
|
|
mapEngine.SetSeed(singletonServer.seed)
|
|
|
|
mapEngine.ResetMap(d2enum.RegionAct1Town, 100, 100) // TODO: Mapgen - Needs levels.txt stuff
|
|
|
|
d2mapgen.GenerateAct1Overworld(mapEngine)
|
|
|
|
singletonServer.mapEngines = append(singletonServer.mapEngines, mapEngine)
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
singletonServer.scriptEngine.AddFunction("getMapEngines", func(call otto.FunctionCall) otto.Value {
|
|
|
|
val, err := singletonServer.scriptEngine.ToValue(singletonServer.mapEngines)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Print(err.Error())
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
})
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
if openNetworkServer {
|
|
|
|
createNetworkServer()
|
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
func createNetworkServer() {
|
|
|
|
s, err := net.ResolveUDPAddr("udp4", "0.0.0.0:6669")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
singletonServer.udpConnection, err = net.ListenUDP("udp4", s)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
2020-06-26 17:12:19 -04:00
|
|
|
singletonServer.udpConnection.SetReadBuffer(4096)
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// runNetworkServer runs a while loop, reading from the GameServer's UDP
|
|
|
|
// connection.
|
2020-06-26 17:12:19 -04:00
|
|
|
func runNetworkServer() {
|
|
|
|
buffer := make([]byte, 4096)
|
|
|
|
for singletonServer.running {
|
|
|
|
_, addr, err := singletonServer.udpConnection.ReadFromUDP(buffer)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Socket error: %s\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
buff := bytes.NewBuffer(buffer)
|
|
|
|
packetTypeId, err := buff.ReadByte()
|
|
|
|
packetType := d2netpackettype.NetPacketType(packetTypeId)
|
|
|
|
reader, err := gzip.NewReader(buff)
|
|
|
|
sb := new(strings.Builder)
|
|
|
|
io.Copy(sb, reader)
|
|
|
|
stringData := sb.String()
|
|
|
|
switch packetType {
|
|
|
|
case d2netpackettype.PlayerConnectionRequest:
|
|
|
|
packetData := d2netpacket.PlayerConnectionRequestPacket{}
|
|
|
|
json.Unmarshal([]byte(stringData), &packetData)
|
|
|
|
clientConnection := d2udpclientconnection.CreateUDPClientConnection(singletonServer.udpConnection, packetData.Id, addr)
|
|
|
|
clientConnection.SetPlayerState(packetData.PlayerState)
|
|
|
|
OnClientConnected(clientConnection)
|
|
|
|
case d2netpackettype.MovePlayer:
|
|
|
|
packetData := d2netpacket.MovePlayerPacket{}
|
|
|
|
json.Unmarshal([]byte(stringData), &packetData)
|
|
|
|
netPacket := d2netpacket.NetPacket{
|
|
|
|
PacketType: packetType,
|
|
|
|
PacketData: packetData,
|
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-06-26 17:12:19 -04:00
|
|
|
for _, player := range singletonServer.clientConnections {
|
|
|
|
player.SendPacketToClient(netPacket)
|
|
|
|
}
|
|
|
|
case d2netpackettype.Pong:
|
|
|
|
packetData := d2netpacket.PlayerConnectionRequestPacket{}
|
|
|
|
json.Unmarshal([]byte(stringData), &packetData)
|
|
|
|
singletonServer.manager.Recv(packetData.Id)
|
|
|
|
case d2netpackettype.ServerClosed:
|
|
|
|
singletonServer.manager.Shutdown()
|
|
|
|
case d2netpackettype.PlayerDisconnectionNotification:
|
|
|
|
var packet d2netpacket.PlayerDisconnectRequestPacket
|
|
|
|
json.Unmarshal([]byte(stringData), &packet)
|
|
|
|
log.Printf("Received disconnect: %s", packet.Id)
|
|
|
|
}
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Run sets GameServer.running to true and call runNetworkServer
|
|
|
|
// in a goroutine.
|
2020-06-26 17:12:19 -04:00
|
|
|
func Run() {
|
|
|
|
log.Print("Starting GameServer")
|
|
|
|
singletonServer.running = true
|
|
|
|
singletonServer.scriptEngine.RunScript("scripts/server/server.js")
|
|
|
|
if singletonServer.udpConnection != nil {
|
|
|
|
go runNetworkServer()
|
|
|
|
}
|
|
|
|
log.Print("Network server has been started")
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Stop sets GameServer.running to false and closes the
|
|
|
|
// GameServer's UDP connection.
|
2020-06-26 17:12:19 -04:00
|
|
|
func Stop() {
|
|
|
|
log.Print("Stopping GameServer")
|
|
|
|
singletonServer.running = false
|
|
|
|
if singletonServer.udpConnection != nil {
|
|
|
|
singletonServer.udpConnection.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:01:26 -04:00
|
|
|
// Destroy calls Stop() if the server exists.
|
2020-06-26 17:12:19 -04:00
|
|
|
func Destroy() {
|
|
|
|
if singletonServer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Print("Destroying GameServer")
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
|
|
|
|
log.Printf("Client connected with an id of %s", client.GetUniqueId())
|
|
|
|
singletonServer.clientConnections[client.GetUniqueId()] = client
|
|
|
|
client.SendPacketToClient(d2netpacket.CreateUpdateServerInfoPacket(singletonServer.seed, client.GetUniqueId()))
|
|
|
|
client.SendPacketToClient(d2netpacket.CreateGenerateMapPacket(d2enum.RegionAct1Town))
|
|
|
|
|
|
|
|
playerState := client.GetPlayerState()
|
|
|
|
createPlayerPacket := d2netpacket.CreateAddPlayerPacket(client.GetUniqueId(), playerState.HeroName, int(sx*5)+3, int(sy*5)+3,
|
|
|
|
playerState.HeroType, *playerState.Stats, playerState.Equipment)
|
|
|
|
for _, connection := range singletonServer.clientConnections {
|
|
|
|
connection.SendPacketToClient(createPlayerPacket)
|
|
|
|
if connection.GetUniqueId() == client.GetUniqueId() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
conPlayerState := connection.GetPlayerState()
|
|
|
|
client.SendPacketToClient(d2netpacket.CreateAddPlayerPacket(connection.GetUniqueId(), conPlayerState.HeroName,
|
|
|
|
int(conPlayerState.X*5)+3, int(conPlayerState.Y*5)+3, conPlayerState.HeroType, *conPlayerState.Stats, conPlayerState.Equipment))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
log.Printf("Client disconnected with an id of %s", client.GetUniqueId())
|
|
|
|
delete(singletonServer.clientConnections, client.GetUniqueId())
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
// 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
|
|
|
|
playerState := singletonServer.clientConnections[client.GetUniqueId()].GetPlayerState()
|
|
|
|
playerState.X = packet.PacketData.(d2netpacket.MovePlayerPacket).DestX
|
|
|
|
playerState.Y = packet.PacketData.(d2netpacket.MovePlayerPacket).DestY
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
for _, player := range singletonServer.clientConnections {
|
|
|
|
player.SendPacketToClient(packet)
|
|
|
|
}
|
2020-06-26 20:03:00 -04:00
|
|
|
case d2netpackettype.CastSkill:
|
|
|
|
for _, player := range singletonServer.clientConnections {
|
|
|
|
player.SendPacketToClient(packet)
|
|
|
|
}
|
2020-06-26 17:12:19 -04:00
|
|
|
}
|
|
|
|
return nil
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|