/
main.go
150 lines (131 loc) · 3.47 KB
/
main.go
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
package main
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"strconv"
"strings"
)
func newPrimitive(t string) tview.Primitive {
return tview.NewTextView().
SetTextAlign(tview.AlignCenter).
SetText(t)
}
var inputFieldWidth = 64
var app = tview.NewApplication()
var text = tview.NewTextView().
SetText("Welcome\n(q) to quit")
var inputField = tview.NewInputField().
SetLabel("Command: ").
SetFieldWidth(inputFieldWidth).
SetAcceptanceFunc(tview.InputFieldMaxLength(64))
var grid = tview.NewGrid().
SetRows(3, 0, 3).
SetBorders(false).
AddItem(newPrimitive("evildojo text rpg"), 0, 0, 1, 1, 0, 0, false).
AddItem(text, 1, 0, 1, 1, 0, 0, false).
AddItem(inputField, 2, 0, 1, 1, 0, 0, true)
var currentRoomIndex int = 0
var roomList []Room = []Room{}
// a map of commands that we have registered
// string to bool
var commandMap = make(map[string]bool)
func handleQuit(t string) {
if t == "q" || t == "quit" || t == "exit" {
app.Stop()
}
}
func processHelp() string {
var s string = "Commands:\n"
for k, _ := range commandMap {
// grab the first letter of k and wrap it in []
s = s + "[" + string(k[0]) + "]" + k[1:] + "\n"
//s = s + k + "\n"
}
return s
}
func processGoto(param int) string {
for _, v := range roomList[currentRoomIndex].exits {
if v == param {
currentRoomIndex = param
var s string = "You are now in " + roomList[currentRoomIndex].name + ".\n" + roomList[currentRoomIndex].description
return s
}
}
return "Invalid index supplied."
}
func processCommand(t string) string {
// split t into words
var words []string = strings.Split(t, " ")
if words[0] == "look" || words[0] == "l" {
return roomList[currentRoomIndex].description
} else if words[0] == "goto" || words[0] == "g" {
if len(words) > 1 {
// if the second word is a room index
//var param int = stringToInt(words[1])
var param int = -1
param, err := strconv.Atoi(words[1])
if err != nil {
return "Invalid index supplied."
}
if param != -1 {
return processGoto(param)
} else {
return "I don't understand that command."
}
}
var s string = ""
s = "Where do you want to go?\n"
for _, v := range roomList[currentRoomIndex].exits {
for j, v2 := range roomList {
if roomList[v].name == v2.name {
s = s + strconv.Itoa(j) + " " + v2.name + "\n"
}
}
}
return s
} else if t == "help" || t == "h" || t == "?" {
return processHelp()
}
return "I don't understand that command."
}
func safeProcessCommand(t string) string {
// lowercase t
t = strings.ToLower(t)
// if t is a key in commandMap that is true
if commandMap[t] {
return processCommand(t)
} else {
return "I don't understand that command."
}
}
func inputFieldDoneFunction(key tcell.Key) {
t := inputField.GetText()
// lowercase t
t = strings.ToLower(t)
if t == "q" || t == "quit" || t == "exit" {
app.Stop()
} else {
inputField.SetText("")
text.SetText(processCommand(t))
}
}
func registerCommands() {
commandMap["quit"] = true
commandMap["help"] = true
commandMap["look"] = true
commandMap["goto"] = true
}
func main() {
room0 := roomCreateNewRoom("Starting Room")
roomAddDescription(&room0, "An empty room.")
roomAddExit(&room0, 1)
room1 := roomCreateNewRoom("Command Room")
roomAddDescription(&room1, "A room with a list of commands.")
roomAddExit(&room1, 0)
roomList = append(roomList, room0)
roomList = append(roomList, room1)
registerCommands()
app.SetRoot(grid, true)
inputField.SetDoneFunc(inputFieldDoneFunction)
app.Run()
}