asp如何在聊天室实现趣味答题并计分功能?
时间:2010-06-18 20:00:00
如何在聊天室实现趣味答题并计分功能?
这个创意确实很好,我们可用在聊天室框架中加入一隐含帧(5分钟刷新一次)的做法来实现这一功能。
question.asp
' 隐含帧内的页面
<%@ language=vbscript%>
<%response.expires=0%>
<%
dim conn,rs
application.lock
application("canswername")=application("answername")
' 保存答对题聊友的昵称
application("answername")=""
application("tanswername")=""
' 清除答错题聊友昵称
set conn=server.createobject("adodb.connection")
accessdb="db1.mdb"
' 连接题库
mydsn="driver={microsoft access driver (*.mdb)};"
mydsn=mydsn & "dbq=" &server.mappath(accessdb)
conn.open mydsn
set rs = server.createobject("adodb.recordset")
rs.cursorlocation=3
rs.open "select * from question ",conn
randomize
' 随机取得一道题的id
r=rs.recordcount
id=int((r-1)*rnd)+1
rs.close
set rs=nothing
set rs = server.createobject("adodb.recordset")
rs.open "select * from question where questionid="&id,conn
application("questionname")=rs("questionname")
' 问题
application("questionid")=rs("questionid")
' 题的id
application("questioncent")=rs("questioncent")
' 题的分数
application("questionclass")=rs("questionnotes")
' 题的说明
rs.close
set rs = nothing
set ra = server.createobject("adodb.recordset")
ra.open "select * from answer where questionid="&id,conn
application("answera")=ra("answera")
' 题的选项
application("answerb")=ra("answerb")
application("answerc")=ra("answerc")
application("answerd")=ra("answerd")
application("answertrue")=ra("answertrue")
' 题的正确答案
ra.close
set ra=nothing
conn.close
set conn=nothing
application("questiontime")=now()
application.unlock
%>
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=gb2312'>
<link rel='stylesheet' href='../style.css' type='text/css'>
<meta http-equiv="refresh" content="300;url=question.asp">
<title><%=application("v")%></title>
<script language="javascript">
parent.showsays.document.write("asp之家聊天室系统消息:<br>");
' 向聊天室显示对话窗口写入问题
parent.showsays.document.write("恭喜,上题答对者是:<%=application"canswername")%>朋友!<br>");
parent.showsays.document.write("现在的趣味问题是:<%=application("questionname")%>");
parent.showsays.document.write(" <%=application("questioncent")%>分(请在1分钟之内回答,请输入'#答案')<%=time()%><br>");
parent.showsays.document.write("a::<%=application("answera")%><br>");
parent.showsays.document.write("b::<%=application("answerb")%><br>");
parent.showsays.document.write("c::<%=application("answerc")%><br>");
parent.showsays.document.write("d::<%=application("answerd")%><br>");
</script>
</head>
<body bgcolor='#ffffff'>
</body>
</html>
answer.asp
' 答案处理部分.位于处理发送对话信息的程序中
if mid(fstr,i,1)="#" then
' fstr是保存对话信息的字符串变量
tfstr=mid(fstr,i+1,1)
if cint(datediff("n",application("questiontime"),now()))<=1 then
' 判断1分钟内答题有效
if lcase(tfstr)=lcase(application("answertrue")) then
' 判断答题是否正确
if instr(1,application("answername"),session("user_username"))>0 then
' 判断该聊友是否已答过题
tfstr=session("user_username")&"噢,您已经答过这道题了!"
else
tfstr=session("user_username")&"恭喜,回答正确!"
session("user_exp") =session("user_exp")+application("questioncent")
' 给答题正确的聊友加分
application("answername")=application("answername")&session("user_username")&" "
' 保存答题正确的聊友的昵称
end if
else
if instr(1,application("tanswername"),session("user_username"))>0 then
' 判断聊友是否已答过题
tfstr=session("user_username")&"噢,您已经答过这道题!"
else
application("tanswername")=application("tanswername")&session("user_username")&" "
' 保存答错题聊友的昵称
end if
end if
end if