diff --git a/server/game/Chat.go b/server/game/Chat.go index 2e61339..0501f49 100644 --- a/server/game/Chat.go +++ b/server/game/Chat.go @@ -34,7 +34,7 @@ func (game *Game) ChatCommand(player *Player, message string) bool { switch message { case "/logout": - game.server.Send(Logout, nil, player.address) + player.Send(Logout, nil) game.players.Remove(player) } diff --git a/server/game/Game.go b/server/game/Game.go index f1b3447..4e036ee 100644 --- a/server/game/Game.go +++ b/server/game/Game.go @@ -72,7 +72,7 @@ func (game *Game) Run() { // Broadcast sends the packet to all players. func (game *Game) Broadcast(code byte, data []byte) { game.players.Each(func(player *Player) { - game.server.Send(code, data, player.address) + player.Send(code, data) }) } @@ -83,6 +83,6 @@ func (game *Game) BroadcastOthers(code byte, data []byte, exclude *Player) { return } - game.server.Send(code, data, player.address) + player.Send(code, data) }) } diff --git a/server/game/Login.go b/server/game/Login.go index 4da228a..8daa7a3 100644 --- a/server/game/Login.go +++ b/server/game/Login.go @@ -73,7 +73,7 @@ func (game *Game) sendLoginSuccess(player *Player) { response := []byte{Success} response = AppendString(response, player.ID) response = AppendString(response, player.authToken) - game.server.Send(Login, response, player.address) + player.Send(Login, response) } func getLoginData(data []byte) (string, string, error) { diff --git a/server/game/Player.go b/server/game/Player.go index 5ade4d5..b985da0 100644 --- a/server/game/Player.go +++ b/server/game/Player.go @@ -10,6 +10,7 @@ import ( type Player struct { *Account game *Game + server *Server authToken string address *net.UDPAddr direction Vector3 @@ -22,6 +23,7 @@ func NewPlayer(address *net.UDPAddr, account *Account, game *Game) *Player { Account: account, address: address, game: game, + server: game.server, } } @@ -49,17 +51,21 @@ func (player *Player) State() []byte { return state } +// Send sends a message to the player. +func (player *Player) Send(code byte, data []byte) { + player.server.Send(code, data, player.address) +} + // OnConnect is executed when a connection has been established. func (player *Player) OnConnect() { fmt.Printf("%s connected.\n", player.Name) players := player.game.players - server := player.game.server players.Each(func(other *Player) { - server.Send(PlayerAdd, other.State(), player.address) + player.Send(PlayerAdd, other.State()) if other != player { - server.Send(PlayerAdd, player.State(), other.address) + other.Send(PlayerAdd, player.State()) } }) }