/
F.go
1843 lines (1635 loc) · 42.9 KB
/
F.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package reply
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net"
"net/http"
"net/http/pprof"
"net/url"
"sort"
"strconv"
"strings"
"sync"
"time"
"database/sql"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
psql "github.com/qydysky/part/sql"
"golang.org/x/text/encoding/simplifiedchinese"
_ "modernc.org/sqlite"
"github.com/dustin/go-humanize"
c "github.com/qydysky/bili_danmu/CV"
F "github.com/qydysky/bili_danmu/F"
_ "github.com/qydysky/bili_danmu/Reply/F"
"github.com/qydysky/bili_danmu/Reply/F/danmuXml"
videoInfo "github.com/qydysky/bili_danmu/Reply/F/videoInfo"
send "github.com/qydysky/bili_danmu/Send"
p "github.com/qydysky/part"
pctx "github.com/qydysky/part/ctx"
file "github.com/qydysky/part/file"
pio "github.com/qydysky/part/io"
limit "github.com/qydysky/part/limit"
msgq "github.com/qydysky/part/msgq"
slice "github.com/qydysky/part/slice"
psync "github.com/qydysky/part/sync"
pweb "github.com/qydysky/part/web"
websocket "github.com/qydysky/part/websocket"
encoder "golang.org/x/text/encoding"
)
/*
F额外功能区
*/
var flog = c.C.Log.Base(`功能`)
// 功能开关选取函数
func IsOn(s string) bool {
return c.C.IsOn(s)
}
// 字符重复度检查
// a在buf中出现的字符占a的百分数
func cross(a string, buf []string) float32 {
var s float32
var matched bool
for _, v1 := range a {
for _, v2 := range buf {
for _, v3 := range v2 {
if v3 == v1 {
matched = true
break
}
}
if matched {
break
}
}
if matched {
s += 1
}
matched = false
}
return s / float32(len([]rune(a)))
}
// 在a中仅出现一次出现的字符占a的百分数
// func selfcross(a string) float32 {
// buf := make(map[rune]bool)
// for _, v := range a {
// if _, ok := buf[v]; !ok {
// buf[v] = true
// }
// }
// return 1 - float32(len(buf))/float32(len([]rune(a)))
// }
// 在a的每个字符串中
// 出现的字符次数最多的
// 占出现的字符总数的百分数
// *单字符串中的重复出现计为1次
func selfcross2(a []string) (float32, string) {
buf := make(map[rune]float32)
for _, v := range a {
block := make(map[rune]bool)
for _, v1 := range v {
if _, ok := block[v1]; ok {
continue
}
block[v1] = true
buf[v1] += 1
}
}
var (
max float32
maxS string
all float32
)
for k, v := range buf {
all += v
if v > max {
max = v
maxS = string(k)
}
}
return max / all, maxS
}
// 功能区
// 显示营收
func init() {
if !IsOn("统计营收") {
return
}
go func() {
var ShowRev = make(map[int]float64)
clog := c.C.Log.Base_add(`营收`)
for {
if _, ok := ShowRev[c.C.Roomid]; !ok && c.C.Roomid != 0 {
ShowRev[c.C.Roomid] = 0
}
for room, rev := range ShowRev {
if c.C.Roomid != room {
clog.L(`I: `, fmt.Sprintf("%d ¥%.2f", room, c.C.Rev))
delete(ShowRev, room)
} else if c.C.Rev != rev {
ShowRev[room] = c.C.Rev
clog.L(`I: `, fmt.Sprintf("%d ¥%.2f", room, c.C.Rev))
}
}
time.Sleep(time.Minute)
}
}()
}
// Ass 弹幕转字幕
type Ass struct {
file string //弹幕ass文件名
startT time.Time //开始记录的基准时间
header string //ass开头
wrap encoder.Encoding //编码
}
var (
Ass_height = 720 //字幕高度
Ass_width = 1280 //字幕宽度
Ass_font = 50 //字幕字体大小
Ass_T = 7 //单条字幕显示时间
Ass_loc = 7 //字幕位置 小键盘对应的位置
)
var ass = Ass{
header: `[Script Info]
Title: Default Ass file
ScriptType: v4.00+
WrapStyle: 0
ScaledBorderAndShadow: yes
PlayResX: ` + strconv.Itoa(Ass_height) + `
PlayResY: ` + strconv.Itoa(Ass_width) + `
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,,` + strconv.Itoa(Ass_font) + `,&H40FFFFFF,&H000017FF,&H80000000,&H40000000,0,0,0,0,100,100,0,0,1,4,4,` + strconv.Itoa(Ass_loc) + `,20,20,50,1
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
`,
wrap: simplifiedchinese.GB18030,
}
func init() {
accept := map[string]bool{
`GB18030`: true,
`utf-8`: true,
}
if v, ok := c.C.K_v.LoadV("Ass编码").(string); ok {
if v1, ok := accept[v]; ok && v1 {
c.C.Log.Base(`Ass`).L(`T: `, "编码:", v)
if v == `utf-8` {
ass.wrap = nil
}
}
}
}
// 设定字幕文件名,为""时停止输出
func Ass_f(ctx context.Context, save_path string, filePath string, st time.Time) {
if !IsOn(`仅保存当前直播间流`) {
return
}
ass.file = filePath
if filePath == "" {
return
}
fl := flog.Base_add(`Ass`)
fl.L(`I: `, `开始`)
f := &file.File{
Config: file.Config{
FilePath: ass.file + ".ass",
AutoClose: true,
Coder: ass.wrap,
},
}
_, _ = f.Write([]byte(ass.header), true)
ass.startT = st
ctx, done := pctx.WaitCtx(ctx)
defer done()
<-ctx.Done()
ass.file = ""
fl.L(`I: `, "结束")
}
// 传入要显示的单条字幕
func Assf(s string) {
if !IsOn("生成Ass弹幕") {
return
}
if ass.file == "" {
return
}
if s == "" {
return
}
st := time.Since(ass.startT) + time.Duration(p.Rand().MixRandom(0, 2000))*time.Millisecond
et := st + time.Duration(Ass_T)*time.Second
var b string
// b += "Comment: " + strconv.Itoa(loc) + " "+ Dtos(showedt) + "\n"
b += `Dialogue: 0,`
b += dtos(st) + `,` + dtos(et)
b += `,Default,,0,0,0,,{\fad(200,500)\blur3}` + s + "\n"
f := file.New(ass.file+".ass", -1, true)
f.Config.Coder = ass.wrap
if _, e := f.Write([]byte(b), true); e != nil {
flog.Base(`Assf`).L(`E: `, e)
}
}
// 时间转化为0:00:00.00规格字符串
func dtos(t time.Duration) string {
M := int(math.Floor(t.Minutes())) % 60
S := int(math.Floor(t.Seconds())) % 60
Ns := t.Nanoseconds() / int64(time.Millisecond) % 1000 / 10
return fmt.Sprintf("%d:%02d:%02d.%02d", int(math.Floor(t.Hours())), M, S, Ns)
}
// 获取实例的Common
func StreamOCommon(roomid int) (array []*c.Common) {
if roomid != -1 { //返回特定房间
if v, ok := c.StreamO.Load(roomid); ok {
return []*c.Common{v.(*M4SStream).Common()}
}
} else { //返回所有
c.StreamO.Range(func(_, v interface{}) bool {
array = append(array, v.(*M4SStream).Common())
return true
})
}
return
}
// 获取实例的录制状态
func StreamOStatus(roomid int) (Islive bool) {
v, ok := c.StreamO.Load(roomid)
return ok && (!pctx.Done(v.(*M4SStream).Status) || v.(*M4SStream).exitSign.Islive())
}
// 开始实例
func StreamOStart(roomid int) {
if StreamOStatus(roomid) {
flog.L(`W: `, `已录制 `+strconv.Itoa(roomid)+` 不能重复录制`)
return
}
var tmp = new(M4SStream)
if e := tmp.LoadConfig(c.C.Copy()); e != nil {
flog.L(`E: `, e)
return
}
tmp.common.Roomid = roomid
//实例回调,避免重复录制
tmp.Callback_start = func(ms *M4SStream) error {
//流服务添加
if _, ok := c.StreamO.LoadOrStore(ms.common.Roomid, tmp); ok {
return fmt.Errorf("已存在此直播间(%d)录制", ms.common.Roomid)
}
return nil
}
tmp.Callback_stop = func(ms *M4SStream) {
c.StreamO.Delete(ms.common.Roomid) //流服务去除
}
tmp.Start()
}
// 停止实例
//
// -2 其他房间
// -1 所有房间
// 针对某房间
func StreamOStop(roomid int) {
switch roomid {
case -2: // 其他房间
c.StreamO.Range(func(_roomid, v interface{}) bool {
if c.C.Roomid == _roomid {
return true
}
if !pctx.Done(v.(*M4SStream).Status) {
v.(*M4SStream).Stop()
}
c.StreamO.Delete(_roomid)
return true
})
case -1: // 所有房间
c.StreamO.Range(func(k, v interface{}) bool {
if !pctx.Done(v.(*M4SStream).Status) {
v.(*M4SStream).Stop()
}
c.StreamO.Delete(k)
return true
})
default: // 针对某房间
if v, ok := c.StreamO.Load(roomid); ok {
if !pctx.Done(v.(*M4SStream).Status) {
v.(*M4SStream).Stop()
}
c.StreamO.Delete(roomid)
}
}
}
// 实例切断
func StreamOCut(roomid int) (setTitle func(string)) {
if v, ok := c.StreamO.Load(roomid); ok {
if !pctx.Done(v.(*M4SStream).Status) {
v.(*M4SStream).Cut()
flog.L(`I: `, `已切片 `+strconv.Itoa(roomid))
return func(title string) {
if title != "" {
v.(*M4SStream).Common().Title = title
}
}
}
}
return func(s string) {}
}
// type Obs struct {
// c obsws.Client
// Prog string //程序路径
// }
// var obs = Obs{
// c: obsws.Client{Host: "127.0.0.1", Port: 4444},
// Prog: "obs",
// }
// func Obsf(on bool) {
// if !IsOn("调用obs") {
// return
// }
// l := c.C.Log.Base(`obs`)
// if on {
// if sys.Sys().CheckProgram("obs")[0] != 0 {
// l.L(`W: `, "obs已经启动")
// return
// }
// if sys.Sys().CheckProgram("obs")[0] == 0 {
// if obs.Prog == "" {
// l.L(`E: `, "未知的obs程序位置")
// return
// }
// l.L(`I: `, "启动obs")
// p.Exec().Start(exec.Command(obs.Prog))
// sys.Sys().Timeoutf(3)
// }
// // Connect a client.
// if err := obs.c.Connect(); err != nil {
// l.L(`E: `, err)
// return
// }
// } else {
// if sys.Sys().CheckProgram("obs")[0] == 0 {
// l.L(`W: `, "obs未启动")
// return
// }
// obs.c.Disconnect()
// }
// }
// func Obs_R(on bool) {
// if !IsOn("调用obs") {
// return
// }
// l := c.C.Log.Base("obs_R")
// if sys.Sys().CheckProgram("obs")[0] == 0 {
// l.L(`W: `, "obs未启动")
// return
// } else {
// if err := obs.c.Connect(); err != nil {
// l.L(`E: `, err)
// return
// }
// }
// //录
// if on {
// req := obsws.NewStartRecordingRequest()
// if err := req.Send(obs.c); err != nil {
// l.L(`E: `, err)
// return
// }
// resp, err := req.Receive()
// if err != nil {
// l.L(`E: `, err)
// return
// }
// if resp.Status() == "ok" {
// l.L(`I: `, "开始录制")
// }
// } else {
// req := obsws.NewStopRecordingRequest()
// if err := req.Send(obs.c); err != nil {
// l.L(`E: `, err)
// return
// }
// resp, err := req.Receive()
// if err != nil {
// l.L(`E: `, err)
// return
// }
// if resp.Status() == "ok" {
// l.L(`I: `, "停止录制")
// }
// sys.Sys().Timeoutf(3)
// }
// }
// type Autoban struct {
// Banbuf []string
// buf []string
// }
// var autoban = Autoban{}
// func Autobanf(s string) bool {
// if !IsOn("Autoban") {
// return false
// }
// l := c.C.Log.Base("autoban")
// if len(autoban.Banbuf) == 0 {
// f := file.New("Autoban.txt", -1, false)
// for {
// if data, e := f.ReadUntil('\n', 50, 5000); e != nil {
// if !errors.Is(e, io.EOF) {
// l.L(`E: `, e)
// }
// break
// } else {
// autoban.Banbuf = append(autoban.Banbuf, string(data))
// }
// }
// }
// if len(autoban.buf) < 10 {
// autoban.buf = append(autoban.buf, s)
// return false
// }
// defer func() {
// autoban.buf = append(autoban.buf[1:], s)
// }()
// var res []float32
// {
// pt := float32(len([]rune(s)))
// if pt <= 5 {
// return false
// } //字数过少去除
// res = append(res, pt)
// }
// {
// pt := selfcross(s)
// // if pt > 0.5 {return false}//自身重复高去除
// // res = append(res, pt)
// pt1 := cross(s, autoban.buf)
// if pt+pt1 > 0.3 {
// return false
// } //历史重复高去除
// res = append(res, pt, pt1)
// }
// {
// pt := cross(s, autoban.Banbuf)
// if pt < 0.8 {
// return false
// } //ban字符重复低去除
// res = append(res, pt)
// }
// l.L(`W: `, res)
// return true
// }
type Danmuji struct {
Buf map[string]string
Inuse_auto bool
reflect_limit *limit.Limit
mute bool
}
var danmuji = Danmuji{
Inuse_auto: IsOn("自动弹幕机"),
Buf: map[string]string{
"弹幕机在么": "在",
},
reflect_limit: limit.New(1, "4s", "8s"),
}
func init() { //初始化反射型弹幕机
f := file.New("config/config_auto_reply.json", 0, true)
if !f.IsExist() {
return
}
bb, err := f.ReadAll(100, 1<<16)
if !errors.Is(err, io.EOF) {
return
}
var buf map[string]interface{}
_ = json.Unmarshal(bb, &buf)
for k, v := range buf {
if k == v {
continue
}
danmuji.Buf[k] = v.(string)
}
}
func Danmujif(s string) {
if !IsOn("反射弹幕机") {
return
}
if danmuji.reflect_limit.TO() {
return
}
for k, v := range danmuji.Buf {
if strings.Contains(s, k) {
Msg_senddanmu(v)
break
}
}
}
func Danmuji_auto() {
if !IsOn("反射弹幕机") || !IsOn("自动弹幕机") || danmuji.mute {
return
}
danmuji.mute = true
var (
list []string
timeout int
)
for _, v := range c.C.K_v.LoadV(`自动弹幕机_内容`).([]interface{}) {
list = append(list, v.(string))
}
timeout = int(c.C.K_v.LoadV(`自动弹幕机_发送间隔s`).(float64))
if timeout < 5 {
timeout = 5
}
go func() {
for i := 0; true; i++ {
if i >= len(list) {
i = 0
}
if msg := list[i]; msg != `` {
Msg_senddanmu(msg)
}
time.Sleep(time.Duration(timeout) * time.Second)
}
}()
}
type Autoskip struct {
roomid int
buf map[string]Autoskip_item
sync.Mutex
now uint
ticker *time.Ticker
}
type Autoskip_item struct {
Exprie uint
Num uint
}
var autoskip = Autoskip{
buf: make(map[string]Autoskip_item),
ticker: time.NewTicker(time.Duration(2) * time.Second),
}
func init() {
go func() {
for {
<-autoskip.ticker.C
autoskip.Lock()
if len(autoskip.buf) == 0 {
autoskip.Unlock()
continue
}
autoskip.now += 1
if autoskip.roomid != c.C.Roomid {
autoskip.buf = make(map[string]Autoskip_item)
autoskip.roomid = c.C.Roomid
flog.Base_add(`弹幕合并`).L(`T: `, `房间更新:`, autoskip.roomid)
autoskip.Unlock()
continue
}
for k, v := range autoskip.buf {
if v.Exprie <= autoskip.now {
delete(autoskip.buf, k)
{ //超时显示
if v.Num > 3 {
c.C.Danmu_Main_mq.Push_tag(`tts`, Danmu_mq_t{ //传入消息队列
uid: `0multi`,
m: map[string]string{
`{num}`: strconv.Itoa(int(v.Num)),
`{msg}`: k,
},
})
Msg_showdanmu(Danmu_item{
msg: strconv.Itoa(int(v.Num)) + " x " + k,
uid: `0multi`,
roomid: autoskip.roomid,
})
} else if v.Num > 1 {
Msg_showdanmu(Danmu_item{
msg: strconv.Itoa(int(v.Num)) + " x " + k,
uid: `0default`,
roomid: autoskip.roomid,
})
}
}
}
}
{ //copy map
tmp := make(map[string]Autoskip_item)
for k, v := range autoskip.buf {
tmp[k] = v
}
autoskip.buf = tmp
}
autoskip.Unlock()
}
}()
}
func Autoskipf(s string) uint {
if !IsOn("弹幕合并") || s == "" {
return 0
}
autoskip.Lock()
defer autoskip.Unlock()
if autoskip.roomid != c.C.Roomid {
autoskip.buf = make(map[string]Autoskip_item)
autoskip.roomid = c.C.Roomid
flog.Base_add(`弹幕合并`).L(`T: `, `房间更新:`, autoskip.roomid)
return 0
}
{ //验证是否已经存在
if v, ok := autoskip.buf[s]; ok && autoskip.now < v.Exprie {
autoskip.buf[s] = Autoskip_item{
Exprie: v.Exprie,
Num: v.Num + 1,
}
return v.Num
}
}
{ //设置
autoskip.buf[s] = Autoskip_item{
Exprie: autoskip.now + 8,
Num: 1,
}
}
return 0
}
type Lessdanmu struct {
roomid int
buf []string
limit *limit.Limit
max_num int
threshold float32
}
var lessdanmu = Lessdanmu{
threshold: 0.7,
}
func init() {
if max_num, ok := c.C.K_v.LoadV(`每秒显示弹幕数`).(float64); ok && int(max_num) >= 1 {
flog.Base_add(`更少弹幕`).L(`T: `, `每秒弹幕数:`, int(max_num))
lessdanmu.max_num = int(max_num)
lessdanmu.limit = limit.New(int(max_num), "1s", "0s") //timeout right now
}
}
func Lessdanmuf(s string) (show bool) {
if !IsOn("相似弹幕忽略") {
return true
}
if lessdanmu.roomid != c.C.Roomid {
lessdanmu.buf = nil
lessdanmu.roomid = c.C.Roomid
lessdanmu.threshold = 0.7
flog.Base_add(`更少弹幕`).L(`T: `, `房间更新:`, lessdanmu.roomid)
return true
}
if len(lessdanmu.buf) < 20 {
lessdanmu.buf = append(lessdanmu.buf, s)
return true
}
o := cross(s, lessdanmu.buf)
if o == 1 {
return false
} //完全无用
Jiezouf(lessdanmu.buf)
slice.DelFront(&lessdanmu.buf, 1)
show = o < lessdanmu.threshold
if show && lessdanmu.max_num > 0 {
return !lessdanmu.limit.TO()
}
return
}
/*
Moredanmu
目标:弹幕机自动发送弹幕
原理:留存弹幕,称为buf。将当前若干弹幕在buf中的位置找出,根据位置聚集情况及该位置出现语句的频率,选择发送的弹幕
*/
// type Moredanmu struct {
// buf []string
// }
// var moredanmu = Moredanmu{
// }
// func moredanmuf(s string) {
// if !moredanmu.Inuse {return}
// // if len(moredanmu.buf) < bufsize {
// moredanmu.buf = append(moredanmu.buf, s)
// // }
// // b := p.Buf("danmu.buf").Load()
// // if b.Get() != nil {
// // moredanmu.buf = *b.Get()
// // }
// }
// func moredanmu_get(tb []string) {
// if !moredanmu.Inuse {return}
// var tmp string
// for _,v := range tb {
// tmp += v
// }
// // for _,v := range tb {
// // tmp += len([]rune(v[:len(v)-1]))
// // }
// var max float32
// var loc int
// for i := 0; len(moredanmu.buf) >= i + len(tb); i++ {
// if m := cross(tmp, moredanmu.buf[i:i + len(tb)]);m > max {
// max = m
// loc = i
// }
// }
// if loc != 0 {
// p := moredanmu.buf[loc:loc + len(tb)]
// for i,v := range p{
// if m := cross(v, p);m > max {
// max = m
// loc = i
// }
// }
// fmt.Println(len(moredanmu.buf),"=>",p[loc])
// }
// }
type Shortdanmu struct {
lastdanmu []rune
l sync.Mutex
}
var shortdanmu = Shortdanmu{}
func Shortdanmuf(s string) string {
if !IsOn("精简弹幕") {
return s
}
shortdanmu.l.Lock()
defer shortdanmu.l.Unlock()
if len(shortdanmu.lastdanmu) == 0 {
shortdanmu.lastdanmu = []rune(s)
return s
}
var new string
for k, v := range []rune(s) {
if k >= len(shortdanmu.lastdanmu) {
new += string([]rune(s)[k:])
break
}
if v != shortdanmu.lastdanmu[k] {
switch k {
case 0, 1, 2:
new = s
default:
new = "..." + string([]rune(s)[k-1:])
}
break
}
}
// if new == "" {new = "...."}
shortdanmu.lastdanmu = []rune(s)
return new
}
type Jiezou struct {
alertdanmu string
skipS map[string]interface{}
avg float32
turn int
sync.Mutex
}
var jiezou = Jiezou{
alertdanmu: "",
skipS: map[string]interface{}{ //常见语气词忽略
"了": nil,
"的": nil,
"哈": nil,
"是": nil,
",": nil,
"这": nil,
},
}
func Jiezouf(s []string) bool {
if !IsOn("Jiezou") {
return false
}
now, S := selfcross2(s)
jiezou.avg = (8*jiezou.avg + 2*now) / 10
if jiezou.turn < len(s) {
jiezou.turn += 1
return false
}
if _, ok := jiezou.skipS[S]; ok {
return false
}
jiezou.Lock()
if now > 1.3*jiezou.avg { //触发
c.C.Log.Base("jiezou").L(`W: `, "节奏注意", now, jiezou.avg, S)
jiezou.avg = now //沉默
jiezou.Unlock()
//发送弹幕
if jiezou.alertdanmu != "" {
Msg_senddanmu(jiezou.alertdanmu)
}
return true
}
jiezou.Unlock()
return false
}
// 保存所有消息到json
type saveToJson struct {
msg *msgq.MsgType[[]byte]
once sync.Once
}
func (t *saveToJson) Init() {
t.once.Do(func() {
if path, ok := c.C.K_v.LoadV(`save_to_json`).(string); ok && path != `` {
f := file.New(path, 0, false)
_ = f.Delete()
_, _ = f.Write([]byte("["), true)
f.Close()
t.msg = msgq.NewType[[]byte]()
t.msg.Pull_tag(map[string]func([]byte) (disable bool){
`data`: func(b []byte) (disable bool) {
f := file.New(path, -1, false)
_, _ = f.Write(b, true)
_, _ = f.Write([]byte(","), true)
f.Close()
return false
},
`stop`: func(_ []byte) (disable bool) {
f := file.New(path, -1, false)
_ = f.SeekIndex(-1, file.AtEnd)
_, _ = f.Write([]byte("]"), true)
f.Close()
return true
},
})
}
})
}
func (t *saveToJson) Write(data []byte) {
if t.msg != nil {
t.msg.PushLock_tag(`data`, data)
}
}
func (t *saveToJson) Close() {
if t.msg != nil {
t.msg.PushLock_tag(`stop`, nil)
}
}
var SaveToJson saveToJson
// 进入房间发送弹幕
func Entry_danmu() {
flog := flog.Base_add(`进房弹幕`)
//检查与切换粉丝牌,只在cookie存在时启用
F.Get(c.C).Get(`CheckSwitch_FansMedal`)
if v, _ := c.C.K_v.LoadV(`进房弹幕_有粉丝牌时才发`).(bool); v && c.C.Wearing_FansMedal == 0 {
flog.L(`T: `, `无粉丝牌`)
return
}
if v, _ := c.C.K_v.LoadV(`进房弹幕_仅发首日弹幕`).(bool); v {
res := F.Get_weared_medal()
if res.TodayIntimacy > 0 {
flog.L(`T: `, `今日已发弹幕`)
return
}
}
if array, ok := c.C.K_v.LoadV(`进房弹幕_内容`).([]interface{}); ok && len(array) != 0 {
rand := p.Rand().MixRandom(0, int64(len(array)-1))
send.Danmu_s(array[rand].(string), c.C.Roomid)
}
}
// 保持所有牌子点亮
func Keep_medal_light() {
if v, _ := c.C.K_v.LoadV(`保持牌子亮着`).(bool); !v {
return
}