2020-06-13 18:32:09 -04:00
|
|
|
package d2server
|
|
|
|
|
|
|
|
import (
|
2020-06-18 14:11:04 -04:00
|
|
|
"encoding/json"
|
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-21 18:40:37 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2map/d2mapengine"
|
2020-06-26 16:50:24 -04:00
|
|
|
packet "github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket"
|
|
|
|
packettype "github.com/OpenDiablo2/OpenDiablo2/d2networking/d2netpacket/d2netpackettype"
|
|
|
|
d2udp "github.com/OpenDiablo2/OpenDiablo2/d2networking/d2server/d2udpclientconnection"
|
2020-06-18 14:11:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2script"
|
2020-06-13 18:32:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type GameServer struct {
|
2020-06-22 20:31:42 -04:00
|
|
|
sync.RWMutex
|
2020-06-26 16:50:24 -04:00
|
|
|
version string
|
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 16:50:24 -04:00
|
|
|
realm *d2mapengine.MapRealm
|
2020-06-18 14:11:04 -04:00
|
|
|
scriptEngine *d2script.ScriptEngine
|
|
|
|
udpConnection *net.UDPConn
|
|
|
|
seed int64
|
|
|
|
running bool
|
2020-06-26 16:50:24 -04:00
|
|
|
maxClients int
|
|
|
|
lastAdvance float64
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:50:24 -04:00
|
|
|
func (srv *GameServer) handlePlayerConnRequest(addr *net.UDPAddr, data string) {
|
|
|
|
packetData := &packet.PlayerConnectionRequestPacket{}
|
|
|
|
json.Unmarshal([]byte(data), packetData)
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-06-26 16:50:24 -04:00
|
|
|
srvCon := srv.udpConnection
|
|
|
|
packetId := packetData.Id
|
|
|
|
clientCon := d2udp.CreateUDPClientConnection(srvCon, packetId, addr)
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-06-26 16:50:24 -04:00
|
|
|
state := packetData.PlayerState
|
|
|
|
clientCon.SetPlayerState(state)
|
|
|
|
OnClientConnected(clientCon)
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:50:24 -04:00
|
|
|
func (srv *GameServer) handleMovePlayer(addr *net.UDPAddr, data string) {
|
|
|
|
packetData := &packet.MovePlayerPacket{}
|
|
|
|
json.Unmarshal([]byte(data), packetData)
|
2020-06-18 14:11:04 -04:00
|
|
|
|
2020-06-26 16:50:24 -04:00
|
|
|
netPacket := packet.NetPacket{
|
|
|
|
PacketType: packettype.MovePlayer,
|
|
|
|
PacketData: packetData,
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:50:24 -04:00
|
|
|
for _, player := range srv.clientConnections {
|
|
|
|
player.SendPacketToClient(netPacket)
|
2020-06-18 14:11:04 -04:00
|
|
|
}
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:50:24 -04:00
|
|
|
func (srv *GameServer) handlePong(addr *net.UDPAddr, data string) {
|
|
|
|
packetData := packet.PlayerConnectionRequestPacket{}
|
|
|
|
json.Unmarshal([]byte(data), &packetData)
|
|
|
|
srv.manager.Recv(packetData.Id)
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-26 16:50:24 -04:00
|
|
|
func (srv *GameServer) handlePlayerDisconnectNotification(data string) {
|
|
|
|
var packet packet.PlayerDisconnectRequestPacket
|
|
|
|
json.Unmarshal([]byte(data), &packet)
|
|
|
|
log.Printf("Received disconnect: %s", packet.Id)
|
2020-06-13 18:32:09 -04:00
|
|
|
}
|