-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientMessage.go
81 lines (63 loc) · 1.63 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
// ClientMessage
package main
import (
"strings"
"time"
)
//this is suppose to be an event
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 newClientMessage2(isCombat bool, cmd string, line string) ClientMessage {
val := strings.TrimSpace(strings.TrimPrefix(line, cmd))
msg := ClientMessage{CombatAction: isCombat, Command: cmd, Value: val}
if msg.IsTradeCommand() && len(val) == 0 {
msg.setValue(cmd)
}
return msg
}
func (msg *ClientMessage) IsTradeCommand() bool {
switch msg.Command {
case "accept", "done", "add", "reject":
return true
}
return false
}
func (msg *ClientMessage) setValue(val string) {
msg.Value = val
}
func (msg *ClientMessage) setCommand(cmd string) {
msg.Command = cmd
}
func (msg *ClientMessage) setCommandAndValue(cmd string, val string) {
msg.Command = cmd
msg.Value = val
}
func (msg *ClientMessage) setCommandWithTimestamp(cmd string) {
msg.Command = cmd + ";" + time.Now().String()
}
func (msg *ClientMessage) setMsgWithTimestamp(cmd string, value string) {
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 (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]
}