diff --git a/internal/Messages/messages.go b/internal/Messages/messages.go new file mode 100644 index 0000000..f4a8117 --- /dev/null +++ b/internal/Messages/messages.go @@ -0,0 +1,13 @@ +package messages + +/* +const ( + Message string = "Message" + PlayerAction string = "PlayerAction" + System string = "System" +) + +type PlayerMessage struct { + +} +*/ diff --git a/internal/gameServer/gameServer.go b/internal/gameServer/gameServer.go index e8bde41..9e1ffdb 100644 --- a/internal/gameServer/gameServer.go +++ b/internal/gameServer/gameServer.go @@ -1,6 +1,9 @@ package gameServer import ( + "fmt" + "log" + "daydev.org/shipsgs/internal/config" "daydev.org/shipsgs/internal/player" "daydev.org/shipsgs/internal/utils" @@ -16,13 +19,14 @@ type GameServer struct { maxPlayers int currentPlayers int - // Channel to register players and their connection - join chan *player.Player - // Storing connected Players playerConns map[*player.Player]bool - // Channel to un register players + // Notify Game Server a New Player + connection chan *player.Player + + //Quit the loops correctly and shut down the Game Server + Shutdown chan bool } func (m *GameServer) Init(logger *utils.Logger, config *config.S_Config) { @@ -38,11 +42,21 @@ func (m *GameServer) Init(logger *utils.Logger, config *config.S_Config) { } func (m *GameServer) Update() { - //p := <-m.join + for { + // select - go through the messages + select { + case c := <-m.connection: + fmt.Println("a new connection", c) + } + + //default shutdown + if m.Shutdown == nil { + m.ShutdownServer() + break + } + + } - // If room doesnt exist, spawn it, name it - //Add a player to a room - // } func (m *GameServer) Scheduled1S() { @@ -52,3 +66,16 @@ func (m *GameServer) Scheduled1S() { func (m *GameServer) Scheduled10S() { } + +func (m *GameServer) ShutdownServer() { + fmt.Println("shutting down the GameServer") + +} + +func (m *GameServer) CreateLobby() { + if m.currentLobbies >= m.maxLobbies { + log.Fatal("Server cannot spawn more lobbies than it already has") + return + } + +} diff --git a/internal/room/room.go b/internal/room/room.go index 9de7f89..6d9eac9 100644 --- a/internal/room/room.go +++ b/internal/room/room.go @@ -28,6 +28,14 @@ type Room struct { //Room state + // Channel to register players and their connection + join chan *player.Player + + // Channel to un register players + leave chan *player.Player + + // Channel to update All in the room + updateAll chan bool } func (r *Room) PlayerJoin() { @@ -43,7 +51,17 @@ func (r *Room) TextAnnounce() { } func (r *Room) Update() { - + for { + select { + case pj := <-r.join: + // player joins + case pl := <-r.leave: + // player leaves + case <-r.updateAll: + // update the room + } + + } } func RandomName() string { diff --git a/internal/server/server.go b/internal/server/server.go index ecbe9b7..31e8fe3 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -43,6 +43,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { player := player.Player{ Conn: conn, } + player.Name = "Yale" log.Println(r.RemoteAddr) diff --git a/main.go b/main.go index 67ea9d8..5d9a551 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "os" "daydev.org/shipsgs/internal/config" + "daydev.org/shipsgs/internal/gameServer" "daydev.org/shipsgs/internal/server" "daydev.org/shipsgs/internal/utils" "github.com/xlab/closer" @@ -14,6 +15,9 @@ import ( var Logger *utils.Logger var ch_sighup *chan bool +//GameServer Public Global Instance +var GS *gameServer.GameServer + func main() { logFile, err := os.OpenFile("runlog.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { @@ -31,6 +35,11 @@ func main() { //If debug - fill Stdout too - LOG to use? //Logger = utils.New(os.Stdout, utils.LevelInfo) + //GameServer + var gs gameServer.GameServer + GS = &gs + go gs.Update() + config.ReadConfig() log.Println("Starting Healthy") @@ -44,6 +53,8 @@ func cleanup() { fmt.Println("Shutting down ") *ch_sighup <- true + //Investigate why no messages on correct closing + GS.Shutdown = nil Logger.PrintInfo("main", map[string]string{ "Info": "Closing Application Normally",