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

xmpp

parent 7be8d5bf
...@@ -39,6 +39,9 @@ class Game ...@@ -39,6 +39,9 @@ class Game
def refresh_interval def refresh_interval
5 5
end end
def show_chat_self
false
end
end end
...@@ -124,6 +124,9 @@ class Iduel < Game ...@@ -124,6 +124,9 @@ class Iduel < Game
end end
end end
end end
def show_chat_self
true
end
private private
def connect def connect
require 'socket' require 'socket'
......
...@@ -27,11 +27,9 @@ class Window_Chat < Window_Scrollable ...@@ -27,11 +27,9 @@ class Window_Chat < Window_Scrollable
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) Game_Event.push Game_Event::Chat.new(chatmessage) if $game.show_chat_self
true true
end end
when :ESC
true
end end
end end
@chat_input.refresh @chat_input.refresh
......
...@@ -7,8 +7,8 @@ class Game_Event ...@@ -7,8 +7,8 @@ class Game_Event
else else
Error.new('登录', '用户名或密码错误') Error.new('登录', '用户名或密码错误')
end end
when :users #when :users
AllUsers.new data.collect{|user|parse_user(user)} # AllUsers.new data.collect{|user|parse_user(user)}
when :rooms when :rooms
rooms_wait = [] rooms_wait = []
rooms_start = [] rooms_start = []
...@@ -21,21 +21,21 @@ class Game_Event ...@@ -21,21 +21,21 @@ class Game_Event
end end
end end
AllRooms.new rooms_wait + rooms_start AllRooms.new rooms_wait + rooms_start
when :newuser #when :newuser
NewUser.new parse_user data #NewUser.new parse_user data
when :missinguser #when :missinguser
MissingUser.new parse_user data #MissingUser.new parse_user data
when :newroom when :newroom
NewRoom.new parse_room data NewRoom.new parse_room data
when :missingroom when :missingroom
MissingRoom.new parse_room data MissingRoom.new parse_room data
when :chat #when :chat
case data[:channel] # case data[:channel]
when :lobby # when :lobby
Chat.new ChatMessage.new User.new(data[:from][:id],data[:from][:name]), data[:message], :lobby # Chat.new ChatMessage.new User.new(data[:from][:id],data[:from][:name]), data[:message], :lobby
else # else
Chat.new ChatMessage.new User.new(data[:from][:id],data[:from][:name]), data[:message], User.new(data[:channel]) # Chat.new ChatMessage.new User.new(data[:from][:id],data[:from][:name]), data[:message], User.new(data[:channel])
end # end
end end
end end
def self.parse_room(room) def self.parse_room(room)
......
...@@ -15,6 +15,8 @@ class Ygocore < Game ...@@ -15,6 +15,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/muc'
end end
def refresh_interval def refresh_interval
...@@ -22,9 +24,54 @@ class Ygocore < Game ...@@ -22,9 +24,54 @@ class Ygocore < Game
end end
def login(username, password) def login(username, password)
@username = username @username = username
@password = password @password = password
@nickname_conflict = []
@@im = Jabber::Client.new(Jabber::JID::new(@username, 'my-card.in', 'mycard'))
@@im_room = Jabber::MUC::MUCClient.new(@@im)
#Jabber.debug = true
@@im.on_exception do |exception, c, where|
$log.error('聊天出错') { [exception, c, where] }
if where == :close
Game_Event.push(Game_Event::Chat.new(ChatMessage.new(User.new(:system, 'System'), '聊天连接断开, 可能是网络问题或帐号从其他地点登录')))
else
Game_Event.push(Game_Event::Chat.new(ChatMessage.new(User.new(:system, 'System'), '聊天连接断开, 5秒后重新连接')))
sleep 5
im_connect
end
end
@@im_room.add_message_callback do |m|
user = m.from.resource == nickname ? @user : User.new(m.from.resource.to_sym, m.from.resource)
Game_Event.push Game_Event::Chat.new ChatMessage.new(user, m.body, :lobby) rescue $log.error('收到聊天消息') { $! }
end
@@im_room.add_private_message_callback do |m|
if m.body #忽略无消息的正在输入等内容
user = m.from.resource == nickname ? @user : User.new(m.from.resource.to_sym, m.from.resource)
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|
Game_Event.push Game_Event::NewUser.new User.new m.from.resource.to_sym, m.from.resource
end
@@im_room.add_leave_callback do |m|
Game_Event.push Game_Event::MissingUser.new User.new m.from.resource.to_sym, m.from.resource
end
connect connect
im_connect
end
def nickname
return @nickname if @nickname
if @nickname_conflict.include? @username
1.upto(9) do |i|
result = "#{@username}-#{i}"
return result unless @nickname_conflict.include? result
end
raise 'can`t get available nickname'
else
@username
end
end end
def connect def connect
...@@ -35,24 +82,54 @@ class Ygocore < Game ...@@ -35,24 +82,54 @@ class Ygocore < Game
end end
end end
def im_connect
Thread.new {
begin
@@im.allow_tls = false
@@im.use_ssl = true
@@im.connect('my-card.in', 5223)
#ruby19/windows下 使用tls连接时会卡住
@@im.auth(@password)
@@im.send(Jabber::Presence.new.set_type(:available))
begin
nickname = nickname()
@@im_room.join(Jabber::JID.new('lobby@conference.my-card.in/' + nickname))
rescue Jabber::ServerError => exception
if exception.error.error == 'conflict'
@nickname_conflict << nickname
retry
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) 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)
when User @@im_room.send msg
send(:chat, channel: chatmessage.channel.id, message: chatmessage.message, time: chatmessage.time) 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
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)
room.pvp = room_config[:pvp] room.pvp = room_config[:pvp]
room.match = room_config[:match] room.match = room_config[:match]
room.tag = room_config[:tag] room.tag = room_config[:tag]
room.password = room_config[:password] room.password = room_config[:password]
room.ot = room_config[:ot] room.ot = room_config[:ot]
room.lp = room_config[:lp] room.lp = room_config[:lp]
if $game.rooms.any? { |game_room| game_room.name == room_name } if $game.rooms.any? { |game_room| game_room.name == room_name }
Widget_Msgbox.new("建立房间", "房间名已存在", :ok => "确定") Widget_Msgbox.new("建立房间", "房间名已存在", :ok => "确定")
else else
...@@ -115,70 +192,70 @@ class Ygocore < Game ...@@ -115,70 +192,70 @@ class Ygocore < Game
#写入配置文件并运行ygocore #写入配置文件并运行ygocore
Dir.chdir(File.dirname(path)) do Dir.chdir(File.dirname(path)) do
case option case option
when Room when Room
room = option room = option
room_name = if room.ot != 0 or room.lp != 8000 room_name = if room.ot != 0 or room.lp != 8000
mode = case when room.match? then mode = case when room.match? then
1; when room.tag? then 1; when room.tag? then
2 2
else else
0 0
end end
room_name = "#{room.ot}#{mode}FFF#{room.lp},5,1,#{room.name}" room_name = "#{room.ot}#{mode}FFF#{room.lp},5,1,#{room.name}"
elsif room.tag? elsif room.tag?
"T#" + room.name "T#" + room.name
elsif room.pvp? and room.match? elsif room.pvp? and room.match?
"PM#" + room.name "PM#" + room.name
elsif room.pvp? elsif room.pvp?
"P#" + room.name "P#" + room.name
elsif room.match? elsif room.match?
"M#" + room.name "M#" + room.name
else else
room.name room.name
end end
if room.password and !room.password.empty? if room.password and !room.password.empty?
room_name += "$" + room.password room_name += "$" + room.password
end end
system_conf = {} system_conf = {}
begin begin
IO.readlines('system.conf').each do |line| IO.readlines('system.conf').each do |line|
line.force_encoding "UTF-8" line.force_encoding "UTF-8"
next if line[0, 1] == '#' next if line[0, 1] == '#'
field, contents = line.chomp.split(' = ', 2) field, contents = line.chomp.split(' = ', 2)
system_conf[field] = contents system_conf[field] = contents
end
rescue
system_conf['antialias'] = 2
system_conf['textfont'] = 'c:/windows/fonts/simsun.ttc 14'
system_conf['numfont'] = 'c:/windows/fonts/arialbd.ttf'
end end
(system_conf['nickname'] = "#{$game.user.name}#{"$" unless $game.password.nil? or $game.password.empty?}#{$game.password}") rescue nil rescue
system_conf['lastip'] = $game.server system_conf['antialias'] = 2
system_conf['lastport'] = $game.port.to_s system_conf['textfont'] = 'c:/windows/fonts/simsun.ttc 14'
system_conf['roompass'] = room_name system_conf['numfont'] = 'c:/windows/fonts/arialbd.ttf'
open('system.conf', 'w') { |file| file.write system_conf.collect { |key, value| "#{key} = #{value}" }.join("\n") } end
args = '-j' (system_conf['nickname'] = "#{$game.user.name}#{"$" unless $game.password.nil? or $game.password.empty?}#{$game.password}") rescue nil
when :replay system_conf['lastip'] = $game.server
args = '-r' system_conf['lastport'] = $game.port.to_s
when :deck system_conf['roompass'] = room_name
args = '-d' open('system.conf', 'w') { |file| file.write system_conf.collect { |key, value| "#{key} = #{value}" }.join("\n") }
when String args = '-j'
system_conf = {} when :replay
begin args = '-r'
IO.readlines('system.conf').each do |line| when :deck
line.force_encoding "UTF-8" args = '-d'
next if line[0, 1] == '#' when String
field, contents = line.chomp.split(' = ', 2) system_conf = {}
system_conf[field] = contents begin
end IO.readlines('system.conf').each do |line|
rescue line.force_encoding "UTF-8"
system_conf['antialias'] = 2 next if line[0, 1] == '#'
system_conf['textfont'] = 'c:/windows/fonts/simsun.ttc 14' field, contents = line.chomp.split(' = ', 2)
system_conf['numfont'] = 'c:/windows/fonts/arialbd.ttf' system_conf[field] = contents
end end
system_conf['lastdeck'] = option rescue
open('system.conf', 'w') { |file| file.write system_conf.collect { |key, value| "#{key} = #{value}" }.join("\n") } system_conf['antialias'] = 2
args = '-d' system_conf['textfont'] = 'c:/windows/fonts/simsun.ttc 14'
system_conf['numfont'] = 'c:/windows/fonts/arialbd.ttf'
end
system_conf['lastdeck'] = option
open('system.conf', 'w') { |file| file.write system_conf.collect { |key, value| "#{key} = #{value}" }.join("\n") }
args = '-d'
end end
IO.popen("ygopro_vs.exe #{args}") IO.popen("ygopro_vs.exe #{args}")
WM.iconify rescue nil WM.iconify rescue nil
...@@ -197,7 +274,7 @@ class Ygocore < Game ...@@ -197,7 +274,7 @@ class Ygocore < Game
def self.get_announcements def self.get_announcements
#公告 #公告
$config['ygocore'] ||= {} $config['ygocore'] ||= {}
$config['ygocore']['announcements'] ||= [Announcement.new("正在读取公告...", nil, nil)] $config['ygocore']['announcements'] ||= [Announcement.new("正在读取公告...", nil, nil)]
Thread.new do Thread.new do
begin begin
......
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