GMod E2 - PlayX Controller

@name PlayX Controller
@inputs PlayX:wirelink
@outputs
@persist URI:string Length RequestList:array VideoInProgress MinimumNextDelay
@trigger

#RunFlags
runOnChat(1)

#Startup
if(first()|dupefinished())
{
    #Default the vars
    Video = ""
    Length = 0
    RequestList = array()
    VideoInProgress = 0
   
    #Settings
    MinimumNextDelay = 5000
}

#When player is detected, set the defaults
if(~PlayX & PlayX)
{
    PlayX["DisableJW",number] = 1
    print("[PLAYX] - Player Detected.")
}

#Chat Control
if(chatClk())
{
    #Chat Parsing
    LastSaid = lastSaid()
    Command = LastSaid:explode(": "):string(1):lower()
    Arg = LastSaid:explode(": "):string(2)
   
    #Play Video
    if(Command == "play")
    {
        if(VideoInProgress)
        {
            lastSpoke():plyPrint("[PLAYX] - Video already playing, please wait until it is over to start a video.")
        }
        else
        {
            URI = Arg
            VideoInProgress = 1
           
            PlayX["URI",string] = URI
            timer("Open Video",100)
        }
    }
   
    #Make a request
    if(Command == "request")
    {
        #Check if it's at least a URL
        if(Arg:lower():find("http://"))
        {
            RequestList:insertString(1,Arg)
           
            #Notify
            findByClass("player")
            Ply = findToArray()
            for(Index = 1, Ply:count())
            {
                Ply[Index,entity]:plyPrint("[PLAYX] - " + lastSpoke():name() + " requested " + Arg)
            }
           
            #Start this guy if not playing anything
            if(!VideoInProgress)
            {
                timer("Next Video",1000)
            }
        }
        else
        {
            lastSpoke():plyPrint("[PLAYX] - Invalid link!")
        }
    }
   
    #Force Stop
    if(Command == "stop video" & lastSpoke() == owner())
    {
        stoptimer("Video End")
        stoptimer("Next Video")
        VideoInProgress = 0
        PlayX["Close",number] = 1
        timer("Unclose",50)
    }
   
    #Play Next in Queue
    if(Command == "play next" & (lastSpoke():isAdmin()|lastSpoke() == owner()))
    {
        stoptimer("Video End")
        stoptimer("Next Video")
        VideoInProgress = 1
        timer("Next Video",1000)
    }
   
    #Remove the entire playlist
    if(Command == "purge queue" & lastSpoke() == owner())
    {
        RequestList = array()
    }
   
    #Request playlist
    if(Command == "view playlist")
    {
        AntiCount = RequestList:count()
        lastSpoke():plyPrint("[PLAYX] - Displaying current request list...")
        for(Index = 1, RequestList:count())
        {
            lastSpoke():plyPrint(AntiCount + ". " + RequestList[Index,string])
            AntiCount--
        }
    }
}

#Reset "Close" state
if(clk("Unclose"))
{
    PlayX["Close",number] = 0
}

#Open Inputted URL
if(clk("Open Video"))
{
    PlayX["Open",number] = 1
    timer("Get Video Info",1000)
}

#Check for video data
if(clk("Get Video Info"))
{
    PlayX["Open",number] = 0
    #Check for Error
    if(PlayX["InputError",string])
    {
        Error = PlayX["InputError",string]:lower()
       
        if(Error == "no provider was auto-detected")
        {
            print("PLAYX ERROR: Invalid URL: " + URI)
            timer("Next Video",1000)
        }
       
        if(Error == "cvar playx_wire_input is not 1")
        {
            print("PLAYX ERROR: Wire Input not allowed on Server, have an admin turn this on.")
        }
        VideoInProgress = 0
    }
    else
    {
        #Check if it's a valid video
        if(PlayX["Title",string] == "")
        {
            print("PLAYX ERROR: Video Cannot Load.")
            timer("Next Video",1000)
        }
        else
        {
            #Tell everyone a video has started
            findByClass("player")
            Ply = findToArray()
            for(Index = 1, Ply:count())
            {
                Ply[Index,entity]:plyPrint("[PLAYX] - Now Playing: " + PlayX["Title",string])
            }
           
            timer("Video End",PlayX["Length",number]*1000)
        }
    }
}

#A video ended, do stuff
if(clk("Video End"))
{
    timer("Next Video",MinimumNextDelay)
}

#Play the next video in the list
if(clk("Next Video"))
{
    VideoInProgress = 1
   
    if(RequestList:count())
    {
        Request = RequestList:popString()
        PlayX["URI",string] = Request
        timer("Open Video",100)
    }
    else
    {
        VideoInProgress = 0
    }
}

No comments:

Post a Comment