Mercurial > hgrepos > hgweb.cgi > skipweb
changeset 3382:d81aba10f003
syusei
author | KASHIWAGURA Aya <c115046@?.koeki-u.ac.jp> |
---|---|
date | Wed, 26 Jul 2017 19:39:11 +0900 |
parents | 71eb998829a5 |
children | d67dd93548e4 |
files | event/oasis2017/idol/home.rb |
diffstat | 1 files changed, 97 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/event/oasis2017/idol/home.rb Wed Jul 26 19:14:22 2017 +0900 +++ b/event/oasis2017/idol/home.rb Wed Jul 26 19:39:11 2017 +0900 @@ -3,6 +3,9 @@ # tail -f /usr/local/apache2/logs/error_log +require 'sqlite3' +file = "db/uesrs.sq3" + require 'cgi' c = CGI.new(:accept_charset => 'utf-8') @@ -10,29 +13,112 @@ image = "" -#def img() - # printf("<img src=\"%s\">\n",image) -#end +# 前回のアクセスで既にcookieが定義されていたら取得 +# ブラウザからのcookieは、c.cookies[変数名][0] で受け取る +cookie_id=c.cookies["id"][0] -if gin =="" - gin = "red" +# 前回のアクセスで入力された値を取得 +form_name=c["playname"] # nameをフォームから取得(もしあれば) +form_kwd=c["keyword"] # keywordも取得(もしあれば) +form_clr=c["clear"] # クリアボタン + +if cookie_id == nil # (1)もしcookieからのidがなければ新規生成 + # 新規idは、現在時刻を秒(to_i)にした文字列(to_s)と乱数の組み合わせ + newid = Time.now.to_i.to_s + "/" + rand(99999).to_s + id=newid # 新規idをidとする +else # (2)cookieからのidがあればそれをidとする + 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))") + +if form_clr > "" && id > ''# クリアボタンが押されていたら + db.execute("DELETE FROM point WHERE id=?", id) +end +if form_name > "" # formからの名前設定がもしあれば + db.execute("REPLACE INTO users VALUES(?, ?, ?)", id, form_name, Time.now.to_s) end -pt = ARGV[0].to_i #ライブを重ねてたまったポイント +pt = 0 # 開始直後は0点 +# SELECTの検索結果に値があるかは [0] が nil かどうかで判定できる + +name = db.execute("SELECT name FROM users WHERE id=?", id)[0] + +print(<<-EOF) + + +<!DOCTYPE html> +<html lang="ja"> +<head><title>アイキス、しよ?</title> +<link rel="stylesheet" type="text/css" href="ema3.css" > +<meta name="viewport" content="width=device-width"> +</head> +<body> + +EOF + +if !name # もしユーザのプレイ名が未設定なら + title = "アイドルを育成しよう!" + hello = "プレイ名を入力してね" + input = '<input name="playname">' + +print('<form action="name.rb" method="POST">') +printf(<<-EOF, -puts "Content-type: text/html; charset=utf-8 +<h1>%s</h1> +<p>%s</p> + +<p>%s<br> +<label><input type="checkbox" name="clear">クリアする</label><br> +<input type="submit" value="送信"> +<input type="reset" value="リセット"> +<input type="hidden" name="playname" value="id" > + +</p> +</form></body></html> +EOF + title, hello, input,) + +elsif #プレイ名入力済みなら + + if gin =="" + gin = "red" + end + + puts "Content-type: text/html; charset=utf-8" + r = db.execute("SELECT count(*) FROM point WHERE id=?", id)[0] + pt = (r ? r[0] : 0) + + print(<<-EOF) <html> -<head><title>アイキス!★ホーム</title>" -printf("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s.css\" >", gin) +<head><title>アイキス!★ホーム</title> +EOF -puts "<meta name=\"viewport\" content=\"width=device-width\"> + printf("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s.css\" >", gin) + puts "<meta name=\"viewport\" content=\"width=device-width\"> </head> <body> <form method=\"POST\" action=\"home.rb\">" + + + + + case gin when "red" print(<<-"EOF") @@ -209,9 +295,9 @@ Episode 3<br> EOF - end end +end