Skip to content

Commit

Permalink
Merge pull request #225 from true-runes/development
Browse files Browse the repository at this point in the history
v3.2.0
  • Loading branch information
nikukyugamer authored Jul 3, 2022
2 parents 64d9e82 + 4d8bc77 commit cd65099
Show file tree
Hide file tree
Showing 22 changed files with 604 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ vendor/
.vscode/
.history/
.env
.envrc
node_modules
.clasp.*
.clasprc.json
Expand Down
23 changes: 23 additions & 0 deletions app/models/character.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,27 @@ class Character < ApplicationRecord

has_many :character_nicknames, dependent: :destroy
has_many :nicknames, through: :character_nicknames

def product_names_for_result_tweet
# TODO: 共通の値としてどこかに置きたい
name_convert_list = {
'幻想水滸伝' => 'I',
'幻想水滸伝II' => 'II',
'幻想水滸外伝Vol.1' => '外伝1',
'幻想水滸外伝Vol.2' => '外伝2',
'幻想水滸伝III' => 'III',
'幻想水滸伝IV' => 'IV',
'Rhapsodia' => 'R',
'幻想水滸伝V' => 'V',
'幻想水滸伝ティアクライス' => 'TK',
'幻想水滸伝 紡がれし百年の時' => '紡時'
}

converted_product_names = products.map do |product|
name_convert_list[product.name]
end

# Character.first.product_names_for_result_tweet #=> "(I,II,外伝2)"
"(#{converted_product_names.join(',')})"
end
end
30 changes: 30 additions & 0 deletions app/models/concerns/counting_tools.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module CountingTools
extend ActiveSupport::Concern

included do
scope :other_tweets_exists, -> { where.not(other_tweet_ids_text: '') }
end

module ClassMethods
def foobar; end
end

def three_chara_names
[chara_1, chara_2, chara_3]
end

def convert_chara_names_to_array_in_character_and_products_hashes(chara_names)
result_array = []

chara_names.each do |chara_name|
inserted_hash = {}

inserted_hash['character'] = Character.find_by(name: chara_name)
inserted_hash['products'] = Character.find_by(name: chara_name).products

result_array << inserted_hash
end

result_array
end
end
77 changes: 77 additions & 0 deletions app/models/counting_all_character.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class CountingAllCharacter < ApplicationRecord
include CountingTools

belongs_to :tweet, optional: true
belongs_to :direct_message, optional: true
belongs_to :user # 鍵でも User のレコードは取得できるので optional 指定は不要

scope :invisible, -> { where(is_invisible: true) }
scope :out_of_counting, -> { where(is_out_of_counting: true) }
scope :valid_records, -> { where(is_out_of_counting: false).where(is_invisible: false) }

enum vote_method: { by_tweet: 0, by_direct_message: 1, by_others: 99 }

def self.tweets_whose_invisible_status_is_different_between_sheet_and_database
sheet_invisible_tweet_ids = CountingAllCharacter.invisible.pluck(:tweet_id)

result = []
sheet_invisible_tweet_ids.each do |tweet_id|
database_tweet = Tweet.find_by(id: tweet_id)

result << CountingAllCharacter.find_by(tweet_id: tweet_id) if CountingAllCharacter.find_by(tweet_id: tweet_id).is_invisible == database_tweet.is_public
end

result
end

# キャラ1〜3は、AIがsuggestしたものに含まれるか否か?
# キャラ名はキャラデータベースにあるものと一致しているか?
# 同一人物の複数ツイートで複数計上していないか
def self.tweeted_all_characters
chara_1_column_characters = CountingAllCharacter.pluck(:chara_1)
chara_2_column_characters = CountingAllCharacter.pluck(:chara_2)
chara_3_column_characters = CountingAllCharacter.pluck(:chara_3)

(chara_1_column_characters + chara_2_column_characters + chara_3_column_characters).uniq.compact.sort
end

def self.character_db_diff
result = []

tweeted_all_characters.each do |character|
result << character if Character.where(name: character).blank?
end

result
end

# もとの行の内容が知りたい
# どうだったらOKでどうだったらNGなのか?
# キャラ計上数が4以上だとNG
# other_tweetがvalid_recordsでないならスルー
def self.check_other_tweets
result = []

CountingAllCharacter.valid_records.other_tweets_exists.each do |c|
c.other_tweets.each do |t|
# tweet = Tweet.find_by(id: t.tweet_id)

# result << tweet if tweet.is_public?

result << t if !t.is_out_of_counting && !t.is_invisible?
end
end

result
end

def other_tweets
# CountingAllCharacter.where('other_tweet_ids_text like ?', '%|%')
CountingAllCharacter.includes(:tweet).where(
tweet:
{
id_number: other_tweet_ids_text.split(',')
}
)
end
end
13 changes: 13 additions & 0 deletions app/models/counting_unite_attack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CountingUniteAttack < ApplicationRecord
include CountingTools

belongs_to :tweet, optional: true
belongs_to :direct_message, optional: true
belongs_to :user # 鍵でも User のレコードは取得できるので optional 指定は不要

scope :invisible, -> { where(is_invisible: true) }
scope :out_of_counting, -> { where(is_out_of_counting: true) }
scope :valid_records, -> { where(is_out_of_counting: false).where(is_invisible: false) }

enum vote_method: { by_tweet: 0, by_direct_message: 1, by_others: 99 }
end
8 changes: 8 additions & 0 deletions app/models/direct_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def self.for_spreadsheet
.order(id_number: :asc)
end

def self.missing_records
where(id_number: 1540436647376031755..1540790299618066435)
end

def is_missing_record?
id_number.in?(1540436647376031755..1540790299618066435)
end

# self.user と同義
def sender
User.find_by(id_number: sender_id_number)
Expand Down
20 changes: 12 additions & 8 deletions app/services/dm/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ class Importer
INTERVAL_SECONDS = 10

# 最新のものから過去のものに向かって取得していき、取得できなくなったら抜ける
def self.exec(twitter_client: nil, number_of_getters: 1)
def self.exec(twitter_client: nil, number_of_getters: 1, get_dm_per_request: 50)
twitter_client = TwitterRestApi.client(account_key: :gensosenkyo) if twitter_client.blank?

next_cursor = nil
loop_counter = 0

while true
# count は 1 にしておくのが望ましい(それでも28とか取ってきちゃうので)
# count は ループ段階に入ったら 1 にしておくのが望ましい(それでも28とか取ってきちゃうので)
if next_cursor.nil?
dm_events = twitter_client.direct_messages_events(count: 1)
# ここで API リクエスト が発生する
dm_events = twitter_client.direct_messages_events(count: get_dm_per_request)
else
dm_events = twitter_client.direct_messages_events(count: 1, cursor: next_cursor)
# ここで API リクエスト が発生する
dm_events = twitter_client.direct_messages_events(count: get_dm_per_request, cursor: next_cursor)
end

dm_events = dm_events.to_h
Expand All @@ -33,8 +35,6 @@ def self.exec(twitter_client: nil, number_of_getters: 1)

create_user_record(dm, twitter_client)
create_direct_message_record(dm, event)

sleep INTERVAL_SECONDS
end
end

Expand All @@ -43,7 +43,8 @@ def self.exec(twitter_client: nil, number_of_getters: 1)
break if next_cursor.nil?
break if loop_counter >= number_of_getters

sleep INTERVAL_SECONDS * 5
# ループの最初に API リクエスト が発生したので待つ
sleep INTERVAL_SECONDS * 3
end

"[DONE] Dm::Importer.exec"
Expand All @@ -57,7 +58,7 @@ def self.create_user_record(dm, twitter_client)
existing_user = User.find_by(id_number: user_id_number)

if existing_user.blank?
# Twitter API によりユーザー情報を取得する
# Twitter API によりユーザー情報を取得する(API リクエスト が発生する)
target_user = twitter_client.user(user_id_number)

user = User.new(
Expand All @@ -70,6 +71,9 @@ def self.create_user_record(dm, twitter_client)
)

user.save!

# API リクエスト が発生したので待つ
sleep INTERVAL_SECONDS
end
end

Expand Down
140 changes: 140 additions & 0 deletions app/services/sheets/counting/all_characters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
module Sheets
module Counting
class AllCharacters
def self.import_via_tweet
sheet_names = YAML.load_file(Rails.root.join('config/counting_sheet_names.yml'))['names']

ActiveRecord::Base.transaction do
sheet_names.each_with_index do |sheet_name, i|
rows = SheetData.get_rows(sheet_id: ENV.fetch('COUNTING_ALL_CHARACTERS_SHEET_ID', nil), range: "#{sheet_names[i]}!A2:Q101")

rows.each do |row|
# TODO: 設定ファイルを用いてよりスマートに定義したい
column_vs_value = {
id_on_sheet: row[0],
tweet_id_number: row[2],
other_tweet_ids_text: row[5],
is_invisible: row[7], # "FALSE" のような文字列なので注意
is_out_of_counting: row[8], # "FALSE" のような文字列なので注意
contents: row[11],
memo: row[12],
chara_1: row[14],
chara_2: row[15],
chara_3: row[16]
}

next if column_vs_value[:id_on_sheet].blank? || column_vs_value[:tweet_id_number].blank? || column_vs_value[:contents].blank?

tweet = Tweet.find_by(id_number: column_vs_value[:tweet_id_number])
tweet_id = tweet.id
user_id = tweet.user.id

unique_attrs = {
id_on_sheet: column_vs_value[:id_on_sheet],
user_id: user_id,
vote_method: :by_tweet,
tweet_id: tweet_id,
contents: column_vs_value[:contents],
memo: column_vs_value[:memo]
}

mutable_attrs = {
# 123456,654321,777777 のような文字列になる
other_tweet_ids_text: column_vs_value[:other_tweet_ids_text].split('|').map(&:strip).join(','),
is_invisible: column_vs_value[:is_invisible].to_boolean,
is_out_of_counting: column_vs_value[:is_out_of_counting].to_boolean,
chara_1: column_vs_value[:chara_1],
chara_2: column_vs_value[:chara_2],
chara_3: column_vs_value[:chara_3]
}

CountingAllCharacter.find_or_initialize_by(unique_attrs).update!(mutable_attrs)
end
end
end

'[DONE] Sheets::Counting::AllCharacters.import_via_tweet'
end

def self.import_via_dm
sheet_names = YAML.load_file(Rails.root.join('config/counting_sheet_names.yml'))['names']

# rubocop:disable Metrics/BlockLength
ActiveRecord::Base.transaction do
sheet_names.each_with_index do |sheet_name, i|
rows = SheetData.get_rows(sheet_id: ENV.fetch('COUNTING_DIRECT_MESSAGES_SHEET_ID', nil), range: "#{sheet_names[i]}!A2:Q101")

rows.each do |row|
# TODO: 設定ファイルを用いてスマートに定義したい
column_vs_value = {
id_on_sheet: row[0],
dm_id_number: row[2],
is_invisible: row[5], # "FALSE" のような文字列なので注意
is_out_of_counting: row[6], # "FALSE" のような文字列なので注意
category: row[9],
contents: row[10],
memo: row[11],
input_01: row[13],
input_02: row[14],
input_03: row[15],
input_04: row[16],
input_05: row[17],
input_06: row[18],
input_07: row[19],
input_08: row[20],
input_09: row[21],
input_10: row[22]
}

# DMの書式が自由すぎるので、こちらで条件を吸収する
next if column_vs_value[:category] != '①オールキャラ部門' && column_vs_value[:category] != '両部門'

next if column_vs_value[:id_on_sheet].blank? || column_vs_value[:dm_id_number].blank? || column_vs_value[:contents].blank?

dm = DirectMessage.find_by(id_number: column_vs_value[:dm_id_number])
dm_id = dm.id
user_id = dm.user.id

unique_attrs = {
id_on_sheet: column_vs_value[:id_on_sheet],
user_id: user_id,
vote_method: :by_direct_message,
direct_message_id: dm_id,
other_tweet_ids_text: nil,
contents: column_vs_value[:contents],
memo: column_vs_value[:memo]
}

# 「両部門の場合は、N列とO列に協力攻撃、P列Q列R列にオールキャラ部門を入力する」という例外規定
case column_vs_value[:category]
when '①オールキャラ部門'
chara_1 = column_vs_value[:input_01]
chara_2 = column_vs_value[:input_02]
chara_3 = column_vs_value[:input_03]
when '両部門'
chara_1 = column_vs_value[:input_03]
chara_2 = column_vs_value[:input_04]
chara_3 = column_vs_value[:input_05]
end

mutable_attrs = {
is_invisible: column_vs_value[:is_invisible].to_boolean,
is_out_of_counting: column_vs_value[:is_out_of_counting].to_boolean,
chara_1: chara_1,
chara_2: chara_2,
chara_3: chara_3
}

CountingAllCharacter.find_or_initialize_by(unique_attrs).update!(mutable_attrs)
end

puts "#{sheet_names[i]} is Done." # rubocop:disable Rails/Output
end
end
# rubocop:enable Metrics/BlockLength

'[DONE] Sheets::Counting::AllCharacters.import_via_dm'
end
end
end
end
Loading

0 comments on commit cd65099

Please sign in to comment.