Commit 668f6cd2 authored by Peter Xin's avatar Peter Xin

FIX.

Added Git module
Added Commands module
parent c96e4c4e
# Commands Reflector
require File.dirname(__FILE__) + '/Global.rb'
require File.dirname(__FILE__) + '/Sqlite.rb'
require File.dirname(__FILE__) + '/MSETranslator.rb'
require File.dirname(__FILE__) + '/HashJudger.rb'
require File.dirname(__FILE__) + '/Log.rb'
module Commands
module_function
......@@ -7,6 +12,43 @@ module Commands
end
def change_password(password)
Authorize.change_password password
end
def generate_all
Log.logger.info "Generate all the card images."
self.clear_mse
data = Sqlite.split Sqlite.load
MSETranslator.generate_mse_all(data)
MSETranslator.export_mse_all
end
def generate_delta
Log.logger.info "Generate delta card image(s)"
self.clear_mse
data = Sqlite.load
changes = HashJudger.compare data
generates = changes[0] + changes[2]
removes = changes[1]
for card in removes
id = YGOCoreJudgers.get_id card
path = Global.full_image_name id
File.delete path if File.exist? path
end
data = Sqlite.split generates
MSETranslator.generate_mse_all(data)
MSETranslator.export_mse_all
end
def generate_single(id)
Log.logger.info "Generate single card image #{id}"
self.clear_mse
MSETranslator.generate_mse Sqlite.load(id)
MSETranslator.export_mse_all
end
def clear_mse
Log.logger.info "Clearing MSE Path"
Dir.glob("#{Global.mse_set_path}/*.mse-set") {|set| File.delete set}
end
end
\ No newline at end of file
......@@ -13,11 +13,7 @@ module Git
return `cd #{global.answer_path} && git push`
end
def commit
end
def hook
def commit(message = "")
return `cd #{global.answer_path} && git add . && git commit -a -m \"Update card images\"`
end
end
\ No newline at end of file
......@@ -12,6 +12,7 @@ module Global
attr_accessor :answer_uri
attr_accessor :mse_path
attr_accessor :mse_set_path
attr_accessor :sql_fix_name
end
self.language = 'cn'
self.git_path = 'ygopro-images-raw'
......@@ -22,6 +23,7 @@ module Global
self.answer_path = "ygopro-images"
self.answer_uri = "https://github.com/mycard/ygo-images.git"
self.mse_path = "MagicSetEditor2"
self.sql_fix_name = "fix.sql"
self.mse_set_path = "mse-sets"
module_function
def full_database_path
......
# Judge the hash.
# offer the CSV version and JSON Version.
# Use the JSON Version.
require 'JSON'
......@@ -7,6 +6,7 @@ require File.dirname(__FILE__) + '/Authorize.rb'
require File.dirname(__FILE__) + '/Global.rb'
require File.dirname(__FILE__) + '/Log.rb'
require File.dirname(__FILE__) + '/Sqlite.rb'
require File.dirname(__FILE__) + '/YgoCoreJudgers.rb'
module HashJudger
module_function
......@@ -36,20 +36,24 @@ module HashJudger
return [Authorize.md5_file(image_path), Authorize.md5(data)]
end
@@temp_id_to_data = {}
def generate_document(sql_data)
answer = {}
@@temp_id_to_data = {}
for data in sql_data
id = data[0]
id = YGOCoreJudgers.get_id data
hash = self.generate_hash id, data
answer[id.to_s] = hash if hash != nil
next if hash == nil
answer[id.to_s] = hash
@@temp_id_to_data[id.to_s] = data
end
return answer
end
def compare
def compare(data)
# 读取两组数据
old_data = self.load
new_data = generate_document(Sqlite.load)
new_data = generate_document(data)
# 生成编号序列
# 添加
adds = new_data.keys - old_data.keys
......@@ -67,6 +71,8 @@ module HashJudger
Log.logger.info "Following cards removed:"
removes.each {|id| Log.logger.info "#{[id]}"}
end
adds.map! {|id| @@temp_id_to_data[id]}
removes.map!{|id| @@temp_id_to_data[id]}
# 公共部分
commons = new_data.keys & old_data.keys
changes = []
......@@ -77,13 +83,13 @@ module HashJudger
new_piece = new_data[id]
if old_piece[0] != new_piece[0] and old_piece[1] != new_piece[1]
Log.logger.info "[#{id}] Both changed"
changes.push id
changes.push @@temp_id_to_data[id]
elsif old_piece[0] != new_piece[0]
Log.logger.info "[#{id}] image changed"
changes.push id
changes.push @@temp_id_to_data[id]
elsif old_piece[1] != new_piece[1]
Log.logger.info "[#{id}] data changed"
changes.push id
changes.push @@temp_id_to_data[id]
end
end
Log.logger.info "Oooooops but no data or file changed." if changes == []
......
require File.dirname(__FILE__) + '/MSEConstants.rb'
require File.dirname(__FILE__) + '/YGOCoreJudgers.rb'
require File.dirname(__FILE__) + '/YgoCoreJudgers.rb'
require File.dirname(__FILE__) + '/Global.rb'
require File.dirname(__FILE__) + '/Log.rb'
require 'zip'
module MSETranslator
module_function
......@@ -94,7 +95,6 @@ module MSETranslator
end
def generate_mse(data, key = "")
require 'zip'
path = File.join Global.mse_set_path, "data#{key}-#{Global.language}.mse-set"
clear_data = []
Zip::File.open(path, Zip::File::CREATE) do |zipfile|
......
# sinatra server core
require "sinatra"
require File.dirname(__FILE__) + "/Git.rb"
require File.dirname(__FILE__) + "/Command.rb"
post '/command' do
end
post '/hook' do
Git.pull
Commands.generate_delta
Git.commit
Git.push
end
\ No newline at end of file
......@@ -5,16 +5,37 @@ require File.dirname(__FILE__) + '/Log.rb'
require File.dirname(__FILE__) + '/MSEConstants.rb'
module Sqlite
def self.load
def self.load(id = -1)
begin
db = SQLite3::Database.new Global.full_database_path
db.execute "select * from datas join texts on datas.id == texts.id"
self.run_fix(db)
command = "select * from datas join texts on datas.id == texts.id"
command += " where datas.id == #{id}" if id > 0
ans = db.execute command
db.close
ans
rescue Exception => e
Log.logger.error "Failed to load sql while running command: #{command}."
Log.logger.error e.inspect
nil
end
end
def self.run_fix(db)
if File.exist?(Global.sql_fix_name)
begin
Log.logger.info "Going to run fix sql"
content = File.open(Global.sql_fix_name) {|f| f.read}
db.execute(content)
rescue Exception => e
Log.logger.error "Failed to run the sql fix."
Log.logger.error e.inspect
end
else
Log.logger.warn "No file named #{Global.sql_fix_name}. Skip the sql fix process."
end
end
def self.split(data)
size = MSEConstants::SingleFileMaxCount
count = (data.size - 1) / size + 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment