Files
Twitch-Drops-Miner/TwitchChannelPointsMiner/classes/entities/EventPrediction.py
hpeter 5177ae155f Fix EventPrediction on refund
12/02/23 13:39:08 - INFO - [on_message]: +2000 --> Streamer(username=xxxxxxxx, channel_id=xxxxxxxx, channel_points=2.62k) - Reason: REFUND.
12/02/23 13:39:08 - ERROR - [on_message]: Exception raised for topic: predictions-user-v1 and message: 
{
   "type":"prediction-result",
   "data":{
      "timestamp":"2023-02-12T12:41:45.371055608Z",
      "prediction":{
         "id":"xxxxxxxxxx",
         "event_id":"xxxxxxxxxx",
         "outcome_id":"xxxxxxxxxx",
         "channel_id":"xxxxxxxx",
         "points":2000,
         "predicted_at":"2023-02-12T12:41:28.150531011Z",
         "updated_at":"2023-02-12T12:41:45.365963188Z",
         "user_id":"xxxxxxxx",
         "result":{
            "type":"REFUND",
            "points_won":"None",
            "is_acknowledged":false
         },
         "user_display_name":"None"
      }
   }
}
Traceback (most recent call last):
  File "\TwitchChannelPointsMiner\classes\WebSocketsPool.py", line 325, in on_message
    points = event_prediction.parse_result(
  File "\TwitchChannelPointsMiner\classes\entities\EventPrediction.py", line 69, in parse_result
    points["placed"] = self.bet.decision["amount"]
KeyError: 'amount'
2023-02-12 14:20:38 +01:00

95 lines
2.7 KiB
Python

from TwitchChannelPointsMiner.classes.entities.Bet import Bet
from TwitchChannelPointsMiner.classes.entities.Streamer import Streamer
from TwitchChannelPointsMiner.classes.Settings import Settings
from TwitchChannelPointsMiner.utils import _millify, float_round
class EventPrediction(object):
__slots__ = [
"streamer",
"event_id",
"title",
"created_at",
"prediction_window_seconds",
"status",
"result",
"box_fillable",
"bet_confirmed",
"bet_placed",
"bet",
]
def __init__(
self,
streamer: Streamer,
event_id,
title,
created_at,
prediction_window_seconds,
status,
outcomes,
):
self.streamer = streamer
self.event_id = event_id
self.title = title.strip()
self.created_at = created_at
self.prediction_window_seconds = prediction_window_seconds
self.status = status
self.result: dict = {"string": "", "type": None, "gained": 0}
self.box_fillable = False
self.bet_confirmed = False
self.bet_placed = False
self.bet = Bet(outcomes, streamer.settings.bet)
def __repr__(self):
return f"EventPrediction(event_id={self.event_id}, streamer={self.streamer}, title={self.title})"
def __str__(self):
return (
f"EventPrediction: {self.streamer} - {self.title}"
if Settings.logger.less
else self.__repr__()
)
def elapsed(self, timestamp):
return float_round((timestamp - self.created_at).total_seconds())
def closing_bet_after(self, timestamp):
return float_round(self.prediction_window_seconds - self.elapsed(timestamp))
def print_recap(self) -> str:
return f"{self}\n\t\t{self.bet}\n\t\tResult: {self.result['string']}"
def parse_result(self, result) -> dict:
result_type = result["type"]
points = {}
points["placed"] = (
self.bet.decision["amount"] if result_type != "REFUND" else 0
)
points["won"] = (
result["points_won"]
if result["points_won"] or result_type == "REFUND"
else 0
)
points["gained"] = (
points["won"] - points["placed"] if result_type != "REFUND" else 0
)
points["prefix"] = "+" if points["gained"] >= 0 else ""
action = (
"Lost"
if result_type == "LOSE"
else ("Refunded" if result_type == "REFUND" else "Gained")
)
self.result = {
"string": f"{result_type}, {action}: {points['prefix']}{_millify(points['gained'])}",
"type": result_type,
"gained": points["gained"],
}
return points