#!/usr/bin/env ruby22
# -*- coding: utf-8 -*-
require 'cgi'
require 'sqlite3'
require 'kconv'
c = CGI.new(:tag_maker => "html5", :accept_charest => "UTF-8")
db = SQLite3::Database.new("./sql/main.sq3")

id = c.cookies["id"][0]
person = id.to_i

data = "select * from test where id = ?"
data2 = "select * from test where id = ?"
#idが?(後で代入)のデータをtest票から探す。という命令をdataにしくむ。全てがほしい場合は「*」。「*」を「name」に変えると該当データのnameだけとりだす。
cook = db.execute(data, person)               #dataに仕組んでいた命令を、personを代入しつつ実行。
cook2 = db.execute(data2, person)
jikan1 = "select * from time where cd = ?"
timeupd = "update time set now = ?, id = ? where cd = ?"

que = "select * from toi where id = ?"
queupd = "update toi set q1 = ?, q2 = ?, q3 = ?, q4 = ?, q5 = ? where id = ?"
toi = db.execute(que, person)

bomd = "update test set pt = ? where id = ?"
ins4 = 'insert into time values(?, 61, "n")'
printf("Content-type: text/html; charset=UTF-8\n\n")

begin
name = cook[0][1]
cal = cook[0][2]
age = cook[0][3]
sex = cook[0][4]
aria = cook[0][5]
exp = cook[0][6]
pt = cook[0][7]
#lv = cook[0][8]
rescue
printf(<<_EOS_)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta name="viewport"
content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="format-detection" content="telephone=no">
<link media="only screen and (max-device-width:480px)"
href="smart.css" type="text/css" rel="stylesheet" />
<link media="screen and (min-device-width:481px)" href="profeel.css"
type="text/css" rel="stylesheet" />
<link rel="shortcut icon" href="./img/cabage.png"/>
<title>Miyazac Quest</title>
<style type="text/css">
<!--
-->
</style>
</head>

<body>
<header></header>
<h1>記録が無くなったようだ...</h1><h2>記憶も消してやりなおしたまえ......</h2>
<div class="f1"><a href="index.html"><div class="lets"><span>新規記憶の作成</span></div></a></div>
<div class="f1"><a href="cookie.rb"><div class="lets"><span>作成前に記憶を消す</span></div></a></div>
<address>skip&#x40;xxxx.co.jp</address>
<footer></footer>
</body>
</html>

_EOS_
exit(0)            ##プログラム終了! これがないと以降のプログラムが動いてしまう。
end



q1 = toi[0][1]
q2 = toi[0][2]
q3 = toi[0][3]
q4 = toi[0][4]
q5 = toi[0][5]

per ="%"

##################--ptを獲得するための乱数生成--#############
##################----#############################

def qrc()
  srand(4567845678)
  x = rand(10000000)
  srand(x)
  qrnumber = Hash.new("n")
  qrarr = ["a", "b", "c", "d", "e"]
  qrarr.length.times do |i|
    qrnumber[rand(100000000)] = qrarr[i]
  end
  return qrnumber
end

##################ptを獲得した際の反応。####################
#########前の人が書き込んだ時間と比べて60秒以内なら追加ポイント####
kpt = 0


begin
  jikan2 = db.execute(jikan1, "j")
  now1 = jikan2[0][1]
rescue
  ("a".."j").each{|x| db.execute(ins4,x)}
end


qrAC = qrc            ###生成したハッシュには、"乱数"=>"記号"が入っている
getAC = ARGV[0].to_i  ###URL?以降の文字(特定の乱数)を取得してgetACに代入
qr = qrAC[getAC]      ###特定の"乱数"が一致すると、"乱数"=>"記号"がqrに正しく入力される。
if qr =="a" && q1 == 0
  q1 += 1
  jikan2 = db.execute(jikan1, "a")
  now1 = jikan2[0][1]
  nowper = jikan2[0][2]
  now2 = Time.now.to_i
  db.execute(timeupd, now2, person, "a")
  if person == nowper
  elsif now1 == nil
  elsif now2-now1 < 40
    pt += 5
    kpt += 5
  end
  pt += 7
  kpt += 7
elsif qr == "b" && q2 == 0
  q2 += 1
  jikan2 = db.execute(jikan1, "b")
  now1 = jikan2[0][1]
  nowper = jikan2[0][2]
  now2 = Time.now.to_i
  db.execute(timeupd, now2, person, "b")
  if person == nowper
  elsif now1 == nil
  elsif now2-now1 < 20
    pt += 2
    kpt += 2
  end
  pt += 12
  kpt += 12
elsif qr == "c" && q3 == 0
  q3 += 1
  jikan2 = db.execute(jikan1, "c")
  now1 = jikan2[0][1]
  nowper = jikan2[0][2]
  now2 = Time.now.to_i
  db.execute(timeupd, now2, person, "c")
  if person == nowper
  elsif now1 == nil
  elsif now2-now1 < 25
    pt += 10
    kpt += 10
  end
  pt += 3
  kpt += 3
elsif qr == "d" && q4 == 0
  q4 += 1
  jikan2 = db.execute(jikan1, "d")
  now1 = jikan2[0][1]
  nowper = jikan2[0][2]
  now2 = Time.now.to_i
  db.execute(timeupd, now2, person, "d")
  if person == nowper
  elsif now1 == nil
  elsif now2-now1 < 60
    pt += 2
    kpt += 2
  end
  pt += 15
  kpt += 15
elsif qr == "e" && q5 == 0
  q5 += 1
  jikan2 = db.execute(jikan1, "e")
  now1 = jikan2[0][1]
  nowper = jikan2[0][2]
  now2 = Time.now.to_i
  db.execute(timeupd, now2, person, "e")
  if person == nowper
  elsif now1 == nil
  elsif now2-now1 < 50
    pt += 4
    kpt += 4
  end
  pt += 10
  kpt += 10
end


db.execute(bomd, pt, person)
db.execute(queupd, q1, q2, q3, q4, q5, person)

kkget = ""

########################~ここまでpt処理~###################
#######################################################

##################################レベル処理する####
lv = 1
gage = 0
e1 = db.execute("select e1 from box where id = ?",person)[0][0]#じぶんのidのboxにある一体目の経験値
no1 = db.execute("select no1 from box where id = ?",person)[0][0]
no2 = db.execute("select no2 from box where id = ?",person)[0][0].to_i#2体目id
no3 = db.execute("select no3 from box where id = ?",person)[0][0].to_i#3体目id
lv2 = db.execute("select lv2 from zukan where no = ?",no1)[0][0].to_i
lv3 = db.execute("select lv3 from zukan where no = ?",no1)[0][0].to_i
lv4 = db.execute("select lv4 from zukan where no = ?",no1)[0][0].to_i
lv5 = db.execute("select lv5 from zukan where no = ?",no1)[0][0].to_i
if e1 < lv2
lv = 1
gage = 100-(e1/lv2.to_f*100)
elsif e1 >= lv5
lv = 5
gage = 0
elsif e1 >= lv4
lv = 4
gage = 100-(e1-lv4)/(lv5-lv4.to_f)*100
elsif e1 >= lv3
lv = 3
gage = 100-(e1-lv3)/(lv4-lv3.to_f)*100
elsif e1 >= lv2
lv = 2
gage = 100-(e1-lv2)/(lv3-lv2.to_f)*100
end#########################################


image = "./img/#{cal}#{lv}.png"
alt = "#{cal}.png"



if lv == 1
  color = "black"
elsif lv == 2
  color = "navy"
elsif lv == 3
  color = "green"
elsif lv == 4
  color = "red"
elsif lv == 5
  color = "yellow"
else
  color = "pink"
  image = "./img/snail.png"
  alt = "snails_bug"
end
#expが10あがる毎にlvが1あがる。
#expの1の位*10で、次のレベルアップに必要な経験値の割合が出る。
#(exp=12の場合、2*10=『20%』、次のレベルアップまであと80%が必要、と分かる。)
#(exp=35の場合、5*10=『50%』、次のレベルアップまであと50%が必要、と分かる。)
#exppoint = "select e1 from box where id = ?"  #じぶんのidのboxにある一体目の経験値
#expp = db.execute(exppoint,id)
#exp = expp[0][0].to_i
#gage = 0
#gage2 = db.execute("select no1 from box where id = ?",id)
#exp2 = exp
#esa = "<br><br><br>"

wcname ="select name from zukan where no = ?"
cal1 = db.execute(wcname,no1)[0][0]
begin
cal2 = db.execute(wcname,no2.to_s)[0][0]
rescue
cal2 = db.execute(wcname,no2.to_s)[0]
end
begin
cal3 = db.execute(wcname,no3.to_s)[0][0]
rescue
cal3 = db.execute(wcname,no3.to_s)[0]
end
chengeform = '<select name="chenge"><option value = 0>' + "#{cal1}"
if no2 > 0 && no3 >0
  chengeform +="<option value = 2>#{cal2}<option value = 3>#{cal3}"
elsif no2 > 0 && no3 > 0
  chengeform +="<option value = 2>#{cal2}"
end

kakunin = ""
#########################
#########################
###################################--HTML--##################
###################################--------##################

printf(<<_EOS_, per, gage, per, per, per, color, kakunin, kkget, image, alt, name, person, cal, lv, chengeform,pt,chengeform)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>

<meta name="viewport"
content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="format-detection" content="telephone=no">
<link media="only screen and (max-device-width:480px)"
href="smart.css" type="text/css" rel="stylesheet" />
<link media="screen and (min-device-width:481px)" href="profeel.css"
type="text/css" rel="stylesheet" />




<link rel="shortcut icon" href="./img/cabage.png"/>
<title>Miyazac Quest!</title>
<style type="text/css">
<!--
p.gage{
background:#adf;
margin-left:0%s;
margin-right:%d%s;
text-align:left;
border-radius: 10ex;
}
div.gage{
background:#ddf;
margin-left:20%s;
margin-right:20%s;
border-radius: 10ex;
}
div.cal div{float:left; border:solid %s;}

-->
</style>
</head>

<body>
<header><p>%s</p></header>
<h1>Miyazac Quest</h1>
<h2>%s</h2>

<div class="cal">
<div>
<img src=%s alt=%s width="172" height="172">
</div>
<table class="state"><tr><td>name</td><td>:%s</td></tr>
<tr><td>id</td><td>:%04d</td></tr>
<tr><td>cara</td><td>:%s</td></tr>
<tr><td>lv.</td><td>%d</td></tr>
</table>
</div>

<div class="gage">
<p class="gage">exp</p>
</div>

<form method="POST" action="quest.rb">
%s
<input type="submit" value="~魔王の迷宮へ~">
</form>
<p>coin:%d$</p>
<table class="anime"><tr><td>

<form method="POST" action="prof.rb">
<label><input type="checkbox" name="hdn">宿屋<div><br>
<label><input type="radio" name="yado" value="g1">馬小屋:10$<img src="./img/food.png" width="16" height="16"></label><br>
<label><input type="radio" name="yado" value="g2">客室:50$<img src="./img/niku.png" width="16" height="16"></label><br><br>
<input type="reset" value="やめる">
<input type="submit" value="泊まる"></div></label>
</form>
</td><td>
<form method="POST" action="battle.rb">
<label><input type="checkbox" name="hdn">闘技場<div><br><br><br>
<label>%s</label>
<label><input type="submit" value="バトル"></label><br>
<label><input type="reset" value="いかない"></label></div></label>
<!--</div></label>-->
</form>
</td><td>

<form method="POST" action="cookie.rb">
<label><input type="checkbox" name="hdn">人生リセット<div><br><br><br>
<input type="reset" value="やめる">
<input type="submit" value="記憶を消す"></div></label>
</form>
</td></tr></table>
<footer></footer>
</body>
</html>

_EOS_
