上传屏蔽字

This commit is contained in:
liwei1dao 2022-11-11 14:27:20 +08:00
parent c9a072b28a
commit 4735e4f3ed
7 changed files with 67 additions and 14 deletions

4
bin/wordfilter.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -89,7 +89,7 @@ func (this *Battle) CreateEveBattle(session comm.IUserSession, req *pb.BattleEVE
return return
} }
if record, code = this.modelBattle.createeve(session, conn, pb.BattleType_pve, req); code != pb.ErrorCode_Success { if record, code = this.modelBattle.createeve(session, conn, pb.BattleType_eve, req); code != pb.ErrorCode_Success {
return return
} }
return return

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"go_dreamfactory/comm" "go_dreamfactory/comm"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"go_dreamfactory/sys/wordfilter"
"time" "time"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
@ -51,6 +52,9 @@ func (this *apiComp) Send(session comm.IUserSession, req *pb.ChatSendReq) (code
code = pb.ErrorCode_ConfigNoFound code = pb.ErrorCode_ConfigNoFound
return return
} }
if msg.Ctype == pb.ChatType_Text { //过滤敏感词
msg.Content = wordfilter.Replace(msg.Content, '*')
}
switch msg.Channel { switch msg.Channel {
case pb.ChatChannel_World: case pb.ChatChannel_World:
if this.module.options.GM { //判断gm命令 if this.module.options.GM { //判断gm命令

View File

@ -40,6 +40,7 @@ import (
"go_dreamfactory/modules/worldtask" "go_dreamfactory/modules/worldtask"
"go_dreamfactory/services" "go_dreamfactory/services"
"go_dreamfactory/sys/db" "go_dreamfactory/sys/db"
"go_dreamfactory/sys/wordfilter"
"time" "time"
"go_dreamfactory/lego" "go_dreamfactory/lego"
@ -120,6 +121,12 @@ type Service struct {
//初始化worker需要的一些系统工具 //初始化worker需要的一些系统工具
func (this *Service) InitSys() { func (this *Service) InitSys() {
this.ServiceBase.InitSys() this.ServiceBase.InitSys()
//初始化配置中心系统 每个服务都会用到的就在这个初始化就好
if err := wordfilter.OnInit(this.GetSettings().Sys["wordfilter"]); err != nil {
panic(fmt.Sprintf("init sys.wordfilter err: %s", err.Error()))
} else {
log.Infof("init sys.wordfilter success!")
}
//定时系统 //定时系统
if err := timewheel.OnInit(nil, timewheel.SetTick(time.Minute)); err != nil { if err := timewheel.OnInit(nil, timewheel.SetTick(time.Minute)); err != nil {
panic(fmt.Sprintf("init sys.timewheel err: %s", err.Error())) panic(fmt.Sprintf("init sys.timewheel err: %s", err.Error()))

View File

@ -34,6 +34,9 @@ type (
///更新去噪模式 ///更新去噪模式
UpdateNoisePattern(pattern string) UpdateNoisePattern(pattern string)
} }
WordJson struct {
Word string `json:"word"`
}
) )
var ( var (

View File

@ -1,18 +1,23 @@
package wordfilter package wordfilter
import ( import (
"errors"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
"go_dreamfactory/lego/utils/mapstructure" "go_dreamfactory/lego/utils/mapstructure"
) )
type Option func(*Options) type Option func(*Options)
type Options struct { type Options struct {
WorldFile string //词组文件
Debug bool //日志是否开启 Debug bool //日志是否开启
Log log.Ilogf Log log.Ilogf
} }
func SetWorldFile(v string) Option {
return func(o *Options) {
o.WorldFile = v
}
}
func SetDebug(v bool) Option { func SetDebug(v bool) Option {
return func(o *Options) { return func(o *Options) {
o.Debug = v o.Debug = v
@ -28,16 +33,12 @@ func SetLog(v log.Ilogf) Option {
func newOptions(config map[string]interface{}, opts ...Option) (options *Options, err error) { func newOptions(config map[string]interface{}, opts ...Option) (options *Options, err error) {
options = &Options{} options = &Options{}
if config != nil { if config != nil {
mapstructure.Decode(config, &options) mapstructure.Decode(config, options)
} }
for _, o := range opts { for _, o := range opts {
o(options) o(options)
} }
options.Log = log.NewTurnlog(options.Debug, log.Clone("sys.wordfilter", 3))
if options.Log = log.NewTurnlog(options.Debug, log.Clone("sys.Blockcache", 2)); options.Log == nil {
err = errors.New("log is nil")
}
return return
} }
@ -46,8 +47,6 @@ func newOptionsByOption(opts ...Option) (options *Options, err error) {
for _, o := range opts { for _, o := range opts {
o(options) o(options)
} }
if options.Log = log.NewTurnlog(options.Debug, log.Clone("sys.Blockcache", 2)); options.Log == nil { options.Log = log.NewTurnlog(options.Debug, log.Clone("sys.wordfilter", 3))
err = errors.New("log is nil")
}
return return
} }

View File

@ -2,11 +2,16 @@ package wordfilter
import ( import (
"bufio" "bufio"
"fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"regexp" "regexp"
"strings"
"time" "time"
jsoniter "github.com/json-iterator/go"
) )
func newSys(options *Options) (sys *Sys, err error) { func newSys(options *Options) (sys *Sys, err error) {
@ -15,6 +20,9 @@ func newSys(options *Options) (sys *Sys, err error) {
trie: NewTrie(), trie: NewTrie(),
noise: regexp.MustCompile(`[\|\s&%$@*]+`), noise: regexp.MustCompile(`[\|\s&%$@*]+`),
} }
if options.WorldFile != "" {
err = sys.Loadjson(options.WorldFile)
}
return return
} }
@ -70,6 +78,34 @@ func (Sys *Sys) Load(rd io.Reader) error {
return nil return nil
} }
func (Sys *Sys) Loadjson(fliepath string) (err error) {
var (
file *os.File
bytes []byte
word *WordJson
words []string
)
if file, err = os.Open(fliepath); err != nil {
err = fmt.Errorf("no found file:%s", fliepath)
return
}
defer file.Close()
if bytes, err = ioutil.ReadAll(file); err != nil {
err = fmt.Errorf("read file:%s err:%v", fliepath, err)
return
}
word = &WordJson{}
if err = jsoniter.Unmarshal(bytes, word); err != nil {
err = fmt.Errorf("read file:%s json.Unmarshal err:%v", fliepath, err)
return
}
words = strings.Split(word.Word, "|")
for _, v := range words {
Sys.trie.Add(v)
}
return
}
// AddWord 添加敏感词 // AddWord 添加敏感词
func (Sys *Sys) AddWord(words ...string) { func (Sys *Sys) AddWord(words ...string) {
Sys.trie.Add(words...) Sys.trie.Add(words...)