Commit f4622cef authored by 神楽坂玲奈's avatar 神楽坂玲奈

sync

parent b3b52e18
......@@ -10,6 +10,7 @@ class Deck
attr_accessor :side
attr_accessor :extra
attr_accessor :temp
Key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_="
def initialize(main, side=[], extra=[], temp=[])
@main = main
@side = side
......@@ -40,5 +41,57 @@ class Deck
end
self.new(main, side, extra, temp)
end
def self.ygopro_deck_to_url_param(file)
card_usages = []
side = false
last_id = nil
count = 0
IO.readlines(file).each do |line|
if line[0] == '#'
next
elsif line[0, 5] == '!side'
card_usages.push({card_id: last_id, side: side, count: count}) if last_id
side = true
last_id = nil
else
card_id = line.to_i
if card_id.zero?
next
else
if card_id == last_id
count += 1
else
card_usages.push({card_id: last_id, side: side, count: count}) if last_id
last_id = card_id
count = 1
end
end
end
end
card_usages.push({card_id: last_id, side: side, count: count}) if last_id
result = ""
card_usages.each do |card_usage|
c = (card_usage[:side] ? 1 : 0) << 29 | card_usage[:count] << 27 | card_usage[:card_id]
4.downto(0) do |i|
result << Key[(c >> i * 6) & 0x3F]
end
end
require 'uri'
"name=#{URI.escape(File.basename(file, ".ydk"), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&cards=#{result}"
end
#def self.decode(str)
# card_usages = []
# (0...str.length).step(5) do |i|
# decoded = 0
# str[i, 5].each do |char|
# decoded = (decoded << 6) + Key.index(char)
# side = decoded >> 29
# count = decoded >> 27 & 0x3
# card_id = decoded & 0x07FFFFFF
# card_usages.push(card_id: card_id, side: side, count: count)
# end
# end
#end
end
module Deck_Sync
require_relative 'deck'
class <<self
def start
Update.status = '正在同步卡组'
require 'open-uri'
require 'uri'
require 'net/http'
require 'json'
require 'date'
Thread.new {
just_updated = []
$log.info('下载卡组'){"https://my-card.in/decks/?user=#{URI.escape $game.user.id.bare.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")}"}
open("https://my-card.in/decks/?user=#{URI.escape $game.user.id.bare.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")}") { |list|
JSON.parse(list.read).each { |deck|
file = File.join(File.dirname(Ygocore.ygocore_path), 'deck', "#{deck['name']}.ydk")
if (!File.file?(file) || DateTime.parse(deck['updated_at']).to_time > File.mtime(file))
open(file, 'w') { |f|
main = []
side = []
deck['cards'].each { |card_usage|
card_usage['count'].times {
(card_usage['side'] ? side : main).push card_usage['card_id']
}
}
f.puts "#mycard deck sync #{deck['user']}"
f.puts "#main"
f.puts main.join("\n")
f.puts "!side"
f.puts side.join("\n")
}
File.utime(Time.now, DateTime.parse(deck['updated_at']).to_time, file)
end
if DateTime.parse(deck['updated_at']).to_time >= File.mtime(file)
just_updated.push file
end
}
} rescue $log.error('卡组下载') { [$!.inspect, *$!.backtrace].collect { |str| str.force_encoding("UTF-8") }.join("\n") }
Thread.new { watch } unless @watching
@watching = true
Dir.glob(File.join(File.dirname(Ygocore.ygocore_path), 'deck', '*.ydk')) { |deck|
next if just_updated.include? deck
update(deck)
}
Update.status = nil
}
end
def watch
require 'fssm'
FSSM.monitor(File.join(File.dirname(Ygocore.ygocore_path), 'deck'), '*.ydk') do
update { |base, relative| Deck_Sync.update File.join base, relative }
delete { |base, relative| Deck_Sync.delete File.join base, relative }
create { |base, relative| Deck_Sync.update File.join base, relative }
end
end
def update(deck)
Update.status = "正在同步卡组: #{File.basename(deck, ".ydk")}"
path = "/decks/?#{Deck.ygopro_deck_to_url_param(deck)}&user=#{URI.escape $game.user.id.bare.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")}&updated_at=#{URI.escape File.mtime(deck).to_datetime.iso8601, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")}"
$log.info("卡组上传") { path }
req = Net::HTTP::Put.new path
response = Net::HTTP.start('my-card.in', 443, use_ssl: true) { |http| http.request(req) }
Update.status = nil
end
def delete(deck)
Update.status = "正在同步卡组: #{File.basename(deck, ".ydk")}"
path = "/decks/?name=#{URI.escape File.basename(deck, ".ydk"), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")}&user=#{URI.escape $game.user.id.bare.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")}"
$log.info("卡组删除") { path }
req = Net::HTTP::Delete.new path
response = Net::HTTP.start('my-card.in', 443, use_ssl: true) { |http| http.request(req) }
Update.status = nil
end
end
end
......@@ -15,6 +15,7 @@ class Scene_Lobby < Scene
require_relative 'window_lobbybuttons'
require_relative 'chatmessage'
require_relative 'scene_duel'
require_relative 'deck_sync'
attr_reader :chat_window
def start
......@@ -29,6 +30,7 @@ class Scene_Lobby < Scene
@active_window = @roomlist
@chat_window = Window_Chat.new(313, $config['screen']['height'] - 225, 698, 212)
@count = 0
Deck_Sync.start
super
end
......
......@@ -2,10 +2,11 @@
require "fileutils"
require_relative 'card'
module Update
Version = '1.1.7'
Version = '1.2.0'
URL = "https://my-card.in/mycard/update.json?version=#{Version}"
class <<self
attr_reader :thumbnails, :images, :status
attr_reader :thumbnails, :images
attr_accessor :status
def start
Dir.glob("mycard-update-*-*.zip") do |file|
......
......@@ -165,7 +165,8 @@ class Window_Deck < Window_Scrollable
result << key[(c >> i * 6) & 0x3F]
end
end
Dialog.web "https://my-card.in/decks/new#{'.pdf' if index[1] == :buy}?name=#{File.basename(@items[index[0]], ".ydk")}&cards=#{result}#{'#share' if index[1] == :share}"
require 'uri'
Dialog.web "https://my-card.in/decks/new#{'.pdf' if index[1] == :buy}?name=#{URI.escape(File.basename(@items[index[0]], ".ydk"), Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&cards=#{result}#{'#share' if index[1] == :share}"
when :delete
require_relative 'widget_msgbox'
......
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