globalData.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { parseIorDetail } from "./parseGameData.js";
  2. /**
  3. * 全局运行时数据
  4. */
  5. export const GLOBAL_DATA = {
  6. filtedLeagues: [],
  7. filtedGames: [],
  8. gamesMap: {},
  9. straightFixturesVersion: 0,
  10. straightFixturesCount: 0,
  11. specialFixturesVersion: 0,
  12. specialFixturesCount: 0,
  13. straightOddsVersion: 0,
  14. // straightOddsCount: 0,
  15. specialsOddsVersion: 0,
  16. // specialsOddsCount: 0,
  17. currencies: [],
  18. currenciesUpdatedAt: 0,
  19. requestErrorCount: 0,
  20. loopActive: false,
  21. loopResultTime: 0,
  22. };
  23. export const getCurrenciesInfo = () => {
  24. const { currencies, currenciesUpdatedAt } = GLOBAL_DATA;
  25. if (!currenciesUpdatedAt) {
  26. return Promise.reject({
  27. cause: 503,
  28. message: 'currencies data is not ready',
  29. data: { currenciesUpdatedAt },
  30. });
  31. }
  32. return Promise.resolve({ data: currencies, updatedAt: currenciesUpdatedAt });
  33. }
  34. export const findCurrencyInfo = (currencyCode) => {
  35. const code = currencyCode?.toUpperCase();
  36. const currencies = GLOBAL_DATA.currencies?.currencies ?? GLOBAL_DATA.currencies;
  37. if (!code || !Array.isArray(currencies)) {
  38. return null;
  39. }
  40. return currencies.find(currency => currency.code?.toUpperCase() === code) ?? null;
  41. }
  42. const normalizeIds = (ids) => {
  43. if (ids === undefined || ids === null || ids === '') {
  44. return [];
  45. }
  46. if (Array.isArray(ids)) {
  47. return ids.flatMap(normalizeIds);
  48. }
  49. return String(ids).split(',').map(id => id.trim()).filter(Boolean);
  50. }
  51. const getDateInTimezone = (offsetHours) => {
  52. const nowUTC = new Date();
  53. const targetTime = new Date(nowUTC.getTime() + offsetHours * 60 * 60 * 1000);
  54. const year = targetTime.getUTCFullYear();
  55. const month = String(targetTime.getUTCMonth() + 1).padStart(2, '0');
  56. const day = String(targetTime.getUTCDate()).padStart(2, '0');
  57. return `${year}-${month}-${day}`;
  58. }
  59. const isEventAvailable = (event) => {
  60. if (!event?.starts) {
  61. return false;
  62. }
  63. const startsTime = new Date(event.starts).getTime();
  64. if (!Number.isFinite(startsTime)) {
  65. return false;
  66. }
  67. const todayEndTime = new Date(`${getDateInTimezone(-4)} 23:59:59 GMT-4`).getTime();
  68. const tomorrowEndTime = todayEndTime + 24 * 60 * 60 * 1000;
  69. return startsTime >= Date.now() - 3 * 60 * 60 * 1000 && startsTime <= tomorrowEndTime;
  70. }
  71. const sortEventsByStarts = (events) => {
  72. return events.sort((a, b) => new Date(a.starts).getTime() - new Date(b.starts).getTime());
  73. }
  74. export const getEventsByIds = (ids) => {
  75. const normalizedIds = normalizeIds(ids);
  76. const { gamesMap={} } = GLOBAL_DATA;
  77. if (!normalizedIds.length) {
  78. return sortEventsByStarts(Object.values(gamesMap).filter(isEventAvailable));
  79. }
  80. return sortEventsByStarts(normalizedIds.map(id => gamesMap[id]).filter(Boolean).filter(isEventAvailable));
  81. }
  82. /**
  83. * 获取盘口详情
  84. * @param {*} ior
  85. * @param {*} id
  86. * @returns
  87. */
  88. export const getIorInfo = async(ior, id) => {
  89. if (!id || !ior) {
  90. return Promise.reject({ cause: 400, message: 'id and ior are required', data: { id, ior } });
  91. }
  92. const { gamesMap } = GLOBAL_DATA;
  93. const iorInfo = parseIorDetail(ior, id, gamesMap);
  94. if (iorInfo.cause === 400) {
  95. return Promise.reject({ cause: 400, message: iorInfo.message, data: { id, ior } });
  96. }
  97. return Promise.resolve(iorInfo);
  98. }