Big work on GameServer lead to bug in closing the application

master
Evgeny Kovalev 2 years ago
parent df6256224c
commit b2a1571f8d
  1. 13
      internal/Messages/messages.go
  2. 43
      internal/gameServer/gameServer.go
  3. 18
      internal/room/room.go
  4. 1
      internal/server/server.go
  5. 11
      main.go

@ -0,0 +1,13 @@
package messages
/*
const (
Message string = "Message"
PlayerAction string = "PlayerAction"
System string = "System"
)
type PlayerMessage struct {
}
*/

@ -1,6 +1,9 @@
package gameServer package gameServer
import ( import (
"fmt"
"log"
"daydev.org/shipsgs/internal/config" "daydev.org/shipsgs/internal/config"
"daydev.org/shipsgs/internal/player" "daydev.org/shipsgs/internal/player"
"daydev.org/shipsgs/internal/utils" "daydev.org/shipsgs/internal/utils"
@ -16,13 +19,14 @@ type GameServer struct {
maxPlayers int maxPlayers int
currentPlayers int currentPlayers int
// Channel to register players and their connection
join chan *player.Player
// Storing connected Players // Storing connected Players
playerConns map[*player.Player]bool 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) { 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() { 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() { func (m *GameServer) Scheduled1S() {
@ -52,3 +66,16 @@ func (m *GameServer) Scheduled1S() {
func (m *GameServer) Scheduled10S() { 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
}
}

@ -28,6 +28,14 @@ type Room struct {
//Room state //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() { func (r *Room) PlayerJoin() {
@ -43,8 +51,18 @@ func (r *Room) TextAnnounce() {
} }
func (r *Room) Update() { 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 { func RandomName() string {
s := rand.NewSource(time.Now().UnixNano()) s := rand.NewSource(time.Now().UnixNano())

@ -43,6 +43,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
player := player.Player{ player := player.Player{
Conn: conn, Conn: conn,
} }
player.Name = "Yale"
log.Println(r.RemoteAddr) log.Println(r.RemoteAddr)

@ -6,6 +6,7 @@ import (
"os" "os"
"daydev.org/shipsgs/internal/config" "daydev.org/shipsgs/internal/config"
"daydev.org/shipsgs/internal/gameServer"
"daydev.org/shipsgs/internal/server" "daydev.org/shipsgs/internal/server"
"daydev.org/shipsgs/internal/utils" "daydev.org/shipsgs/internal/utils"
"github.com/xlab/closer" "github.com/xlab/closer"
@ -14,6 +15,9 @@ import (
var Logger *utils.Logger var Logger *utils.Logger
var ch_sighup *chan bool var ch_sighup *chan bool
//GameServer Public Global Instance
var GS *gameServer.GameServer
func main() { func main() {
logFile, err := os.OpenFile("runlog.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) logFile, err := os.OpenFile("runlog.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
@ -31,6 +35,11 @@ func main() {
//If debug - fill Stdout too - LOG to use? //If debug - fill Stdout too - LOG to use?
//Logger = utils.New(os.Stdout, utils.LevelInfo) //Logger = utils.New(os.Stdout, utils.LevelInfo)
//GameServer
var gs gameServer.GameServer
GS = &gs
go gs.Update()
config.ReadConfig() config.ReadConfig()
log.Println("Starting Healthy") log.Println("Starting Healthy")
@ -44,6 +53,8 @@ func cleanup() {
fmt.Println("Shutting down ") fmt.Println("Shutting down ")
*ch_sighup <- true *ch_sighup <- true
//Investigate why no messages on correct closing
GS.Shutdown = nil
Logger.PrintInfo("main", map[string]string{ Logger.PrintInfo("main", map[string]string{
"Info": "Closing Application Normally", "Info": "Closing Application Normally",

Loading…
Cancel
Save