globalData.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. export const getEventsByIds = (ids) => {
  72. const normalizedIds = normalizeIds(ids);
  73. const { gamesMap={} } = GLOBAL_DATA;
  74. if (!normalizedIds.length) {
  75. return Object.values(gamesMap).filter(isEventAvailable);
  76. }
  77. return normalizedIds.map(id => gamesMap[id]).filter(Boolean).filter(isEventAvailable);
  78. }
  79. /**
  80. * 获取盘口详情
  81. * @param {*} ior
  82. * @param {*} id
  83. * @returns
  84. */
  85. export const getIorInfo = async(ior, id) => {
  86. if (!id || !ior) {
  87. return Promise.reject({ cause: 400, message: 'id and ior are required', data: { id, ior } });
  88. }
  89. const { gamesMap } = GLOBAL_DATA;
  90. const iorInfo = parseIorDetail(ior, id, gamesMap);
  91. if (iorInfo.cause === 400) {
  92. return Promise.reject({ cause: 400, message: iorInfo.message, data: { id, ior } });
  93. }
  94. return Promise.resolve(iorInfo);
  95. }