changeset 3506:2e3a09602fbe

henka
author KASHIWAGURA Aya <c115046@?.koeki-u.ac.jp>
date Sat, 29 Jul 2017 22:14:13 +0900
parents 6d4687efeb72
children 07016687e668
files event/oasis2017/idol/white_1.rb event/oasis2017/idol/white_2.rb event/oasis2017/idol/white_3.rb
diffstat 3 files changed, 160 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/event/oasis2017/idol/white_1.rb	Sat Jul 29 21:55:22 2017 +0900
+++ b/event/oasis2017/idol/white_1.rb	Sat Jul 29 22:14:13 2017 +0900
@@ -6,6 +6,57 @@
 require 'cgi'
 c = CGI.new(:accept_charset => 'utf-8')
 
+
+
+
+
+require 'sqlite3'
+file = "../db/point.sq3"
+
+# 前回のアクセスで既にcookieが定義されていたら取得
+# ブラウザからのcookieは、c.cookies[変数名][0] で受け取る
+cookie_id=c.cookies["id"][0]
+
+# 前回のアクセスで入力された値を取得
+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 = 0 # 開始直後は0点
+# SELECTの検索結果に値があるかは [0] が nil かどうかで判定できる
+
+name = db.execute("SELECT name FROM users WHERE id=?", id)[0]
+
+
 page = Hash.new
 n = 0
 img = ""
@@ -85,7 +136,7 @@
   puts('<input type="submit" value="次へ">')
 
 end
-
+db.close
 puts "</form>
 </body>
 </html>"
--- a/event/oasis2017/idol/white_2.rb	Sat Jul 29 21:55:22 2017 +0900
+++ b/event/oasis2017/idol/white_2.rb	Sat Jul 29 22:14:13 2017 +0900
@@ -6,12 +6,63 @@
 require 'cgi'
 c = CGI.new(:accept_charset => 'utf-8')
 
+
+
+
+
+require 'sqlite3'
+file = "../db/point.sq3"
+
+# 前回のアクセスで既にcookieが定義されていたら取得
+# ブラウザからのcookieは、c.cookies[変数名][0] で受け取る
+cookie_id=c.cookies["id"][0]
+
+# 前回のアクセスで入力された値を取得
+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 = 0 # 開始直後は0点
+# SELECTの検索結果に値があるかは [0] が nil かどうかで判定できる
+
+name = db.execute("SELECT name FROM users WHERE id=?", id)[0]
+
+
 page = Hash.new
 n = 0
 img = ""
 
 
-open("red1a.txt","r:utf-8") do |cat|
+open("white1.txt","r:utf-8") do |cat|
 
   while line = cat.gets
     if /(\d+)\s+(\S+)\s+/ =~line
@@ -65,7 +116,7 @@
  
 when "6"
  print(<<-"EOF")
-<a href="">NEXT STAGE・・・</a>
+<a href="home.rb">NEXT STAGE・・・</a>
 
 
 
@@ -85,7 +136,7 @@
   puts('<input type="submit" value="次へ">')
 
 end
-
+db.close
 puts "</form>
 </body>
 </html>"
--- a/event/oasis2017/idol/white_3.rb	Sat Jul 29 21:55:22 2017 +0900
+++ b/event/oasis2017/idol/white_3.rb	Sat Jul 29 22:14:13 2017 +0900
@@ -6,12 +6,63 @@
 require 'cgi'
 c = CGI.new(:accept_charset => 'utf-8')
 
+
+
+
+
+require 'sqlite3'
+file = "../db/point.sq3"
+
+# 前回のアクセスで既にcookieが定義されていたら取得
+# ブラウザからのcookieは、c.cookies[変数名][0] で受け取る
+cookie_id=c.cookies["id"][0]
+
+# 前回のアクセスで入力された値を取得
+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 = 0 # 開始直後は0点
+# SELECTの検索結果に値があるかは [0] が nil かどうかで判定できる
+
+name = db.execute("SELECT name FROM users WHERE id=?", id)[0]
+
+
 page = Hash.new
 n = 0
 img = ""
 
 
-open("red1a.txt","r:utf-8") do |cat|
+open("white1.txt","r:utf-8") do |cat|
 
   while line = cat.gets
     if /(\d+)\s+(\S+)\s+/ =~line
@@ -65,7 +116,7 @@
  
 when "6"
  print(<<-"EOF")
-<a href="">NEXT STAGE・・・</a>
+<a href="home.rb">NEXT STAGE・・・</a>
 
 
 
@@ -85,7 +136,7 @@
   puts('<input type="submit" value="次へ">')
 
 end
-
+db.close
 puts "</form>
 </body>
 </html>"

yatex.org