Skip to content

Commit

Permalink
Merge pull request #267 from true-runes/development
Browse files Browse the repository at this point in the history
v3.5.0
  • Loading branch information
nikukyugamer authored Jul 11, 2022
2 parents f1e1545 + 04ab2af commit df4220f
Show file tree
Hide file tree
Showing 27 changed files with 1,365 additions and 57 deletions.
4 changes: 4 additions & 0 deletions app/lib/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ def self.normalized_screen_name(screen_name)
end

def self.formatted_product_names_for_tweet(character_name)
return '' if Character.find_by(name: character_name).blank?

products = Character.find_by(name: character_name).products

return '' if products.blank?

product_name_long_to_short = {
'幻想水滸伝' => 'I',
'幻想水滸伝II' => 'II',
Expand Down
9 changes: 5 additions & 4 deletions app/models/counting_all_character.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CountingAllCharacter < ApplicationRecord
scope :by_tweet, -> { where(vote_method: :by_tweet) }
scope :by_dm, -> { where(vote_method: :by_direct_message) }

enum vote_method: { by_tweet: 0, by_direct_message: 1, by_others: 99 }, _prefix: true
enum vote_method: { by_tweet: 0, by_direct_message: 1, op_cl_illustrations_bonus: 2, by_others: 99 }, _prefix: true

def self.tweets_whose_invisible_status_is_different_between_sheet_and_database
sheet_invisible_tweet_ids = CountingAllCharacter.invisible.pluck(:tweet_id)
Expand All @@ -28,9 +28,10 @@ def self.tweets_whose_invisible_status_is_different_between_sheet_and_database

# キャラの数え上げには SQL を使わずに個数を愚直に数える方法を採る
def self.all_character_names_including_duplicated
chara_1_column_characters = CountingAllCharacter.valid_records.pluck(:chara_1)
chara_2_column_characters = CountingAllCharacter.valid_records.pluck(:chara_2)
chara_3_column_characters = CountingAllCharacter.valid_records.pluck(:chara_3)
# オールキャラではさらに統合集計が必要なので「ボ・OP・CLイラスト(協力攻撃)」は除外している
chara_1_column_characters = CountingAllCharacter.valid_records.where.not(vote_method: :op_cl_illustrations_bonus).pluck(:chara_1)
chara_2_column_characters = CountingAllCharacter.valid_records.where.not(vote_method: :op_cl_illustrations_bonus).pluck(:chara_2)
chara_3_column_characters = CountingAllCharacter.valid_records.where.not(vote_method: :op_cl_illustrations_bonus).pluck(:chara_3)

(chara_1_column_characters + chara_2_column_characters + chara_3_column_characters).compact_blank.sort
end
Expand Down
206 changes: 201 additions & 5 deletions app/models/counting_bonus_vote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,200 @@ class CountingBonusVote < ApplicationRecord

enum vote_method: { by_tweet: 0, by_direct_message: 1, by_others: 99 }, _prefix: true
enum bonus_category: {
op_cl_illustrations: 0,
op_cl_illustrations: 0, # 未使用
short_stories: 1,
result_illustrations: 2,
result_illustrations: 2, # 未使用
fav_quotes: 3,
sosenkyo_campaigns: 4
}, _prefix: true

def self.ranking_short_stories
base_records = CountingBonusVote.valid_records.where(bonus_category: :short_stories)

ranking_records = []

base_records.each do |record|
url = record.tweet.blank? ? '' : record.tweet.url
id_on_sheet = record.id_on_sheet
vote_method = record.vote_method
theme = record.short_stories_theme
contents = record.contents
chara_names = [
record.chara_01, record.chara_02, record.chara_03, record.chara_04,
record.chara_05, record.chara_06, record.chara_07, record.chara_08,
record.chara_09, record.chara_10
].compact_blank.sort.reject { |el| el == "FALSE"}

chara_names.each do |chara_name|
ranking_records << {
url: url,
id_on_sheet: id_on_sheet,
vote_method: vote_method,
theme: theme,
contents: contents,
character_name: chara_name
}
end
end

ranking_records.sort_by do |element|
[element[:character_name]]
end
end

def self.ranking_fav_quotes
base_records = CountingBonusVote.valid_records.where(bonus_category: :fav_quotes)
chara_columns = %i[
chara_01 chara_02 chara_03 chara_04 chara_05
chara_06 chara_07 chara_08 chara_09 chara_10
]

ranking_records = []

base_records.each do |record|
id_on_sheet = record.id_on_sheet
url = record.tweet.blank? ? '' : record.tweet.url
vote_method = record.vote_method
contents = record.contents
character_names = chara_columns.map { |c| record[c] }.compact_blank.reject { |el| el == "FALSE"}

# キャラが複数いる場合には分割する(一キャラ一台詞一レコード)
# この分割の結果、ツイート人数(DM人数)とキャラレコード数が一致しなくなることに注意する
character_names.each do |character_name|
ranking_records << {
id_on_sheet: id_on_sheet,
url: url,
vote_method: vote_method,
contents: contents,
character_name: character_name
}
end
end

ranking_records.sort_by do |element|
[element[:character_name]]
end
end

def self.ranking_result_illustrations
base_records = OnRawSheetResultIllustrationTotalling.all.reject do |record|
record.character_name_by_sheet_totalling.start_with?('TEMP_')
end

ranking_records = []

base_records.each do |record|
ranking_records << {
name: record.character_name_by_sheet_totalling,
number_of_applications: record.number_of_applications
}
end

ranking_records.sort_by do |element|
[
element[:name], element[:number_of_applications]
]
end
end

def self.ranking_op_cl_illustrations
all_characters_base_records = CountingAllCharacter.where(vote_method: :op_cl_illustrations_bonus)
all_characters_ranking_records = []
all_characters_base_records.each do |record|
all_characters_ranking_records << {
chara_1: record.chara_1
}
end

unite_attacks_ranking_records = []
unite_attacks_base_records = CountingUniteAttack.where(vote_method: :op_cl_illustrations_bonus)
unite_attacks_base_records.each do |record|
unite_attacks_ranking_records << {
product_name: record.product_name,
unite_attack_name: record.unite_attack_name
}
end

{
all_characters: all_characters_ranking_records,
unite_attacks: unite_attacks_ranking_records
}
end

def self.ranking_sosenkyo_campaigns
base_records = CountingBonusVote.valid_records.where(bonus_category: :sosenkyo_campaigns)

chara_columns = %i[
chara_01 chara_02 chara_03 chara_04 chara_05
chara_06 chara_07 chara_08 chara_09 chara_10
chara_11 chara_12
]
ranking_records = []

base_records.each do |record|
url = record.tweet.blank? ? '' : record.tweet.url
id_on_sheet = record.id_on_sheet
vote_method = record.vote_method
contents = record.contents

character_names = chara_columns.map { |c| record[c] }.compact_blank.reject { |el| el == "FALSE"}

character_names.each do |character_name|
ranking_records << {
url: url,
id_on_sheet: id_on_sheet,
vote_method: vote_method,
contents: contents,
character_name: character_name
}
end
end

ranking_records.sort_by do |element|
[element[:character_name]]
end
end

# category_name は書き込み対象のシートを指している(名前が良くないので変えるべき)
# 書き込み対象のシートによって何を書き込むかが異なるので、base_records がいろいろ変わる
# base_records のソースが必ずしも CountingBonusVote のレコードであるとは限らない
def self.ranking(written_sheet_name)
return unless written_sheet_name.in?(
[
'ボ・お題小説',
'ボ・推し台詞',
'ボ・開票イラスト',
'ボ・選挙運動',
'ボ・OP・CLイラスト(オールキャラ)',
]
)

if written_sheet_name.in?(['ボ・推し台詞'])
base_records = CountingBonusVote.valid_records.where(bonus_category: :fav_quotes)
chara_columns = %i[chara_01 chara_02 chara_03 chara_04 chara_05 chara_06 chara_07 chara_08 chara_09 chara_10]
ret_array = []

base_records.each do |record|
character_names = chara_columns.map { |c| record[c] }
character_names = character_names.compact_blank.reject { |el| el == "FALSE"}

# キャラが複数いる場合には分割する(一キャラ一台詞一レコード)
# この分割の結果、ツイート人数(DM人数)とキャラレコード数が一致しなくなることに注意する
character_names.each do |character_name|
inserted_hash = {}

inserted_hash[:vote_method] = record.vote_method
inserted_hash[:character_name] = character_name

ret_array << inserted_hash
end
end

return ret_array
end
end

# 「推し台詞」
def self.all_fav_quote_character_names_including_duplicated
chara_columns = %i[chara_01 chara_02 chara_03 chara_04 chara_05 chara_06 chara_07 chara_08 chara_09 chara_10]
character_names = []
Expand All @@ -28,8 +215,7 @@ def self.all_fav_quote_character_names_including_duplicated
character_names += CountingBonusVote.valid_records.where(bonus_category: :fav_quotes).pluck(column)
end

# TODO: compact_blank が使えるはず
character_names.compact.reject(&:empty?).sort
character_names.compact_blank.sort
end

def self.fav_quote_character_name_to_number_of_votes
Expand All @@ -40,14 +226,15 @@ def self.fav_quote_ranking_style
Presenter::Counting.key_to_rank_number_by_sosenkyo_style(fav_quote_character_name_to_number_of_votes)
end

# 「お題小説」
def short_stories_theme
return 'そういえば' if tweet&.id_number == 1541078991146467329 # 例外的な処理

return if bonus_category != 'short_stories'

pickuped_theme_names = ['記念', 'そういえば', 'フリー', 'その他']

# NOTE: 一般的な記述の中に「お題」の語句が含まれていると誤判定する
# NOTE: 一般的な記述の中に「お題」の語句が含まれていると誤判定してしまうので注意する
this_theme = pickuped_theme_names.find do |theme|
contents.include?(theme)
end
Expand All @@ -56,4 +243,13 @@ def short_stories_theme
'その他' => 'フリー'
}[this_theme] || this_theme
end

def contents_resource
case vote_method
when 'by_tweet'
tweet
when 'by_direct_message'
direct_message
end
end
end
9 changes: 4 additions & 5 deletions app/models/counting_unite_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class CountingUniteAttack < ApplicationRecord
scope :by_tweet, -> { where(vote_method: :by_tweet) }
scope :by_dm, -> { where(vote_method: :by_direct_message) }

enum vote_method: { by_tweet: 0, by_direct_message: 1, by_others: 99 }, _prefix: true
enum vote_method: { by_tweet: 0, by_direct_message: 1, op_cl_illustrations_bonus: 2, by_others: 99 }, _prefix: true

def self.full_ranking
group(:product_name, :unite_attack_name).having('unite_attack_name is not null').order('count_all desc').count
def self.ranking
group(:product_name, :unite_attack_name, :kana).having('unite_attack_name is not null').order('count_all desc').count
end

def self.product_name_ranking
Expand All @@ -42,8 +42,7 @@ def self.invalid_records_whose_product_name_is_incorrect

# 不正レコードのチェッカ
def self.invalid_records_whose_attack_name_is_incorrect
# TODO: compact_blank が使えるはず
correct_attack_names = OnRawSheetUniteAttack.pluck(:name, :name_en).flatten.reject(&:empty?)
correct_attack_names = OnRawSheetUniteAttack.pluck(:name, :name_en).flatten.compact_blank

invalid_records = []

Expand Down
17 changes: 15 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def all_counting_records
end

def on_all_character_division_all_character_names
# TODO: compact_blank が使えるはず
counting_all_characters.map(&:three_chara_names).flatten.compact.reject(&:empty?)
counting_all_characters.map(&:three_chara_names).flatten.compact_blank
end

def on_all_character_division_voting_over_three?
Expand All @@ -58,4 +57,18 @@ def on_all_character_division_voting_over_three?
def on_all_character_division_voting_to_the_same_characters?
on_all_character_division_all_character_names.size != on_all_character_division_all_character_names.uniq.size
end

def all_tweets_and_dms
all_characters_tweets = counting_all_characters.valid_records
unite_attacks_tweets = counting_unite_attacks.valid_records
bonus_votes_tweets = counting_bonus_votes.valid_records
dms = direct_messages # DM の性質上、valid_records のスコープは無い

{
all_characters_tweets: all_characters_tweets,
unite_attacks_tweets: unite_attacks_tweets,
bonus_votes_tweets: bonus_votes_tweets,
dms: dms
}
end
end
15 changes: 9 additions & 6 deletions app/services/sheets/counting/all_characters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def self.import_via_tweet
'[DONE] Sheets::Counting::AllCharacters.import_via_tweet'
end

def self.import_via_dm
def self.import_via_dm # rubocop:disable Metrics/MethodLength
sheet_names = YAML.load_file(Rails.root.join('config/counting_sheet_names.yml'))['names']

# rubocop:disable Metrics/BlockLength
Expand Down Expand Up @@ -91,28 +91,29 @@ def self.import_via_dm
input_10: row[22]
}

# DMの書式が自由すぎるので、こちらで条件を吸収する
next if column_vs_value[:category] != '①オールキャラ部門' && column_vs_value[:category] != '両部門'
next unless column_vs_value[:category].in?(['①オールキャラ部門', '両部門', 'ボ・OP・CLイラスト'])
next if column_vs_value[:is_invisible] == 'TRUE' || column_vs_value[:is_out_of_counting] == 'TRUE'
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

# NOTE: dm&.user は不要としてもいい
# NOTE: dm&.user は実質不要っぽい
user = dm&.user || create_or_find_by_user(column_vs_value)
user_id = user&.id

this_vote_method = column_vs_value[:category] == 'ボ・OP・CLイラスト' ? :op_cl_illustrations_bonus : :by_direct_message

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

# 「両部門の場合は、N列とO列に協力攻撃、P列Q列R列にオールキャラ部門を入力する」という例外規定
# カテゴリごとにキャラ名が入っている列が異なる(もっときれいに書きたい)
case column_vs_value[:category]
when '①オールキャラ部門'
chara_1 = column_vs_value[:input_01]
Expand All @@ -122,6 +123,8 @@ def self.import_via_dm
chara_1 = column_vs_value[:input_03]
chara_2 = column_vs_value[:input_04]
chara_3 = column_vs_value[:input_05]
when 'ボ・OP・CLイラスト'
chara_1 = column_vs_value[:input_03]
end

mutable_attrs = {
Expand Down
Loading

0 comments on commit df4220f

Please sign in to comment.