package game import ( "fmt" "os" "strconv" "time" "thehouseoficarus/internal/net" ) func (g *Game) executeShutdown(sess *net.Session, args []string, rawInput string) { if !g.checkAdmin(sess) { sess.WriteLine("Unknown command.") return } if len(args) == 0 { sess.WriteLine("Usage: shutdown or shutdown cancel") return } if args[0] == "cancel" { g.shutdownMu.Lock() if !g.shutdownActive { g.shutdownMu.Unlock() sess.WriteLine("No shutdown in progress.") return } close(g.shutdownCancel) g.shutdownActive = false g.shutdownMu.Unlock() for _, other := range g.Hub.AllSessions() { if other.Player != nil { other.WriteLine("[Server] Shutdown cancelled.") } } return } minutes, err := strconv.Atoi(args[0]) if err != nil || minutes <= 0 { sess.WriteLine("Invalid minutes.") return } g.shutdownMu.Lock() if g.shutdownActive { g.shutdownMu.Unlock() sess.WriteLine("A shutdown is already in progress.") return } g.shutdownActive = true g.shutdownCancel = make(chan struct{}) g.shutdownMu.Unlock() go func() { remaining := minutes * 60 ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() for _, other := range g.Hub.AllSessions() { if other.Player != nil { other.WriteLine(fmt.Sprintf("[Server] Shutting down in %d minute(s).", minutes)) } } for { select { case <-ticker.C: remaining -= 30 if remaining <= 0 { for _, other := range g.Hub.AllSessions() { if other.Player != nil { other.WriteLine("[Server] Shutting down NOW.") } } os.Exit(0) } mins := remaining / 60 secs := remaining % 60 if secs == 0 { for _, other := range g.Hub.AllSessions() { if other.Player != nil { other.WriteLine(fmt.Sprintf("[Server] Shutting down in %d minute(s).", mins)) } } } else { for _, other := range g.Hub.AllSessions() { if other.Player != nil { other.WriteLine(fmt.Sprintf("[Server] Shutting down in %d minute(s) %d seconds.", mins, secs)) } } } case <-g.shutdownCancel: return } } }() sess.WriteLine(fmt.Sprintf("Shutdown scheduled in %d minute(s).", minutes)) }