You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.1 KiB

package gameServer
import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
)
type Player struct {
Name string `json:"Name"`
Password string `json:"Password"`
//Connection
Conn *websocket.Conn
AuthString string `json:"AuthString"`
Level string `json:"Level"` // hidden from user, for balancing purposes
Kills int `json:"Kills"`
Killed int `json:"Killed"`
Won int `json:"Won"`
Lost int `json:"Lost"`
WinRate int `json:"WinRate"`
Health int `json:"Health"`
Authed bool `json:"Authed"`
GS *GameServer `json:"GS"`
}
func (pc *Player) Receiver() {
for {
_, message, err := pc.Conn.ReadMessage()
if err != nil {
fmt.Println("pc err: " + err.Error())
}
var msg Packet
err = json.Unmarshal(message, &msg)
if err != nil {
fmt.Println("pc err: " + err.Error())
}
if !pc.Authed && msg.Type != Auth {
reply := Packet{
Type: System,
Status: 401,
Message: nil,
}
authRequired, err := json.Marshal(reply)
if err != nil {
fmt.Println("pc err: " + err.Error())
}
pc.Conn.WriteMessage(websocket.TextMessage, authRequired)
//Stop processing the packet of unauthed connection
return
}
switch msg.Type {
case Auth:
pc.Auth(msg)
case Message:
case PlayerAction:
pc.PlayerAction(msg)
case System:
case Lobby:
pc.Lobby(msg)
default:
}
}
}
//TODO
func (pc *Player) Auth(p Packet) (pr Packet) {
// FOR DEV PURPOSES ONLY
// This function meant to become an authentication function!
pReply := Packet{
Type: Auth,
Status: 200,
Message: nil,
}
fmt.Println("Auth successful")
pc.Authed = true
return pReply
}
func (pc *Player) Lobby(p Packet) {
switch p.Status {
case LobbyListRequest: // Lobby list request
p.Message = pc.GS.Lobbies
p.Status = LobbyListAnswer
p.Type = Lobby
pMarshall, err := json.Marshal(p)
if err != nil {
fmt.Println("pc.Lobby: " + err.Error())
}
pc.Conn.WriteMessage(websocket.TextMessage, pMarshall)
default:
fmt.Println("player unsupported status")
}
}
func (pc *Player) ReceivedMessage(p Packet) {
}
func (pc *Player) PlayerAction(p Packet) {
}