Merged room and player into gameServer. First communication testing. Stable release with communication at a start

master
Evgeny Kovalev 1 year ago
parent f22ef27ea1
commit c367bf5381
  1. 24
      internal/Messages/messages.go
  2. 4
      internal/gameServer/gameServer.go
  3. 52
      internal/gameServer/messages.go
  4. 76
      internal/gameServer/player.go
  5. 10
      internal/gameServer/room.go
  6. 2
      internal/gameServer/room_test.go
  7. 47
      internal/player/player.go

@ -1,24 +0,0 @@
package messages
const (
Auth string = "Auth"
Message string = "Message"
PlayerAction string = "PlayerAction"
System string = "System"
)
/*
*/
type PlayerMsg struct {
Type string "json:`Type`"
Status int "json:`Status`"
Message interface{} "json:`Message`"
}
type AuthMsg struct {
Login string "json:`Login`"
Password string "json:`Password`"
}

@ -31,10 +31,14 @@ type GameServer struct {
//1 Second Update planning
OneSecond chan bool
//Created Rooms
Lobbies [5]Room
//sync threads
WG sync.WaitGroup
}
//TODO update room creation to be dynamic and according to the config
func (m *GameServer) Init(logger *utils.Logger, config *config.S_Config) {
m.Logger = logger
m.Config = config

@ -0,0 +1,52 @@
package gameServer
const (
Auth string = "Auth"
Message string = "Message"
PlayerAction string = "PlayerAction"
System string = "System"
Lobby string = "Lobby"
)
const (
OK int = 200
LobbyListRequest int = 201
LobbyListAnswer int = 202
BadRequest int = 400
Forbidden int = 403
Unauthorized = 401
InternalServerError int = 500
)
/*
Status:
200 - OK
201 - Lobby List Request
202 - Lobby List Answer
400 - Bad request
403 - Forbidden
401 - Unauthorized
500 - Internal server error
*/
type Packet struct {
Type string "json:`Type`"
Status int "json:`Status`"
Message interface{} "json:`Message`"
}
type AuthMsg struct {
Login string "json:`Login`"
Password string "json:`Password`"
}
type LobbyList struct {
Lobbies []Room "json:`Lobbies`"
}

@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
messages "daydev.org/shipsgs/internal/Messages"
"github.com/gorilla/websocket"
)
@ -35,14 +34,20 @@ type Player struct {
func (pc *Player) Receiver() {
for {
_, command, err := pc.Conn.ReadMessage()
_, message, err := pc.Conn.ReadMessage()
if err != nil {
fmt.Println("pc err: " + err.Error())
}
if pc.Authed == false {
reply := messages.PlayerMsg{
Type: messages.System,
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,
}
@ -52,8 +57,67 @@ func (pc *Player) Receiver() {
fmt.Println("pc err: " + err.Error())
}
pc.Conn.WriteMessage(websocket.TextMessage, authRequired)
//Stop processing the packet of unauthed connection
return
}
fmt.Println(command)
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) {
}

@ -1,10 +1,8 @@
package room
package gameServer
import (
"math/rand"
"time"
"daydev.org/shipsgs/internal/player"
)
const ()
@ -24,15 +22,15 @@ type Room struct {
Name string `json:"Name"`
//Players
players map[*player.Player]bool
players map[*Player]bool
//Room state
// Channel to register players and their connection
join chan *player.Player
join chan *Player
// Channel to un register players
leave chan *player.Player
leave chan *Player
// Channel to update All in the room
updateAll chan bool

@ -1,4 +1,4 @@
package room
package gameServer
import "testing"

@ -1,47 +0,0 @@
package player
import (
"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"`
}
func (pc *Player) Receiver() {
for {
fmt.Println("ticker")
_, command, err := pc.Conn.ReadMessage()
if err != nil {
fmt.Println("pc err: " + err.Error())
}
pc.Conn.WriteMessage(websocket.TextMessage, []byte("otvet"))
fmt.Println(command)
}
}
Loading…
Cancel
Save