index.js
21.6 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.calc_pre_win = exports.create_match_data = void 0;
const info_db = require("../info_mysql/table");
const nami_db = require("../nami_mysql/table");
const moment = require("moment");
const _ = require("lodash");
const common_1 = require("../../libs/common");
async function best_player(team_id, 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;
}
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) => {
var v1 = _(res).find((b) => 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, 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 = [0, 0, 0];
var res = data.map(function (item) {
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) => 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 = {
s: (bf, is_host) => {
if (is_host) {
return bf[0] > bf[1];
}
else {
return bf[0] < bf[1];
}
},
p: (bf, is_host) => {
return bf[0] == bf[1];
},
f: (bf, is_host) => {
if (is_host) {
return bf[0] < bf[1];
}
else {
return bf[0] > bf[1];
}
},
no_f: (bf, is_host) => {
if (is_host) {
return bf[0] >= bf[1];
}
else {
return bf[0] <= bf[1];
}
},
no_p: (bf, is_host) => {
return bf[0] != bf[1];
},
jq: (bf, is_host) => {
if (is_host) {
return bf[0] > 0;
}
else {
return bf[1] > 0;
}
},
no_jq: (bf, is_host) => {
if (is_host) {
return bf[0] == 0;
}
else {
return bf[1] == 0;
}
},
};
function trait_calc(list, team_id) {
var max = { s: 0, p: 0, f: 0, no_f: 0, no_p: 0, jq: 0, no_jq: 0 };
var now = { 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) => 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, 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) => a.yiqiu_host_team_id == team_id), team_id);
var t2 = trait_calc(data.filter((a) => a.yiqiu_guest_team_id == team_id), team_id);
return { t0, t1, t2 };
}
async function meeting(teamids, 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) {
try {
var bf = typeof item.score == 'string' ? JSON.parse(item.score) : item.score;
bf = bf.map((a) => 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) {
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;
}
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) {
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: ['负', '平']
};
}
return null;
}
async function start1(issue_name) {
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) => a.id)
}
}
});
for (let index = 0; index < data.length; index++) {
const item = data[index];
var temp = sxjx_list.find((a) => 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, index1) => {
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) => Number(a));
var lstp_temp = await lstp(eu);
var model = { id: item.id };
var user_zs;
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) => {
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);
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) => 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)];
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 {
model.pre_all = [...model.pre, ...predictScoreAndHalfFull(model.pre)];
}
catch (error) {
console.log(`predictScoreAndHalfFull error: ${JSON.stringify(model.pre)} ${error.message}`);
}
await info_db.tools_sxjx.create(model);
await (0, common_1.sleep)(2000);
}
}
function predictScoreAndHalfFull(pre) {
const [winDrawLose, totalGoals] = pre;
const scorePredictions = [];
const halfFullPredictions = [];
for (const outcome of winDrawLose) {
for (const goals of totalGoals) {
const count = Math.floor(Math.random() * 3) + 2;
for (let i = 0; i < count; i++) {
if (outcome === "胜") {
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 === "平") {
if (goals % 2 === 0) {
const score = goals / 2;
scorePredictions.push(`${score}-${score}`);
}
}
else if (outcome === "负") {
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) {
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 (0, common_1.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) => a.dataValues);
for (let index = 0; index < ret.length; index++) {
var item = 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, index) => item.find((a) => a == open_result[index]) || '');
await info_db.tools_sxjx.update({
open_result,
win_all,
}, {
where: {
id: item.id
}
});
}
catch (error) {
console.log(`calc_pre_win update win all error :${item.id} ${error.message}`);
}
await (0, common_1.sleep)(88);
}
await (0, common_1.sleep)(1000);
}
exports.calc_pre_win = calc_pre_win;
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));
}
}
exports.create_match_data = create_match_data;
//# sourceMappingURL=index.js.map