Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ライバルスキル比較機能 #30

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/skills_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
173 changes: 120 additions & 53 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def show_kind (kind)
"EXT(B)"
when 11
"MAS(B)"
else
" - "
end
end

Expand Down
62 changes: 30 additions & 32 deletions app/views/skills/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,47 @@
<div class="row">
<div class="form-horizontal" role="form">
<%= form_for(@skill) do |s| %>
<%= render 'shared/error_messages', object: s.object %>

<div class="form-group">
<%= s.label :name, "曲名", :class => "col-xs-offset-1 control-label" %>
<div class="col-xs-offset-1 col-xs-10">
<%= s.collection_select :music_id, Music.all.order("name ASC"), :id, :name, {}, {:class => 'searchable form-control', autofocus: true} %>
</div>
<%= render 'shared/error_messages', object: s.object %>
<div class="form-group">
<%= s.label :name, "曲名", :class => "col-xs-offset-1 control-label" %>
<div class="col-xs-offset-1 col-xs-10">
<%= s.collection_select :music_id, Music.all.order("name ASC"), :id, :name, {}, {:class => 'searchable form-control', autofocus: true} %>
</div>
</div>

<div class="form-group">
<%= s.label :kind, "難易度", :class => "col-xs-offset-1 control-label" %>
<div class="col-xs-offset-1 col-xs-10">
<%= s.select :kind, kind_choices, {}, {:class => "form-control"} %>
</div>
<div class="form-group">
<%= s.label :kind, "難易度", :class => "col-xs-offset-1 control-label" %>
<div class="col-xs-offset-1 col-xs-10">
<%= s.select :kind, kind_choices, {}, {:class => "form-control"} %>
</div>
</div>

<div class="form-group">
<%= s.label :rate, "達成率", :class => "col-xs-offset-1 control-label" %>
<div class="col-xs-offset-1 col-xs-10">
<%= s.text_field :rate, placeholder: "例: 86.79", class: "form-control" %>
</div>
<div class="form-group">
<%= s.label :rate, "達成率", :class => "col-xs-offset-1 control-label" %>
<div class="col-xs-offset-1 col-xs-10">
<%= s.text_field :rate, placeholder: "例: 86.79", class: "form-control" %>
</div>
</div>

<div class="form-group">
<%= s.label :comment, "コメント", :class => "col-xs-offset-1 control-label" %>
<div class="col-xs-offset-1 col-xs-10">
<%= s.text_field :comment, placeholder: "省略可", class: "form-control" %>
</div>
<div class="form-group">
<%= s.label :comment, "コメント", :class => "col-xs-offset-1 control-label" %>
<div class="col-xs-offset-1 col-xs-10">
<%= s.text_field :comment, placeholder: "省略可", class: "form-control" %>
</div>
</div>

<div class="form-group">
<div class=" col-xs-offset-1 col-xs-10">
<%= s.check_box :isfc %>
<%= s.label :isfc, "FULL COMBO", :class => "control-label" %>
</div>

<div class="form-group">
<div class=" col-xs-offset-1 col-xs-10">
<%= s.check_box :isfc %>
<%= s.label :isfc, "FULL COMBO", :class => "control-label" %>
</div>
</div>

<div class="form-group">
<div class="col-xs-10 col-xs-offset-1">
<%= s.submit "登録", class: "btn btn-large btn-primary" %>
</div>
<div class="form-group">
<div class="col-xs-10 col-xs-offset-1">
<%= s.submit "登録", class: "btn btn-large btn-primary" %>
</div>
</div>
<% end %>
</div>
</div>
Expand Down
65 changes: 65 additions & 0 deletions app/views/users/_skilltable_with_rival.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<h2>Hot</h2>
<table id="sortableTable" class="table table-bordered table-hover table-condensed display compact">
<thead>
<tr>
<th></th>
<th>曲名</th>
<th>レベル</th>
<th>達成率</th>
<th>スキル</th>
<th>スキル差分</th>
</tr>
</thead>

<tbody>
<% @merged_skill_hot.each do |music, skill| %>
<tr class=<%= show_color(skill["me"].kind) %>>
<td></td>
<td>
<% if current_user == @user and skill["me"].id >= 0 %>
<%= link_to music.name, edit_skill_path(skill["me"]) %>
<% else %>
<%= music.name %>
<% end %>
</td>
<td><%= show_kind(skill["me"].kind) %><br /><%= show_kind(skill["rival"].kind) %></td>
<td><%= number_with_precision(skill["me"].rate,precision: 2) %>%<br /><%= number_with_precision(skill["rival"].rate,precision: 2) %>%</td>
<td><%= number_with_precision(skill["me"].sp,precision: 2) %><br /><%= number_with_precision(skill["rival"].sp,precision: 2) %></td>
<td><%= number_with_precision((skill["me"].sp - skill["rival"].sp),precision: 2) %></td>
</tr>
<% end %>
</tbody>
</table>

<h2>Other</h2>
<table id="sortableTable2" class="table table-bordered table-hover table-condensed display compact">
<thead>
<tr>
<th></th>
<th>曲名</th>
<th>レベル</th>
<th>達成率</th>
<th>スキル</th>
<th>スキル差分</th>
</tr>
</thead>

<tbody>
<% @merged_skill_other.each do |music, skill| %>
<tr class=<%= show_color(skill["me"].kind) %>>
<td></td>
<td>
<% if current_user == @user and skill["me"].id >= 0 %>
<%= link_to music.name, edit_skill_path(skill["me"]) %>
<% else %>
<%= music.name %>
<% end %>
</td>
<td><%= show_kind(skill["me"].kind) %><br /><%= show_kind(skill["rival"].kind) %></td>
<td><%= number_with_precision(skill["me"].rate,precision: 2) %>%<br /><%= number_with_precision(skill["rival"].rate,precision: 2) %>%</td>
<td><%= number_with_precision(skill["me"].sp,precision: 2) %><br /><%= number_with_precision(skill["rival"].sp,precision: 2) %></td>
<td><%= number_with_precision((skill["me"].sp - skill["rival"].sp),precision: 2) %></td>
</tr>
<% end %>
</tbody>
</table>
Loading