aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_quit.go
blob: 89b6e7f03178b526fe178cff56a36ca051164c92 (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
package game

import (
	"thehouseoficarus/internal/combat"
	"thehouseoficarus/internal/engine"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) doQuit(sess *net.Session) {
	p := sess.Player.(*player.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)

	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)
	}
}