frog_oas.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """Persistent, outcome-verified weighted voting. Scores are not win probabilities."""
  2. import json
  3. import re
  4. import random
  5. from datetime import datetime
  6. from pathlib import Path
  7. from uuid import uuid4
  8. import cv2
  9. import requests
  10. from tasks.FrogBoss.oas_sources import DASHEN_UIDS
  11. def fingerprint(image):
  12. # Only the two lineups: excludes countdown, votes, chest and result text.
  13. bits = []
  14. for x in (300, 807):
  15. crop = image[112:233, x:x + 398]
  16. gray = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY)
  17. small = cv2.resize(gray, (33, 8))
  18. bits.extend((small[:, 1:] > small[:, :-1]).flatten())
  19. return ''.join('1' if bit else '0' for bit in bits)
  20. def same_lineup(a, b):
  21. return len(a) == len(b) == 512 and sum(x != y for x, y in zip(a, b)) <= 20
  22. def parse_side(text):
  23. # Ambiguous text is deliberately excluded rather than guessed by word order.
  24. red = bool(re.search(r'押红|押左|压红|压左|我红|我左|红优|红方胜|红色胜', text))
  25. blue = bool(re.search(r'押蓝|押右|压蓝|压右|我蓝|我右|蓝优|蓝方胜|蓝色胜', text))
  26. if re.search(r'不押|不压|别押|别压|不要押|不要压', text):
  27. return None
  28. return ('LEFT' if red else 'RIGHT') if red != blue else None
  29. class OasHistory:
  30. def __init__(self, path):
  31. self.path = Path(path)
  32. self.events = []
  33. if self.path.exists():
  34. for line in self.path.read_text(encoding='utf-8').splitlines():
  35. try:
  36. event = json.loads(line)
  37. except ValueError:
  38. continue # Ignore a truncated final record after interruption.
  39. if isinstance(event, dict):
  40. self.events.append(event)
  41. def append(self, kind, **data):
  42. event = dict(version=1, kind=kind, recorded_at=datetime.now().isoformat(), **data)
  43. self.path.parent.mkdir(parents=True, exist_ok=True)
  44. # Leading newline separates a possible truncated tail from the new event.
  45. with self.path.open('a', encoding='utf-8') as stream:
  46. stream.write('\n' + json.dumps(event, ensure_ascii=False) + '\n')
  47. stream.flush()
  48. self.events.append(event)
  49. return event
  50. def reliability(self, source):
  51. decisions = {e['id']: e for e in self.events if e.get('kind') == 'decision'}
  52. correct = total = 0
  53. seen = set()
  54. for result in self.events:
  55. if result.get('kind') != 'result' or result['id'] in seen:
  56. continue
  57. seen.add(result['id'])
  58. vote = decisions.get(result['id'], {}).get('votes', {}).get(source)
  59. if vote in ('LEFT', 'RIGHT'):
  60. total += 1
  61. correct += vote == result['winner']
  62. # Unverified newcomers start neutral; verified sources use raw win rate.
  63. return correct / total if total else 0.5
  64. def settle(self, signature, winner):
  65. if winner not in ('LEFT', 'RIGHT'):
  66. return None
  67. # Include already settled records when checking ambiguity: never transfer
  68. # an old result to a newer decision with an identical lineup.
  69. matches = [e for e in self.events if e.get('kind') == 'decision'
  70. and same_lineup(e['signature'], signature)]
  71. if len(matches) != 1:
  72. self.append('unverified_result', signature=signature, winner=winner,
  73. reason='missing_or_ambiguous_decision')
  74. return None
  75. decision = matches[0]
  76. if any(e.get('kind') == 'result' and e['id'] == decision['id'] for e in self.events):
  77. return None
  78. return self.append('result', id=decision['id'], winner=winner,
  79. outcomes={s: v == winner for s, v in decision['votes'].items()})
  80. def choose(self, signature, left, right, predictions):
  81. now = datetime.now()
  82. # Re-entry in the same slot reuses the original frozen decision.
  83. slot = f'{now.date()}:{now.hour // 2}'
  84. for e in reversed(self.events):
  85. if e.get('kind') == 'decision' and e['slot'] == slot and same_lineup(e['signature'], signature):
  86. return e
  87. crowd = ('LEFT' if left > right else 'RIGHT') if left != right and left + right > 0 else None
  88. votes = {p['uid']: p['side'] for p in predictions if p.get('side') in ('LEFT', 'RIGHT')}
  89. expert_left = sum(v == 'LEFT' for v in votes.values())
  90. expert_right = sum(v == 'RIGHT' for v in votes.values())
  91. expert_side = ('LEFT' if expert_left > expert_right else 'RIGHT') if expert_left != expert_right else None
  92. if crowd:
  93. votes['crowd'] = crowd
  94. cold_start = not any(e.get('kind') == 'result' for e in self.events)
  95. weights = {} if cold_start else {uid: self.reliability(uid) for uid in votes}
  96. scores = {'LEFT': 0.0, 'RIGHT': 0.0}
  97. if cold_start:
  98. # Two equal votes: the expert majority as a whole and the crowd.
  99. for vote in (expert_side, crowd):
  100. if vote:
  101. scores[vote] += 1
  102. else:
  103. for uid, vote in votes.items():
  104. scores[vote] += weights[uid]
  105. tied = abs(scores['LEFT'] - scores['RIGHT']) < 1e-12
  106. side = random.choice(('LEFT', 'RIGHT')) if tied else max(scores, key=scores.get)
  107. return self.append('decision', id=uuid4().hex, slot=slot, signature=signature,
  108. left=left, right=right, votes=votes, weights=weights,
  109. scores=scores, side=side, strategy_version=2,
  110. mode='cold_start' if cold_start else 'win_rate',
  111. expert_counts={'LEFT': expert_left, 'RIGHT': expert_right},
  112. expert_side=expert_side, crowd_side=crowd, random_tiebreak=tied)
  113. def fetch_predictions(history):
  114. now = datetime.now()
  115. used = {(e.get('uid'), e.get('feed_id')) for e in history.events
  116. if e.get('kind') == 'fetch' and e.get('accepted')}
  117. predictions = []
  118. with requests.Session() as session:
  119. for uid in DASHEN_UIDS:
  120. feed_id = None
  121. try:
  122. response = session.get('https://inf.ds.163.com/v1/web/feed/basic/getSomeOneFeeds',
  123. params={'feedTypes': '1,2,3,4,6,7,10,11', 'someOneUid': uid}, timeout=(3, 5))
  124. response.raise_for_status()
  125. feeds = response.json().get('result', {}).get('feeds', [])
  126. if not feeds:
  127. history.append('fetch', uid=uid, accepted=False, reason='no_feed')
  128. continue
  129. feed_id = feeds[0]['id']
  130. response = session.get('https://inf.ds.163.com/v1/web/feed/basic/facade',
  131. params={'feedId': feed_id}, timeout=(3, 5))
  132. response.raise_for_status()
  133. feed = response.json()['result']['feed']
  134. content = feed['content']
  135. if isinstance(content, str):
  136. content = json.loads(content)
  137. body = content['body']['text']
  138. raw_time = feed.get('createTime')
  139. published = None
  140. try:
  141. published = datetime.fromtimestamp(float(raw_time) / 1000)
  142. except (TypeError, ValueError, OverflowError, OSError):
  143. pass
  144. side = parse_side(body)
  145. fresh = published is not None and published.date() == now.date() and published.hour // 2 == now.hour // 2 and published <= now
  146. accepted = fresh and side is not None and (uid, feed_id) not in used
  147. history.append('fetch', uid=uid, feed_id=feed_id, create_time=raw_time,
  148. body=body, side=side, accepted=accepted,
  149. reason='accepted' if accepted else 'stale_unknown_ambiguous_or_duplicate')
  150. if accepted:
  151. predictions.append(dict(uid=uid, side=side, feed_id=feed_id))
  152. except (requests.RequestException, ValueError, KeyError, IndexError, TypeError) as exc:
  153. history.append('fetch', uid=uid, feed_id=feed_id, accepted=False,
  154. reason='request_or_parse_error', error=str(exc))
  155. return predictions