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

FIX.

Added Git module
Added Commands module
parent c96e4c4e
# Commands Reflector # 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 Commands
module_function module_function
...@@ -7,6 +12,43 @@ module Commands ...@@ -7,6 +12,43 @@ module Commands
end end
def change_password(password) 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
end end
\ No newline at end of file
...@@ -13,11 +13,7 @@ module Git ...@@ -13,11 +13,7 @@ module Git
return `cd #{global.answer_path} && git push` return `cd #{global.answer_path} && git push`
end end
def commit def commit(message = "")
return `cd #{global.answer_path} && git add . && git commit -a -m \"Update card images\"`
end
def hook
end end
end end
\ No newline at end of file
...@@ -12,6 +12,7 @@ module Global ...@@ -12,6 +12,7 @@ module Global
attr_accessor :answer_uri attr_accessor :answer_uri
attr_accessor :mse_path attr_accessor :mse_path
attr_accessor :mse_set_path attr_accessor :mse_set_path
attr_accessor :sql_fix_name
end end
self.language = 'cn' self.language = 'cn'
self.git_path = 'ygopro-images-raw' self.git_path = 'ygopro-images-raw'
...@@ -22,6 +23,7 @@ module Global ...@@ -22,6 +23,7 @@ module Global
self.answer_path = "ygopro-images" self.answer_path = "ygopro-images"
self.answer_uri = "https://github.com/mycard/ygo-images.git" self.answer_uri = "https://github.com/mycard/ygo-images.git"
self.mse_path = "MagicSetEditor2" self.mse_path = "MagicSetEditor2"
self.sql_fix_name = "fix.sql"
self.mse_set_path = "mse-sets" self.mse_set_path = "mse-sets"
module_function module_function
def full_database_path def full_database_path
......
# Judge the hash. # Judge the hash.
# offer the CSV version and JSON Version.
# Use the JSON Version. # Use the JSON Version.
require 'JSON' require 'JSON'
...@@ -7,6 +6,7 @@ require File.dirname(__FILE__) + '/Authorize.rb' ...@@ -7,6 +6,7 @@ require File.dirname(__FILE__) + '/Authorize.rb'
require File.dirname(__FILE__) + '/Global.rb' require File.dirname(__FILE__) + '/Global.rb'
require File.dirname(__FILE__) + '/Log.rb' require File.dirname(__FILE__) + '/Log.rb'
require File.dirname(__FILE__) + '/Sqlite.rb' require File.dirname(__FILE__) + '/Sqlite.rb'
require File.dirname(__FILE__) + '/YgoCoreJudgers.rb'
module HashJudger module HashJudger
module_function module_function
...@@ -36,20 +36,24 @@ module HashJudger ...@@ -36,20 +36,24 @@ module HashJudger
return [Authorize.md5_file(image_path), Authorize.md5(data)] return [Authorize.md5_file(image_path), Authorize.md5(data)]
end end
@@temp_id_to_data = {}
def generate_document(sql_data) def generate_document(sql_data)
answer = {} answer = {}
@@temp_id_to_data = {}
for data in sql_data for data in sql_data
id = data[0] id = YGOCoreJudgers.get_id data
hash = self.generate_hash 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 end
return answer return answer
end end
def compare def compare(data)
# 读取两组数据 # 读取两组数据
old_data = self.load old_data = self.load
new_data = generate_document(Sqlite.load) new_data = generate_document(data)
# 生成编号序列 # 生成编号序列
# 添加 # 添加
adds = new_data.keys - old_data.keys adds = new_data.keys - old_data.keys
...@@ -67,6 +71,8 @@ module HashJudger ...@@ -67,6 +71,8 @@ module HashJudger
Log.logger.info "Following cards removed:" Log.logger.info "Following cards removed:"
removes.each {|id| Log.logger.info "#{[id]}"} removes.each {|id| Log.logger.info "#{[id]}"}
end end
adds.map! {|id| @@temp_id_to_data[id]}
removes.map!{|id| @@temp_id_to_data[id]}
# 公共部分 # 公共部分
commons = new_data.keys & old_data.keys commons = new_data.keys & old_data.keys
changes = [] changes = []
...@@ -77,13 +83,13 @@ module HashJudger ...@@ -77,13 +83,13 @@ module HashJudger
new_piece = new_data[id] new_piece = new_data[id]
if old_piece[0] != new_piece[0] and old_piece[1] != new_piece[1] if old_piece[0] != new_piece[0] and old_piece[1] != new_piece[1]
Log.logger.info "[#{id}] Both changed" Log.logger.info "[#{id}] Both changed"
changes.push id changes.push @@temp_id_to_data[id]
elsif old_piece[0] != new_piece[0] elsif old_piece[0] != new_piece[0]
Log.logger.info "[#{id}] image changed" Log.logger.info "[#{id}] image changed"
changes.push id changes.push @@temp_id_to_data[id]
elsif old_piece[1] != new_piece[1] elsif old_piece[1] != new_piece[1]
Log.logger.info "[#{id}] data changed" Log.logger.info "[#{id}] data changed"
changes.push id changes.push @@temp_id_to_data[id]
end end
end end
Log.logger.info "Oooooops but no data or file changed." if changes == [] Log.logger.info "Oooooops but no data or file changed." if changes == []
......
require File.dirname(__FILE__) + '/MSEConstants.rb' 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__) + '/Global.rb'
require File.dirname(__FILE__) + '/Log.rb' require File.dirname(__FILE__) + '/Log.rb'
require 'zip'
module MSETranslator module MSETranslator
module_function module_function
...@@ -94,7 +95,6 @@ module MSETranslator ...@@ -94,7 +95,6 @@ module MSETranslator
end end
def generate_mse(data, key = "") def generate_mse(data, key = "")
require 'zip'
path = File.join Global.mse_set_path, "data#{key}-#{Global.language}.mse-set" path = File.join Global.mse_set_path, "data#{key}-#{Global.language}.mse-set"
clear_data = [] clear_data = []
Zip::File.open(path, Zip::File::CREATE) do |zipfile| Zip::File.open(path, Zip::File::CREATE) do |zipfile|
......
# sinatra server core # sinatra server core
require "sinatra" require "sinatra"
require File.dirname(__FILE__) + "/Git.rb"
require File.dirname(__FILE__) + "/Command.rb"
post '/command' do post '/command' do
end end
post '/hook' do post '/hook' do
Git.pull
Commands.generate_delta
Git.commit
Git.push
end end
\ No newline at end of file
...@@ -5,16 +5,37 @@ require File.dirname(__FILE__) + '/Log.rb' ...@@ -5,16 +5,37 @@ require File.dirname(__FILE__) + '/Log.rb'
require File.dirname(__FILE__) + '/MSEConstants.rb' require File.dirname(__FILE__) + '/MSEConstants.rb'
module Sqlite module Sqlite
def self.load def self.load(id = -1)
begin begin
db = SQLite3::Database.new Global.full_database_path 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 rescue Exception => e
Log.logger.error "Failed to load sql while running command: #{command}."
Log.logger.error e.inspect Log.logger.error e.inspect
nil nil
end end
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) def self.split(data)
size = MSEConstants::SingleFileMaxCount size = MSEConstants::SingleFileMaxCount
count = (data.size - 1) / size + 1 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