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 @@
+ | 曲名 | +レベル | +達成率 | +スキル | +スキル差分 | +
---|---|---|---|---|---|
+ | + <% 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) %> | +
+ | 曲名 | +レベル | +達成率 | +スキル | +スキル差分 | +
---|---|---|---|---|---|
+ | + <% 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) %> | +
Skill | -Hot | -Other | -All | -|||||
---|---|---|---|---|---|---|---|---|
Name | +Skill | +Hot | +Other | +All | +||||
- ><%= 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 %> |