view event/oasis2017/quiz/quiz.rb @ 3944:e45173af0101 default tip master

chenge game link
author KOMATSU Kotaro <c118089@roy.e.koeki-u.ac.jp>
date Mon, 20 Jan 2020 18:13:48 +0900
parents 443436e1759b
children
line wrap: on
line source

#!/usr/bin/env ruby22
# -*- coding: utf-8 -*-
#
# 残る課題 [cssの調整] [他ゲームとのデータベースファイルの連系]
# ユーザ名をクリアしたいときは http:/..../quiz.rb?reset

require 'cgi'
require 'csv'
c = CGI.new(:accept_charset => 'utf-8')

require 'sqlite3'
file = "../db/point.sq3"

# roy{c115080}% sqlite3 point.sq3              [~/public_html/mycgi/skip/quiz/db]
# SQLite version 3.8.8.2 2015-01-30 14:30:45
# Enter ".help" for usage hints.
# sqlite> .sch
# CREATE TABLE users(id text primary key, name text, creation text);
# CREATE TABLE point(id, keyword text, FOREIGN KEY(id) REFERENCES users(id));
# sqlite> select * from users;
# 1501044365/65675|unko|2017-07-26 14:43:14 +0900
# sqlite> select * from point;
# 1501044365/65675|もっけ
# sqlite> select * from users NATURAL INNER JOIN point;
# 1501044365/65675|unko|2017-07-26 14:43:14 +0900|もっけ
# 1501044365/65675|unko|2017-07-26 14:43:14 +0900|はっこ
# 1501044365/65675|unko|2017-07-26 14:43:14 +0900|
# 1501044365/65675|unko|2017-07-26 14:43:14 +0900|qa1
# 1501044365/65675|unko|2017-07-26 14:43:14 +0900|qa2
# 1501044365/65675|unko|2017-07-26 14:43:14 +0900|qa3

myname = File.basename($0)

#cookie関係
cookie_id=c.cookies["id"][0]

form_name	=c["playname"]  # 名前入力
form_mode	=c["mode"]	# 問題種別
form_qn		=c["qn"]	# 問題番号
form_rd		=c["rd"]	# 解答番号

if cookie_id == nil
  newid = Time.now.to_i.to_s + "/" + rand(99999).to_s
  id = newid
else
  id = cookie_id
end

# idをすぐcookieで相手ブラウザに送る
# 変数の期限を24時間に設定し、expire変数に入れる
expire = (Time.now+24*3600).gmtime.strftime("%a, %d %b %Y %H:%M:%S GMT")
printf("Content-type: text/html; charset=UTF-8\n")
printf("Set-Cookie: id=%s; expires=%s\n\n", id, expire)

db = SQLite3::Database.new(file)
db.execute("PRAGMA foreign_keys=on")
db.execute("CREATE TABLE IF NOT EXISTS " +
           "users(id text primary key, name text, creation text)")
db.execute("CREATE TABLE IF NOT EXISTS " +
           "point(id, keyword text, FOREIGN KEY(id) REFERENCES users(id), " +
           "UNIQUE(id,keyword))")

################################################################## ユーザ登録
playname = form_name
if form_name > ""		# フォームでnameが送られたならそれ優先
  now = Time.now.strftime("%F %T")
  db.execute("REPLACE INTO users VALUES(?, ?, ?)", id, form_name, now);
else				# フォームでnameなければdbから
  dbn = db.execute("SELECT name from users WHERE id=?", id)[0]
  if dbn != nil &&  dbn[0]
    playname = dbn[0]		# データベースからのの名前をプレイ名とする
  end
end
if playname == "" || ARGV[0] == "reset"
  print(<<-EOF)
<!DOCTYPE html>
<html lang="ja">
<head><title>登録</title>
<link rel="stylesheet" type="text/css" href="quizpg.css" >
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
        <form action="#{myname}" method="POST">
<h1 class="eleg">庄内クイズへようこそ!</h1>
<p class=\"big\">プレイヤー名を入れてください</p><br>
<input type="text" name="playname">
	<input type="submit" value="送信">
	<input type="reset" value="リセット">
	</form>
        <p>ゲームを進める上での注意</p>
        <ol><li>画像を押すと大きくなりますが、通信量を消費するのでご了承ください。動画再生も同じく。</li>
<li>3問正解したら他のQRコードを探しに行こう。</li></ol>
	
</body></html>
	EOF
  exit
end
printf("<h1><a class=\"name\">%s</a>さんのチャレンジ!</h1>\n", playname)
################################################################## 初回処理

number = c["rd"]
def outinput(value, label)
  printf("<label><input type=\"radio\" name=\"rd\" value=\"%s\">%s</label>", value, label)
end

def imgsrc(img)
  printf("<a href=\"%s.jpg\"\><img src=\"%s_mini.jpg\" class=\"shadow\"></a>", img, img)
end

## 問題情報の読み込み
question = {}
n_question = 0
CSV.foreach("quiz.csv", encoding: 'utf-8') do |row|
  mode = row.shift			# 先頭 = モード
  next if /^#/ =~ mode			# 見出し行はスキップ
  if question[mode] == nil
    question[mode] = {}
  end
  qn = row.shift			# 問題番号
  question[mode][qn] = row		# {1 => [tama, 玉簾, ....]}
  n_question += 1			# 全部で何問あるか
end

puts("<!DOCTYPE html>\n<html lang=\"ja\">")

if form_mode > ""
  quiz_mode = form_mode
else
  quiz_mode = ARGV[0] || "kantan"	# kantan, futsu, muzukashi
end
if not /kantan|futsu|muzukashi/ =~ quiz_mode then
  print(<<-EOF)
	<head><title>ERROR</title></head><body>
	<h1>無効なコードです</h1></body></html>
	EOF
  exit 0
end

level = {"kantan" => "簡単", "futsu"=>"普通", "muzukashi"=>"難しい"}[quiz_mode]

print(<<-EOF)
<!DOCTYPE html>
<html lang="ja">
<head><title>#{level}</title>
<link rel="stylesheet" type="text/css" href="quizpg.css" >
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>

EOF

print"<html><head><title>クイズ</title></head><body>\n"

print"<form method=\"POST\" action=\"./#{myname}\">\n"

q = question[quiz_mode]			# 3種のうちどれか

if form_qn == nil || form_qn == ""	# 問題番号未指定なら問1
  form_qn = "1"
end
ansinfo = q[form_qn]			# 今の問の選択肢と解答配列
# ↑で [hiyori,舞鶴公園,日和山公園,飯森山公園,2,skipseikai1.mp4] 各々文字列
image, s1, s2, s3, ans, movie = *ansinfo

if number == ans then
  print "<h2 class=\"underline\">正解!!</h2>"
  db.execute("REPLACE INTO point VALUES(?, ?)",
             id, "quiz-" + quiz_mode + "-" + form_qn)	# ex. quiz-kantan-1
  printf(<<-EOF, movie, quiz_mode, 1+form_qn.to_i)
        <video controls autoplay width="320" height="240">
	 <source src="%s">
	</video>
	<p><input type="hidden" name="mode" value="%s">
	<input type="hidden" name="qn" value="%d"></p>
	EOF
  # form_qn を1ふやして hidden に仕込む
  if form_qn.to_i < q.length		# まだ次の問がある
    puts('<p><input type="submit" value="次の問題へ進む"></p>')
  else					# 用意した問すべて終了
   # puts("<p>おめでとう!</p>")
    n_right = db.execute("SELECT count(*) FROM point WHERE id=?", id)[0][0]
    printf(<<-EOF, n_question, n_right.to_i)
         <div class="box5">  
         <p>おめでとう!</p>
         <p>全%d問中 <a class="name">%d問</a>正解しました。</p></div>
	EOF
    if n_equestion == n_right.to_i
      puts("<p>全問クリア達成!")
    end
  end
else
  printf("<h1 class=\"eleg\">第%d問</h1>\n", form_qn)
  if /\d/ =~ number
    puts("<h2 class=\"underline\">はずれ!もう一度!</h2>")
  end
  printf("<p>")
  imgsrc(image)
  printf("</p>")
  n=0
  for i in [s1, s2, s3] do
    outinput((n+=1).to_s, i)			# <input name="rd" ....>
    puts("<br>")
  end
  printf(<<-EOF, quiz_mode, form_qn)
	<input type="hidden" name="mode" value="%s">
	<input type="hidden" name="qn" value="%s">
	<p><input type="submit" value="送信">
	<input type="reset" value="リセット"></p>
	EOF
end
print"</form>\n"
print "</body>\n"
print "</html>\n"

yatex.org