Re: Untitled

From Lousy Curlew, 4 Days ago, written in Plain Text, viewed 4 times. This paste will slip away in 3 Weeks. This paste is a reply to Untitled from Sexy Camel - go back
URL https://paste.afonso.co/view/5b5362d9/diff Embed
Viewing differences between Untitled and Re: Untitled
# -*- encoding: utf-8 -*-
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import *
#
clr.AddReference("System.Drawing")
from System.Drawing import *
#
clr.AddReference("System.Net")
from System.Net import WebRequest, ServicePointManager, SecurityProtocolType
#
clr.AddReference("System.IO")
from System.IO import StreamReader
#
import base64, json, codecs
#
from System.Threading import *
from datetime import datetime, timedelta
from collections import OrderedDict
class API:
        def __init__(self):
                self.data = {}
                self.advantageGame = None
                self.scorebugPoints = ("0", "0", None)
                self.matchId = None
                self.events = []
                self.draw = None
                self.gameId = None
                self.gameIndex = None
                self.selectedIndex = None
                self.matchesFilteredByDraw = None
                self.lastScore = None
                self.lastStats = None
                self.statusConn = None
                self.roundName = ""
                self.points = {
                        "HomeTeam" : "",
                        "AwayTeam" : ""                
                }
                self.sets = {
                        "HomeTeam" : [0,0,0,0,0],
                        "AwayTeam" : [0,0,0,0,0]        
                }
                self.ad = {
                        "HomeTeam" : False,
                        "AwayTeam" : False        
                }
                self.matchFinished = None
                self.oldSet = 0
                self.test = CheckTesting.Checked
                Globals["Rewind"] = False
                self.status = False
                self.manualHomeService = False
                self.manualAwayService = False
                Globals["SponsorOnAnimantion"] = False
                #self.set_all_controls_visible(self)
                
        def Init(self):
                Globals["frmApiCrionet"] = Script.Form.Form
                self.GetDefault()
                self.matchId = None
                self.draw = None
                self.gameId = None
                self.gameIndex = None
                self.selectedIndex = None
                self.matchesFilteredByDraw = None
                self.statusConn = None
                self.roundName = ""
                self.points = {
                        "HomeTeam" : "",
                        "AwayTeam" : ""                
                }
                self.sets = {
                        "HomeTeam" : [0,0,0,0,0],
                        "AwayTeam" : [0,0,0,0,0]
                }
                self.ad = {
                        "HomeTeam" : False,
                        "AwayTeam" : False        
                }
                self.matchFinished = None
                self.oldSet = 0
                status = self.__Login()
                
                if status:
                        return self.__GetScheduledMatches()
                else:
                        return None
                        
                self.manualHomeService = False
                self.manualAwayService = False
                
        def set_all_controls_visible(self, parent):
                for control in Script.Form.Form.Controls:
                        control.Visible = True
                        #if control.Controls.Count > 0:
                                #self.set_all_controls_visible(control)
                
        def GetDefault(self):
                self.advantageGame = None
                Globals["OnlineAPIAdvantageCount"] = 0
                Globals["OnlineAPIAdvantageUpdate"] = True
                self.scorebugPoints = ("0", "0", None)
GetDefault(self):                                                
                        self.CheckStatusOnline()
                        if int(self.data["Results"]["CurrentSet"]) > int(self.oldSet):
                                Project.ExecuteScript("Online_API", "SetFinished")
                                self.oldSet = self.data["Results"]["CurrentSet"]
                                return
                                
                        
                        
                        Globals["PadelScoreboard"].UpdateInterface()
                        self.__UpdateData()
                        
                        
                        
                except Exception as e:
                        import os
                        import sys
                        e_type, e_object, e_traceback = sys.exc_info()
                        e_filename = os.path.split(
                                e_traceback.tb_frame.f_code.co_filename
                        )[1]
                        e_message = str(e)
                        e_line_number = e_traceback.tb_lineno
                        print('exception line number: %s'%e_line_number)
                        print('exception message: %s' %e_message)
                        
        def __UpdateData(self):
                #print "Update data"
                try:
                        frm = Script.Form.Form
                        teams = ["Home", "Away"]
                        
                        for i in range(2):
                                if len(self.data["Results"][teams[i] + "Team"]["Players"]) > 0:
                                        frm.Controls["lbl" + teams[i] + "Team"].Text = self.data["Results"][teams[i] + "Team"]["Players"][0]["lastName"][:3].upper() + "/" + self.data["Results"][teams[i] + "Team"]["Players"][1]["lastName"][:3].upper()
                                else:
                                        frm.Controls["lbl" + teams[i] + "Team"].Text = "-"                        
                                        
                                frm.Controls["lbl" + teams[i] + "Point"].Text = self.data["Results"][teams[i] + "Team"]["CurrentGame"]
                                for x in range(5):
                                        frm.Controls["lbl" + teams[i] + "Set" + str(x+1).zfill(2)].Text = str(self.data["Results"][teams[i] + "Team"]["Sets"][x])
                                        frm.Controls["lbl" + teams[i] + "Tie" + str(x+1).zfill(2)].Text = str(self.data["Results"][teams[i] + "Team"]["TieBreaks"][x]) if self.data["Results"][teams[i] + "Team"]["TieBreaks"][x] and self.data["Results"][teams[i] + "Team"]["TieBreaks"][x] != -1 else "-"
                                                
                                        #frm.Controls["lbl" + teams[i] + "GoldPoint"].Text = str(self.data["Results"][teams[i] + "Team"]["Golden Points Converted"]) + "/" + str(self.data["Results"][teams[i] + "Team"]["Golden Points Total"])
                                        #frm.Controls["lbl" + teams[i] + "BreakPoint"].Text = str(self.data["Results"][teams[i] + "Team"]["Break Points Converted"]) + "/" + str(self.data["Results"][teams[i] + "Team"]["Break Points Total"])
                                        #frm.Controls["lbl" + teams[i] + "UnforcedError"].Text = "0"
                                        #frm.Controls["lbl" + teams[i] + "Winner"].Text = "0"
                                        #frm.Controls["lbl" + teams[i] + "Smash"].Text = "0"
                                        
                                        #if self.manualHomeService:
                                                #frm.Controls["HomeService"].Checked = True
                                        
                                        #if self.manualAwayService:
                                                #frm.Controls["AwayService"].Checked = True
                                        
                                        if self.manualHomeService == False and self.manualAwayService == False:
                                                        pass
                                        if self.data["Results"][teams[i] + "Team"]["Service"] == 0:
                                                frm.Controls["lbl" + teams[i] + "Team"].ForeColor = Color.WhiteSmoke
                                                #frm.Controls[teams[i] + "Service"].Checked = False
                                        else:
                                                frm.Controls["lbl" + teams[i] + "Team"].ForeColor = Color.SpringGreen
                                                #frm.Controls[teams[i] + "Service"].Checked = True
                        self.fillColorSetWinner()
                        self.CheckStatusOnline()
                except Exception as e:
                        print "Error on __UpdateData: %s" % e
                        
        def fillColorSetWinner(self):
                frm = Script.Form.Form
                for i in range(3):
                        valueHome = int(frm.Controls["lblHomeSet%02d"%(i+1)].Text)
                        valueAway = int(frm.Controls["lblAwaySet%02d"%(i+1)].Text)
                        if valueHome > valueAway:
                                #print "Home Maior"
                                frm.Controls["lblHomeSet%02d"%(i+1)].ForeColor = Color.SpringGreen
                        elif valueAway > valueHome:
                                #print "Away Maior"
                                frm.Controls["lblAwaySet%02d"%(i+1)].ForeColor = Color.SpringGreen
                        else:
                                #print "Os Dois Iguais"
                                frm.Controls["lblHomeSet%02d"%(i+1)].ForeColor = Color.WhiteSmoke
                                frm.Controls["lblAwaySet%02d"%(i+1)].ForeColor = Color.WhiteSmoke
                                        
        def UpdateInfo(self, error = ""):
                #print "UpdateInfo"
                if self.matchId and self.draw and self.selectedIndex:
                        btnReload.Enabled = True
                else:
                        btnReload.Enabled = False
                        
                msg = ""
                if self.test:
                        msg += ("TESTING.... " + "\n")
                msg += ("Status API: " + str(self.statusConn) + "\n")
                if self.matchId:        
                        msg += ("Competition ID: " + str(self.matchId) + "\n")
                
                if self.draw:
                        msg += ("Draw: " + str(self.draw) + "\n")
                
                if self.gameId:
                        msg += ("Game ID: " + str(self.gameId) + "\n")
                        msg += ("Match Finished: " + str(self.matchFinished) + "\n")
                        
                msg += ("Last Score Update: " + str(self.lastScore) + "\n")
                
                curSet = int(self.data["Results"]["CurrentSet"])-1
                setIsFinish = self.data["Results"]["Sets Finished"][curSet]
                msg += ("Set(%s) Finish: %s " % (str(curSet + 1),setIsFinish )+ "\n")
                
                if not error == "":
                        msg += ("Error: " + error + "\n\n")
                        
                lblInfo.Text = msg
                
        def clearAll(self):
                #print "Limpando tudo!"
                cboDraws.Text = ""
                cboDraws.Items.Clear()
                cboGameDate.Text = ""
                cboGameDate.Items.Clear()
                cboDrawsMatchs.Text = ""
                cboDrawsMatchs.Items.Clear()
                self.matchId = None
                self.draw = None
                self.gameId = None
                self.gameIndex = None
                self.selectedIndex = None
                self.matchesFilteredByDraw = None
                self.manualHomeService = False
                self.manualAwayService = False
                HomeService.Checked = False
                AwayService.Checked = False
                Globals["API"].LastSum2Sets = 0
                
        def clearRefresh(self):
                self.gameId = None
                self.matchesFilteredByDraw = None
                
        def __SaveJSON(self, data, name):
                time4json = datetime.today().strftime('%Y%m%d_%H%M%S%f')
                output_file = codecs.open(Project.AddonsDataPath + "\\API\\MATCHDATA\\" + "%s_%s.json" % (name,time4json), "w", encoding="utf-8")
                json.dump(data, output_file, sort_keys=False, indent=4, ensure_ascii=False)
                
        def createHomePoints(self):
                pointsIdx = {"0":"15","15":"30","30":"40","40":""}
                try:
                        points = self.data["Results"]["HomeTeam"]["CurrentGame"]
                except:
                        points = "0"
                
                newPoints = pointsIdx[points]
                self.data["Results"]["HomeTeam"]["CurrentGame"] = newPoints
                self.CheckStatusOnline()
                
        def createAwayPoints(self):
                pointsIdx = {"0":"15","15":"30","30":"40","40":""}
                try:
                        points = self.data["Results"]["AwayTeam"]["CurrentGame"]
                except:
                        points = "0"
                newPoints = pointsIdx[points]
                self.data["Results"]["AwayTeam"]["CurrentGame"] = newPoints
                self.CheckStatusOnline()
                
        def CheckStatusOnline(self):
                try:
                        results = self.data["Results"]
                        currentSet = int(results["CurrentSet"])
                        home = results["HomeTeam"]
                        away = results["AwayTeam"]
                        homePoints = home["CurrentGame"].upper()
                        awayPoints = away["CurrentGame"].upper()
                        homeGames = int(home["Sets"][currentSet - 1])
                        awayGames = int(away["Sets"][currentSet - 1])

                        game = (self.gameId, currentSet, homeGames, awayGames)
                        finished = self.data["Match Finished"] or results["Sets Finished"][currentSet - 1]
                        if game != self.advantageGame or finished or (homePoints, awayPoints) == ("0", "0"):
                                self.advantageGame = game
                                Globals["OnlineAPIAdvantageCount"] = 0
                                Globals["OnlineAPIAdvantageUpdate"] = True

                        if finished:
                                self.scorebugPoints = ("", "", None)
                                return

                        isTieBreak = self.data["TieBreak"] or (homeGames, awayGames) == (6, 6)
                        isDeuce 
data["TieBreak"]
                        currentSet 
= not isTieBreak and (homePoints, awayPoints) == ("40", "40")
                        advantageTeam 
int(self.data["Results"]["CurrentSet"])
                        homeCurrentGame 
= None
                        if not isTieBreak:
                                if homePoints == "AD":
                                        advantageTeam 
self.data["Results"]["HomeTeam"]["CurrentGame"]
                        awayCurrentGame 
= "HomeTeam"
                                elif awayPoints == "AD":
                                        advantageTeam 
self.data["Results"]["AwayTeam"]["CurrentGame"]
                        homeCurrentSet 
= "AwayTeam"

                        advantageCount 
int(self.data["Results"]["HomeTeam"]["Sets"][currentSet-1])
                        awayCurrentSet 
= Globals["OnlineAPIAdvantageCount"]
                        if isDeuce:
                                Globals["OnlineAPIAdvantageUpdate"] = True
                        elif advantageTeam and Globals["OnlineAPIAdvantageUpdate"]:
                                advantageCount = min(advantageCount + 1, 2)
                                Globals["OnlineAPIAdvantageCount"] = advantageCount
                                Globals["OnlineAPIAdvantageUpdate"] = False
                        isStarPoint = isDeuce and advantageCount == 2

int(self.data["Results"]["AwayTeam"]["Sets"][currentSet-1])
                        
                        if isTieBreak:
                                homeTiePoints homeCurrentTieBreak = int(homePoints or "0")
                                awayTiePoints 
int(homeCurrentGame)
                                awayCurrentTieBreak 
= int(awayPoints or "0")
                                homeSetPoint = homeTiePoints >= 6 and homeTiePoints > awayTiePoints
                                awaySetPoint = awayTiePoints >= 6 and awayTiePoints > homeTiePoints
int(awayCurrentGame)
                        else:
                                homeGamePoint homeCurrentTieBreak = advantageTeam == "HomeTeam" or isStarPoint or (
                                        homePoints == "40" and awayPoints not in ("40", "AD"))
                                awayGamePoint 
0
                                awayCurrentTieBreak 
= advantageTeam == "AwayTeam" or isStarPoint or (
                                        awayPoints == "40" and homePoints not in ("40", "AD"))
                                homeSetPoint 
0
                        
                        homeScore 
= homeGames >= 5 and homeGames > awayGames and homeGamePoint
                                awaySetPoint 
0
                        awayScore 
= awayGames >= 5 and awayGames > homeGames and awayGamePoint

                        pointTitle = None
                        if homeSetPoint or awaySetPoint:
                                leadingTeam, otherTeam = (home, away) if homeSetPoint else (away, home)
                                setsWon = sum(1 
0
                        
for i t in range(self.data["Sets"])
                                        
data["Sets"]):
                                
if results["Sets Finished"][i] and leadingTeam["Sets"][i] > otherTeam["Sets"][i])
                                if setsWon == 
self.data["Sets"] // 2:
                                        pointTitle = "Tournament Point" 
data["Results"]["Sets Finished"][t]:
                                        
if str(self.self.data["Results"]["HomeTeam"]["Sets"][t] > self.data["Results"]["AwayTeam"]["Sets"][t]:
                                                homeScore += 1
                                        else:
                                                awayScore += 1
                        
                        # Checking SetPoint
                        matchPoint = False
                        setPoint = False 
                        advantagePoint = False
                        tournamentPoint = (str(self.
roundName) == "F" else "F")
                        startPoint = False
                        if homeCurrentSet == 6 and awayCurrentSet == 6:
                                if homeCurrentTieBreak > 5 and homeCurrentTieBreak > awayCurrentTieBreak: #and homeCurrentTieBreak > awayCurrentTieBreak+1                                
                                        setPoint = True
                                        if homeScore == (self.data["Sets"]/2):
                                                matchPoint = True
                                if awayCurrentTieBreak > 5 and awayCurrentTieBreak > homeCurrentTieBreak: #and awayCurrentTieBreak > homeCurrentTieBreak+1                                
                                        setPoint = True
                                        if awayScore == (self.data["Sets"]/2):
                                                matchPoint = True
                        else:
                                if homeCurrentSet > 4 and homeCurrentSet > awayCurrentSet:
                                        if homeCurrentGame == "40" and Globals["OnlineAPIAdvantageCount"] == 2:
                                                setPoint = True
                                                if homeScore == (self.data["Sets"]/2):
                                                        matchPoint = True
                                        elif homeCurrentGame == "Ad":
                                                setPoint = True
                                                if homeScore == (self.data["Sets"]/2):
                                                        matchPoint = True
                                        elif homeCurrentGame == "40" and not awayCurrentGame == "40" and not awayCurrentGame == "Ad":
                                                setPoint = True
                                                if homeScore == (self.data["Sets"]/2):
                                                        matchPoint = True                                
                                elif awayCurrentSet > 4 and awayCurrentSet > homeCurrentSet:
                                        if awayCurrentGame == "40" and Globals["OnlineAPIAdvantageCount"] == 2:
                                                setPoint = True
                                                if awayScore == (self.data["Sets"]/2):
                                                        matchPoint = True
                                        elif awayCurrentGame == "Ad":
                                                setPoint = True
                                                if awayScore == (self.data["Sets"]/2):
                                                        matchPoint = True
                                        elif awayCurrentGame == "40" and not homeCurrentGame == "40" and not homeCurrentGame == "Ad":
                                                setPoint = True
                                                if awayScore == (self.data["Sets"]/2):
                                                        matchPoint = True                
            if homeCurrentGame == 'Ad' or awayCurrentGame == 'Ad':
                if Globals["OnlineAPIAdvantageUpdate"]:
                    Globals["OnlineAPIAdvantageCount"] += 1
                    Globals["OnlineAPIAdvantageUpdate"] = False
                if Globals["OnlineAPIIsDeucesTitleOnAir"]:
                    Globals["OnlineAPIIsDeucesTitleOnAir"] = False
                    Project.ExecuteScript("Online_API", "PopupOut")
            if homeCurrentGame == '40' and awayCurrentGame == '40':
                Globals["OnlineAPIAdvantageUpdate"] = True
                if Globals["OnlineAPIIsAdvantageTitleOnAir"]:
                    Globals["OnlineAPIIsAdvantageTitleOnAir"] = False
                    Project.ExecuteScript("Online_API", "PopupOut")
                if Globals["OnlineAPIAdvantageCount"] == 1:
                    Globals["OnlineAPIIsDeucesTitleOnAir"] = True
                    Project.ExecuteScript("Online_API", "PopupIn", "Deuce 2")
                elif Globals["OnlineAPIAdvantageCount"] == 2:
                    Globals["OnlineAPIIsDeucesTitleOnAir"] = True
                    if matchPoint:
                        if tournamentPoint:
                            Project.ExecuteScript("Online_API", "PopupIn", "Tournament Point Star Point")
                        else:
                            Project.ExecuteScript("Online_API", "PopupIn", 
"Match Point"
Point Star Point")
                    elif setPoint:
                        Project.ExecuteScript("Online_API", "PopupIn", "Set Point Star Point")
                    else:
                        Project.ExecuteScript("Online_API", "PopupIn", "Star Point")
                else:
                    Globals["OnlineAPIIsDeucesTitleOnAir"] = False
                    Project.ExecuteScript("Online_API", "PopupOut")
            elif setPoint and not self.matchFinished:
                if matchPoint and not self.matchFinished:
                    try:
                        if tournamentPoint:
                            Project.ExecuteScript("Online_API", "PopupIn", "Tournament Point")
                        else:
                            Project.ExecuteScript("Online_API", "PopupIn", "Match Point")
                    except Exception as e:
                        print "Form has some errors"
                        print str(e)
                        Core.StatusManager.Add(str(e), 'error')
                else:
                    try:
                        Project.ExecuteScript("Online_API", "PopupIn", "Set Point")
                    except Exception as e:
                        print "Form has some errors"
                        print str(e)
                        Core.StatusManager.Add(str(e), 'error')
                    #btnSetPoint_Click(None, None)
            elif homeCurrentGame == 'Ad' or awayCurrentGame == 'Ad':
                if Globals["OnlineAPIAdvantageCount"] == 1:
                    Globals["OnlineAPIIsAdvantageTitleOnAir"] = True
                    Project.ExecuteScript("Online_API", "PopupIn", "Advantage 1")
                elif Globals["OnlineAPIAdvantageCount"] == 2:
                    Globals["OnlineAPIIsAdvantageTitleOnAir"] = True
                    Project.ExecuteScript("Online_API", "PopupIn", "Advantage 2")
                else:
                    Globals["OnlineAPIIsAdvantageTitleOnAir"] = False
                    Project.ExecuteScript("Online_API", "PopupOut")                        
                        elif homeCurrentGame == '40' or awayCurrentGame == '40':
                                homeService = Globals["API"].data["Results"]["HomeTeam"]["Service"] != 0
                                if (homeCurrentGame == '40' and not homeService) or (awayCurrentGame == '40' and homeService):
                                        Project.ExecuteScript("Online_API", "PopupIn", "Breakpoint")
                                else:
                                        pointTitle = "Set Point"

                        title = pointTitle
                        
Project.ExecuteScript("Online_API", "PopupOut")
                        else:                
                                Project.ExecuteScript("Online_API", "PopupOut")
                        #else:
                        #        
if isStarPoint:
                                homePoints = awayPoints = "SP"
                                title = pointTitle + " Star Point" 
self.data["Golden Point"] == "GOLDEN POINT ALL SETS": # GOLDEN POINT ALL SETS
                        #                
if pointTitle else "Star Point"
                        elif isDeuce and advantageCount == 1:
                                homePoints = awayPoints = "D2"
                                title = "Deuce 2"
                        elif advantageTeam:
                                label = "AD%d" % advantageCount
                                homePoints, awayPoints = (label, "") if advantageTeam == "HomeTeam" else ("", label)
                                if pointTitle is None:
                                        title = "Advantage %d" % advantageCount
                        elif not title and not isTieBreak and not isDeuce:
                                homeService = home["Service"] != 0
                                if (homePoints 
homeCurrentGame == "40" and not homeService) or (awayPoints awayCurrentGame == "40" and homeService):
                                        title = "Breakpoint"

                        self.scorebugPoints = (homePoints, awayPoints, title)
"40":
                        #                        Project.ExecuteScript("Online_API", "GoldPointIn")
                        #                else:
                        #                        Project.ExecuteScript("Online_API", "PopupOut")
                        #                        Project.ExecuteScript("Online_API", "GoldPointOut")
                        #        else:
                        #                Project.ExecuteScript("Online_API", "PopupOut")
                        #                Project.ExecuteScript("Online_API", "GoldPointOut")
                        
                except Exception as e:
                        print "Erro print"Erro CheckStatusOnline: %s" % e


%s"% e
                        
                        
'error')'error')
                

Reply to "Re: Untitled"

Here you can reply to the paste above