fixes to WS server suporting player and GS. Player and GameServer merged to support eachother and future functions

master
Evgeny Kovalev 1 year ago
parent ff86522f0e
commit f22ef27ea1
  1. 9
      internal/gameServer/gameServer.go
  2. 59
      internal/gameServer/player.go
  3. 5
      internal/server/server.go

@ -6,7 +6,6 @@ import (
"sync"
"daydev.org/shipsgs/internal/config"
"daydev.org/shipsgs/internal/player"
"daydev.org/shipsgs/internal/utils"
)
@ -21,10 +20,10 @@ type GameServer struct {
currentPlayers int
// Storing connected Players
playerConns map[*player.Player]bool
playerConns map[*Player]bool
// Notify Game Server a New Player
Connection chan *player.Player
Connection chan *Player
//Quit the loops correctly and shut down the Game Server
Shutdown chan bool
@ -49,9 +48,9 @@ func (m *GameServer) Init(logger *utils.Logger, config *config.S_Config) {
m.currentLobbies = 0
m.currentPlayers = 0
m.Connection = make(chan *player.Player)
m.Connection = make(chan *Player)
m.playerConns = make(map[*player.Player]bool)
m.playerConns = make(map[*Player]bool)
m.WG.Add(2)

@ -0,0 +1,59 @@
package gameServer
import (
"encoding/json"
"fmt"
messages "daydev.org/shipsgs/internal/Messages"
"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 {
_, command, err := pc.Conn.ReadMessage()
if err != nil {
fmt.Println("pc err: " + err.Error())
}
if pc.Authed == false {
reply := messages.PlayerMsg{
Type: messages.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)
}
fmt.Println(command)
}
}

@ -7,7 +7,6 @@ import (
"net/http"
"daydev.org/shipsgs/internal/gameServer"
"daydev.org/shipsgs/internal/player"
"github.com/gorilla/websocket"
)
@ -43,10 +42,12 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
return
}
player := &player.Player{
player := &gameServer.Player{
Conn: conn,
}
player.Name = "Yale"
player.Authed = false
player.GS = GS
GS.Connection <- player

Loading…
Cancel
Save