--[[
飺
    ״̬ʾűýűɼӲʾʼϷͼδɼɵң
ɼӲʾ뿪Ϸҡ

ע⣺ 1.˽űҪڿʼϷǰҳܷʹá
       2. 鿪ʼϷǰűʱ䡢˳ϷϢ¼¼ڵԴڡ
ԴĬǹرյġԽopendebugwindow()ǰעͷ'--'ȥнűʾԴڡ

:
[2010621]
1. ˼¼IPϢ,ڵԴڻ־ļ

[2010513]
1. ˵Դڼ¼ʼϷʱ䡣ÿһϷΪһ¼
   Ϸʼٿű򲻼¼ʼϷϢ
2. ԶǷʾԴڡ
3. ˶d3dͼش,״̬ʱ䵽200ms
4. ûԶǷ¼־ļܡĬϲ¼־ļ
־ļWar3Player_Note.log־λָĿ¼

                                       ߣҹ
				       ԣѩMg
                                       			]]


if getwshscriptversion() < 8 then
    msgbox('ʹø߰汾')
end

--û޸Ĵ
--opendebugwindow()	--ǷʾԴڣȥopendebugwindow()עͷ'--'нűʾԴ
isWriteLog = true	--Ƿ¼־ļ true/false
--û޸Ĵ

idWsh = 0
ridWsh = 0
idSet = 0
ridSet = 0
iInitState = 0
iGamestatus = 0
player = {}
leavedPlayer = {}
iLeaved = 0
iSetTimel = 10000
iSetTimes = 3000
iplaying = 0
iLoadedState = 1
iPlayerNum = 0

function isOneLoadedMap()

    if 1==isplaying() and iGamestatus>1 then
	iplaying = 2
	return false
    end
    if iplaying == 2 and 0==isplaying() then 
        iplaying = 3
	return false
    end
    if 0==isplaying() and iGamestatus==2 then
	iplaying = 1
        for i = 0,15,1 do
            playerID, playerIP, playerPort, playerName, playerState = getplayerinfo(i)
            if playerState==256 then
                return true
            end  
        end
    else
	iplaying = 4
    end
    return false
end

function isPlayerOnline(oldPlayerID)
    for i = 0,15,1 do
        playerID, playerIP, playerPort, playerName, playerState = getplayerinfo(i)
        if oldPlayerID==playerID and playerName~='' then
            return true
        end  
    end
    return false
end

function writeLog(content)
    local file = io.open(getwshpath().."War3Player_Note.log", "a+");
    if(file) then
	file:write(content)
    end
    io.close(file)
end

function myd3dsettext(nShowstate,strText,nAlpha)
    war3w = getwar3window()
    win3wl,win3wr,win3wt,win3wb = getclientrect(war3w)
    x=(win3wr-win3wl)*0.05
    y=(win3wb-win3wt)*0.69
    if((win3wr-win3wl)<790) then
        iFontW=5
        iFontH=10
    elseif((win3wr-win3wl)>=790 and (win3wr-win3wl)<1010) then
        iFontW=6
        iFontH=12
    else
        iFontW=7
        iFontH=14
    end
    if nShowstate==0 then 
        idWsh, ridWsh = d3dsettext(idWsh, ridWsh,'֡', iSetTimel, x, y, "", 0x0000ff, nAlpha, iFontW, 0, 500, 0, 0, 0,0,2000)    
        idSet, ridSet = d3dsettext(idSet, ridSet,strText, iSetTimel, (iFontH+2)*4+x, y, "", 0xffffff, nAlpha, iFontW, 0, 500, 0, 0, 0,0,2000)
    elseif nShowstate==1 then
        idWsh, ridWsh = d3dsettext(idWsh, ridWsh,'֡',iSetTimes, x, y, "", 0x0000ff, nAlpha, iFontW, 0, 500, 0, 0, 0,0,2000)    
        idSet, ridSet = d3dsettext(idSet, ridSet,strText, iSetTimes, (iFontH+2)*4+x, y, "", 0xffffff, nAlpha, iFontW, 0, 500, 0, 0, 0,0,2000)
    else 
        idWsh, ridWsh = d3dsettext(idWsh, ridWsh,'֡',0, x, y, "", 0x0000ff, nAlpha, iFontW, iFontH, 500, 0, 0, 0,0,2000)    
        idSet, ridSet = d3dsettext(idSet, ridSet,strText, 0, (iFontH+2)*4+x, y, "", 0xffffff, nAlpha, iFontW, 0, 500, 0, 0, 0,0,2000)
    end
end

function monitorPlayerfunc()
    iGamestatus = getgamestatus()
    if isOneLoadedMap() then
        playerNotLoaded = ''
        playerLoaded = ''
        for i = 0,15,1 do
            playerID, playerIP, playerPort, playerName, playerState = getplayerinfo(i)
            if playerState == 256 and playerName~='' then
                if playerLoaded ~= '' then playerLoaded = playerLoaded .. '' end
                playerLoaded = playerLoaded .. playerName
            elseif  playerName~='' then
                if playerNotLoaded ~= '' then playerNotLoaded = playerNotLoaded .. '' end
                playerNotLoaded = playerNotLoaded .. playerName
            end
        end
	strText =''
	if playerLoaded ~= '' then
	    strText = strText..''..playerLoaded ..'ͼ\n'
	end
	if playerNotLoaded~='' then 
	    strText = strText..''..playerNotLoaded ..'ͼδ'
	end
	if strText ~='' then 
            myd3dsettext(1,strText,255)
	end
    end
    if iGamestatus > 1 and iInitState == 0 then
	hostName = ''
	hostIP = ''
        iMinPlayerId = 16
        for i = 0,15,1 do
            playerID, playerIP, playerPort, playerName, playerState = getplayerinfo(i)
            if playerID~=nil and playerName ~= '' then
		if playerID<iMinPlayerId then
		    iMinPlayerId = playerID
		    hostName = playerName
		    hostIP = playerIP
		end
                player[iPlayerNum] = {}
                player[iPlayerNum][0] = playerID
                player[iPlayerNum][1] = playerIP
                player[iPlayerNum][2] = playerPort
                player[iPlayerNum][3] = playerName
                player[iPlayerNum][4] = playerState
                iPlayerNum = iPlayerNum+1
            end
        end
	if iGamestatus == 2 and hostName ~= '' then
	    strText = '\n'..os.date("%Y-%m-%d %X",os.time{year=1970, month=1, day=1, hour=0})..'  ['..hostName..'] IP['..hostIP ..']ʼϷ\n'
	    trace(strText)
	    if isWriteLog then
		writeLog(strText)
	    end
	elseif iGamestatus ~= 2 then
	    trace('\n')
	end
        iInitState = 1
    elseif iGamestatus < 2 then
        iInitState = 0
    end
    if iplaying==2 then
	if iLoadedState == 1 then 
	    myd3dsettext(2,' ',255)
	    iLoadedState = 0
	end
        if iPlayerNum>0 and table.getn(player)>0 then
            for i=0, table.getn(player), 1 do
                if player[i][3]~=nil and player[i][3]~='' and not isPlayerOnline(player[i][0]) then
                    isInLeaved = false
		    if iLeaved>0 then
		        for j=0, iLeaved-1, 1 do
			    if leavedPlayer[j]~=nil and player[i][0] == leavedPlayer[j][0] then
			        isInLeaved = true
			    end
		        end
		    end
                    if not isInLeaved and player[i][3]~=''  then
                        strText = os.date("%Y-%m-%d %X",os.time{year=1970, month=1, day=1, hour=0})..'  ['..player[i][3]..'] IP['..player[i][1] ..']Ѿ뿪Ϸ\n'
			trace(strText)
			if isWriteLog then
			    writeLog(strText)
	    		end
			leavedPlayer[iLeaved] = {}
			leavedPlayer[iLeaved] = player[i]
			leavedPlayer[iLeaved][5] = gettickcount()
			iLeaved = iLeaved +1
		    end
                end
            end
	    strLeavedPlayer = ''
	    if iLeaved>0 then
		for i=0, iLeaved-1, 1 do
			if gettickcount()-leavedPlayer[i][5]<iSetTimel then
			   if strLeavedPlayer ~= '' then strLeavedPlayer = strLeavedPlayer .. '' end
			    strLeavedPlayer = strLeavedPlayer .. leavedPlayer[i][3]
			end
		end
	    end
            if strLeavedPlayer~=''  then
                strText = '['..strLeavedPlayer..']Ѿ뿪Ϸ'
                myd3dsettext(0,strText,255)
            end
        end
    elseif iplaying == 3 then
	myd3dsettext(2,' ',255)
    elseif iplaying == 4 then
	iplaying = 0 --end
        player = {}
        leavedPlayer = {}
        iLeaved = 0
	iPlayerNum = 0
	iLoadedState = 1
    end
end

tmMonitor = settimer(200, 'monitorPlayerfunc')