Improved server
This commit is contained in:
@ -3,70 +3,78 @@ package game
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"server/core"
|
||||
"server/packet"
|
||||
"server/game/packet"
|
||||
)
|
||||
|
||||
var (
|
||||
Clients = NewManager()
|
||||
LoginSuccess = []byte{0}
|
||||
LoginFailure = []byte{1}
|
||||
)
|
||||
|
||||
// Login checks the account credentials and gives a network peer access to an account.
|
||||
func Login(data []byte, address *net.UDPAddr, server *core.Server) error {
|
||||
var loginRequest [2]string
|
||||
err := json.Unmarshal(data, &loginRequest)
|
||||
func (game *Game) Login(data []byte, address *net.UDPAddr, server *Server) error {
|
||||
username, password, err := getLoginData(data)
|
||||
|
||||
if err != nil {
|
||||
server.Send(packet.Login, LoginFailure, address)
|
||||
return err
|
||||
}
|
||||
|
||||
username := loginRequest[0]
|
||||
password := loginRequest[1]
|
||||
|
||||
if password != "password" {
|
||||
server.Send(packet.Login, LoginFailure, address)
|
||||
return errors.New("login failure")
|
||||
}
|
||||
|
||||
randomBytes := make([]byte, 32)
|
||||
_, err = rand.Read(randomBytes)
|
||||
player := game.players.Get(address)
|
||||
player.AuthToken = createAuthToken()
|
||||
player.Name = username
|
||||
player.KeepAlive()
|
||||
|
||||
if err != nil {
|
||||
server.Send(packet.Login, LoginFailure, address)
|
||||
return err
|
||||
if username == "user0" {
|
||||
player.Position.X = 5.0
|
||||
}
|
||||
|
||||
client := Clients.Get(address)
|
||||
client.KeepAlive()
|
||||
client.authToken = base64.StdEncoding.EncodeToString(randomBytes)
|
||||
client.player.Name = username
|
||||
client.player.Position.X = 5.0
|
||||
playerState := []byte(client.player.Name + "\u0000")
|
||||
playerState = appendVector3(playerState, client.player.Position)
|
||||
if username == "user1" {
|
||||
player.Position.X = -5.0
|
||||
}
|
||||
|
||||
server.Send(packet.Login, append(LoginSuccess, []byte(client.authToken)...), address)
|
||||
server.Send(packet.PlayerState, playerState, address)
|
||||
if username == "user2" {
|
||||
player.Position.Z = -5.0
|
||||
}
|
||||
|
||||
if username == "user3" {
|
||||
player.Position.Z = 5.0
|
||||
}
|
||||
|
||||
server.Send(packet.Login, append(LoginSuccess, []byte(player.AuthToken)...), address)
|
||||
player.OnConnect()
|
||||
|
||||
game.players.Each(func(other *Player) bool {
|
||||
server.Send(packet.PlayerState, other.State(), address)
|
||||
return true
|
||||
})
|
||||
|
||||
fmt.Printf("%s logged in.\n", username)
|
||||
return nil
|
||||
}
|
||||
|
||||
func appendVector3(data []byte, vector Vector3) []byte {
|
||||
bits := math.Float32bits(vector.X)
|
||||
data = binary.LittleEndian.AppendUint32(data, bits)
|
||||
func getLoginData(data []byte) (string, string, error) {
|
||||
loginRequest := [2]string{}
|
||||
err := json.Unmarshal(data, &loginRequest)
|
||||
|
||||
bits = math.Float32bits(vector.Y)
|
||||
data = binary.LittleEndian.AppendUint32(data, bits)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
bits = math.Float32bits(vector.Z)
|
||||
return binary.LittleEndian.AppendUint32(data, bits)
|
||||
username := loginRequest[0]
|
||||
password := loginRequest[1]
|
||||
return username, password, nil
|
||||
}
|
||||
|
||||
func createAuthToken() string {
|
||||
randomBytes := make([]byte, 32)
|
||||
rand.Read(randomBytes)
|
||||
return base64.StdEncoding.EncodeToString(randomBytes)
|
||||
}
|
||||
|
Reference in New Issue
Block a user