index.ts
25.8 KB
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
//处理纳米数据,共用调用
import * as info_db from '../info_mysql/table';
import * as nami_db from '../nami_mysql/table';
import * as moment from 'moment';
import * as _ from 'lodash';
import { sleep } from '../../libs/common';
async function best_player(team_id: number, date = moment().format('YYYY-MM-DD')) {
var data = await nami_db.sequelize.query(
`SELECT id,player_stats,home_team_id,away_team_id from db_nami.matches where
match_time<UNIX_TIMESTAMP('${moment(date).format('YYYY-MM-DD')}}') and
(home_team_id=${team_id} or away_team_id=${team_id}) and
JSON_LENGTH(player_stats)>0
ORDER BY match_time desc LIMIT 10;`
)
.then(result => {
if (result && result.length > 1 && result[1]) {
return result[1];
}
return null;
})
.catch(err => {
console.log(`best_player ${team_id} >>> ${err.message}`);
return null;
});
if (!data || data.length < 1) {
return [];
}
for (let index = 0; index < data.length; index++) {
try {
const item = data[index];
var v = _(item.player_stats).filter(a => a.team_id == team_id).orderBy('rating', 'desc').take(3).map(a => ({ player_id: a.player_id, rating: a.rating })).value();
if (v[0].rating < 1) {
continue;
}
//select short_name_zh,logo from players where id in (1332014)
var res = await nami_db.players.findAll({
attributes: ['id', 'short_name_zh', 'logo'],
where: {
id: {
$in: v.map(a => a.player_id)
}
}
});
v.forEach((a: any) => {
var v1: any = _(res).find((b: any) => b.id == a.player_id);
if (v1) {
a.short_name_zh = v1.short_name_zh;
a.logo = v1.logo;
}
})
return v;
} catch (error) {
}
return [];
}
return [];
}
async function history_team(team_id: number, is_host = false, date = moment().format('YYYY-MM-DD')) {
var data = await info_db.sequelize.query(
`select match_time,yiqiu_host_team_id,yiqiu_guest_team_id,score from ms_soccer_game where
match_time<'${moment(date).format('YYYY-MM-DD')}' and (yiqiu_host_team_id=${team_id} or yiqiu_guest_team_id =${team_id} )
ORDER BY match_time desc LIMIT 20`
)
.then(result => {
if (result && result.length > 1 && result[1]) {
return result[1];
}
return null;
})
.catch(err => {
console.log(`history_team ${team_id} >>> ${err.message}`);
return null;
});
if (!data || data.length < 1) {
return { data: [], avg_jq: 0 };
}
var sum_jq_time = 0;
var time = 0;
var sum_jq = 0;
var host_jq = 0;
var spf: any = [0, 0, 0];
var res = data.map(function (item: any) {
var r = 0;
var is_host_team = item.yiqiu_host_team_id == team_id;
try {
var bf = JSON.parse(item.score);
bf = bf.map((a: any) => Number(a));
if (time < 4) {
if (is_host) {
if (item.yiqiu_host_team_id == team_id) {
time++;
sum_jq_time += bf[0];
}
} else {
if (item.yiqiu_guest_team_id == team_id) {
time++;
sum_jq_time += bf[1];
}
}
}
sum_jq += (bf[0] + bf[1]);
host_jq += (item.yiqiu_host_team_id == team_id ? bf[0] : bf[1]);
if (bf[0] == bf[1]) {
r = 1;
} else if (item.yiqiu_host_team_id == team_id) {
r = bf[0] > bf[1] ? 2 : 0;
} else {
r = bf[0] > bf[1] ? 0 : 2;
}
if (is_host_team == is_host) {
spf[r]++;
}
return r;
} catch (error) {
return 0;
}
}).reverse();
return { data: res, avg_jq: time > 0 ? 1.0 * sum_jq_time / time : 0, sum_jq, host_jq, spf };
}
var trait_action: any = {
s: (bf: number[], is_host: boolean) => {
if (is_host) {
return bf[0] > bf[1]
} else {
return bf[0] < bf[1]
}
},
p: (bf: number[], is_host: boolean) => {
return bf[0] == bf[1]
},
f: (bf: number[], is_host: boolean) => {
if (is_host) {
return bf[0] < bf[1]
} else {
return bf[0] > bf[1]
}
},
no_f: (bf: number[], is_host: boolean) => {
if (is_host) {
return bf[0] >= bf[1]
} else {
return bf[0] <= bf[1]
}
},
no_p: (bf: number[], is_host: boolean) => {
return bf[0] != bf[1];
},
jq: (bf: number[], is_host: boolean) => {
if (is_host) {
return bf[0] > 0
} else {
return bf[1] > 0
}
},
no_jq: (bf: number[], is_host: boolean) => {
if (is_host) {
return bf[0] == 0
} else {
return bf[1] == 0
}
},
}
function trait_calc(list: any[], team_id: number) {
var max: any = { s: 0, p: 0, f: 0, no_f: 0, no_p: 0, jq: 0, no_jq: 0 }
var now: any = { s: 0, p: 0, f: 0, no_f: 0, no_p: 0, jq: 0, no_jq: 0 }
for (let index = list.length - 1; index >= 0; index--) {
const item = list[index];
var is_host = item.yiqiu_host_team_id == team_id;
var bf;
try {
bf = JSON.parse(item.score).map((a: any) => Number(a));
} catch (error) {
continue;
}
for (const key in now) {
if (trait_action[key].call(null, bf, is_host)) {
now[key]++;
} else {
now[key] = 0;
}
if (now[key] > max[key]) {
max[key] = now[key]
}
}
}
return { now, max }
}
async function trait_team(team_id: number, date = moment().format('YYYY-MM-DD')) {
var data = await info_db.sequelize.query(
`select yiqiu_host_team_id,yiqiu_guest_team_id,score from ms_soccer_game where
match_time<'${moment(date).format('YYYY-MM-DD')}' and status=4 and score is not null and (yiqiu_host_team_id=${team_id} or yiqiu_guest_team_id =${team_id} )
ORDER BY match_time desc LIMIT 1000`
)
.then(result => {
if (result && result.length > 1 && result[1]) {
return result[1];
}
return null;
})
.catch(err => {
console.log(`trait_team ${team_id} >>> ${err.message}`);
return null;
});
if (!data || data.length < 1) {
return null;
}
var t0 = trait_calc(data, team_id);
var t1 = trait_calc(data.filter((a: any) => a.yiqiu_host_team_id == team_id), team_id);
var t2 = trait_calc(data.filter((a: any) => a.yiqiu_guest_team_id == team_id), team_id);
return { t0, t1, t2 };
}
async function meeting(teamids: any[], date = moment().format('YYYY-MM-DD')) {
var data = await info_db.sequelize.query(
`select match_time,yiqiu_host_team_id,yiqiu_guest_team_id,score from ms_soccer_game where
match_time<'${moment(date).format('YYYY-MM-DD')}' and yiqiu_host_team_id in(${teamids.join(',')}) and yiqiu_guest_team_id in(${teamids.join(',')})
ORDER BY match_time desc LIMIT 20`
)
.then(result => {
if (result && result.length > 1 && result[1]) {
return result[1];
}
return null;
})
.catch(err => {
console.log(`meeting ${teamids.join(',')} >>> ${err.message}`);
return null;
});
if (!data || data.length < 1) {
return {};
}
var sum_jq = 0;
var host_jq = 0;
var res = data.map(function (item: any) {
try {
var bf = typeof item.score == 'string' ? JSON.parse(item.score) : item.score;
bf = bf.map((a: any) => Number(a));
sum_jq += (bf[0] + bf[1]);
host_jq += (item.yiqiu_host_team_id == teamids[0] ? bf[0] : bf[1]);
if (bf[0] == bf[1]) {
return 1;
}
if (item.yiqiu_host_team_id == teamids[0]) {
return bf[0] > bf[1] ? 2 : 0;
}
return bf[0] > bf[1] ? 0 : 2;
} catch (error) {
}
return 0;
}).reverse();
return {
sum_jq: sum_jq,
host_jq: host_jq,
z_spf: res
}
}
async function calc_odds(matchid: any) {
var data = await nami_db.sequelize.query(
`
select
avg( CAST(JSON_UNQUOTE(JSON_EXTRACT(latest_immediate_odds, '$[0]')) AS DECIMAL(10, 2)) )s,
avg(CAST(JSON_UNQUOTE(JSON_EXTRACT(latest_immediate_odds, '$[1]')) AS DECIMAL(10, 2))) p,
avg(CAST(JSON_UNQUOTE(JSON_EXTRACT(latest_immediate_odds, '$[2]')) AS DECIMAL(10, 2)) )f
from odds_asian_immediate where id= :matchid
;
select
avg( CAST(JSON_UNQUOTE(JSON_EXTRACT(latest_immediate_odds, '$[0]')) AS DECIMAL(10, 2)) )s,
avg(CAST(JSON_UNQUOTE(JSON_EXTRACT(latest_immediate_odds, '$[1]')) AS DECIMAL(10, 2))) p,
avg(CAST(JSON_UNQUOTE(JSON_EXTRACT(latest_immediate_odds, '$[2]')) AS DECIMAL(10, 2)) )f
from odds_european_immediate where id= :matchid`,
{
replacements: { matchid },
}
)
.then(result => {
if (result && result.length > 1 && result[1]) {
return result[1];
}
return null;
})
.catch(err => {
console.log(`calc_odds ${matchid} >>> ${err.message}`);
return null;
});
if (!data || data.length != 2 || !data[0][0].s || !data[1][0].s) {
return null;
}
// 亚洲态度:取全部公司的均值后,计算百分比,根据下面方式取概率大的一方加平。
// 如:通过多家亚指公司算出均值如下:
// 1.01 -0.78 0.85
// 第一步:先把本金加上水位都加1.则为2.01 -0.78 1.85
// 第二步:让球方为主队,则主水2.01+盘口0.78=2.79
// 第三步:用2.79 1.85计算百分比:
// 胜:4.64/(1.23+1.85)=60%
// 负:1-60%=40%
// 平:平固定算20%;
// 取概率大的一方加平,则可取:胜 平 百分比为80%。
var asia_data = data[0][0];
var asia;
if (asia_data.p > 0) {
asia = {
rate: (asia_data.f + 1) / (asia_data.s + 1 + asia_data.p) + 0.2,
pre: ['胜', '平']
}
} else {
asia = {
rate: (asia_data.s + 1) / (asia_data.f + 1 - asia_data.p) + 0.2,
pre: ['负', '平']
}
}
var europe_data = data[1][0];
var europe;
if (europe_data.s < europe_data.f) {
europe = {
rate: (1 / europe_data.s + 1 / europe_data.p) / (1 / europe_data.s + 1 / europe_data.p + 1 / europe_data.f),
pre: ['胜', '平']
}
} else {
europe = {
rate: (1 / europe_data.f + 1 / europe_data.p) / (1 / europe_data.s + 1 / europe_data.p + 1 / europe_data.f),
pre: ['负', '平']
}
}
return {
asia,
europe
}
}
async function lstp(sp: any) {
// let sql = `
// select game_id as GameId,
// competition_id,
// score as Score,
// host_id as SportsdtHostId,
// guest_id as SportsdtGuestId,
// host_name,
// guest_name,
// game_date as Date,
// game_name,
// yiqiu_game_id,
// JSON_ARRAY(first_odds_host,first_odds_guest,first_odds_handicap,sport_odds_host,sport_odds_guest,sport_odds_handicap) as Data
// from db_info.stat_soccer_ahodds
// where first_odds_host=${sp[0]}
// and first_odds_guest=${sp[1]}
// and first_odds_handicap=${sp[2]}
// and game_date<'${moment().add(-1, 'days').format("YYYY-MM-DD HH:mm:ss")}'
// order by game_date desc
// limit 300 `
// let list = await info_db.sequelize.query(sql)
// if (list[0].length > 0) {
// list = list[0]
// } else {
// list = []
// }
// if (list && list.length) {
// return;
// }
if (sp[0] < sp[2]) {
return {
rate: (1 / sp[0] + 1 / sp[1]) / (1 / sp[0] + 1 / sp[1] + 1 / sp[2]),
pre: ['胜', '平']
}
} else {
return {
rate: (1 / sp[2] + 1 / sp[1]) / (1 / sp[0] + 1 / sp[1] + 1 / sp[2]),
pre: ['负', '平']
}
}
// list = JSON.parse(JSON.stringify(list))
// list = _.filter(list, (o) => {
// return o.host_name && o.guest_name
// })
// list = _.unionBy(list, "yiqiu_game_id");
// return base.getItemResultByList(list, true)
return null;
}
async function start1(issue_name: any) {
// var issue_name = moment().format('YYYY-MM-DD');
var data = await info_db.ms_soccer_game.findAll({
attributes: ['id', 'leisu_match_id', 'odds', 'yiqiu_host_team_id', 'yiqiu_guest_team_id', 'leisu_host_id', 'leisu_guest_id', 'match_time'],
where: {
sporttery_issue_name: issue_name,
odds: {
$ne: null
},
match_time: {
$gte: moment().format('YYYY-MM-DD HH:mm:ss')
}
}
});
var data1 = await info_db.ms_soccer_game.findAll({
attributes: ['id', 'leisu_match_id', 'odds', 'yiqiu_host_team_id', 'yiqiu_guest_team_id', 'leisu_host_id', 'leisu_guest_id', 'match_time'],
where: {
sporttery_issue_name: null,
$or: [
{ is_beidan: 1 },
{ is_zucai: 1 }
],
odds: {
$ne: null
},
match_time: {
$between: [moment().format('YYYY-MM-DD HH:mm:ss'), moment(issue_name).add(1, 'day').format('YYYY-MM-DD 00:00:00')]
}
}
});
data = data.concat(data1);
if (!data || data.length < 1) return;
var sxjx_list = await info_db.tools_sxjx.findAll({
where: {
id: {
$in: data.map((a: any) => a.id)
}
}
});
for (let index = 0; index < data.length; index++) {
const item: any = data[index];
var temp: any = sxjx_list.find((a: any) => a.id == item.id);
var odds = await calc_odds(item.leisu_match_id);
if (!odds) {
continue;
}
if (temp) {
temp.xxzs[0] = odds.europe;
temp.xxzs[1] = odds.asia;
temp.xxzs[2] = {
rate: (odds.asia.rate + odds.europe.rate) / 2,
pre: [odds.asia.pre[0]]
}
temp.xxzs = temp.xxzs.map((a: any, index1: any) => {
if (index1 == 3) return a;
var rate = _.toInteger(a.rate.toFixed(2) * 100 + 0.0001);
a.rate = rate > 90 ? 90 : rate;
return a;
})
temp.xxzs[4] = {
rate: _.toInteger((odds.asia.rate + odds.europe.rate + temp.xxzs[2].rate + temp.xxzs[3].rate) / 4),
pre: [odds.asia.pre[0]],
}
await temp.save();
continue;
}
var eu = item.odds['3000181'].eu.map((a: any) => Number(a));
var lstp_temp: any = await lstp(eu);
var model: any = { id: item.id };
var user_zs: any;
if (eu[0] < eu[2]) {
user_zs = {
rate: (1 / eu[0] + 1 / eu[1]) / (1 / eu[0] + 1 / eu[1] + 1 / eu[2]),
pre: ['胜', '平']
}
} else {
user_zs = {
rate: (1 / eu[2] + 1 / eu[1]) / (1 / eu[0] + 1 / eu[1] + 1 / eu[2]),
pre: ['负', '平']
}
}
model.xxzs = [
odds.europe,
odds.asia,
user_zs,
lstp_temp,
{
rate: (odds.asia.rate + odds.europe.rate + user_zs.rate + lstp_temp.rate) / 4,
pre: [odds.asia.pre[0]],
}
]
model.xxzs.forEach((a: any) => {
var rate = _.toInteger(a.rate.toFixed(2) * 100 + 0.0001);
a.rate = rate > 90 ? 90 : rate;
})
model.xxzs[4] = {
rate: _.toInteger((odds.asia.rate + odds.europe.rate + user_zs.rate + lstp_temp.rate) / 4),
pre: [odds.asia.pre[0]],
}
model.meeting = await meeting([item.yiqiu_host_team_id, item.yiqiu_guest_team_id], item.match_time);
var history_host_team = await history_team(item.yiqiu_host_team_id, true, item.match_time)
var history_guest_team = await history_team(item.yiqiu_guest_team_id, false, item.match_time)
// { data: res, avg_jq: time > 0 ? 1.0 * sum_jq_time / time : 0, sum_jq, host_jq }
model.history = [history_host_team.data, history_guest_team.data,
{ sum_jq: history_host_team.sum_jq, host_jq: history_host_team.host_jq, spf: history_host_team.spf },
{ sum_jq: history_guest_team.sum_jq, host_jq: history_guest_team.host_jq, spf: history_guest_team.spf }
];
model.best_player = [await best_player(item.leisu_host_id, item.match_time), await best_player(item.leisu_guest_id, item.match_time)];
model.pre = [_(model.xxzs).map((a: any) => a.pre).flatten().countBy().toPairs().orderBy([1], ['desc']).take(2).map(a => a[0]).value(), [2, 3]];
var sum_avg_jq = history_host_team.avg_jq + history_guest_team.avg_jq;
if (sum_avg_jq > 6) sum_avg_jq = 6.1;
model.pre[1] = [Math.floor(sum_avg_jq), Math.ceil(sum_avg_jq)];
// if (model.meeting && model.meeting.sum_jq && model.meeting.z_spf.length) {
// var avg_jq = model.meeting.sum_jq * 1.0 / model.meeting.z_spf.length;
// model.pre[1] = [Math.floor(avg_jq), Math.ceil(avg_jq)];
// } else if (history_host_team.sum_jq && history_guest_team.sum_jq) {
// var avg_jq = (1.0 * history_host_team.sum_jq / history_host_team.data.length + 1.0 * history_guest_team.sum_jq / history_guest_team.data.length) / 2.0;
// model.pre[1] = [Math.floor(avg_jq), Math.ceil(avg_jq)];
// }
model.pre[1] = _.uniq(model.pre[1]);
var h = await trait_team(item.yiqiu_host_team_id, item.match_time)
var g = await trait_team(item.yiqiu_guest_team_id, item.match_time)
model.trait = [h, g];
try {
// 胜平负 总进球 比分 半全场
//[["胜", "平"], [3, 4]]
model.pre_all = [...model.pre, ...predictScoreAndHalfFull(model.pre)];
} catch (error: any) {
console.log(`predictScoreAndHalfFull error: ${JSON.stringify(model.pre)} ${error.message}`)
}
await info_db.tools_sxjx.create(model);
await sleep(2000);
}
}
function predictScoreAndHalfFull(pre: any) {
// 输入参数结构: pre = [["胜", "平"], [3, 4]]
const [winDrawLose, totalGoals] = pre;
// 比分预测结果
const scorePredictions = [];
// 半全场预测结果
const halfFullPredictions = [];
// 根据胜平负和总进球生成比分预测
for (const outcome of winDrawLose) {
for (const goals of totalGoals) {
// 随机生成2-4个比分预测
const count = Math.floor(Math.random() * 3) + 2;
for (let i = 0; i < count; i++) {
if (outcome === "胜") {
// 主队胜的比分: 主队进球 > 客队进球,且总和=goals
// 主队至少进1球,最多进goals-1球
// 客队最多进主队进球-1球
const minHome = Math.max(1, Math.ceil(goals / 2));
const maxHome = goals;
if (minHome > maxHome) continue; // 无解情况跳过
const home = Math.floor(Math.random() * (maxHome - minHome + 1)) + minHome;
const away = goals - home;
scorePredictions.push(`${home}-${away}`);
} else if (outcome === "平") {
// 平局的比分: 主队进球 = 客队进球,且总和=goals
if (goals % 2 === 0) {
const score = goals / 2;
scorePredictions.push(`${score}-${score}`);
}
} else if (outcome === "负") {
// 客队胜的比分: 主队进球 < 客队进球,且总和=goals
// 客队至少进1球,最多进goals-1球
// 主队最多进客队进球-1球
const minAway = Math.max(1, Math.ceil(goals / 2));
const maxAway = goals;
if (minAway > maxAway) continue; // 无解情况跳过
const away = Math.floor(Math.random() * (maxAway - minAway + 1)) + minAway;
const home = goals - away;
scorePredictions.push(`${home}-${away}`);
}
}
}
}
// 根据胜平负生成半全场预测
for (const outcome of winDrawLose) {
// 随机生成2-4个半全场预测
const count = Math.floor(Math.random() * 3) + 2;
for (let i = 0; i < count; i++) {
if (outcome === "胜") {
// 可能的情况: 胜胜, 平胜
const options = ["胜胜", "平胜"];
halfFullPredictions.push(options[Math.floor(Math.random() * options.length)]);
} else if (outcome === "平") {
// 可能的情况: 平平, 胜平, 负平
const options = ["平平", "胜平", "负平"];
halfFullPredictions.push(options[Math.floor(Math.random() * options.length)]);
} else if (outcome === "负") {
// 可能的情况: 负负, 平负
const options = ["负负", "平负"];
halfFullPredictions.push(options[Math.floor(Math.random() * options.length)]);
}
}
}
// 去重并限制数量
const uniqueScores = [...new Set(scorePredictions)].slice(0, 4);
const uniqueHalfFull = [...new Set(halfFullPredictions)].slice(0, 4);
return [uniqueScores, uniqueHalfFull];
}
async function calc_pre_win() {
await info_db.sequelize.query(
`
UPDATE tools_sxjx a
JOIN ms_soccer_game b ON a.id = b.id
SET
a.score=b.score,
a.half=CONCAT('[', REPLACE(b.half, '-', ','), ']'),
a.pre_win = JSON_ARRAY(
CASE
WHEN JSON_EXTRACT(b.score, '$[0]') > JSON_EXTRACT(b.score, '$[1]') THEN '胜'
WHEN JSON_EXTRACT(b.score, '$[0]') < JSON_EXTRACT(b.score, '$[1]') THEN '负'
ELSE '平'
END,
LEAST(JSON_EXTRACT(b.score, '$[0]') + JSON_EXTRACT(b.score, '$[1]'), 7)
)
WHERE a.created_at>DATE_SUB(CURDATE(), INTERVAL 7 DAY)
and a.pre_win IS NULL
and b.status=4
AND b.score IS NOT NULL;
update tools_sxjx
set is_win= (
JSON_CONTAINS(JSON_EXTRACT(pre, '$[0]'), JSON_EXTRACT(pre_win, '$[0]')) OR
JSON_CONTAINS(JSON_EXTRACT(pre, '$[0]'), JSON_EXTRACT(pre_win, '$[1]')) OR
JSON_CONTAINS(JSON_EXTRACT(pre, '$[1]'), JSON_EXTRACT(pre_win, '$[0]')) OR
JSON_CONTAINS(JSON_EXTRACT(pre, '$[1]'), JSON_EXTRACT(pre_win, '$[1]'))
)
WHERE created_at>DATE_SUB(CURDATE(), INTERVAL 7 DAY) and pre IS NOT NULL AND pre_win IS NOT NULL and is_win is null;
`,
{
type: info_db.sequelize.QueryTypes.UPDATE
}
)
.then(result => {
console.log(`calc_pre_win ok ${moment().format('YYYY-MM-DD HH:mm:ss')}`);
})
.catch(err => {
console.error(`calc_pre_win error :`, err);
});
await sleep(88);
var ret = await info_db.tools_sxjx.findAll({
attributes: ['id', 'score', 'half', 'pre_all'],
limit: 1000,
where: {
created_at: { $gt: moment().subtract(7, 'days').toDate() },
win_all: null,
open_result: null,
pre_all: { $ne: null },
pre_win: { $ne: null },
score: { $ne: null },
half: { $ne: null }
}
}).map((a: any) => a.dataValues);
for (let index = 0; index < ret.length; index++) {
var item: any = ret[index];
try {
var qc_spf = item.score[0] > item.score[1] ? '胜' : item.score[0] < item.score[1] ? '负' : '平';
var bc_spf = item.half[0] > item.half[1] ? '胜' : item.half[0] < item.half[1] ? '负' : '平';
var open_result = [
qc_spf,
item.score[0] + item.score[1],
`${item.score.join('-')}`,
`${bc_spf}${qc_spf}`
]
var win_all = item.pre_all.map((item: any, index: number) => item.find((a: any) => a == open_result[index]) || '');
await info_db.tools_sxjx.update({
open_result,
win_all,
}, {
where: {
id: item.id
}
});
} catch (error: any) {
console.log(`calc_pre_win update win all error :${item.id} ${error.message}`);
}
await sleep(88);
}
await sleep(1000);
}
//生成比赛数据
async function create_match_data() {
for (let index = -3; index < 1; index++) {
await start1(moment().add(-index, 'days').format('YYYY-MM-DD')).catch(a => console.log(a))
}
}
// //2个小时执行1次
// schedule.scheduleJob('0 */2 * * *', async function () {
// for (let index = -3; index < 1; index++) {
// await start1(moment().add(-index, 'days').format('YYYY-MM-DD')).catch(a => console.log(a))
// }
// });
// //1个小时执行1次
// schedule.scheduleJob('0 * * * *', function () {
// calc_pre_win();
// })
export { create_match_data, calc_pre_win }