aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_quit.go
blob: 46403f4ef6636c2e651c6d5db121542d9fa2e15b (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
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)
	}
}