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.
104 lines
1.7 KiB
104 lines
1.7 KiB
package gameServer |
|
|
|
import ( |
|
"fmt" |
|
"log" |
|
"sync" |
|
|
|
"daydev.org/shipsgs/internal/config" |
|
"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]bool |
|
|
|
// Notify Game Server a New Player |
|
Connection chan *Player |
|
|
|
//Quit the loops correctly and shut down the Game Server |
|
Shutdown chan bool |
|
|
|
//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 |
|
|
|
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) |
|
|
|
m.playerConns = make(map[*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 |
|
} |
|
|
|
}
|
|
|