Skip to content

Commit

Permalink
Merge pull request #158 from true-runes/development
Browse files Browse the repository at this point in the history
v2.2.0
  • Loading branch information
nikukyugamer authored Jun 24, 2022
2 parents aba1a1b + 7b638bb commit effdc57
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 9 deletions.
5 changes: 5 additions & 0 deletions app/controllers/realtime_report_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RealtimeReportController < ApplicationController
def index
render json: Counting::RealtimeReport.run
end
end
18 changes: 10 additions & 8 deletions app/lib/event_basic_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ def self.end_datetime
Time.zone.parse('2022-06-26 23:59:59')
end

def used_hashtags
[
'#幻水総選挙2022',
'#幻水総選挙2022協力攻撃',
'#幻水総選挙運動',
'#幻水総選挙お題小説',
'#幻水総選挙推し台詞',
]
def self.used_hashtags
YAML.load_file(
Rails.root.join('config/used_hashtags.yml')
)['used_hashtags']
end

def self.sheet_names
YAML.load_file(
Rails.root.join('config/sheet_names.yml')
)['sheet_names']
end
end
3 changes: 2 additions & 1 deletion app/lib/sheet_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ def self.write_rows(sheet_id:, range:, values:)
service = SheetService.new.service

# 戻り値に updated_cells, updated_columns, updated_range, updated_rows メソッドが生えてる
# range は A1 形式しか選択できない (https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/SheetsV4/SheetsService.html#update_spreadsheet_value-instance_method)
service.update_spreadsheet_value(
sheet_id,
range, # 貼り付け開始フォーカス位置
Google::Apis::SheetsV4::ValueRange.new(values: values), # values は行列を示す二次元配列になる
Google::Apis::SheetsV4::ValueRange.new(values: values), # values は行列を示す二次元配列になることに注意する
value_input_option: VALUE_INPUT_OPTION
)
end
Expand Down
120 changes: 120 additions & 0 deletions app/services/counting/realtime_report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
module Counting
class RealtimeReport
def self.run
{
all_characters: data(Tweet.gensosenkyo_2022_votes_for_api),
unite_attacks: data(Tweet.unite_attacks_votes_for_api),
short_stories: limited_data(Tweet.short_stories_for_api),
fav_quotes: limited_data(Tweet.fav_quotes_for_api),
sosenkyo_campaigns: limited_data(Tweet.sosenkyo_campaigns_for_api)
}
end

# TODO: リファクタリング
def self.data(tweets) # rubocop:disable Metrics/MethodLength
{
sum: tweets.count,
votes_per_day: {
'2022-06-24': tweets.where(tweeted_at: Time.zone.parse('2022-06-24 21:00:00')..Time.zone.parse('2022-06-24 23:59:59')).count,
'2022-06-25': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 00:00:00')..Time.zone.parse('2022-06-25 23:59:59')).count,
'2022-06-26': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 00:00:00')..Time.zone.parse('2022-06-26 23:59:59')).count
},
votes_per_hour: {
'2022-06-24': [
'00': 0,
'01': 0,
'02': 0,
'03': 0,
'04': 0,
'05': 0,
'06': 0,
'07': 0,
'08': 0,
'09': 0,
'10': 0,
'11': 0,
'12': 0,
'13': 0,
'14': 0,
'15': 0,
'16': 0,
'17': 0,
'18': 0,
'19': 0,
'20': 0,
'21': tweets.where(tweeted_at: Time.zone.parse('2022-06-24 21:00:00')..Time.zone.parse('2022-06-24 21:59:59')).count,
'22': tweets.where(tweeted_at: Time.zone.parse('2022-06-24 22:00:00')..Time.zone.parse('2022-06-24 22:59:59')).count,
'23': tweets.where(tweeted_at: Time.zone.parse('2022-06-24 23:00:00')..Time.zone.parse('2022-06-24 23:59:59')).count,
],
'2022-06-25': [
'00': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 00:00:00')..Time.zone.parse('2022-06-25 00:59:59')).count,
'01': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 01:00:00')..Time.zone.parse('2022-06-25 01:59:59')).count,
'02': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 02:00:00')..Time.zone.parse('2022-06-25 02:59:59')).count,
'03': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 03:00:00')..Time.zone.parse('2022-06-25 03:59:59')).count,
'04': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 04:00:00')..Time.zone.parse('2022-06-25 04:59:59')).count,
'05': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 05:00:00')..Time.zone.parse('2022-06-25 05:59:59')).count,
'06': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 06:00:00')..Time.zone.parse('2022-06-25 06:59:59')).count,
'07': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 07:00:00')..Time.zone.parse('2022-06-25 07:59:59')).count,
'08': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 08:00:00')..Time.zone.parse('2022-06-25 08:59:59')).count,
'09': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 09:00:00')..Time.zone.parse('2022-06-25 09:59:59')).count,
'10': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 10:00:00')..Time.zone.parse('2022-06-25 10:59:59')).count,
'11': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 11:00:00')..Time.zone.parse('2022-06-25 11:59:59')).count,
'12': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 12:00:00')..Time.zone.parse('2022-06-25 12:59:59')).count,
'13': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 13:00:00')..Time.zone.parse('2022-06-25 13:59:59')).count,
'14': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 14:00:00')..Time.zone.parse('2022-06-25 14:59:59')).count,
'15': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 15:00:00')..Time.zone.parse('2022-06-25 15:59:59')).count,
'16': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 16:00:00')..Time.zone.parse('2022-06-25 16:59:59')).count,
'17': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 17:00:00')..Time.zone.parse('2022-06-25 17:59:59')).count,
'18': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 18:00:00')..Time.zone.parse('2022-06-25 18:59:59')).count,
'19': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 19:00:00')..Time.zone.parse('2022-06-25 19:59:59')).count,
'20': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 20:00:00')..Time.zone.parse('2022-06-25 20:59:59')).count,
'21': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 21:00:00')..Time.zone.parse('2022-06-25 21:59:59')).count,
'22': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 22:00:00')..Time.zone.parse('2022-06-25 22:59:59')).count,
'23': tweets.where(tweeted_at: Time.zone.parse('2022-06-25 23:00:00')..Time.zone.parse('2022-06-25 23:59:59')).count,
],
'2022-06-26': [
'00': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 00:00:00')..Time.zone.parse('2022-06-26 00:59:59')).count,
'01': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 01:00:00')..Time.zone.parse('2022-06-26 01:59:59')).count,
'02': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 02:00:00')..Time.zone.parse('2022-06-26 02:59:59')).count,
'03': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 03:00:00')..Time.zone.parse('2022-06-26 03:59:59')).count,
'04': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 04:00:00')..Time.zone.parse('2022-06-26 04:59:59')).count,
'05': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 05:00:00')..Time.zone.parse('2022-06-26 05:59:59')).count,
'06': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 06:00:00')..Time.zone.parse('2022-06-26 06:59:59')).count,
'07': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 07:00:00')..Time.zone.parse('2022-06-26 07:59:59')).count,
'08': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 08:00:00')..Time.zone.parse('2022-06-26 08:59:59')).count,
'09': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 09:00:00')..Time.zone.parse('2022-06-26 09:59:59')).count,
'10': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 10:00:00')..Time.zone.parse('2022-06-26 10:59:59')).count,
'11': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 11:00:00')..Time.zone.parse('2022-06-26 11:59:59')).count,
'12': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 12:00:00')..Time.zone.parse('2022-06-26 12:59:59')).count,
'13': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 13:00:00')..Time.zone.parse('2022-06-26 13:59:59')).count,
'14': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 14:00:00')..Time.zone.parse('2022-06-26 14:59:59')).count,
'15': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 15:00:00')..Time.zone.parse('2022-06-26 15:59:59')).count,
'16': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 16:00:00')..Time.zone.parse('2022-06-26 16:59:59')).count,
'17': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 17:00:00')..Time.zone.parse('2022-06-26 17:59:59')).count,
'18': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 18:00:00')..Time.zone.parse('2022-06-26 18:59:59')).count,
'19': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 19:00:00')..Time.zone.parse('2022-06-26 19:59:59')).count,
'20': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 20:00:00')..Time.zone.parse('2022-06-26 20:59:59')).count,
'21': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 21:00:00')..Time.zone.parse('2022-06-26 21:59:59')).count,
'22': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 22:00:00')..Time.zone.parse('2022-06-26 22:59:59')).count,
'23': tweets.where(tweeted_at: Time.zone.parse('2022-06-26 23:00:00')..Time.zone.parse('2022-06-26 23:59:59')).count,
]
},
# 言語別はここだけとする
votes_per_lang: {
ja: tweets.where(language: 'ja').count,
others: tweets.count - tweets.where(language: 'ja').count
}
}
end

def self.limited_data(tweets)
{
sum: tweets.count,
votes_per_lang: {
ja: tweets.where(language: 'ja').count,
others: tweets.count - tweets.where(language: 'ja').count
}
}
end
end
end
70 changes: 70 additions & 0 deletions app/services/natural_language/suggest_unite_attack_names.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module NaturalLanguage
class SuggestUniteAttackNames
# 現状、単にデータベースの値を見ているだけなので "NaturalLanguage" ではない
def self.exec(tweet_or_dm)
content_text = tweet_or_dm.content_text

# 戻り値は {:title=>"幻想水滸伝IV", :attack_name=>"麗しの友情攻撃"} の形になる
exec_by_text(content_text)
end

# 完全一致で調べている
def self.exec_by_text(content_text)
return { title: '', attack_name: '' } if content_text.nil? || content_text.lines.size < 3

content_text_title_name = content_text.lines[0].chomp
content_text_attack_name = content_text.lines[1].chomp

found_title_name = title_names.find { |title_name| title_name == content_text_title_name }

attack_names = OnRawSheetUniteAttack.where(
sheet_name: convert_title_name_to_sheet_name(found_title_name)
).pluck(:name, :name_en).flatten
found_attack_name = attack_names.find { |attack_name| attack_name == content_text_attack_name}

{ title: found_title_name || '', attack_name: found_attack_name || '' }
end

def self.title_names
[
'幻想水滸伝',
'Suikoden',
'幻想水滸伝II',
'Suikoden II',
'幻想水滸伝III',
'Suikoden III',
'幻想水滸伝IV',
'Suikoden IV',
'ラプソディア',
'Suikoden Tactics',
'幻想水滸伝V',
'Suikoden V',
'幻想水滸伝ティアクライス',
'Suikoden Tierkreis',
'幻想水滸伝 紡がれし百年の時',
'Suikoden The Woven Web of a Century',
]
end

def self.convert_title_name_to_sheet_name(title_name)
{
'幻想水滸伝' => '幻水I',
'Suikoden' => '幻水I',
'幻想水滸伝II' => '幻水II',
'Suikoden II' => '幻水II',
'幻想水滸伝III' => '幻水III',
'Suikoden III' => '幻水III',
'幻想水滸伝IV' => '幻水IV',
'Suikoden IV' => '幻水IV',
'ラプソディア' => 'Rhapsodia',
'Suikoden Tactics' => 'Rhapsodia',
'幻想水滸伝V' => '幻水V',
'Suikoden V' => '幻水V',
'幻想水滸伝ティアクライス' => 'TK',
'Suikoden Tierkreis' => 'TK',
'幻想水滸伝 紡がれし百年の時' => '紡時',
'Suikoden The Woven Web of a Century' => '紡時'
}[title_name]
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
get 'result_illustration_applications', to: 'result_illustration_applications#index', format: 'json'
get 'check_votes_and_bonuses', to: 'check_votes_and_bonuses#index', format: 'json'
get 'characters', to: 'characters#index', format: 'json'
get 'realtime_report', to: 'realtime_report#index', format: 'json'
end
12 changes: 12 additions & 0 deletions config/sheet_names.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sheet_names:
- ①オールキャラ部門
- ②協力攻撃部門
- DM・①オールキャラ部門
- DM・②協力攻撃部門
- ボーナス票・OP・CLイラスト
- ボーナス票・お題小説
- ボーナス票・開票イラスト
- ボーナス票・推し台詞
- ボーナス票・選挙運動
- 開発用スプレッドシート
- 統合用(予定)
6 changes: 6 additions & 0 deletions config/used_hashtags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
used_hashtags:
- '#幻水総選挙2022'
- '#幻水総選挙2022協力攻撃'
- '#幻水総選挙運動'
- '#幻水総選挙お題小説'
- '#幻水総選挙推し台詞'
37 changes: 37 additions & 0 deletions spec/requests/realtime_report_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'rails_helper'

RSpec.describe RealtimeReportController, type: :request do
describe '#index' do
it '期待通りのレスポンスが返ってくること' do
get realtime_report_path

expect(response).to have_http_status :ok

hashed_json = JSON.parse(response.body)

expect(hashed_json.keys).to match_array %w(
all_characters
unite_attacks
fav_quotes
short_stories
sosenkyo_campaigns
)

['all_characters', 'unite_attacks'].each do |key|
expect(hashed_json[key].keys).to match_array %w(
sum
votes_per_day
votes_per_hour
votes_per_lang
)
end

['fav_quotes', 'short_stories', 'sosenkyo_campaigns'].each do |key|
expect(hashed_json[key].keys).to match_array %w(
sum
votes_per_lang
)
end
end
end
end

0 comments on commit effdc57

Please sign in to comment.