diff --git a/app/controllers/skills_controller.rb b/app/controllers/skills_controller.rb index 05a890f..f58ebcd 100644 --- a/app/controllers/skills_controller.rb +++ b/app/controllers/skills_controller.rb @@ -1,7 +1,7 @@ class SkillsController < ApplicationController before_action :set_skill, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!, only: [:edit, :update, :destroy, :manage, :import] - #before_action :signed_in_user, only: [:edit, :create, :destroy, :update] + #before_action :signed_in_user, only: [:edit, :create, :destroy, :update] #before_action :correct_user, only: [:edit, :destroy, :update, :destroy] def self.kind_choices diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 5ee76ec..a1cea38 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -35,79 +35,80 @@ def show def drum @user = User.find(params[:id]) - - @hot= @user.skills.find_by_sql(['SELECT s.* - FROM skills AS s - WHERE s.user_id = ? AND - (s.music_id >= 712) AND - (s.kind BETWEEN 0 AND 3) - AND NOT EXISTS - ( SELECT 1 FROM skills AS t - WHERE s.music_id = t.music_id AND - s.user_id = t.user_id AND - s.sp < t.sp AND (t.kind BETWEEN 0 AND 3)) - ORDER BY sp DESC', @user.id]) - @other = @user.skills.find_by_sql(['SELECT s.* - FROM skills AS s - WHERE s.user_id = ? AND - (s.music_id BETWEEN 1 AND 711) AND - (s.kind BETWEEN 0 AND 3) - AND NOT EXISTS - ( SELECT 1 FROM skills AS t - WHERE s.music_id = t.music_id AND - s.user_id = t.user_id AND - s.sp < t.sp AND (t.kind BETWEEN 0 AND 3)) - ORDER BY sp DESC', @user.id]) - + @hot = fetch_skill(user: @user, is_hot: true, is_drum: true) + @other = fetch_skill(user: @user, is_hot: false, is_drum: true) ActiveRecord::Associations::Preloader.new.preload(@hot, :music) ActiveRecord::Associations::Preloader.new.preload(@other, :music) - #@hot = @user.skills.where(music_id: 712..900, kind: 0..3).order("sp DESC").group("music_id").order("sp DESC") - #@other = @user.skills.where(music_id: 1..711, kind: 0..3).order("sp DESC").group("music_id").order("sp DESC")# 終端位置変更の必要あり # 必要な情報をフェッチ @hot_sp = @user.dhot @other_sp = @user.dother @skill_sp = @user.d @all_sp = @user.dall + + return if params[:rival] == nil + + # ライバル情報を取得 + @rival = User.find_by_id(params[:rival]) + if @rival == nil then + flash[:error] = "ID" + params[:rival].to_s + "のユーザは存在しません" + return + end + + # スキル情報取得(ライバル) + @hot_rival = fetch_skill(user: @rival, is_hot: true, is_drum: true) + @other_rival = fetch_skill(user: @rival, is_hot: false, is_drum: true) + ActiveRecord::Associations::Preloader.new.preload(@hot_rival, :music) + ActiveRecord::Associations::Preloader.new.preload(@other_rival, :music) + + # 必要な情報をフェッチ(ライバル) + @hot_sp_rival = @rival.dhot + @other_sp_rival = @rival.dother + @skill_sp_rival = @rival.d + @all_sp_rival = @rival.dall + + # 自分と相手のスキルをDBから取得して結合 + @merged_skill_hot = merge_rival_score(@hot, @hot_rival) + @merged_skill_other = merge_rival_score(@other, @other_rival) end def guitar @user = User.find(params[:id]) - @hot= @user.skills.find_by_sql(['SELECT s.* - FROM skills AS s - WHERE s.user_id = ? AND - (s.music_id >= 712) AND - (s.kind BETWEEN 4 AND 11) - AND NOT EXISTS - ( SELECT 1 FROM skills AS t - WHERE s.music_id = t.music_id AND - s.user_id = t.user_id AND - s.sp < t.sp AND (t.kind BETWEEN 4 AND 11)) - ORDER BY sp DESC', @user.id]) - @other = @user.skills.find_by_sql(['SELECT s.* - FROM skills AS s - WHERE s.user_id = ? AND - (s.music_id BETWEEN 1 AND 711) AND - (s.kind BETWEEN 4 AND 11) - AND NOT EXISTS - ( SELECT 1 FROM skills AS t - WHERE s.music_id = t.music_id AND - s.user_id = t.user_id AND - s.sp < t.sp AND (t.kind BETWEEN 4 AND 11)) - ORDER BY sp DESC', @user.id]) - - + @hot = fetch_skill(user: @user, is_hot: true, is_drum: false) + @other = fetch_skill(user: @user, is_hot: false, is_drum: false) ActiveRecord::Associations::Preloader.new.preload(@hot, :music) ActiveRecord::Associations::Preloader.new.preload(@other, :music) - #@hot = @user.skills.where(music_id: 712..900, kind: 4..11).order("sp DESC").group("music_id").order("sp DESC") - #@other = @user.skills.where(music_id: 1..711, kind: 4..11).order("sp DESC").group("music_id").order("sp DESC") # 終端位置変更の必要あり - # 必要な情報をフェッチ @hot_sp = @user.ghot @other_sp = @user.gother @skill_sp = @user.g @all_sp = @user.gall + + return if params[:rival] == nil + + # ライバル情報を取得 + @rival = User.find_by_id(params[:rival]) + if @rival == nil then + flash[:error] = "ID" + params[:rival].to_s + "のユーザは存在しません" + return + end + + # スキル情報取得(ライバル) + @hot_rival = fetch_skill(user: @rival, is_hot: true, is_drum: false) + @other_rival = fetch_skill(user: @rival, is_hot: false, is_drum: false) + ActiveRecord::Associations::Preloader.new.preload(@hot_rival, :music) + ActiveRecord::Associations::Preloader.new.preload(@other_rival, :music) + + # 必要な情報をフェッチ(ライバル) + @hot_sp_rival = @rival.ghot + @other_sp_rival = @rival.gother + @skill_sp_rival = @rival.g + @all_sp_rival = @rival.gall + + # 自分と相手のスキルをDBから取得して結合 + @merged_skill_hot = merge_rival_score(@hot, @hot_rival) + @merged_skill_other = merge_rival_score(@other, @other_rival) end def new @@ -293,6 +294,72 @@ def user_params :password_confirmation, :g_comment, :d_comment, :g, :ghot, :gohter, :gall, :d, :dhot, :dother, :dall, :place) end + def fetch_skill(user:, is_hot: true, is_drum: true) + if is_hot then + music_id_min = 712 + music_id_max = 900 + else + music_id_min = 1 + music_id_max = 711 + end + if is_drum then + music_kind_min = 0 + music_kind_max = 3 + else + music_kind_min = 4 + music_kind_max = 11 + end + return user.skills.find_by_sql( ['SELECT s.* + FROM skills AS s + WHERE s.user_id = :user_id AND + (s.music_id BETWEEN :music_id_min AND :music_id_max) AND + (s.kind BETWEEN :music_kind_min AND :music_kind_max) + AND NOT EXISTS + ( SELECT 1 FROM skills AS t + WHERE s.music_id = t.music_id AND + s.user_id = t.user_id AND + s.sp < t.sp AND (t.kind BETWEEN :music_kind_min AND :music_kind_max)) + ORDER BY sp DESC', + {user_id: user.id, + music_id_min: music_id_min, + music_id_max: music_id_max, + music_kind_min: music_kind_min, + music_kind_max: music_kind_max}] ) + end + + # 曲名をキーに自分とライバルのスキル情報を結合する + def merge_rival_score(skill_me, skill_rival) + merged_skill = {} + skill_me.each do |skill| + merged_skill[skill.music] = {} if merged_skill[skill.music] == nil + merged_skill[skill.music]["me"] = skill + end + + skill_rival.each do |skill| + merged_skill[skill.music] = {} if merged_skill[skill.music] == nil + merged_skill[skill.music]["rival"] = skill + end + + merged_skill.each do |key, value| + if value["me"] == nil then + value["me"] = Marshal.load(Marshal.dump(value["rival"])) + value["me"].kind = -1 + value["me"].rate = 0.0 + value["me"].sp = 0.0 + value["me"].isfc = false + value["me"].id = -1 + elsif value["rival"] == nil then + value["rival"] = Marshal.load(Marshal.dump(value["me"])) + value["rival"].kind = -1 + value["rival"].rate = 0.0 + value["rival"].sp = 0.0 + value["rival"].isfc = false + value["rival"].id = -1 + end + end + return merged_skill + end + # Before actions #def signed_in_user diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 6a6d8b1..dea76a1 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -114,6 +114,8 @@ def show_kind (kind) "EXT(B)" when 11 "MAS(B)" + else + " - " end end diff --git a/app/views/skills/new.html.erb b/app/views/skills/new.html.erb index e541d29..ea5c515 100644 --- a/app/views/skills/new.html.erb +++ b/app/views/skills/new.html.erb @@ -4,49 +4,47 @@
<%= form_for(@skill) do |s| %> - <%= render 'shared/error_messages', object: s.object %> - -
- <%= s.label :name, "曲名", :class => "col-xs-offset-1 control-label" %> -
- <%= s.collection_select :music_id, Music.all.order("name ASC"), :id, :name, {}, {:class => 'searchable form-control', autofocus: true} %> -
+ <%= render 'shared/error_messages', object: s.object %> +
+ <%= s.label :name, "曲名", :class => "col-xs-offset-1 control-label" %> +
+ <%= s.collection_select :music_id, Music.all.order("name ASC"), :id, :name, {}, {:class => 'searchable form-control', autofocus: true} %>
+
-
- <%= s.label :kind, "難易度", :class => "col-xs-offset-1 control-label" %> -
- <%= s.select :kind, kind_choices, {}, {:class => "form-control"} %> -
+
+ <%= s.label :kind, "難易度", :class => "col-xs-offset-1 control-label" %> +
+ <%= s.select :kind, kind_choices, {}, {:class => "form-control"} %>
+
-
- <%= s.label :rate, "達成率", :class => "col-xs-offset-1 control-label" %> -
- <%= s.text_field :rate, placeholder: "例: 86.79", class: "form-control" %> -
+
+ <%= s.label :rate, "達成率", :class => "col-xs-offset-1 control-label" %> +
+ <%= s.text_field :rate, placeholder: "例: 86.79", class: "form-control" %>
+
-
- <%= s.label :comment, "コメント", :class => "col-xs-offset-1 control-label" %> -
- <%= s.text_field :comment, placeholder: "省略可", class: "form-control" %> -
+
+ <%= s.label :comment, "コメント", :class => "col-xs-offset-1 control-label" %> +
+ <%= s.text_field :comment, placeholder: "省略可", class: "form-control" %>
+
-
-
- <%= s.check_box :isfc %> - <%= s.label :isfc, "FULL COMBO", :class => "control-label" %> -
- +
+
+ <%= s.check_box :isfc %> + <%= s.label :isfc, "FULL COMBO", :class => "control-label" %>
+
-
-
- <%= s.submit "登録", class: "btn btn-large btn-primary" %> -
+
+
+ <%= s.submit "登録", class: "btn btn-large btn-primary" %>
+
<% end %>
diff --git a/app/views/users/_skilltable_with_rival.html.erb b/app/views/users/_skilltable_with_rival.html.erb new file mode 100644 index 0000000..b3e4864 --- /dev/null +++ b/app/views/users/_skilltable_with_rival.html.erb @@ -0,0 +1,65 @@ +

Hot

+ + + + + + + + + + + + + + <% @merged_skill_hot.each do |music, skill| %> + > + + + + + + + + <% end %> + +
曲名レベル達成率スキルスキル差分
+ <% if current_user == @user and skill["me"].id >= 0 %> + <%= link_to music.name, edit_skill_path(skill["me"]) %> + <% else %> + <%= music.name %> + <% end %> + <%= show_kind(skill["me"].kind) %>
<%= show_kind(skill["rival"].kind) %>
<%= number_with_precision(skill["me"].rate,precision: 2) %>%
<%= number_with_precision(skill["rival"].rate,precision: 2) %>%
<%= number_with_precision(skill["me"].sp,precision: 2) %>
<%= number_with_precision(skill["rival"].sp,precision: 2) %>
<%= number_with_precision((skill["me"].sp - skill["rival"].sp),precision: 2) %>
+ +

Other

+ + + + + + + + + + + + + + <% @merged_skill_other.each do |music, skill| %> + > + + + + + + + + <% end %> + +
曲名レベル達成率スキルスキル差分
+ <% if current_user == @user and skill["me"].id >= 0 %> + <%= link_to music.name, edit_skill_path(skill["me"]) %> + <% else %> + <%= music.name %> + <% end %> + <%= show_kind(skill["me"].kind) %>
<%= show_kind(skill["rival"].kind) %>
<%= number_with_precision(skill["me"].rate,precision: 2) %>%
<%= number_with_precision(skill["rival"].rate,precision: 2) %>%
<%= number_with_precision(skill["me"].sp,precision: 2) %>
<%= number_with_precision(skill["rival"].sp,precision: 2) %>
<%= number_with_precision((skill["me"].sp - skill["rival"].sp),precision: 2) %>
diff --git a/app/views/users/_sptable.html.erb b/app/views/users/_sptable.html.erb index 641888d..5a37c91 100644 --- a/app/views/users/_sptable.html.erb +++ b/app/views/users/_sptable.html.erb @@ -1,29 +1,38 @@ 最終更新: <%= updated_at %> - - - - - - + + + + + + + <% skillcolor = get_skillcolor(sp) %> - - - - + + + + + + + <% if name_rival != nil %> + + <% skillcolor = get_skillcolor(sp_rival) %> + + + + + + + + + + + + + <% end %>
SkillHotOtherAll
NameSkillHotOtherAll
-
><%= number_with_precision sp, precision: 2 %>
-
-
><%= number_with_precision hot, precision: 2 %>
-
-
><%= number_with_precision other, precision: 2 %>
-
-
><%= number_with_precision all, precision: 2 %>
-
><%= name %>
><%= number_with_precision sp, precision: 2 %>
><%= number_with_precision hot, precision: 2 %>
><%= number_with_precision other, precision: 2 %>
><%= number_with_precision all, precision: 2 %>
><%= name_rival %>
><%= number_with_precision sp_rival, precision: 2 %>
><%= number_with_precision hot_rival, precision: 2 %>
><%= number_with_precision other_rival, precision: 2 %>
><%= number_with_precision all_rival, precision: 2 %>
差分
<%= number_with_precision sp - sp_rival, precision: 2 %>
<%= number_with_precision hot - hot_rival, precision: 2 %>
<%= number_with_precision other - other_rival, precision: 2 %>
<%= number_with_precision all - all_rival, precision: 2 %>
- - diff --git a/app/views/users/drum.html.erb b/app/views/users/drum.html.erb index 0214d7a..661b846 100644 --- a/app/views/users/drum.html.erb +++ b/app/views/users/drum.html.erb @@ -9,5 +9,21 @@
<%= simple_format @user.d_comment %>
-<%= render partial: 'sptable', locals: {sp: @user.d, hot: @user.dhot, other: @user.dother, all: @user.dall, updated_at: @user.skill_updated_at_d} %> -<%= render 'skilltable' %> + +<%= form_tag(drum_user_path, method: "get") do %> + <%= label_tag(:rival, "ライバルID:") %> + <% if @rival == nil then %> + <%= text_field_tag(:rival) %> + <% else %> + <%= text_field_tag(:rival, @rival.id) %> + <% end %> + <%= submit_tag("比較") %> +<% end %> + +<% if @rival == nil then %> + <%= render partial: 'sptable', locals: { name: @user.name, sp: @user.d, hot: @user.dhot, other: @user.dother, all: @user.dall, updated_at: @user.skill_updated_at_d, name_rival: nil } %> + <%= render 'skilltable' %> +<% else %> + <%= render partial: 'sptable', locals: { name: @user.name, sp: @user.d, hot: @user.dhot, other: @user.dother, all: @user.dall, updated_at: @user.skill_updated_at_d, name_rival: @rival.name, sp_rival: @rival.d, hot_rival: @rival.dhot, other_rival: @rival.dother, all_rival: @rival.dall } %> + <%= render 'skilltable_with_rival' %> +<% end %> diff --git a/app/views/users/guitar.html.erb b/app/views/users/guitar.html.erb index 903ab99..12e2ee3 100644 --- a/app/views/users/guitar.html.erb +++ b/app/views/users/guitar.html.erb @@ -9,5 +9,20 @@ <%= simple_format @user.g_comment %>
-<%= render partial: 'sptable', locals: {sp: @user.g, hot: @user.ghot, other: @user.gother, all: @user.gall, updated_at: @user.skill_updated_at_g} %> -<%= render 'skilltable' %> +<%= form_tag(guitar_user_path, method: "get") do %> + <%= label_tag(:rival, "ライバルID:") %> + <% if @rival == nil then %> + <%= text_field_tag(:rival) %> + <% else %> + <%= text_field_tag(:rival, @rival.id) %> + <% end %> + <%= submit_tag("比較") %> +<% end %> + +<% if @rival == nil then %> + <%= render partial: 'sptable', locals: { name: @user.name, sp: @user.g, hot: @user.ghot, other: @user.gother, all: @user.gall, updated_at: @user.skill_updated_at_g, name_rival: nil } %> + <%= render 'skilltable' %> +<% else %> + <%= render partial: 'sptable', locals: { name: @user.name, sp: @user.g, hot: @user.ghot, other: @user.gother, all: @user.gall, updated_at: @user.skill_updated_at_g, name_rival: @rival.name, sp_rival: @rival.g, hot_rival: @rival.ghot, other_rival: @rival.gother, all_rival: @rival.gall } %> + <%= render 'skilltable_with_rival' %> +<% end %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 2439551..defaeb3 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -12,8 +12,8 @@

<%= link_to "Drummania のスキル表へ", drum_user_path %>

-<%= render partial: 'sptable', locals: {sp: @user.d, hot: @user.dhot, other: @user.dother, all: @user.dall, updated_at: @user.skill_updated_at_d} %> +<%= render partial: 'sptable', locals: {name: @user.name, sp: @user.d, hot: @user.dhot, other: @user.dother, all: @user.dall, updated_at: @user.skill_updated_at_d, name_rival: nil} %>

<%= link_to "GuitarFreaks のスキル表へ", guitar_user_path %>

-<%= render partial: 'sptable', locals: {sp: @user.g, hot: @user.ghot, other: @user.gother, all: @user.gall, updated_at: @user.skill_updated_at_g} %> +<%= render partial: 'sptable', locals: {name: @user.name, sp: @user.g, hot: @user.ghot, other: @user.gother, all: @user.gall, updated_at: @user.skill_updated_at_g, name_rival: nil} %>