Skip to content

Commit

Permalink
Merge pull request #13 from cdphp/feature/add-new-names
Browse files Browse the repository at this point in the history
chat
  • Loading branch information
oiahoon authored Aug 31, 2017
2 parents df7e79c + 4d832ea commit fa10f73
Show file tree
Hide file tree
Showing 14 changed files with 726 additions and 24 deletions.
10 changes: 1 addition & 9 deletions poker/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,12 @@ GEM
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
arel (7.1.4)
awesome_print (1.7.0)
babel-source (5.8.35)
babel-transpiler (0.7.0)
babel-source (>= 4.0, < 6)
execjs (~> 2.0)
browserify-rails (4.2.0)
addressable (>= 2.4.0)
railties (>= 4.0.0, < 5.2)
sprockets (>= 3.6.0)
builder (3.2.3)
byebug (9.0.6)
case_transform (0.2)
Expand Down Expand Up @@ -103,7 +97,6 @@ GEM
nio4r (2.0.0)
nokogiri (1.7.1)
mini_portile2 (~> 2.1.0)
public_suffix (3.0.0)
puma (3.8.2)
rack (2.0.1)
rack-test (0.6.3)
Expand Down Expand Up @@ -195,7 +188,6 @@ PLATFORMS
DEPENDENCIES
active_model_serializers (~> 0.10.4)
awesome_print
browserify-rails
byebug
chartjs-ror
coffee-rails (~> 4.1.0)
Expand All @@ -216,4 +208,4 @@ DEPENDENCIES
web-console (~> 3.0)

BUNDLED WITH
1.12.5
1.15.1
4 changes: 3 additions & 1 deletion poker/app/channels/chat_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def unsubscribed
end

def speak data
@chat = Chat.new message: data['message'], user_id: data['me']
group_id, name_id = PokerEncoder::decode(data['me']).split('x')
@chat = Chat.new( message: data['message'], user_id: name_id || 0,
group_id: group_id)
@chat.speaking
end

Expand Down
15 changes: 15 additions & 0 deletions poker/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ApplicationController < ActionController::Base

serialization_scope :view_context

CHARACTER_GROUP = ChatName.names.keys

def require_login
unless current_user && current_user.id && current_room && current_room.id
redirect_to (controller_name == 'rooms') ? new_usersession_path(room_id: params[:id]) : new_usersession_path
Expand Down Expand Up @@ -35,6 +37,19 @@ def current_room
Room.find_by(id: session[:current_room_id])
end

def chat_session
group_id, name_id = nil, nil
group_id, name_id = PokerEncoder::decode(session[:chat_user_id]).split('x')
ensure
unless group_id && name_id
group_id = rand(CHARACTER_GROUP.size)
group_name = CHARACTER_GROUP[group_id]
name_id = rand(ChatName.names[group_name].size)
session[:chat_user_id] = PokerEncoder::encode("#{group_id}x#{name_id}")
session[:user_name] = ChatName.names[group_name][name_id]
end
end

private
def print_user_sessions
if Rails.env.eql? "development"
Expand Down
6 changes: 2 additions & 4 deletions poker/app/controllers/chats_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ChatsController < ApplicationController
before_action :chat_session

def index
session[:chat_user_id] = rand(::Sanguo.names.size)
load_chats
end

Expand All @@ -21,8 +21,6 @@ def load_chats


def who_am_i
session[:user_id] ||= rand(::Sanguo.names.size)
session[:user_name] ||= ::Sanguo.names[session[:user_id]]
{ id: session[:user_id], name: session[:user_name] }
{ id: session[:chat_user_id], name: session[:user_name] }
end
end
3 changes: 2 additions & 1 deletion poker/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module ApplicationHelper

def display_user message
Sanguo.names[message.user_id.to_i]
group_name = ChatName.names.keys[message.group_id.to_i]
ChatName.names[group_name][message.user_id.to_i]
end

# def current_room
Expand Down
17 changes: 17 additions & 0 deletions poker/app/lib/chat_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ChatName
@names = {}

class << self
attr_accessor :names
end

include ChatNames::Sanguo
include ChatNames::Honglou
include ChatNames::Shuihu


def push_names group_name, name_array
@names[group_name] = name_array
end

end
Loading

0 comments on commit fa10f73

Please sign in to comment.