Commit e77701fc authored by 神楽坂玲奈's avatar 神楽坂玲奈

0.8.5, xmpp

parent ff51a04b
...@@ -20,7 +20,7 @@ end ...@@ -20,7 +20,7 @@ end
spec = Gem::Specification.new do |s| spec = Gem::Specification.new do |s|
s.name = 'mycard' s.name = 'mycard'
s.version = '0.8.4' s.version = '0.8.5'
s.extra_rdoc_files = ['README.txt', 'LICENSE.txt'] s.extra_rdoc_files = ['README.txt', 'LICENSE.txt']
s.summary = 'a card game' s.summary = 'a card game'
s.description = s.summary s.description = s.summary
......
#!/usr/bin/env ruby #!/usr/bin/env ruby
begin begin
Windows = RUBY_PLATFORM["win"] || RUBY_PLATFORM["ming"] Windows = RUBY_PLATFORM["win"] || RUBY_PLATFORM["ming"]
Dir.glob('post_update_*.rb').sort.each { |file| load file } Dir.glob('post_update_*.rb').sort.each { |file| load file }
Thread.abort_on_exception = true Thread.abort_on_exception = true
require_relative 'resolution' require_relative 'resolution'
require_relative 'announcement' require_relative 'announcement'
require_relative 'config' require_relative 'config'
require_relative 'association' require_relative 'association'
#i18n #i18n
require 'i18n' require 'i18n'
require 'locale' require 'locale'
I18n.load_path += Dir['locales/*.yml'] I18n.load_path += Dir['locales/*.yml']
I18n::Backend::Simple.include(I18n::Backend::Fallbacks) I18n::Backend::Simple.include(I18n::Backend::Fallbacks)
#读取配置文件 #读取配置文件
$config = Config.load $config = Config.load
Config.save Config.save
#读取命令行参数 #读取命令行参数
log = "log.log" log = "log.log"
log_level = "INFO" log_level = "INFO"
profile = nil profile = nil
ARGV.each do |arg| ARGV.each do |arg|
arg = arg.dup.force_encoding("UTF-8") arg = arg.dup.force_encoding("UTF-8")
arg.force_encoding("GBK") unless arg.valid_encoding? arg.force_encoding("GBK") unless arg.valid_encoding?
case arg case arg
when /--log=(.*)/ when /--log=(.*)/
log.replace $1 log.replace $1
when /--log-level=(.*)/ when /--log-level=(.*)/
log_level.replace $1 log_level.replace $1
when /--profile=(.*)/ when /--profile=(.*)/
profile = $1 profile = $1
when /^mycard:.*|\.ydk$|\.yrp$|\.deck$/ when /^mycard:.*|\.ydk$|\.yrp$|\.deck$/
require_relative 'quickstart' require_relative 'quickstart'
$scene = false $scene = false
when /register_association/ when /register_association/
Association.register Association.register
$scene = false $scene = false
end end
end end
unless $scene == false unless $scene == false
#加载文件 #加载文件
require 'logger' require 'logger'
require 'sdl' require 'sdl'
include SDL include SDL
require_relative 'dialog' require_relative 'dialog'
require_relative 'graphics' require_relative 'graphics'
require_relative 'window' require_relative 'window'
require_relative 'widget_msgbox' require_relative 'widget_msgbox'
#日志 #日志
if log == "STDOUT" #调试用 if log == "STDOUT" #调试用
log = STDOUT log = STDOUT
end end
$log = Logger.new(log) $log = Logger.new(log, 1, 1024000)
$log.level = Logger.const_get log_level $log.level = Logger.const_get log_level
#性能分析 #性能分析
if profile if profile
if profile == "STDOUT" if profile == "STDOUT"
profile = STDOUT profile = STDOUT
else else
profile = open(profile, 'w') profile = open(profile, 'w')
end end
require 'profiler' require 'profiler'
RubyVM::InstructionSequence.compile_option = { RubyVM::InstructionSequence.compile_option = {
:trace_instruction => true, :trace_instruction => true,
:specialized_instruction => false :specialized_instruction => false
} }
Profiler__::start_profile Profiler__::start_profile
end end
SDL::Event::APPMOUSEFOCUS = 1 SDL::Event::APPMOUSEFOCUS = 1
SDL::Event::APPINPUTFOCUS = 2 SDL::Event::APPINPUTFOCUS = 2
SDL::Event::APPACTIVE = 4 SDL::Event::APPACTIVE = 4
SDL.putenv ("SDL_VIDEO_CENTERED=1"); SDL.putenv ("SDL_VIDEO_CENTERED=1");
SDL.init(INIT_VIDEO) SDL.init(INIT_VIDEO)
WM::set_caption("MyCard", "MyCard") WM::set_caption("MyCard", "MyCard")
WM::icon = Surface.load("graphics/system/icon.gif") WM::icon = Surface.load("graphics/system/icon.gif")
$screen = Screen.open($config['screen']['width'], $config['screen']['height'], 0, HWSURFACE | ($config['screen']['fullscreen'] ? FULLSCREEN : 0)) $screen = Screen.open($config['screen']['width'], $config['screen']['height'], 0, HWSURFACE | ($config['screen']['fullscreen'] ? FULLSCREEN : 0))
TTF.init TTF.init
#声音 #声音
begin begin
SDL.init(INIT_AUDIO) SDL.init(INIT_AUDIO)
Mixer.open(Mixer::DEFAULT_FREQUENCY, Mixer::DEFAULT_FORMAT, Mixer::DEFAULT_CHANNELS, 1536) Mixer.open(Mixer::DEFAULT_FREQUENCY, Mixer::DEFAULT_FORMAT, Mixer::DEFAULT_CHANNELS, 1536)
Mixer.set_volume_music(60) Mixer.set_volume_music(60)
rescue rescue
nil nil
end end
#标题场景 #标题场景
require_relative 'scene_title' require_relative 'scene_title'
$scene = Scene_Title.new $scene = Scene_Title.new
#自动更新, 加载放到SDL前面会崩, 原因不明 #自动更新, 加载放到SDL前面会崩, 原因不明
require_relative 'update' require_relative 'update'
Update.start Update.start
WM::set_caption("MyCard v#{Update::Version}", "MyCard") WM::set_caption("MyCard v#{Update::Version}", "MyCard")
#文件关联 #文件关联
Association.start Association.start
#初始化完毕 #初始化完毕
$log.info("main") { "初始化成功" } $log.info("main") { "初始化成功" }
end end
rescue Exception => exception rescue Exception => exception
open('error-程序出错请到论坛反馈.txt', 'w') { |f| f.write [exception.inspect, *exception.backtrace].join("\n") } open('error-程序出错请到论坛反馈.txt', 'w') { |f| f.write [exception.inspect, *exception.backtrace].join("\n") }
$scene = false $scene = false
end end
#主循环 #主循环
begin begin
$scene.main while $scene $scene.main while $scene
rescue Exception => exception rescue Exception => exception
exception.backtrace.each { |backtrace| break if backtrace =~ /^(.*)\.rb:\d+:in `.*'"$/ } #由于脚本是从main.rb开始执行的,总会有个能匹配成功的文件 exception.backtrace.each { |backtrace| break if backtrace =~ /^(.*)\.rb:\d+:in `.*'"$/ } #由于脚本是从main.rb开始执行的,总会有个能匹配成功的文件
$log.fatal($1) { [exception.inspect, *exception.backtrace].collect { |str| str.force_encoding("UTF-8") }.join("\n") } $log.fatal($1) { [exception.inspect, *exception.backtrace].collect { |str| str.force_encoding("UTF-8") }.join("\n") }
$game.exit if $game $game.exit if $game
require_relative 'scene_error' require_relative 'scene_error'
$scene = Scene_Error.new $scene = Scene_Error.new
retry retry
ensure ensure
if profile if profile
Profiler__::print_profile(profile) Profiler__::print_profile(profile)
profile.close profile.close
end end
$log.close rescue nil $log.close rescue nil
end end
\ No newline at end of file
...@@ -66,7 +66,7 @@ class Scene_Lobby < Scene ...@@ -66,7 +66,7 @@ class Scene_Lobby < Scene
when Game_Event::AllUsers when Game_Event::AllUsers
@userlist.items = $game.users @userlist.items = $game.users
when Game_Event::AllRooms, Game_Event::AllServers when Game_Event::AllRooms, Game_Event::AllServers
@roomlist.items = p $game.rooms.find_all { |room| @roomlist.items = $game.rooms.find_all { |room|
$game.filter[:servers].include?(room.server) and $game.filter[:servers].include?(room.server) and
$game.filter[:waiting_only] ? (room.status == :wait) : true and $game.filter[:waiting_only] ? (room.status == :wait) : true and
$game.filter[:normal_only] ? (!room.tag? && (room.ot == 0) && (room.lp = 8000)) : true $game.filter[:normal_only] ? (!room.tag? && (room.ot == 0) && (room.lp = 8000)) : true
......
...@@ -2,7 +2,7 @@ require 'open-uri' ...@@ -2,7 +2,7 @@ require 'open-uri'
require "fileutils" require "fileutils"
require_relative 'card' require_relative 'card'
module Update module Update
Version = '0.8.4' Version = '0.8.5'
URL = "http://my-card.in/mycard/update.json?version=#{Version}" URL = "http://my-card.in/mycard/update.json?version=#{Version}"
class <<self class <<self
attr_reader :thumbnails, :images, :status attr_reader :thumbnails, :images, :status
......
#============================================================================== #==============================================================================
# ■ Scene_Title # ■ Scene_Title
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
#  title #  title
#============================================================================== #==============================================================================
require_relative 'widget_scrollbar' require_relative 'widget_scrollbar'
require_relative 'widget_inputbox' require_relative 'widget_inputbox'
require_relative 'chatmessage' require_relative 'chatmessage'
require_relative 'window_scrollable' require_relative 'window_scrollable'
class Window_Chat < Window_Scrollable class Window_Chat < Window_Scrollable
WLH=16 WLH=16
def initialize(x, y, width, height) def initialize(x, y, width, height)
super(x,y,width,height) super(x,y,width,height)
if @width > 600 #判断大厅还是房间,这个判据比较囧,待优化 if @width > 600 #判断大厅还是房间,这个判据比较囧,待优化
@chat_background = Surface.load("graphics/system/chat.png").display_format @chat_background = Surface.load("graphics/system/chat.png").display_format
else else
@chat_background = Surface.load("graphics/system/chat_room.png").display_format @chat_background = Surface.load("graphics/system/chat_room.png").display_format
end end
@background = @contents.copy_rect(0,0,@contents.w,@contents.h) #new而已。。 @background = @contents.copy_rect(0,0,@contents.w,@contents.h) #new而已。。
@background.fill_rect(0,0,@background.w, @background.h, 0xFFb2cefe) @background.fill_rect(0,0,@background.w, @background.h, 0xFFb2cefe)
@background.put(@chat_background,0,31-4) @background.put(@chat_background,0,31-4)
@tab = Surface.load "graphics/system/tab.png" @tab = Surface.load "graphics/system/tab.png"
@chat_input = Widget_InputBox.new(@x+8, @y+@height-24-10, @width-14, 24) do |key| @chat_input = Widget_InputBox.new(@x+8, @y+@height-24-10, @width-14, 24) do |key|
case key case key
when :ENTER when :ENTER
if !@chat_input.value.empty? if !@chat_input.value.empty?
chatmessage = ChatMessage.new($game.user, @chat_input.value, @channel) chatmessage = ChatMessage.new($game.user, @chat_input.value, @channel)
$game.chat chatmessage $game.chat chatmessage
Game_Event.push Game_Event::Chat.new(chatmessage)# if $game.show_chat_self Game_Event.push Game_Event::Chat.new(chatmessage) if !$game.is_a? Ygocore
true true
end end
end end
end end
@chat_input.refresh @chat_input.refresh
@font = TTF.open("fonts/wqy-microhei.ttc", 14) @font = TTF.open("fonts/wqy-microhei.ttc", 14)
@scrollbar = Widget_ScrollBar.new(self,@x+@width-20-8,@y+31+3,@height-68) @scrollbar = Widget_ScrollBar.new(self,@x+@width-20-8,@y+31+3,@height-68)
@page_size = (@height-68)/WLH @page_size = (@height-68)/WLH
@@list ||= {} @@list ||= {}
@list_splited = {} @list_splited = {}
@@list.each_pair do |channel, chatmessages| @@list.each_pair do |channel, chatmessages|
chatmessages.each do |chatmessage| chatmessages.each do |chatmessage|
add_split(chatmessage) add_split(chatmessage)
end end
end end
@channels = [] @channels = []
self.channel = :lobby self.channel = :lobby
end end
def add(chatmessage) def add(chatmessage)
@@list[chatmessage.channel] ||= [] @@list[chatmessage.channel] ||= []
unless @channels.include? chatmessage.channel unless @channels.include? chatmessage.channel
@channels << chatmessage.channel @channels << chatmessage.channel
refresh refresh
end end
@@list[chatmessage.channel] << chatmessage @@list[chatmessage.channel] << chatmessage
scroll_bottom = @items.size - self.scroll <= @page_size scroll_bottom = @items.size - self.scroll <= @page_size
add_split(chatmessage) add_split(chatmessage)
if chatmessage.channel == @channel if chatmessage.channel == @channel
@scroll = [@items.size - @page_size, 0].max if scroll_bottom @scroll = [@items.size - @page_size, 0].max if scroll_bottom
refresh refresh
end end
end end
def add_split(chatmessage) def add_split(chatmessage)
@list_splited[chatmessage.channel] ||= [] @list_splited[chatmessage.channel] ||= []
@list_splited[chatmessage.channel] << [chatmessage, ""] @list_splited[chatmessage.channel] << [chatmessage, ""]
width = name_width(chatmessage) width = name_width(chatmessage)
line = 0 line = 0
chatmessage.message.each_char do |char| chatmessage.message.each_char do |char|
if char == "\n" if char == "\n"
line += 1 line += 1
width = 0 width = 0
@list_splited[chatmessage.channel] << [chatmessage.message_color, ""] @list_splited[chatmessage.channel] << [chatmessage.message_color, ""]
else else
char_width = @font.text_size(char)[0] char_width = @font.text_size(char)[0]
if char_width + width > @width-14-20 if char_width + width > @width-14-20
line += 1 line += 1
width = char_width width = char_width
@list_splited[chatmessage.channel] << [chatmessage.message_color, char] @list_splited[chatmessage.channel] << [chatmessage.message_color, char]
else else
@list_splited[chatmessage.channel].last[1] << char @list_splited[chatmessage.channel].last[1] << char
width += char_width width += char_width
end end
end end
end end
end end
def mousemoved(x,y) def mousemoved(x,y)
if y-@y < 31 and (x-@x) < @channels.size * 100 if y-@y < 31 and (x-@x) < @channels.size * 100
self.index = @channels[(x-@x) / 100] self.index = @channels[(x-@x) / 100]
else else
self.index = nil self.index = nil
end end
end end
def clicked def clicked
case @index case @index
when nil when nil
when Integer when Integer
else else
self.channel = @index self.channel = @index
end end
end end
def channel=(channel) def channel=(channel)
return if @channel == channel return if @channel == channel
@channel = channel @channel = channel
@channels << channel unless @channels.include? channel @channels << channel unless @channels.include? channel
@list_splited[channel] ||= [] @list_splited[channel] ||= []
@items = @list_splited[channel] @items = @list_splited[channel]
@scroll = [@items.size - @page_size, 0].max @scroll = [@items.size - @page_size, 0].max
refresh refresh
end end
def draw_item(index, status=0) def draw_item(index, status=0)
case index case index
when nil when nil
when Integer #描绘聊天消息 when Integer #描绘聊天消息
draw_item_chatmessage(index, status) draw_item_chatmessage(index, status)
else #描绘频道标签 else #描绘频道标签
draw_item_channel(index, status) draw_item_channel(index, status)
end end
end end
def draw_item_channel(channel, status) def draw_item_channel(channel, status)
index = @channels.index(channel) index = @channels.index(channel)
Surface.blit(@tab,0,@channel == channel ? 0 : 31,100,31,@contents,index*100+3,0) Surface.blit(@tab,0,@channel == channel ? 0 : 31,100,31,@contents,index*100+3,0)
channel_name = ChatMessage.channel_name channel channel_name = ChatMessage.channel_name channel
x = index*100+(100 - @font.text_size(channel_name)[0])/2 x = index*100+(100 - @font.text_size(channel_name)[0])/2
draw_stroked_text(channel_name,x,8,1,@font, [255,255,255], ChatMessage.channel_color(channel)) draw_stroked_text(channel_name,x,8,1,@font, [255,255,255], ChatMessage.channel_color(channel))
end end
def draw_item_chatmessage(index, status) def draw_item_chatmessage(index, status)
x,y = item_rect_chatmessage(index) x,y = item_rect_chatmessage(index)
chatmessage, message = @items[index] chatmessage, message = @items[index]
if chatmessage.is_a? ChatMessage if chatmessage.is_a? ChatMessage
@font.draw_blended_utf8(@contents, chatmessage.user.name+':', x, y, *chatmessage.name_color) if chatmessage.name_visible? @font.draw_blended_utf8(@contents, chatmessage.user.name+':', x, y, *chatmessage.name_color) if chatmessage.name_visible?
@font.draw_blended_utf8(@contents, message, x+name_width(chatmessage), y, *chatmessage.message_color) unless chatmessage.message.empty? @font.draw_blended_utf8(@contents, message, x+name_width(chatmessage), y, *chatmessage.message_color) unless chatmessage.message.empty?
else else
@font.draw_blended_utf8(@contents, message, x, y, *chatmessage) unless message.empty? @font.draw_blended_utf8(@contents, message, x, y, *chatmessage) unless message.empty?
end end
end end
def item_rect(index) def item_rect(index)
case index case index
when nil when nil
when Integer #描绘聊天消息 when Integer #描绘聊天消息
item_rect_chatmessage(index) item_rect_chatmessage(index)
else #描绘频道标签 else #描绘频道标签
item_rect_channel(index) item_rect_channel(index)
end end
end end
def item_rect_channel(channel) def item_rect_channel(channel)
[@channels.index(channel)*100+3, 0, 100, 31] [@channels.index(channel)*100+3, 0, 100, 31]
end end
def item_rect_chatmessage(index) def item_rect_chatmessage(index)
[8, (index-@scroll)*WLH+31+3, @width, self.class::WLH] [8, (index-@scroll)*WLH+31+3, @width, self.class::WLH]
end end
def refresh def refresh
super super
@channels.each {|channel|draw_item_channel(channel, @index==channel)} @channels.each {|channel|draw_item_channel(channel, @index==channel)}
end end
def name_width(chatmessage) def name_width(chatmessage)
chatmessage.name_visible? ? @font.text_size(chatmessage.user.name+':')[0] : 0 chatmessage.name_visible? ? @font.text_size(chatmessage.user.name+':')[0] : 0
end end
def index_legal?(index) def index_legal?(index)
case index case index
when nil,Integer when nil,Integer
super super
else else
@channels.include? index @channels.include? index
end end
end end
def scroll_up def scroll_up
self.scroll -= 1 self.scroll -= 1
end end
def scroll_down def scroll_down
self.scroll += 1 self.scroll += 1
end end
def update def update
@chat_input.update @chat_input.update
end end
end end
\ No newline at end of file
#encoding: UTF-8 #encoding: UTF-8
#============================================================================== #==============================================================================
# Window_UserInfo # Window_UserInfo
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
# 游戏大厅显示用户信息的类 # 游戏大厅显示用户信息的类
#============================================================================== #==============================================================================
class Window_UserInfo < Window class Window_UserInfo < Window
def initialize(x, y, user) def initialize(x, y, user)
@avatar_boarder = Surface.load("graphics/lobby/avatar_boader.png") @avatar_boarder = Surface.load("graphics/lobby/avatar_boader.png")
super(x,y,280,144) super(x,y,280,144)
@font = TTF.open('fonts/wqy-microhei.ttc', 16) @font = TTF.open('fonts/wqy-microhei.ttc', 16)
@user = user @user = user
@background = Surface.load("graphics/lobby/userinfo.png").display_format @background = Surface.load("graphics/lobby/userinfo.png").display_format
refresh refresh
end end
def refresh def refresh
@contents.put(@background, 0, 0) @contents.put(@background, 0, 0)
@thread = @user.avatar(:middle) do |avatar| @thread = @user.avatar(:middle) do |avatar|
clear(0,0,@avatar_boarder.w, @avatar_boarder.h) clear(0,0,@avatar_boarder.w, @avatar_boarder.h)
@contents.put(avatar, 12, 12) @contents.put(avatar, 12, 12)
@contents.put(@avatar_boarder, 0, 0) @contents.put(@avatar_boarder, 0, 0)
end end
@font.draw_blended_utf8(@contents, @user.name, 160, 12, 0x00,0x00,0x00) unless @user.name.empty? @font.draw_blended_utf8(@contents, @user.name, 160, 12, 0x00,0x00,0x00) unless @user.name.empty?
@font.draw_blended_utf8(@contents, "id: #{@user.id}" , 160, 12+16*2, 0x00,0x00,0x00) @font.draw_blended_utf8(@contents, @user.id.to_s , 160, 12+16*2, 0x00,0x00,0x00) unless @user.id.to_s.empty?
@font.draw_blended_utf8(@contents, "Lv: #{@user.level}" , 160, 12+16*3, 0x00,0x00,0x00) if @user.respond_to? :level and @user.level #TODO:规范化,level是iduel专属的,但是又不太想让iduel来重定义这个window @font.draw_blended_utf8(@contents, "Lv: #{@user.level}" , 160, 12+16*3, 0x00,0x00,0x00) if @user.respond_to? :level and @user.level #TODO:规范化,level是iduel专属的,但是又不太想让iduel来重定义这个window
@font.draw_blended_utf8(@contents, "经验: #{@user.exp}", 160, 12+16*4, 0x00,0x00,0x00) if @user.respond_to? :exp and @user.exp @font.draw_blended_utf8(@contents, "经验: #{@user.exp}", 160, 12+16*4, 0x00,0x00,0x00) if @user.respond_to? :exp and @user.exp
end end
def dispose def dispose
@thread.exit @thread.exit
super super
end end
end end
class Game_Event class Game_Event
def self.parse(header, data) def self.parse(header, data)
case header case header
when :login #when :login
if data # if data
Login.new parse_user data # Login.new parse_user data
else # else
Error.new('登录', '用户名或密码错误') # Error.new('登录', '用户名或密码错误')
end # end
#when :rooms #when :rooms
#AllRooms.new data.collect{|room|parse_room(room)} #AllRooms.new data.collect{|room|parse_room(room)}
#when :rooms_update #when :rooms_update
......
...@@ -17,8 +17,8 @@ class Ygocore < Game ...@@ -17,8 +17,8 @@ class Ygocore < Game
load 'lib/ygocore/room.rb' load 'lib/ygocore/room.rb'
load 'lib/ygocore/scene_lobby.rb' load 'lib/ygocore/scene_lobby.rb'
require 'json' require 'json'
#require 'xmpp4r/client' require 'xmpp4r/client'
#require 'xmpp4r/muc' require 'xmpp4r/muc'
end end
def refresh_interval def refresh_interval
...@@ -28,53 +28,54 @@ class Ygocore < Game ...@@ -28,53 +28,54 @@ class Ygocore < Game
def login(username, password) def login(username, password)
@username = username @username = username
@password = password @password = password
#@nickname_conflict = [] @nickname_conflict = []
#@@im = Jabber::Client.new(Jabber::JID::new(@username, 'my-card.in', 'mycard')) @@im = Jabber::Client.new(Jabber::JID::new(@username, 'my-card.in', 'mycard'))
#@@im_room = Jabber::MUC::MUCClient.new(@@im) @@im_room = Jabber::MUC::MUCClient.new(@@im)
#Jabber.debug = true Jabber.logger = $log
Jabber.debug = true
#@@im.on_exception do |exception, c, where|
# $log.error('聊天出错') { [exception, c, where] } @@im.on_exception do |exception, c, where|
# if where == :close $log.error('聊天出错') { [exception, c, where] }
# Game_Event.push(Game_Event::Chat.new(ChatMessage.new(User.new(:system, 'System'), '聊天连接断开, 可能是网络问题或帐号从其他地点登录'))) if where == :close
# else Game_Event.push(Game_Event::Chat.new(ChatMessage.new(User.new(:system, 'System'), '聊天服务连接中断')))
# Game_Event.push(Game_Event::Chat.new(ChatMessage.new(User.new(:system, 'System'), '聊天连接断开, 5秒后重新连接'))) else
# sleep 5 Game_Event.push(Game_Event::Chat.new(ChatMessage.new(User.new(:system, 'System'), '聊天服务连接中断, 5秒后重新连接')))
# im_connect sleep 5
# end im_connect
#end end
#@@im_room.add_message_callback do |m| end
# user = m.from.resource == nickname ? @user : User.new(m.from.resource.to_sym, m.from.resource) @@im_room.add_message_callback do |m|
# Game_Event.push Game_Event::Chat.new ChatMessage.new(user, m.body, :lobby) rescue $log.error('收到聊天消息') { $! } user = m.from.resource == nickname ? @user : User.new(m.from.resource.to_sym, m.from.resource)
#end Game_Event.push Game_Event::Chat.new ChatMessage.new(user, m.body, :lobby) rescue $log.error('收到聊天消息') { $! }
#@@im_room.add_private_message_callback do |m| end
# if m.body #忽略无消息的正在输入等内容 @@im_room.add_private_message_callback do |m|
# user = m.from.resource == nickname ? @user : User.new(m.from.resource.to_sym, m.from.resource) if m.body #忽略无消息的正在输入等内容
# Game_Event.push Game_Event::Chat.new ChatMessage.new(user, m.body, user) rescue $log.error('收到私聊消息') { $! } user = m.from.resource == nickname ? @user : User.new(m.from.resource.to_sym, m.from.resource)
# end Game_Event.push Game_Event::Chat.new ChatMessage.new(user, m.body, user) rescue $log.error('收到私聊消息') { $! }
#end end
#@@im_room.add_join_callback do |m| end
# Game_Event.push Game_Event::NewUser.new User.new m.from.resource.to_sym, m.from.resource @@im_room.add_join_callback do |m|
#end Game_Event.push Game_Event::NewUser.new User.new m.from.resource.to_sym, m.from.resource
#@@im_room.add_leave_callback do |m| end
# Game_Event.push Game_Event::MissingUser.new User.new m.from.resource.to_sym, m.from.resource @@im_room.add_leave_callback do |m|
#end Game_Event.push Game_Event::MissingUser.new User.new m.from.resource.to_sym, m.from.resource
end
connect connect
#im_connect im_connect
end end
#def nickname def nickname
# return @nickname if @nickname return @nickname if @nickname
# if @nickname_conflict.include? @username if @nickname_conflict.include? @username
# 1.upto(9) do |i| 1.upto(9) do |i|
# result = "#{@username}-#{i}" result = "#{@username}-#{i}"
# return result unless @nickname_conflict.include? result return result unless @nickname_conflict.include? result
# end end
# raise 'can`t get available nickname' raise 'can`t get available nickname'
# else else
# @username @username
# end end
#end end
def connect def connect
@recv = Thread.new do @recv = Thread.new do
...@@ -88,7 +89,7 @@ class Ygocore < Game ...@@ -88,7 +89,7 @@ class Ygocore < Game
Game_Event.push Game_Event::Error.new('ygocore', '读取服务器列表失败', true) Game_Event.push Game_Event::Error.new('ygocore', '读取服务器列表失败', true)
end end
EventMachine::connect "mycard-server.my-card.in", 9997, Client #EventMachine::connect "mycard-server.my-card.in", 9997, Client
ws = WebSocket::EventMachine::Client.connect(:host => "mycard-server.my-card.in", :port => 9998); ws = WebSocket::EventMachine::Client.connect(:host => "mycard-server.my-card.in", :port => 9998);
ws.onmessage do |msg, type| ws.onmessage do |msg, type|
$log.info('收到websocket消息'){msg.force_encoding("UTF-8")} $log.info('收到websocket消息'){msg.force_encoding("UTF-8")}
...@@ -107,52 +108,58 @@ class Ygocore < Game ...@@ -107,52 +108,58 @@ class Ygocore < Game
end end
end end
#def im_connect def im_connect
# Thread.new { Thread.new {
# begin begin
# @@im.allow_tls = false @@im.allow_tls = false
# @@im.use_ssl = true @@im.use_ssl = true
# @@im.connect('my-card.in', 5223) @@im.connect('chat.my-card.in', 5223) #ruby19/windows下 使用tls连接时会卡住
# #ruby19/windows下 使用tls连接时会卡住
# begin
# @@im.auth(@password) @@im.auth(@password)
# @@im.send(Jabber::Presence.new.set_type(:available)) rescue Jabber::ClientAuthenticationFailure
# begin Game_Event.push Game_Event::Error.new('登录', '用户名或密码错误')
# nickname = nickname() Thread.exit
# @@im_room.join(Jabber::JID.new(I18n.t('lobby.room'), I18n.t('lobby.server'), nickname)) end
# rescue Jabber::ServerError => exception Game_Event.push Game_Event::Login.new User.new(@@im.jid, @username, true)
# if exception.error.error == 'conflict' @@im.send(Jabber::Presence.new.set_type(:available))
# @nickname_conflict << nickname begin
# retry nickname = nickname()
# end #@@im_room.join(Jabber::JID.new(I18n.t('lobby.room'), I18n.t('lobby.server'), nickname))
# end @@im_room.join(Jabber::JID.new('mycard', 'conference.my-card.in', nickname))
# Game_Event.push Game_Event::AllUsers.new @@im_room.roster.keys.collect { |nick| User.new(nick.to_sym, nick) } rescue p $! rescue Jabber::ServerError => exception
# rescue StandardError => exception if exception.error.error == 'conflict'
# $log.error('聊天连接出错') { exception } @nickname_conflict << nickname
# Game_Event.push(Game_Event::Chat.new(ChatMessage.new(User.new(:system, 'System'), '聊天服务器连接失败'))) retry
# end end
# } end
#end Game_Event.push Game_Event::AllUsers.new @@im_room.roster.keys.collect { |nick| User.new(nick.to_sym, nick) } rescue p $!
rescue StandardError => exception
$log.error('聊天连接出错') { exception }
Game_Event.push(Game_Event::Chat.new(ChatMessage.new(User.new(:system, 'System'), '聊天服务器连接失败')))
end
}
end
#def chat(chatmessage)
# case chatmessage.channel
# when :lobby
# msg = Jabber::Message::new(nil, chatmessage.message)
# @@im_room.send msg
# when User
# msg = Jabber::Message::new(nil, chatmessage.message)
# @@im_room.send msg, chatmessage.channel.id
# #send(:chat, channel: chatmessage.channel.id, message: chatmessage.message, time: chatmessage.time)
# end
#end
def chat(chatmessage) def chat(chatmessage)
case chatmessage.channel case chatmessage.channel
when :lobby when :lobby
send(:chat, channel: :lobby, message: chatmessage.message, time: chatmessage.time) msg = Jabber::Message::new(nil, chatmessage.message)
@@im_room.send msg
when User when User
send(:chat, channel: chatmessage.channel.id, message: chatmessage.message, time: chatmessage.time) msg = Jabber::Message::new(nil, chatmessage.message)
@@im_room.send msg, chatmessage.channel.id
#send(:chat, channel: chatmessage.channel.id, message: chatmessage.message, time: chatmessage.time)
end end
end end
#def chat(chatmessage)
# case chatmessage.channel
# when :lobby
# send(:chat, channel: :lobby, message: chatmessage.message, time: chatmessage.time)
# when User
# send(:chat, channel: chatmessage.channel.id, message: chatmessage.message, time: chatmessage.time)
# end
#end
def host(room_name, room_config) def host(room_name, room_config)
room = Room.new(0, room_name) room = Room.new(0, room_name)
...@@ -185,8 +192,8 @@ class Ygocore < Game ...@@ -185,8 +192,8 @@ class Ygocore < Game
end end
def send(header, data=nil) def send(header, data=nil)
$log.info('发送消息') { {header: header, data: data} } #$log.info('发送消息') { {header: header, data: data} }
Client::MycardChannel.push header: header, data: data #Client::MycardChannel.push header: header, data: data
end end
def exit def exit
...@@ -330,24 +337,24 @@ class Ygocore < Game ...@@ -330,24 +337,24 @@ class Ygocore < Game
#end #end
end end
module Client #module Client
MycardChannel = EM::Channel.new # MycardChannel = EM::Channel.new
include EM::P::ObjectProtocol # include EM::P::ObjectProtocol
#
def post_init # def post_init
send_object header: :login, data: {name: $game.username, password: $game.password} # send_object header: :login, data: {name: $game.username, password: $game.password}
MycardChannel.subscribe { |msg| send_object(msg) } # MycardChannel.subscribe { |msg| send_object(msg) }
end # end
#
def receive_object obj # def receive_object obj
$log.info('收到消息') { obj.inspect } # $log.info('收到消息') { obj.inspect }
Game_Event.push Game_Event.parse obj[:header], obj[:data] # Game_Event.push Game_Event.parse obj[:header], obj[:data]
end # end
#
def unbind # def unbind
Game_Event.push Game_Event::Error.new('ygocore', '网络连接中断', true) # Game_Event.push Game_Event::Error.new('ygocore', '网络连接中断', true)
end # end
end #end
get_announcements get_announcements
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment