1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-07 18:57:18 -05:00
OpenDiablo2/d2networking/d2server/game_server.go

68 lines
1.9 KiB
Go
Raw Normal View History

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