@name Data Server[By Danus]
@inputs C:wirelink
@outputs
@persist Stage Name:string Text:string Log:array
@model models/props_lab/servers.mdl


if(first())
{
    function void logS(S:string,Mes:string,E:entity)
    {
        if(Mes == "server")
        {
            Log:pushString("[Got]"+S+"[From ID "+E:id()+"]")
            C:writeString(Log[Log:count(),string],0,Log:count(),1000)
        }
        elseif(Mes == "connection")
        {
            Log:pushString("[Connected ID]"+E:id())
            C:writeString(Log[Log:count(),string],0,Log:count(),1000)
        }
        elseif(Mes == "!server")
        {
            Log:pushString("[Connected]to "+Name)
            C:writeString(Log[Log:count(),string],0,Log:count(),1000)
        }
        elseif(Mes == "sendto")
        {
            local Chat = S:explode(",")
            Log:pushString("[Request][From ID "+E:id()+"] to "+Chat[1,string])
            C:writeString(Log[Log:count(),string],0,Log:count(),1000)
            Log:pushString("[Sending]....")
            C:writeString(Log[Log:count(),string],0,Log:count(),1000)
            dsSend("mes",Name,E:id():toString()+","+Chat[1,string]+","+Chat[2,string])
        }
        
        if(Log:count() > 14)
        {
            Log:shift()
            for(I = 2,13)
            {
                C:writeString(Log[I,string],0,I,1000)
            }
        }
    }
    
    function void resetM(S,E)
    {
        for(I = S,E)
        {
            C:writeCell(2039,I)
        }
    
    }
    runOnChat(1)
    runOnTick(1)

    Log:pushString("")
    dsSetScope(0)
    entity():setAng(ang(0,180,0))
    setName("Server Name[OFFLINE]")
    C:writeCell(2041,1)
    C:writeString("Danusian Communication System",0,0,1000)
    C:writeString("=========Server[]=============",0,1,1000)

}

if(chatClk(owner()))
{
    local Chat = owner():lastSaid():explode(" ")
        
    if(Chat[1,string] == "!server" & dsGetGroups():count() == 0)
    {
        hideChat(1)
        dsJoinGroup(Chat[2,string])
        Name = Chat[2,string]
        print("Server ["+Chat[2,string]+"] was created!")
        setName("Server Name["+Name+"]")
        C:writeString("=========Server["+Name+"]=============",0,1,1000)
        logS(Name,"!server",noentity())
        
    }
    elseif(Chat[1,string] == "!reset" & dsGetGroups():count() > 0) 
    {
        hideChat(1)
        dsSend("reset",Name,"reset")
        reset()
    }
}

if(dsClk())
{
    if(dsGetGroup() == Name)
    {
        logS(dsGetString(),dsClkName(),dsGetSender())     
    }
}
