#!/usr/bin/env ruby22
# -*- coding: utf-8 -*-

require 'sqlite3'
require 'kconv'
require 'cgi'

c = CGI.new(:tag_maker => "html5", :accept_charest => "UTF-8")
db = SQLite3::Database.new("sql/main.sq3")
id = c.cookies["id"][0].to_i
chenge = c["chenge"].to_i
box = db.execute("select * from box where id = ?",id)
hoge,no1,e1,t1,tm1,no2,e2,t2,tm2,no3,e3,t3,tm3=box[0]
newname = "select name from zukan where no = ?"
if chenge == 2
  db.execute("update box set no1=?,e1=?,t1=?,tm1=?,no2=?,e2=?,t2=?,tm2=? where id = ?",no2,e2,t2,tm2,no1,e1,t1,tm1,id)
calname = db.execute(newname,no2)[0][0]

db.execute("update test set cal = ? where id = ?",calname,id)
elsif chenge == 3
  db.execute("update box set no1=?,e1=?,t1=?,tm1=?,no3=?,e3=?,t3=?,tm3=? where id = ?",no3,e3,t3,tm3,no1,e1,t1,tm1,id)
calname = db.execute(newname,no3)[0][0]
db.execute("update test set cal = ? where id = ?",calname,id)
end


  
  cmps = c["cmps"]
x = 0
per ="%"
newone = ""

data = "select * from test where id = ?"
cook = db.execute(data, id)
begin
cal = cook[0][2]
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
##################################
exppoint = "select e1 from box where id = ?"  #じぶんのidのboxにある一体目の経験値
expup = "update box set e1 = ? where id = ?"
exp2 = db.execute(exppoint,id)
exp = exp2[0][0].to_i
srand
if cmps == "↑"
  exp += 2 
  x = rand(2)+1
elsif cmps == "→"
  exp += 1
  x = rand(3)+1
####################
elsif cmps == "←"        ##当たりルート
  no1 = db.execute("select no1 from box where id = ?",id)[0][0]
  no2 = db.execute("select no2 from box where id = ?",id)[0][0]
  no3 = db.execute("select no3 from box where id = ?",id)[0][0]
  number = rand(8)+1
while number == no1.to_i
  number = rand(8)+1
end
##############2
  if no2 == 0
    newcal = db.execute('select * from zukan where no = ?',"#{number}")
    no2 = newcal[0][0]
    calname = newcal[0][1]
    t2 = newcal[0][2].to_i/5
    db.execute("update box set no2 = ?, t2 = ?, tm2 = ? where id = ?",no2,t2,t2,id)
    newone = "#{calname}が現われた!<br>#{calname}が仲間になった!"
##############3
  elsif no3 == 0
    while number == no1.to_i || number == no2.to_i
      number = rand(8)+1
    end
    newcal = db.execute('select * from zukan where no = ?',"#{number}")
    no3 = newcal[0][0]
    calname = newcal[0][1]
    t3 = newcal[0][2].to_i/5
    db.execute("update box set no3 = ?, t3 = ?, tm3 = ? where id = ?",no3,t3,t3,id)
    newone = "#{calname}が現われた!<br>#{calname}が仲間になった!"
##############
  else
    x = 49
  end
end
db.execute(expup,exp,id)
coin = db.execute("select pt from test where id = ?",id)[0][0]
coin += x
db.execute("update test set pt = ?",coin)
############################################


if x > 0
  xs = x.to_s + "$get!"
else
  xs = ""
end

x = rand(30)
if x == 0
cmpsget ='<input type="submit" name="cmps" value="←">
<input type="submit" name="cmps" value="↑">
<input type="submit" name="cmps" value="→">'
elsif x == 1
cmpsget ='<input type="submit" name="cmps" value="←">
<input type="submit" name="cmps" value="↑" disabled>
<input type="submit" name="cmps" value="→">'
else
cmpsget ='<input type="submit" name="cmps" value="←" disabled>
<input type="submit" name="cmps" value="↑">
<input type="submit" name="cmps" value="→">'
end

lv = 1
gage = 0
e1 = db.execute("select e1 from box where id = ?",id)[0][0]#じぶんのidのboxにある一体目の経験値
no1 = db.execute("select no1 from box where id = ?",id)[0][0]
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


##################################################
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

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


printf(<<_EOS_,per,gage,per,per,per,color,cmps,xs,newone,image,alt,cmpsget)


<!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>だぁんじょん</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.calque div{float:center; border:solid %s;}

-->
</style>
</head>

<body>
<h1>だぁんじょん%s</h1>
<p>%s %s</p>
<div class="calque">
<div>
<img src=%s alt=%s width="172" height="172">
</div>
</div>

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

<form method="POST" action="quest.rb">
%s
</form>
<div class="f1">
<div class="lets">
<a href="prof.rb">出る...</a>
</div></div>
<footer></footer>
</body>
</html>

_EOS_
