You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
1.6 KiB

package gameServer
import (
"fmt"
"log"
"sync"
"daydev.org/shipsgs/internal/config"
"daydev.org/shipsgs/internal/player"
"daydev.org/shipsgs/internal/utils"
)
type GameServer struct {
Logger *utils.Logger
Config *config.S_Config
maxLobbies int
currentLobbies int
maxPlayers int
currentPlayers int
// Storing connected Players
playerConns map[*player.Player]bool
// Notify Game Server a New Player
Connection chan *player.Player
//Quit the loops correctly and shut down the Game Server
Shutdown chan bool
//1 Second Update planning
OneSecond chan bool
//sync threads
WG sync.WaitGroup
}
func (m *GameServer) Init(logger *utils.Logger, config *config.S_Config) {
m.Logger = logger
m.Config = config
m.maxLobbies = m.Config.MaxLobbies
m.maxPlayers = m.Config.MaxPlayers
m.Shutdown = make(chan bool)
m.OneSecond = make(chan bool)
m.currentLobbies = 0
m.currentPlayers = 0
m.Connection = make(chan *player.Player)
m.playerConns = make(map[*player.Player]bool)
m.WG.Add(2)
}
func (m *GameServer) Update() {
for {
select {
case pl := <-m.Connection:
m.playerConns[pl] = true
go pl.Receiver()
case <-m.Shutdown:
m.ShutdownServer()
m.WG.Done()
case <-m.OneSecond:
fmt.Println("OneSecond yikes")
fmt.Println(m.playerConns)
default:
// default actions
}
}
}
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
}
}