diff --git a/internal/Messages/messages.go b/internal/Messages/messages.go deleted file mode 100644 index c6ccd38..0000000 --- a/internal/Messages/messages.go +++ /dev/null @@ -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`" -} diff --git a/internal/gameServer/gameServer.go b/internal/gameServer/gameServer.go index a6b9cbb..bbef898 100644 --- a/internal/gameServer/gameServer.go +++ b/internal/gameServer/gameServer.go @@ -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 diff --git a/internal/gameServer/messages.go b/internal/gameServer/messages.go new file mode 100644 index 0000000..5fc5a9f --- /dev/null +++ b/internal/gameServer/messages.go @@ -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`" +} diff --git a/internal/gameServer/player.go b/internal/gameServer/player.go index 7cc1b86..4ffabcd 100644 --- a/internal/gameServer/player.go +++ b/internal/gameServer/player.go @@ -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 + } + + 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) - fmt.Println(command) + default: + fmt.Println("player unsupported status") } + +} + +func (pc *Player) ReceivedMessage(p Packet) { + +} + +func (pc *Player) PlayerAction(p Packet) { + } diff --git a/internal/room/room.go b/internal/gameServer/room.go similarity index 86% rename from internal/room/room.go rename to internal/gameServer/room.go index b8b8d7c..08374a1 100644 --- a/internal/room/room.go +++ b/internal/gameServer/room.go @@ -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 diff --git a/internal/room/room_test.go b/internal/gameServer/room_test.go similarity index 87% rename from internal/room/room_test.go rename to internal/gameServer/room_test.go index 56f67d9..e17fd0a 100644 --- a/internal/room/room_test.go +++ b/internal/gameServer/room_test.go @@ -1,4 +1,4 @@ -package room +package gameServer import "testing" diff --git a/internal/player/player.go b/internal/player/player.go deleted file mode 100644 index 130cc2a..0000000 --- a/internal/player/player.go +++ /dev/null @@ -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) - - } -}