Mercurial > hgrepos > hgweb.cgi > skipweb
changeset 1714:62a1b89cb04a
add ikusei.rb
author | OOTANI Hiroyuki <c115036@g.koeki-u.ac.jp> |
---|---|
date | Fri, 28 Jul 2017 16:06:58 +0900 |
parents | fa04b5c5475e |
children | 2e2b34951586 |
files | event/oasis2017/ikusei/ikusei.rb |
diffstat | 1 files changed, 191 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/event/oasis2017/ikusei/ikusei.rb Fri Jul 28 16:06:58 2017 +0900 @@ -0,0 +1,191 @@ +#!/usr/bin/env ruby +# -*- 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" +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) + <form action="#{myname}" method="POST"> + <p>プレイ名を入れてください: + <input type="text" name="playname"> + <input type="submit" value="送信"> + <input type="reset" value="リセット"> + </form> + EOF + exit +end +printf("<h1>%sさんのチャレンジ</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.png\"\><img src=\"%s.png\"></a>", img, img) +end + +## 問題情報の読み込み +question = {} +n_question = 0 +CSV.foreach("ikusei.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] || "kayaku" # kantan, futsu, muzukashi +end +if not /kayaku|me|daizu|tori|neko/ =~ quiz_mode then + print(<<-EOF) + <head><title>ERROR</title></head><body> + <h1>無効なコードです</h1></body></html> + EOF + exit 0 +end + +level = {"kayaku" => "火薬", "me"=>"芽", "daizu"=>"大豆", "tamago"=>"鳥", "neko"=>"ねこ"}[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] 各々文字列 +mondai,image, s1, s2, s3, ans, movie = *ansinfo + + +###############動画(音) +if number == ans then + print "<h2>正解!!</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) +<audio controls autoplay loop false preload=\"auto\"> +<source src=\"%s\"> +</audio> + + <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) + <p>おめでとう!</p> + <p>全%d問中 %d問正解しました。</p> + EOF + if n_question == n_right.to_i + puts("<p>全問クリア達成!") + end + end +else + printf("<h1>第%d問</h1>\n", form_qn) + if /\d/ =~ number + puts("<h2>残念! もう一度!</h2>") + end +printf("<h1>%s</h1>\n",mondai ) + 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"