You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.2 KiB
Go

package cmd
import (
"database/sql"
"fmt"
"github.com/shopspring/decimal"
"strconv"
"strings"
"testing"
)
var p = NewCMDParser(Pattern{
Prefix: "j",
Alias: []string{"加入", "加入游戏", "加入蓝方", "加入红方"},
ContentMaxLen: 0,
}, Pattern{
Prefix: "c",
Alias: []string{"切换"},
ContentMaxLen: 1,
}, Pattern{
Prefix: "w",
Alias: []string{"我在哪"},
ContentMaxLen: 0,
}, Pattern{
Prefix: "s",
ContentMaxLen: 4,
Distinct: sql.NullBool{Bool: false, Valid: true},
}, Pattern{
Prefix: "m",
ContentMaxLen: 1,
}, Pattern{
Prefix: "r",
ContentMaxLen: 1,
}, Pattern{
Prefix: "dq",
Alias: []string{"购买粮草", "买粮草"},
ContentMaxLen: 4,
}, Pattern{
Prefix: "dw",
Alias: []string{"购买精英"},
ContentMaxLen: 1,
}, Pattern{
Prefix: "zw",
Alias: []string{"使用精英"},
ContentMaxLen: 1,
}, Pattern{
Prefix: "zz",
Alias: []string{"使用称号"},
ContentMaxLen: 1,
})
func TestParse(t *testing.T) {
//contents := []string{
// "jc2m2b1s",
// "jjjjjjjjjjjj",
// "c1c1c1c1c1c1c1c1c1c1",
// "c5c6c7c8c9c2c3c4c1c2c3c4c5c6c2c3c4c5c6c1",
// "j2jjjjjjjj",
// "j",
// "加入游戏",
// "加入",
// "没有什么意义的弹幕",
// "92813182798dsjks8923kjhsddfh892h3jkl214",
// "昵称为什么可以这么长",
// "六十九的覅哦我就法拉盛就发链接我i苏联空军弗拉放假 ",
// "一堆乱七八糟的弹幕来袭",
// "不服你咬我啊?",
// "红方前排速度m2b2",
// "c1c2c3c4c1c2c3c4",
// "m2",
// "b2",
// "c2",
//}
//p := NewCMDParser(true, "j", "c1", "c2", "c3", "c4", "b1", "b2", "b3", "s", "b2", "b3", "w", "m1", "m2", "m3", "加入", "加入游戏")
//
//for _, content := range contents {
// cmdStruct := p.Parse(content)
// fmt.Printf("弹幕: [%s] 是命令否? [%v]\n", content, cmdStruct.IsCMD)
// fmt.Printf("清洗后的真实命令为:%s len:%d\n\n", cmdStruct.Arr, len(cmdStruct.Arr))
//}
}
func TestA(t *testing.T) {
p.SetDistinct(true)
//cmd := p.Parse("jc2j加入c2jc2加入游戏dq4654购买粮草9999购买精英1m1r2zz1zw2wss1s3s5")
//cmd := p.Parse("输入j 加入游戏哦")
cmd := p.Parse("s1#10s2s3")
fmt.Println(cmd.IsCMD)
for _, match := range cmd.Matches {
fmt.Printf("prefix: %s, content: %s\n", match.Prefix, string(match.Content))
}
}
func TestOutbreak(t *testing.T) {
content := []rune("111,")
contentStr := string(content)
var unitTypeStr string
var count int64
var err error
idxSplit := strings.IndexRune(contentStr, ',')
if idxSplit == -1 {
// 没有分隔符,判断内容位数?
unitTypeStr = string(content)
count = 3
} else {
unitTypeStr = string(content[:idxSplit])
if idxSplit == len(content)-1 {
count = 3
} else {
count, err = strconv.ParseInt(string(content[idxSplit+1:]), 10, 0)
if err != nil {
return
}
}
if count > 30 {
count = 30
}
if count <= 0 {
count = 3
}
}
cost := decimal.NewFromInt(count).Mul(decimal.NewFromFloat(1.3)).Round(0).IntPart()
fmt.Println(unitTypeStr, count, cost)
}
func BenchmarkCmd(b *testing.B) {
content := "jc2"
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
go func() {
p.Parse(content)
}()
}
}