-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClientMessage.go
151 lines (124 loc) · 3.1 KB
/
ClientMessage.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
151
// ClientMessage
package main
import (
"strconv"
"strings"
"time"
)
type ClientMessage struct {
CombatAction bool
Command string
Value string
}
func newClientMessage(cmd string, val string) *ClientMessage {
return &ClientMessage{CombatAction: false, Command: cmd, Value: val}
}
func (msg *ClientMessage) IsTradeCommand() bool {
switch msg.Command {
case "accept", "done", "add", "reject":
return true
}
return false
}
func (msg *ClientMessage) GetItemQuantity() int {
firstValue := strings.Split(msg.Value, " ")[0]
if val, err := strconv.Atoi(firstValue); err == nil {
return val
} else {
return 1
}
}
func (msg *ClientMessage) GetItem() string {
firstValue := strings.Split(msg.Value, " ")[0]
if _, err := strconv.Atoi(firstValue); err == nil {
return strings.TrimSpace(strings.TrimPrefix(msg.Value, firstValue+" "))
} else {
return strings.TrimSpace(msg.Value)
}
}
func (msg *ClientMessage) GetValue() string {
return msg.Value
}
func (msg *ClientMessage) GetCommand() string {
if strings.Contains(msg.Command, ";") {
return strings.Split(msg.Command, ";")[0]
} else {
return msg.Command
}
}
func (msg *ClientMessage) setCommand(cmd string) {
msg.CombatAction = false
msg.Command = cmd
msg.Value = ""
}
func (msg *ClientMessage) setCommandWithTimestamp(cmd string) {
msg.CombatAction = false
msg.Command = cmd + ";" + time.Now().String()
msg.Value = ""
}
func (msg *ClientMessage) setMsgWithTimestamp(cmd string, value string) {
msg.CombatAction = false
msg.Command = cmd + ";" + time.Now().String()
msg.Value = value
}
func (msg *ClientMessage) getTimeStamp() string {
peices := strings.Split(msg.Command, ";")
if len(peices) == 2 {
return peices[1]
} else {
return ""
}
}
func (msg *ClientMessage) setToMovementMessage(direction string) {
msg.CombatAction = false
msg.Command = "move"
msg.Value = direction
}
func (msg *ClientMessage) setToSayMessage(thingToSay string) {
msg.CombatAction = false
msg.Command = "say"
msg.Value = thingToSay
}
func (msg *ClientMessage) setToGetMessage(item string) {
msg.CombatAction = false
msg.Command = "get"
msg.Value = item
}
func (msg *ClientMessage) setToLookMessage(target string) {
msg.CombatAction = false
msg.Command = "look"
msg.Value = target
}
func (msg *ClientMessage) setToAttackMessage(target string) {
msg.CombatAction = true
msg.Command = "attack"
msg.Value = target
}
func (msg *ClientMessage) setToExitMessage() {
msg.CombatAction = false
msg.Command = "exit"
msg.Value = ""
}
func (msg *ClientMessage) setAll(combatAction bool, cmd string, val string) {
msg.CombatAction = combatAction
msg.Command = cmd
msg.Value = val
}
func (msg *ClientMessage) setAllNonCombat(cmd string, val string) {
msg.CombatAction = false
msg.Command = cmd
msg.Value = val
}
func (message *ClientMessage) getPassword() string {
split := strings.Split(message.Value, " ")
return split[1]
}
func (message *ClientMessage) getUsername() string {
split := strings.Split(message.Value, " ")
return split[0]
}
func (msg *ClientMessage) getBid() int {
val, err := strconv.Atoi(msg.Value)
checkError(err, false)
return val
}