#!/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 = ?"
   #idが?(後で代入)のデータをtest票から探す。という命令をdataにしくむ。全てがほしい場合は「*」。「*」を「name」に変えると該当データのnameだけとりだす。

cook = db.execute(data, person)
   #dataに仕組んでいた命令を、personを代入しつつ実行。

upd = "update test set exp = ?, pt = ? where id = ?"
   #「idが?3のとき、expを?1にする。ptを?2にする。」という命令文。
lvupd = "update test set lv = ? where id = ?"

que = "select q10 from toi where id = ?"
queupd = "update toi set q10 = 1 where id = ?"
q10 = db.execute(que, person)

  name = cook[0][1]
  cal = cook[0][2]
  exp = cook[0][6]
  pt = cook[0][7]
  lv = cook[0][8]
  per = "%"
#####################################
##########秘密のこたえを設定##############
if cal == "inu"
m3 = "あゆ"
elsif cal == "sisip"
m3 = "ゆめ"
elsif cal == "kamon"
m3 = "ねぎ"
elsif cal == "boy"
m3 = "ふね"
elsif cal == "girl"
m3 = "かんざし"
else
m3 = "skipskip"
end
#####################################
#####################################


gohan = c["gohan"]
#himitu = ""
if gohan == "g1" && pt >= 2
  exp += 2
  pt -= 2
  db.execute(upd, exp, pt, person)  
  kekka = "#{cal}が成長した!"
elsif gohan == "g2" && pt >= 5
  exp += 6
  pt -= 5
  db.execute(upd, exp, pt, person)
  kekka = "#{cal}がすごく成長した!"
elsif gohan == m3
  if q10[0][0] == 0
    exp += 10
    db.execute(upd, exp, pt, person)  
    kekka = ""
    db.execute(queupd, person)
  else
    kekka ="しつもん に こたえられるのは いっかい だけだよ!"
  end
else
  kekka = "ポイント が たりない!"
end

if gohan == m3
elsif gohan == "g1"
elsif gohan == "g2"
else
kekka = "こたえ が ちがうよ!"
end


if exp >= lv*10 && lv < 5
  lv += 1
  db.execute(lvupd, lv, person)
  kekka += "<br>レベルが上がった!<br>レベルが#{lv}になった!"
end


if cal == "inu"
  image = "./img/inu#{lv}.png"
  alt = "inu#{lv}"
elsif cal == "sisip"
  image = "./img/sisip#{lv}.png"
  alt = "sisip#{lv}"
elsif cal == "kamon"
  image = "./img/kamon#{lv}.png"
  alt = "kamon#{lv}"
elsif cal == "boy"
  image = "./img/boy#{lv}.png"
  alt = "boy#{lv}"
elsif cal == "girl"
  image = "./img/girl#{lv}.png"
  alt = "girl#{lv}"
elsif cal == "snake"
  image = "./img/snake#{lv}.png"
  alt = "snake#{lv}"
else
  image = "./img/snail.png"
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

gage = 0
exp2 = exp
if lv < 5
  while exp2 >= 10
    exp2 -= 10
  end
  gage = 100-exp2*10
end



modoru = '<br><div class="f1"><a href="prof.rb"><div class="lets"><span>ホーム'
back = modoru
if lv < 5 && gohan == "g1" || lv < 5 && gohan == "g2"
  mo = '<input type="button" value="おかわり!" onclick="location.reload();" />'
  back = mo + modoru
elsif lv >= 5
  back += "<br>(レベルマックスだよ!)"
end
back += '</span></div></a></div>'

cook = db.execute(data, person)
exp = cook[0][6]
pt = cook[0][7]
lv = cook[0][8]
  

printf(<<_EOS_, per, gage, per, per, per, color, image, alt, name, person, cal, lv, pt, kekka, back)

<!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/exp.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.cal div{float:left; border:solid %s;}

-->
</style>
</head>

<body>
<header></header>
<h1>ほげほげ!</h1>

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

<p>

こうえきポイント:%dp<br>
%s
</p>

%s
<footer></footer>
</body>
</html>

_EOS_
