<!--#include file="Class_PetStatus.asp"-->
<% 
Private OwnerPetLevel,OwnerPetExp,OwnerPetHp,OwnerPetAtk,OwnerPetDef,OwnerOpenPK,OwnerPKTime,OwnerPetDead
Private OpponentPetLevel,OpponentPetExp,OpponentPetHp,OpponentPetAtk,OpponentPetDef,OpponentPetDead,OpponentPetOpenPK,OpponentPetMaxHp
Private AtkName,Atk1,Atk2,Exp1,Exp2,AtkTemp1,DefTemp1,DefTemp2,AtkTemp2

Class PetPKClass

Public Sub FormatPetPKData
Set ShowPetStatus=New PetStatus
with ShowPetStatus
.LetUserName=CookieUserName
.FormatPetData
OwnerPetLevel=.OwnerPetLevel
OwnerPetExp=  .OwnerPetExp
OwnerPetHp=   .OwnerPetHp
OwnerPetAtk=  .OwnerPetAtk
OwnerPetDef=  .OwnerPetDef
OwnerOpenPK=  .OwnerOpenPK
OwnerPKTime=  .OwnerPKTime
OwnerPetDead= .OwnerPetDead

end with
set ShowPetStatus=nothing

Atkname = request.Form("Atkname")
set RsPet2=ConnPet.execute("SELECT * FROM [PetData] WHERE username='"& Atkname &"'")
if RsPet2.eof then
error("他（她）没有宠物，不能PK啊~")
else

OpponentPetExp=RsPet2("PetExp")
OpponentPetLevel=int(OpponentPetExp/100)+1
OpponentPetHp=RsPet2("PetHp")
OpponentPetAtk=RsPet2("PetAtk")
OpponentPetDef=RsPet2("PetDef")
OpponentPetDead=RsPet2("PetDead")
OpponentPetOpenPK=RsPet2("OpenPK")
OpponentPetMaxHp=RsPet2("PetMaxHp")
OpponentItem1=RsPet2("Item1")
OpponentItem2=RsPet2("Item2")
OpponentItem3=RsPet2("Item3")
OpponentItem4=RsPet2("Item4")
OpponentItem5=RsPet2("Item5")
OpponentPetAtk=OpponentPetAtk+OpponentItem1+OpponentItem2+2+int(OpponentPetLevel/2)
OpponentPetDef=OpponentPetDef+OpponentItem3+OpponentItem4+OpponentItem5+3+int(OpponentPetLevel/2)
End If

End Sub

Public Sub GoPK


	'初始化判断
	if OwnerPetDead = 1 then
           error("你的宝宝好象已经挂了， 救活它再PK吧。")
        end if
     
        PKTimeGap=DateDiff("s",OwnerPkTime, now())
	
            if(CookieUserName = Atkname) then
             error("干吗啊，自己打自己！")
            elseif (OpponentPetHp<Def_PET_PetPkHp or OpponentPetDead = 1) then
                 error("他（她）的宠物已经不行了，不能PK啊~")
            elseif (OpponentPetOpenPK=0) then
                 error("他（她）不接受PK~")
            elseif (OwnerOpenPK=0) then
                 error("你关闭了PK系统，所以不能PK~")
            elseif(PKTimeGap<Def_Pet_PetPKTime) then
                PKTimeGapLeft=Def_Pet_PetPKTime-PKTimeGap
            	 error("不能连续攻击，请"& PKTimeGapLeft &"秒后再攻击")
            else
                
                LevelGap=abs(OwnerPetLevel-OpponentPetLevel)
				
                if(LevelGap>Def_Pet_PetPKlv) then
                    error("你们两个级别相差太大，不能PK.")
            end if
		
		
	 '开始计算PK数据
		
                level1=int(OwnerPetlevel/2)
                level2=int(OpponentPetlevel/2)
                
                
'攻击力1				
AtkTemp1=OwnerPetAtk
'防御力1
DefTemp1=OwnerPetDef
'攻击力2
AtkTemp2=OpponentPetAtk
'防御力2
DefTemp2=OpponentPetDef

Atk1=AtkTemp1-DefTemp2
if Atk1<0 then Atk1=1 End if
Atk2=AtkTemp2-DefTemp1
if Atk2<0 then Atk2=1 End if

Randomize '初始化随机数生成器。
Exp1=Atk1
Exp2=Atk2
if Exp1>10 then Exp1=Int((10 * Rnd) + 1) end if
if Exp2>10 then Exp2=Int((10 * Rnd) + 1) end if

test=0
if test=1 then
response.write "攻击力1："&Atktemp1&"<br>"
response.write "防御力1："&Deftemp1&"<br>"
response.write "攻击力2："&Atktemp2&"<br>"
response.write "防御力2："&Deftemp2&"<br>"
response.Write "HP1："&OwnerPetHp&"<br>"
response.Write "OwnerPetDead:"&OwnerPetDead&"<br>"
end if
               
OpponentPetHp=OpponentPetHp-Atk1
If OpponentPetHp<1 then 
OpponentPetHp=0
OpponentPetDead=1
End if		
OpponentPetLevel=int((OpponentPetExp+Exp2)/100)+1
		

OwnerPetHp=OwnerPetHp-Atk2
If OwnerPetHp<1 Then
OwnerPetHp=0
OwnerPetDead=1
End if	
   



				
Set PetSql=New SQLString
With PetSql
.TableName="PetData"
.SQLType=1
.where="UserName='"&Atkname&"'"
.AddField "PetLevel",OpponentPetLevel
.AddField "PetExp",OpponentPetExp+Exp2
.AddField "PetHp",OpponentPetHp
.AddField "PetDead",OpponentPetDead
ConnPet.Execute(.ReturnSQL)
.clear()
.TableName="PetData"
.SQLType=1
.where="UserName='"&CookieUserName&"'"
.AddField "PetExp",OwnerPetExp+Exp1
.AddField "PetHp",OwnerPetHp
.AddField "PetDead",OwnerPetDead
.AddField "PkTime",Now()
ConnPet.Execute(.ReturnSQL)

'发送攻击短消息
if Def_Pet_sendbattlemsg = 1 then 
.clear()
.TableName="BBSXP_Messages"
.SQLType=0
.AddField "UserName",CookieUserName
.AddField "Incept",Atkname
.AddField "Content","【宠物PK消息】：你的宝宝受到"&CookieUserName&"的宝宝"&Atk1&"点攻击，你增加了 "&Exp2&" 点经验值，你的宝宝进行了反击，使他(她)的宠物受到 "&Atk2&" 点攻击，他(她)的宠物增加了 "&Exp1&"点经验值"
Conn.Execute(.ReturnSQL)
.clear()
.TableName="BBSXP_Users"
.SQLType=1
.Where="UserName='"&Atkname&"'"
.AddField "NewMessage",NewMessage+1
Conn.Execute(.ReturnSQL)
End if

message="<li>你的宝宝给了 "&Atkname&" 的宝宝 "&Atk1&" 点攻击，<br>你增加了 "&Exp1&" 点经验值，他（她）宝宝进行了反击，<br>使你受到 "&Atk2&" 点攻击，<br>他（她）增加了 "&Exp2&" 点经验值！"
succeed(message&"<meta http-equiv=refresh content=3;url='PetPK.asp'")

End With
Set PetSql=Nothing

end if
End Sub

End class 
%>