Untitled

From Chartreuse Parrot, 2 Weeks ago, written in Plain Text, viewed 4 times. This paste will bite the big one in 1 Week.
URL https://paste.afonso.co/view/16d9442f Embed
Download Paste or View Raw
  1. import footballstatshelper
  2. import helper
  3.  
  4. import clr
  5. import System
  6.  
  7. clr.AddReference("System.Drawing")
  8. from System.Drawing import *
  9. from System.Drawing import Color as ColorDrawing
  10. from System.Drawing.Imaging import ImageFormat
  11. from System.Drawing.Drawing2D import *
  12.  
  13. from System import Guid
  14. from System.Windows.Media import SolidColorBrush, Color
  15. from System.IO import Path, File, DirectoryInfo
  16.  
  17.  
  18. clr.AddReference("Wtvision.Sports.Tools.OptaImporter")
  19. from Wtvision.Sports import OptaProxyApi
  20.  
  21. HOME_SHOTS_RGB = "255,255,0"
  22. HOME_MISS_RGB = "255,0,0"
  23. HOME_GOALS_RGB = "255,255,255"
  24.  
  25. AWAY_SHOTS_RGB = "255,255,0"
  26. AWAY_MISS_RGB = "255,0,0"
  27. AWAY_GOALS_RGB = "255,255,255"
  28.  
  29.  
  30. def __onStart(graphicOnAirItem, momentExecution, workFlowtype):
  31.  
  32.         if Globals["OptionsLanguageForm"].selectedLanguage == "":
  33.                 momentExecution.Message = "Select language in OptionsForm !"
  34.                 momentExecution.Cancel = True
  35.                
  36. #       if workFlowtype.ToString() != "Preview":
  37. #               if GameOnline.IsOnAir:
  38. #                       momentExecution.Message = "ONLINE IS ON AIR"
  39. #                       momentExecution.Cancel = True
  40. #                       return
  41. #      
  42.        
  43. def __getData(graphicOnAirItem, viewBag, momentExecution,workFlowtype):
  44.         extradata = graphicOnAirItem.Graphic.ExtraData
  45.         ARVisibility = "vARVisibility"
  46.         ENVisibility = "vENVisibility"
  47.         hometeam = Game.HomeTeam
  48.         awayteam = Game.AwayTeam
  49.         helper.upperTags(viewBag)
  50.        
  51.        
  52. #       homeRGB = helper.get_team_institutional_color(hometeam.RefName)
  53. #       awayRGB = helper.get_team_institutional_color(awayteam.RefName)
  54. #
  55. #       viewBag.SetString("vHomeTeamRGB", homeRGB)
  56. #       viewBag.SetString("vAwayTeamRGB", awayRGB)
  57.        
  58.         homeRGB = footballstatshelper.GetTeamRGB(hometeam)
  59.         awayRGB = footballstatshelper.GetTeamRGB(awayteam)
  60.         viewBag.SetString("vHomeTeamRGB", ",".join(c.ToString() for c in homeRGB))
  61.         viewBag.SetString("vAwayTeamRGB", ",".join(c.ToString() for c in awayRGB))
  62.        
  63.        
  64.         if workFlowtype.ToString() == "Preview":
  65.                
  66.                 graphicOnAir = IntelliflowController.GetGraphicOnAirItems(OutputManager.ActiveChannelOutput)
  67.                 helper.SetExtraData(extradata, "IsOnAir", "False")
  68.                
  69.                 for i in graphicOnAir:
  70.                         if i.Scene.Name == graphicOnAirItem.Scene.Name:
  71.                                 if i.Graphic.Name == graphicOnAirItem.Graphic.Name:
  72.                                         helper.SetExtraData(extradata, "IsOnAir", "True")
  73.        
  74.         #Verification if have key IsOnair in Extradata.
  75.         if not extradata.ContainsKey("IsOnAir"): helper.SetExtraData(extradata, "IsOnAir", "False")
  76.         graphicIsOnAir = extradata["IsOnAir"]
  77.        
  78.         if graphicIsOnAir == "False":
  79.                 if Globals["OptionsLanguageForm"].selectedLanguage == "AR":
  80.                         viewBag.SetString(ARVisibility, "True")
  81.                         viewBag.SetString(ENVisibility, "False")
  82.                 else:
  83.                         viewBag.SetString(ARVisibility, "False")
  84.                         viewBag.SetString(ENVisibility, "True")
  85.                        
  86.                        
  87.                 HomeExpectedGoalsEvents = []
  88.                 AwayExpectedGoalsEvents = []
  89.                
  90.                 matchExpectedGoals = OptaProxyApi.GetMatchExpectedGoals(matchId=Game.ExternalId, overrideCache=True, alwaysFresh=True) 
  91.                 if matchExpectedGoals and matchExpectedGoals.LiveData and matchExpectedGoals.LiveData.LineUps and matchExpectedGoals.LiveData.Events and matchExpectedGoals.LiveData.Events.Count:
  92.                         expectedGoalsEvents = matchExpectedGoals.LiveData.Events
  93.                         homeExpectedGoalsEvents = filter(lambda e: e.ContestantId == Game.HomeTeam.ExternalId, expectedGoalsEvents)
  94.                         awayExpectedGoalsEvents = filter(lambda e: e.ContestantId == Game.AwayTeam.ExternalId, expectedGoalsEvents)
  95.                         homeTeamData = matchExpectedGoals.LiveData.LineUps[0 if Game.HomeTeam.ExternalId == matchExpectedGoals.LiveData.LineUps[0].ContestantId else 1]
  96.                         awayTeamData = matchExpectedGoals.LiveData.LineUps[1 if Game.HomeTeam.ExternalId == matchExpectedGoals.LiveData.LineUps[0].ContestantId else 0]
  97.                         HomeTeamExpectedGoals = next((round(float(stat.Value),2) for stat in homeTeamData.Stats if stat.Type == "expectedGoals"), 0)
  98.                         AwayTeamExpectedGoals = next((round(float(stat.Value),2) for stat in awayTeamData.Stats if stat.Type == "expectedGoals"), 0)
  99.                        
  100.                         for e in homeExpectedGoalsEvents:
  101.                                 expectedGoalQualifier = next((q for q in e.Qualifiers if q.QualifierId == 321), None)
  102.                                 if not expectedGoalQualifier:continue
  103.                                 event = ExpectedGoalsEvent(e.TimeMin, e.TimeSec, e.PlayerName, e.TypeId,e.Qualifiers, round(float(expectedGoalQualifier.Value) if expectedGoalQualifier else 0, 3), e.X, e.Y)
  104.                                 HomeExpectedGoalsEvents.Add(event)
  105.        
  106.                         for e in awayExpectedGoalsEvents:
  107.                                 expectedGoalQualifier = next((q for q in e.Qualifiers if q.QualifierId == 321), None)
  108.                                 if not expectedGoalQualifier:continue
  109.                                 event = ExpectedGoalsEvent(e.TimeMin, e.TimeSec, e.PlayerName, e.TypeId,e.Qualifiers, round(float(expectedGoalQualifier.Value) if expectedGoalQualifier else 0, 3), e.X, e.Y)
  110.                                 AwayExpectedGoalsEvents.Add(event)
  111.                                
  112.                         imageName,hGoals,aGoals,hTarget,aTarget,hBlocked,aBlocked = __generateTrackingImage(graphicOnAirItem, momentExecution, Game.HomeTeam, Game.AwayTeam,HomeExpectedGoalsEvents,AwayExpectedGoalsEvents)
  113.                         viewBag.SetImage("lgImage", "Images/Fields/%s" %imageName)
  114.                        
  115.                         statPosition = 1
  116.                         statName = "GOALS"
  117.                         viewBag.SetTranslatedText("tAwayStatTitle%02d_AR"%(statPosition),"{{%s}}.{{AR}}" % statName)
  118.                         viewBag.SetTranslatedText("tHomeStatTitle%02d_AR"%(statPosition),"{{%s}}.{{AR}}" % statName)
  119.                         viewBag.SetTranslatedText("tAwayStatTitle%02d_EN"%(statPosition), "{{%s}}.{{EN}}" % statName)
  120.                         viewBag.SetTranslatedText("tHomeStatTitle%02d_EN"%(statPosition), "{{%s}}.{{EN}}" % statName)  
  121.                         viewBag.SetString("tHomeStatValue%02d" % (statPosition), Game.HomeTeamScore)
  122.                         viewBag.SetString("tAwayStatValue%02d" % (statPosition), Game.AwayTeamScore)
  123.                        
  124.                         statPosition = 2
  125.                         statName = "ON TARGET"
  126.                         viewBag.SetTranslatedText("tAwayStatTitle%02d_AR"%(statPosition),"{{%s}}.{{AR}}" % statName)
  127.                         viewBag.SetTranslatedText("tHomeStatTitle%02d_AR"%(statPosition),"{{%s}}.{{AR}}" % statName)
  128.                         viewBag.SetTranslatedText("tAwayStatTitle%02d_EN"%(statPosition), "{{%s}}.{{EN}}" % statName)
  129.                         viewBag.SetTranslatedText("tHomeStatTitle%02d_EN"%(statPosition), "{{%s}}.{{EN}}" % statName)  
  130.                         viewBag.SetString("tHomeStatValue%02d" % (statPosition), hTarget if int(Game.HomeTeamScore) < int(hTarget) else int(Game.HomeTeamScore))
  131.                         viewBag.SetString("tAwayStatValue%02d" % (statPosition), aTarget if int(Game.AwayTeamScore) < int(aTarget) else int(Game.AwayTeamScore))
  132.                                
  133.                         statPosition = 3
  134.                         statName = "BLOCKED & OFF TARGET"
  135.                         viewBag.SetTranslatedText("tAwayStatTitle%02d_AR"%(statPosition),"{{%s}}.{{AR}}" % statName)
  136.                         viewBag.SetTranslatedText("tHomeStatTitle%02d_AR"%(statPosition),"{{%s}}.{{AR}}" % statName)
  137.                         viewBag.SetTranslatedText("tAwayStatTitle%02d_EN"%(statPosition), "{{%s}}.{{EN}}" % statName)
  138.                         viewBag.SetTranslatedText("tHomeStatTitle%02d_EN"%(statPosition), "{{%s}}.{{EN}}" % statName)  
  139.                         viewBag.SetString("tHomeStatValue%02d" % (statPosition), hBlocked)
  140.                         viewBag.SetString("tAwayStatValue%02d" % (statPosition), aBlocked)
  141.  
  142.         else:
  143.                 if Globals["OptionsLanguageForm"].selectedLanguage == "AR":
  144.                         viewBag.SetString(ARVisibility, "False")
  145.                         viewBag.SetString(ENVisibility, "True")
  146.                 else:
  147.                         viewBag.SetString(ARVisibility, "True")
  148.                         viewBag.SetString(ENVisibility, "False")
  149.                
  150.                
  151.  
  152.         viewBag.SetImage("lgAwayTeamBadge", "Images/Logos/%s.png" %awayteam.RefName)
  153.         viewBag.SetImage("lgHomeTeamBadge", "Images/Logos/%s.png" %hometeam.RefName)
  154.  
  155.        
  156.         viewBag.SetTranslatedText("tAwayTeamName_AR", "{{%s}}.{{AR}}" % awayteam.FullName.upper())
  157.         viewBag.SetTranslatedText("tAwayTeamName_EN", "{{%s}}.{{EN}}" % awayteam.FullName.upper())
  158.        
  159.         viewBag.SetTranslatedText("tHomeTeamName_AR", "{{%s}}.{{AR}}" % hometeam.FullName.upper())
  160.         viewBag.SetTranslatedText("tHomeTeamName_EN", "{{%s}}.{{EN}}" % hometeam.FullName.upper())
  161.        
  162.        
  163.        
  164.  
  165.                
  166. def __afterProgram(graphicOnAirItem, momentExecution):
  167.         extradata = graphicOnAirItem.Graphic.ExtraData
  168.         graphicIsOnAir = extradata["IsOnAir"]
  169.        
  170.         if graphicIsOnAir == "True": return
  171.         IntelliflowController.TakeIn(graphicOnAirItem.Graphic, OutputManager.ActiveChannelOutput,workflowType = IntelliflowController.EWorkflowType.Preview)
  172.  
  173.  
  174. def __generateTrackingImage(graphicOnAirItem, momentExecution, homeTeam, awayTeam,homeExpectedGoalsEvents,awayExpectedGoalsEvents):
  175.         fieldRootPath = Path.Combine(graphicOnAirItem.GraphicsEngine.Options.RootPath, graphicOnAirItem.Scene.Path, "Images", "Fields")
  176.         originalFieldPath = Path.Combine(fieldRootPath, "field.png")
  177.         if not File.Exists(originalFieldPath):
  178.                 momentExecution.Message = "Path \"%s\" does not exists!" %originalFieldPath
  179.                 momentExecution.Cancel = True
  180.                 return None
  181.        
  182.         enginesRootPath = [fieldRootPath]
  183.         programEngines = list(OutputManager.ActiveChannelOutput.ActiveEnginesAssignmentProfile.ProgramGraphicsEngines)
  184.         for engine in programEngines:
  185.                 enginesRootPath.append(fieldRootPath +"\\")#r3engine
  186.        
  187.         for path in enginesRootPath:
  188.                 for file in DirectoryInfo(path).GetFiles():
  189.                         if file.Name.StartsWith("field_"):
  190.                                 file.Delete()
  191.  
  192.         width = 1492
  193.         height = 932
  194.  
  195.         isValid, r, g, b = stringToRGB(HOME_SHOTS_RGB)
  196.         brushShotsHome = SolidBrush(ColorDrawing.FromArgb(255, r, g, b)) #alpha, RED, GREEN, BLUE
  197.         isValid, r, g, b = stringToRGB(HOME_GOALS_RGB)
  198.         brushGoalsHome = SolidBrush(ColorDrawing.FromArgb(255, r, g, b)) #alpha, RED, GREEN, BLUE
  199.         isValid, r, g, b = stringToRGB(HOME_MISS_RGB)
  200.         brushMissHome = SolidBrush(ColorDrawing.FromArgb(255, r, g, b)) #alpha, RED, GREEN, BLUE
  201.        
  202.         isValid, r, g, b = stringToRGB(AWAY_SHOTS_RGB)
  203.         brushShotsAway = SolidBrush(ColorDrawing.FromArgb(255, r, g, b)) #alpha, RED, GREEN, BLUE
  204.         isValid, r, g, b = stringToRGB(AWAY_GOALS_RGB)
  205.         brushGoalsAway = SolidBrush(ColorDrawing.FromArgb(255, r, g, b)) #alpha, RED, GREEN, BLUE
  206.         isValid, r, g, b = stringToRGB(AWAY_MISS_RGB)
  207.         brushMissAway = SolidBrush(ColorDrawing.FromArgb(255, r, g, b)) #alpha, RED, GREEN, BLUE
  208.        
  209.         img = Image.FromFile(originalFieldPath)
  210.         bmcImg = Bitmap(width, height)
  211.         gh = Graphics.FromImage(bmcImg)
  212.  
  213.         gh.InterpolationMode = InterpolationMode.Bilinear
  214.         gh.PixelOffsetMode = PixelOffsetMode.Half
  215.         gh.SmoothingMode = SmoothingMode.AntiAlias
  216.        
  217.         hGoals = 0
  218.         aGoals = 0
  219.         hTarget= 0
  220.         aTarget= 0
  221.         hBlocked= 0
  222.         aBlocked = 0
  223.        
  224.         gh.DrawImage(img, Rectangle(0, 0, width, height)) #draw origienal field image
  225.         #qualifier 82 to validate shotsOnTarget
  226.         for e in homeExpectedGoalsEvents:
  227.                 has82Qualifier  = next((q for q in e.qualifiers if q.QualifierId == 82), None)
  228.                 shapeSize = Scale(e.value, 1, 0, 100, 50)
  229.                 paint = False
  230.                 if e.eventType == "Goal":
  231.                         hGoals +=1
  232.                         hTarget +=1
  233.                         homeBrush = brushGoalsHome
  234.                         paint = True
  235.                 elif e.eventType == "Miss":
  236.                         hBlocked +=1
  237.                         homeBrush = brushMissHome
  238.                         paint = True
  239.                 else:
  240.                         if has82Qualifier:
  241.                                 hTarget +=1
  242.                                 homeBrush = brushShotsHome
  243.                                 paint = True
  244.                         else:
  245.                                 hBlocked +=1
  246.                                 homeBrush = brushMissHome
  247.                                 paint = True
  248.                 if paint:              
  249.                         gh.FillEllipse(homeBrush, (e.posX * width)/102, abs(height - (e.posY * height)/95), shapeSize, shapeSize) #brush, x, y, width, height
  250.                
  251.         for e in homeExpectedGoalsEvents:
  252.                 shapeSize = Scale(e.value, 1, 0, 100, 50)
  253.                 if e.eventType == "Goal":
  254.                         homeBrush = brushGoalsHome
  255.                         gh.FillEllipse(homeBrush, (e.posX * width)/102, abs(height - (e.posY * height)/95), shapeSize, shapeSize) #brush, x, y, width, height
  256.  
  257.         for e in awayExpectedGoalsEvents:
  258.                 has82Qualifier  = next((q for q in e.qualifiers if q.QualifierId == 82), None)
  259.                 shapeSize = Scale(e.value, 1, 0, 100, 50)
  260.                 if e.eventType == "Goal":
  261.                         aGoals +=1
  262.                         awayBrush = brushGoalsAway
  263.                         aTarget +=1
  264.                 elif e.eventType == "Miss":
  265.                         aBlocked +=1
  266.                         awayBrush = brushMissAway
  267.                         gh.FillEllipse(awayBrush, abs(width - (e.posX * width)/96), (e.posY * height)/110, shapeSize, shapeSize)
  268.                 else:
  269.                         if has82Qualifier:
  270.                                 aTarget +=1
  271.                                 awayBrush = brushShotsAway
  272.                                 gh.FillEllipse(awayBrush, abs(width - (e.posX * width)/96), (e.posY * height)/110, shapeSize, shapeSize) #brush, x, y, width, height
  273.                         else:  
  274.                                 aBlocked +=1
  275.                                 awayBrush = brushMissAway
  276.                                 gh.FillEllipse(awayBrush, abs(width - (e.posX * width)/96), (e.posY * height)/110, shapeSize, shapeSize)
  277.         for e in awayExpectedGoalsEvents:
  278.                 shapeSize = Scale(e.value, 1, 0, 100, 50)
  279.                 if e.eventType == "Goal":
  280.                         awayBrush = brushGoalsAway
  281.                         gh.FillEllipse(awayBrush, abs(width - (e.posX * width)/96), (e.posY * height)/110, shapeSize, shapeSize) #brush, x, y, width, height
  282.        
  283.         imageName = "field_%s.png" %Guid.NewGuid()
  284.        
  285.         for path in enginesRootPath:
  286.                 bmcImg.Save(Path.Combine(path, imageName), ImageFormat.Png)
  287.        
  288.         return imageName,hGoals,aGoals,hTarget,aTarget,hBlocked,aBlocked
  289.  
  290. def Scale(value, maxValue, minValue, maxScale, minScale):
  291.         scaledValue = ((value - minValue) / (maxValue - minValue)) * (maxScale - minScale) + minScale
  292.         return scaledValue
  293.  
  294.  
  295. def stringToRGB(string):
  296.         if not string: return False, 0, 0, 0
  297.  
  298.         strSplitted = string.split(",")
  299.         if len(strSplitted) != 3:
  300.                 return False, 0, 0, 0
  301.        
  302.         r = int(strSplitted[0]) if strSplitted[0].isdigit() else -1
  303.         g = int(strSplitted[1]) if strSplitted[1].isdigit() else -1
  304.         b = int(strSplitted[2]) if strSplitted[2].isdigit() else -1
  305.  
  306.         if r < 0 or r > 255:
  307.                 return False, 0, 0, 0
  308.         elif g < 0 or g > 255:
  309.                 return False, 0, 0, 0
  310.         elif b < 0 or b > 255:
  311.                 return False, 0, 0, 0
  312.         else:
  313.                 return True, r, g, b
  314.  
  315.  
  316.  
  317. class ExpectedGoalsEvent(object):
  318.         def __init__(self, timeMin, timeSec, playerName, eventType,qualifiers, value, posX, posY):
  319.                 self.time = timeMin.ToString().zfill(2) + ":" + timeSec.ToString().zfill(2)
  320.                 self.playerName = playerName
  321.                 self.qualifiers = qualifiers
  322.                
  323.                 self.eventType = ""
  324.                 if eventType == 16:
  325.                         self.eventType = "Goal"
  326.                 elif eventType == 15:
  327.                         self.eventType = "Saved"
  328.                 elif eventType == 14:
  329.                         self.eventType = "Post"
  330.                 else:
  331.                         self.eventType = "Miss"
  332.  
  333.                 self.value = value
  334.                 self.posX = posX
  335.                 self.posY = posY
  336.  

Reply to "Untitled"

Here you can reply to the paste above