package game import ( "fmt" "thehouseoficarus/internal/combat" "thehouseoficarus/internal/engine" "thehouseoficarus/internal/net" ) func (g *Game) executeQuit(sess *net.Session, args []string, rawInput string) { g.doQuit(sess) } func (g *Game) doQuit(sess *net.Session) { p := sess.Player if combat.GetCombat(p.Name) != nil { sess.WriteLine("You can't rest during combat!") return } g.cancelAction(p) p.ActionState = &ActionState{Type: ActionResting} g.cancelRest(p.Name) if g.Hub != nil { for _, other := range g.Hub.PlayersInRoom(p.RoomID) { if other != sess && other.Player != nil { other.WriteLine(g.colorize(other, "broadcast", fmt.Sprintf("\n%s sits down to rest.", p.Name))) } } } ticksLeft := 10 id := g.Ticks.Subscribe(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.AccountStore.SaveCharacter(p) g.charsMu.Lock() delete(g.loggedInChars, p.Name) g.charsMu.Unlock() if g.Hub != nil { g.Hub.LeaveRoom(sess) } sess.Player = nil sess.State = 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) } }