aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_quit.go
blob: e564c7e638cd719fb7029d3bb92ab66abbd123d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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) {
		sess.WriteLine(g.colorize(sess, "warning", "Type \"quit now\" to log out anyway — your bet will be 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()
			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)
	}
}