Newer
Older
skip-web / minato2014 / admin / backup / chusen.rb
@MURAKAMI Masaki MURAKAMI Masaki on 24 Jul 2014 1 KB Updated
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require 'sqlite3'
require 'cgi'

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

check = c['check']
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 == "ID" || 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("Content-type: text/html; charset=UTF-8\n\n")
  
  printf("<p>1等はIDが%04dの方!おめでとー!</p>\n",tousen[i].to_i)
  printf("<p>2等はIDが%04dと%04dと%04dの方!おめでとー!</p>\n",tousen[i+=1].to_i,tousen[i+=1].to_i,tousen[i+=1].to_i)
else
  print"<p>抽選はまだ行いませんよ</p>"
end