package game import ( "strings" "thehouseoficarus/internal/engine" "thehouseoficarus/internal/net" ) func (g *Game) executeQuit(sess *net.Session, args []string, rawInput string) { if len(args) > 0 && strings.EqualFold(args[0], "now") { g.doQuitNow(sess) return } g.doQuit(sess) } func (g *Game) doQuit(sess *net.Session) { p := sess.Player // Combat blocks quitting outright; check it before departing any casino // game so a cancelled quit never costs the player their round. if g.Combat.Get(p.Name) != nil { sess.WriteLine("You can't rest during combat!") return } // Depart casino games the way "stop" does: a table with a wager committed // to the round in flight refuses and the quit is aborted, so deliberate // exits never forfeit a stake or reveal cards mid-hand. Forfeit-on-leave // stays reserved for genuine disconnects and "quit now". if !g.leaveCasinoGames(p) { // A committed wager blocks the departure; whether it is actually lost // depends on whether the game has dealt/started a round for it. A // stake the game has not dealt for yet is refunded on disconnect // (baccarat always, blackjack while the betting window is open), and // a dealt hand or a spin in progress is forfeited. The refusal // therefore states both outcomes instead of promising a forfeit that // an undealt stake does not suffer. sess.WriteLine(g.colorize(sess, "warning", "Type \"quit now\" to log out anyway — a bet still awaiting the deal is refunded; a hand or spin in progress is forfeited.")) return } g.beginQuitCountdown(sess) } // doQuitNow force-quits past a table's mid-round refusal. The logout // countdown starts immediately, but the seat and wager are only forfeited // on the countdown's final tick, immediately before the disconnect — until // then any active or free command (or "stop") cancels the countdown and // leaves the wager intact. func (g *Game) doQuitNow(sess *net.Session) { p := sess.Player if g.Combat.Get(p.Name) != nil { sess.WriteLine("You can't rest during combat!") return } if g.inCasinoGame(p.Name) { sess.WriteLine(g.colorize(sess, "warning", "WARNING: Logging out mid-game will forfeit your bet! Type \"stop\" to cancel.")) } g.beginQuitCountdown(sess) } // beginQuitCountdown runs the shared logout sequence: stop any current // action, then count ten ticks down to the final tick that severs the // session. Casino participation (a "quit now" mid-game) is resolved on // that final tick via MarkDisconnected, so the countdown stays cancellable // the whole way down. func (g *Game) beginQuitCountdown(sess *net.Session) { p := sess.Player g.cancelAction(p) g.cancelRest(p.Name) g.broadcastAction(sess, "%s sits down to rest.", p.Name) ticksLeft := 10 id := g.SubscribeTicks(engine.ToTicks(1), func() bool { switch ticksLeft { case 10: sess.WriteLine("You sit down to rest...") case 6: sess.WriteLine("You catch your breath...") case 3: sess.WriteLine("You close your eyes...") case 0: g.cancelRest(p.Name) g.restoreGodPlayer(p) // Forfeit any committed casino wager and free the seat only // now — immediately before the disconnect. Settling games only // pay seated bettors, so this forfeits a mid-round stake. g.emitCasinoEvents(g.Casino.MarkDisconnected(p.Name)) g.saveCharacter(p) g.charsMu.Lock() delete(g.loggedInChars, p.Name) g.charsMu.Unlock() // The character is leaving the world: drop its queued free/ // active/consume commands with the registry entry, or they // retain this session and replay on a later login as the same // character. if g.queue != nil { g.queue.ClearPlayer(p.Name) } if g.Hub != nil { g.Hub.LeaveRoom(sess) } if g.Hub != nil { g.Hub.SetSessionPlayer(sess, nil) } else { sess.Player = nil } sess.SetState(net.StateMenu) g.showMenu(sess) return false } ticksLeft-- return true }) g.restTimers[p.Name] = id } func (g *Game) cancelRest(playerName string) { if id, ok := g.restTimers[playerName]; ok { g.Ticks.Unsubscribe(id) delete(g.restTimers, playerName) } }