#ifndef YDWEReplayHelperIncluded
#define YDWEReplayHelperIncluded

library YDWEReplayWriter initializer Init
	globals
		private player curplayer = null
		private gamecache gc
	endglobals
	private function IsLivingPlayer takes player p returns boolean
		return (GetPlayerSlotState(p) == PLAYER_SLOT_STATE_PLAYING) /*
		*/ and (GetPlayerController(p) == MAP_CONTROL_USER)
	endfunction
	private function GetLivingfPlayer takes nothing returns player
		local integer i = 0
		loop
			exitwhen IsLivingPlayer(Player(i)) or i >= 11
			set i = i + 1
		endloop
		return Player(i)
	endfunction
	function YDWEWriteToReplay takes string table, string key, integer data returns nothing
		if not IsLivingPlayer(curplayer) then
			set curplayer = GetLivingfPlayer()
		endif
		call StoreInteger(gc, table, key, data)
		if GetLocalPlayer() == curplayer then
			call SyncStoredInteger(gc, table, key)
		endif
	endfunction
	function YDWEReadFromReplay takes string table, string key returns integer
		return GetStoredInteger(gc, table, key)
	endfunction
	private function Init takes nothing returns nothing
		call FlushGameCache(InitGameCache("11.x"))
		set gc = InitGameCache("11.x")
	endfunction
endlibrary

library YDWEReplayHelper requires YDWEReplayWriter
	// ---------------------------------------------------
	// 游戏结束时，设置获得胜利的一方的队伍ID。
	// 建议队伍1的ID为0，队伍2的ID为1。
	// ---------------------------------------------------
	function YDWERHSetGameWinner takes integer WinnerId returns nothing
        call YDWEWriteToReplay("Global", "Winner", WinnerId+1)
	endfunction
	// ---------------------------------------------------
	// 游戏开始时，设置游戏起始点。
	// 建议出兵时调用一次。
	// ---------------------------------------------------
	function YDWERHSetGameStart takes nothing returns nothing
        call YDWEWriteToReplay("Data", "GameStart", 1)
	endfunction
	// ---------------------------------------------------
	// 游戏结束时，设置游戏时长。
	// ---------------------------------------------------
	function YDWERHSetGameTime takes integer m, integer s returns nothing
        call YDWEWriteToReplay("Global", "m", m)
        call YDWEWriteToReplay("Global", "s", s)
	endfunction
//------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------
	// 基地受伤时，设置基地当前血量的百分比。
	// 建议在75%、50%、25%、10%时分别设置一次。
	// ---------------------------------------------------
	function YDWERHSetBaseTowerLift takes integer TeamId, integer value returns nothing
		if TeamId == 0 then
			call YDWEWriteToReplay("Data", "Tree", value)
		elseif TeamId == 1 then
			call YDWEWriteToReplay("Data", "Throne", value)
		else
			call YDWEWriteToReplay("Data", "Fort"+I2S(TeamId), value)
		endif
	endfunction
//------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------
	// 塔被推倒时，设置推倒塔的玩家的ID和推倒的塔的信息。
	//   TowerTeam  塔的队伍ID，建议队伍1的ID为0，队伍2的ID为1。
	//   TowerLevel 塔的等级，建议外塔为1，中塔为2，高地塔为3，主家边上的塔为4。
	//   TowerSide  塔的位置，建议上路塔为0，中路塔为1，下路塔为2，主家边上的塔为1(中路)。
	// ---------------------------------------------------
	function YDWERHSetTowerDeathEvent takes integer PlayerId, integer TowerTeam, integer TowerLevel, integer TowerSide returns nothing
        call YDWEWriteToReplay("Data", "Tower"+I2S(TowerTeam)+I2S(TowerLevel)+I2S(TowerSide), PlayerId)
	endfunction
	// ---------------------------------------------------
	// 英雄升级时，设置升级的玩家的ID。
	// ---------------------------------------------------
	function YDWERHSetLevel takes integer PlayerId, integer level returns nothing
        call YDWEWriteToReplay("Data", "Level"+I2S(level), PlayerId)
	endfunction
	// ---------------------------------------------------
	// 英雄死亡时，设置死亡的玩家的ID和杀人的玩家的ID。
	// ---------------------------------------------------
	function YDWERHSetKillEvent takes integer DeaderId, integer KillerId returns nothing
        call YDWEWriteToReplay("Data", "Hero"+I2S(DeaderId), KillerId)
	endfunction
	// ---------------------------------------------------
	// 英雄死亡时，设置死亡的玩家的ID和助攻的玩家的ID。
	// ---------------------------------------------------
	function YDWERHSetAssistEvent takes integer DeaderId, integer AssisterId returns nothing
        call YDWEWriteToReplay("Data", "Assist"+I2S(AssisterId), DeaderId)
	endfunction
//------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------
	// 英雄获得物品时，设置获得的物品。
	// ---------------------------------------------------
	function YDWERHAddItem takes integer PlayerId, integer ItemId returns nothing
        call YDWEWriteToReplay("Data", "PUI_"+I2S(PlayerId), ItemId)
	endfunction
	// ---------------------------------------------------
	// 英雄失去物品时，设置失去的物品。
	// ---------------------------------------------------
	function YDWERHSubItem takes integer PlayerId, integer ItemId returns nothing
        call YDWEWriteToReplay("Data", "DRI_"+I2S(PlayerId), ItemId)
	endfunction
	// ---------------------------------------------------
	// 游戏结束时，设置英雄身上的物品。
	// 建议index为0~5。
	// ---------------------------------------------------
	function YDWERHSetHeroItem takes integer PlayerId, integer index, integer ItemId returns nothing
		call YDWEWriteToReplay(I2S(PlayerId), "8_"+I2S(index), ItemId)
	endfunction
//------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------
	// 游戏结束时，设置玩家当前金钱。
	// ---------------------------------------------------
	function YDWERHSetGold takes integer PlayerId, integer value returns nothing
		call YDWEWriteToReplay(I2S(PlayerId), "6", value)
	endfunction
//------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------
	// 游戏结束时，设置玩家的杀人数。
	// ---------------------------------------------------
	function YDWERHSetKill takes integer PlayerId, integer value returns nothing
        call YDWEWriteToReplay(I2S(PlayerId), "1", value)
	endfunction
	// ---------------------------------------------------
	// 游戏结束时，设置玩家的死亡数。
	// ---------------------------------------------------
	function YDWERHSetDead takes integer PlayerId, integer value returns nothing
        call YDWEWriteToReplay(I2S(PlayerId), "2", value)
	endfunction
	// ---------------------------------------------------
	// 游戏结束时，设置玩家的助攻数。
	// ---------------------------------------------------
	function YDWERHSetAssist takes integer PlayerId, integer value returns nothing
        call YDWEWriteToReplay(I2S(PlayerId), "5", value)
	endfunction
//------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------
	// (任何时候)，设置玩家的正补数。
	// 建议每210秒设置一次。
	// ---------------------------------------------------
	function YDWERHSetCreepKills takes integer PlayerId, integer value returns nothing
        call YDWEWriteToReplay("Data", "CSK"+I2S(PlayerId), value)
	endfunction
	// ---------------------------------------------------
	// (任何时候)，设置玩家的反补数。
	// 建议每210秒设置一次。
	// ---------------------------------------------------
	function YDWERHSetCreepDenies takes integer PlayerId, integer value returns nothing
        call YDWEWriteToReplay("Data", "CSD"+I2S(PlayerId), value)
	endfunction
	// ---------------------------------------------------
	// (任何时候)，设置玩家的打野数。
	// 建议每210秒设置一次。
	// ---------------------------------------------------
	function YDWERHSetNeutralKills takes integer PlayerId, integer value returns nothing
        call YDWEWriteToReplay("Data", "NK"+I2S(PlayerId), value)
	endfunction

	// ---------------------------------------------------
	// 玩家离开时，设置玩家的正补、反补、打野数。
	// ---------------------------------------------------
	function YDWERHSetKillUnitInPlayerLeave takes integer PlayerId, integer k, integer d, integer n returns nothing
        call YDWEWriteToReplay("Data", "CK"+I2S(k)+"D"+I2S(d)+"N"+I2S(n), PlayerId)
	endfunction
	// ---------------------------------------------------
	// 游戏结束时，设置玩家的正补、反补、打野数。
	// ---------------------------------------------------
	function YDWERHSetKillUnitInGameEnd takes integer PlayerId, integer k, integer d, integer n returns nothing
        call YDWEWriteToReplay(I2S(PlayerId), "3", k)
        call YDWEWriteToReplay(I2S(PlayerId), "4", d)
        call YDWEWriteToReplay(I2S(PlayerId), "7", n)
	endfunction
//------------------------------------------------------------------------------------------------------
	// ---------------------------------------------------
	//  游戏结束和玩家选择英雄(或更改选择)时，设置玩家的所选的英雄。
	// ---------------------------------------------------
	function YDWERHSetHeroId takes integer PlayerId, integer HeroId returns nothing
        call YDWEWriteToReplay(I2S(PlayerId), "9", HeroId)
	endfunction
	// ---------------------------------------------------
	// 游戏结束和玩家更改队伍时，设置玩家的所在的位置(sp模式)。
	// 建议队伍1的玩家依次为1~5，队伍2的玩家依次为6~10。
	// ---------------------------------------------------
	function YDWERHSetColorId takes integer PlayerId, integer ColorId returns nothing
        call YDWEWriteToReplay(I2S(PlayerId), "id", ColorId)
	endfunction
endlibrary

#endif
