Newer
Older
skip-web / archive / index / minato2014 / admin / chusen.rb
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'sqlite3'
require 'cgi'
require 'date'

c = CGI.new(:tag_maker => "html5", :accept_charest => "UTF-8")
db = SQLite3::Database.new("../sql/stamp.sq3")

add = ENV["REMOTE_ADDR"]

if /^(172\.19\.5\.|172\.17\.54\.|172\.21\.90\.)|118.21.142.107|202.214.125.107/ =~ add
check = c['check']

printf("Content-type: text/html; charset=UTF-8\n\n")

sum = 'SELECT SUM(rank) FROM main WHERE NOT id=1;'
rank_sum = db.execute(sum).flatten(2)[0].to_i

t = Time.now
expires = Time.local(2014, 7, 26, 11, 27)
expires2 = Time.local(2014, 7, 26, 13, 27)
if t > expires && rank_sum == 0
elsif t > expires2 && rank_sum == 7
else
  check = ""
  puts check
end

if check == "OK"
  begin
    result = db.execute("select id,exp,rank from main;")
  rescue
    exit
  end
  all = Array.new
  for id,lv,rank in result
    if id == 1 || lv == 0 || rank.to_i != 0
      next
    end
    lv.to_i.times do
      all << id
    end
  end
  
  tousen = Array.new(0)
  atari1 = rand(all.length)
  tousen << all[atari1]
  all.delete_if {|x| x == tousen[0]}
  
  sql = "update main set rank = 1 where id = ?;"
  db.execute(sql,tousen[0])
  
  sql = "update main set rank = 2 where id = ?;"
  while tousen.length < 4
    srand
    atari2 = rand(all.length)
    hoge = all[atari2]
    tousen << all[atari2]
    all.delete_if {|x| x == hoge}
    db.execute(sql,hoge)
  end
  
  i=0
  
  printf("<p>1等はIDが%04dの方!おめでとー!</p>\n",tousen[i].to_i-1)
  printf("<p>2等はIDが%04dと%04dと%04dの方!おめでとー!</p>\n",tousen[i+=1].to_i-1,tousen[i+=1].to_i-1,tousen[i+=1].to_i-1)
else
  print"<p>抽選はまだ行いませんよ</p>\n"
end
else
  printf("Content-type: text/html; charset=UTF-8\n\n")
  
  printf'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/h
tml4/strict.dtd">
<html>
<head>
<title>Delete table</title>
<link rel="stylesheet" type="text/css" href="design.css">
</head>
</body>
<p>管理者用ページのため閲覧できません。</p>'
end
print'</body></html>'