-
Notifications
You must be signed in to change notification settings - Fork 37
/
msg_func.go
75 lines (64 loc) · 1.76 KB
/
msg_func.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
package main
import (
"strings"
"github.com/fatih/camelcase"
dota_gcmessages_msgid "github.com/paralin/go-dota2/protocol"
)
// GetMessageFuncName determines what function name we should assign a message.
func GetMessageFuncName(msg dota_gcmessages_msgid.EDOTAGCMsg) string {
if str, ok := msgMethodNameOverrides[msg]; ok {
return str
}
msgName := msg.String()
msgName = strings.TrimPrefix(msgName, "k_EMsg")
msgName = strings.TrimPrefix(msgName, "DOTA")
switch {
case strings.HasPrefix(msgName, "ClientToGC"):
msgName = strings.TrimPrefix(msgName, "ClientToGC")
case strings.HasPrefix(msgName, "GC"):
msgName = strings.TrimPrefix(msgName, "GC")
}
switch {
case strings.HasSuffix(msgName, "Request"):
msgName = "Request" + strings.TrimSuffix(msgName, "Request")
case strings.HasSuffix(msgName, "Query"):
msgName = "Query" + strings.TrimSuffix(msgName, "Query")
}
msgName = strings.Replace(msgName, "PracticeLobby", "Lobby", -1)
words := camelcase.Split(msgName)
switch {
case len(words) < 2:
return msgName
case len(words) == 2:
if !IsWordVerb(words[0]) && IsWordVerb(words[1]) {
words[0], words[1] = words[1], words[0]
}
}
switch {
case words[0] == "List" && !strings.HasSuffix(words[len(words)-1], "s"):
if words[1] == "Lobby" {
words[1] = "Lobbies"
} else {
words[1] += "s"
}
case words[0] == "Request" && IsWordVerb(words[len(words)-1]):
words[0] = words[len(words)-1]
words = words[:len(words)-1]
}
anyVerbs := false
var verbi int
for i, w := range words {
verbi = i
if IsWordVerb(w) {
anyVerbs = true
break
}
}
if !anyVerbs {
words = append([]string{"Send"}, words...)
} else if !IsWordVerb(words[0]) {
words[0], words[verbi] = words[verbi], words[0]
}
msgName = strings.Join(words, "")
return msgName
}