aboutsummaryrefslogtreecommitdiff
path: root/internal/game/prediction_interface.go
blob: 3281f2d466cb74b986cf3e836f338d19735dbe62 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package game

import (
	"strings"

	"thehouseoficarus/internal/net"
)

func predictionUseTarget(input string) bool {
	switch strings.ToLower(strings.TrimSpace(input)) {
	case "predictions", "prediction", "market", "counter", "exchange", "prediction market", "prediction counter", "prediction exchange", "prediction market counter":
		return true
	}
	return false
}

var predictionVerbs = []string{"markets", "view", "find", "next", "prev", "back", "active", "closed", "holding", "buy", "sell", "collect", "refresh", "rules", "stop"}

func (g *Game) openPredictionCounter(s *net.Session) {
	if !g.atPredictionCounter(s.Player) {
		s.WriteLine("There is no Prediction Exchange here.")
		return
	}
	if !g.leaveCasinoGames(s.Player) {
		s.WriteLine("Finish your current casino round before using the counter.")
		return
	}
	if s.PredictionActive {
		g.executePredictions(s, []string{"refresh"}, "")
		return
	}
	g.closeCryptoCounter(s)
	s.PredictionActive = true
	s.PredictionEpoch++
	s.PredictionBusy = false
	s.PredictionSelected = ""
	s.PredictionScreen = "list"
	s.PredictionPage = 1
	s.PredictionBoardPage = 1
	s.PredictionSearch = ""
	s.PredictionUnavailable = false
	g.executePredictions(s, nil, "open")
}

func (g *Game) closePredictionCounter(s *net.Session) {
	if s.PredictionCancel != nil {
		s.PredictionCancel()
		s.PredictionCancel = nil
	}
	s.PredictionActive = false
	s.PredictionEpoch++ // Invalidate any download, including an in-flight trade.
	s.PredictionBusy = false
	s.PredictionSelected = ""
}

// Exact world commands keep their meaning unless explicitly part of this UI.
// Abbreviations are scoped to the counter; no new global command is registered.
func predictionVerb(cmd string) (string, bool) {
	switch cmd {
	case "list":
		return "markets", true
	case "table", "game":
		return "refresh", true
	case "status", "portfolio", "board":
		return "active", true
	case "help":
		return "help", true
	case "leave", "exit":
		return "stop", true
	}
	for _, v := range predictionVerbs {
		if cmd == v {
			return v, true
		}
	}
	if _, exists := commandRegistry[cmd]; exists {
		return "", false
	}
	found := ""
	for _, v := range predictionVerbs {
		if strings.HasPrefix(v, cmd) {
			if found != "" {
				return "", true
			}
			found = v
		}
	}
	return found, found != ""
}
func (g *Game) predictionInputOwned(s *net.Session, cmd string) bool {
	if !s.PredictionActive || !g.atPredictionCounter(s.Player) {
		return false
	}
	if isAllDigits(cmd) {
		return true
	}
	_, ok := predictionVerb(cmd)
	return ok
}
func (g *Game) handlePredictionInput(s *net.Session, cmd string, args []string) bool {
	if !s.PredictionActive {
		return false
	}
	if !g.atPredictionCounter(s.Player) {
		g.closePredictionCounter(s)
		return false
	}
	if cmd == "help" && len(args) > 0 {
		return false
	}
	if isAllDigits(cmd) {
		action := "view"
		if s.PredictionScreen == "active" || s.PredictionScreen == "closed" {
			action = "holding"
		}
		g.executePredictions(s, []string{action, cmd}, "")
		return true
	}
	verb, owned := predictionVerb(cmd)
	if !owned {
		if cmd == "use" && predictionUseTarget(strings.Join(args, " ")) {
			return false
		}
		switch cmd {
		case "play", "use", "bank", "quit", "logout":
			g.closePredictionCounter(s)
		}
		return false
	}
	if verb == "" {
		s.WriteLine("That shortcut is ambiguous. Choose a command below.")
		g.predictionHints(s)
		return true
	}
	if verb == "stop" {
		g.closePredictionCounter(s)
		s.WriteLine(g.colorize(s, "casino_action", "You step away from the Prediction Exchange. Your contracts stay with the house."))
		return true
	}
	switch verb {
	case "markets":
		verb = "list"
	case "back":
		args = nil
	case "help":
		g.predictionHelp(s)
		return true
	case "rules":
		if s.PredictionScreen != "view" || s.PredictionSelected == "" {
			g.predictionHelp(s)
			return true
		}
	}
	g.executePredictions(s, append([]string{verb}, args...), "")
	return true
}