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

发送战报初步

parent f2bd1309
cd /d %~dp0
ruby lib/main.rb > log.txt
\ No newline at end of file
This diff is collapsed.
......@@ -15,11 +15,16 @@ class Iduel
require 'socket'
require 'digest/md5'
require 'open-uri'
require_relative 'iduel_action'
require_relative 'iduel_event'
@conn = TCPSocket.open(Server, Port)
@conn.set_encoding "GBK"
Thread.abort_on_exception = true
@recv = Thread.new { (recv(@last_info) while @last_info = @conn.gets(RS)) rescue Iduel::Event.push Event::Error.new(0)}
#at_exit{($iduel.qroom(Iduel::Room.new(1,1,1,1,1,1,1));$iduel.quit) if $iduel}
@recv = Thread.new {
while @conn
last_info = @conn.gets(RS)
recv last_info
end rescue Iduel::Event.push Event::Error.new(0)}
end
def send(head, *args)
info = "##{head.to_s(16).upcase}|#{args.join(',')}".encode("GBK") + RS
......@@ -30,49 +35,13 @@ class Iduel
info.chomp!(RS)
info.encode! "UTF-8"
puts ">> #{info}"
info =~ /^\$([A-Z])\|(.*)$/m
#建立房间
#$F|253¢
#$R|¢
#$Q|253,2,zh99998(201448),1¢
#加入房间
#$M||¢
#$F|256¢
#$R|¢
#$Q|256,1,zh99997(201629),zh99998(201448)¢
Event.push case $1
when "A"
Event::Error
when "B"
Event::LOGINOK
when "C"
Event::OLIF
when "F"
Event::JOINROOMOK
when "G"
Event::WATCHROOMSTART
when "J"
Event::UMSG
when "K"
Event::WMSG
when "M"
Event::QROOMOK #TODO
when "O"
Event::PCHAT
when "P"
Event::RMIF
when "Q"
Event::SingleRoomInfo
when "R"
Event::QROOMOK #卡表
else
p $1, $2
system("pause")
Event::UNKNOWN
end.new($2)
Event.push Event.parse info
end
def close
$iduel.quit
@recv.exit
@conn.close
@conn = nil
end
def checknum(head, *args)
Digest::MD5.hexdigest("[#{head}]_#{args.join('_')}_SCNERO")
......@@ -90,6 +59,9 @@ class Iduel
def qroom(room)
send(10, @key, room.id, checknum("QROOM", @session + room.id.to_s))
end
def action(action)
send(2, "#{checknum("RMSG", @session)}@#{@key}", "#{action.escape}▊▊▊000000") #TODO
end
def host(name, password="", lv=0, color = 0)
send(6, @key, name, password, checknum("JOINROOMMSG", @session + name + password + "0"), 0, color, lv, 0, nil, nil) #TODO:v.ak, v.al
end
......@@ -102,7 +74,7 @@ class Iduel
def quit
send(11, @key, checknum("ULO", "#{@session}"))
end
class Iduel::User
class User
@@all = []
attr_accessor :id, :name, :level, :exp
class << self
......@@ -145,7 +117,7 @@ class Iduel
end
end
class Iduel::Room
class Room
@@all = []
attr_accessor :id, :name, :player1, :player2, :private, :color
class << self
......@@ -176,172 +148,7 @@ class Iduel
alias private? private
end
Event = Class.new{@queue = []}
class <<Event
def push(event)
@queue << event
end
def poll
@queue.shift
end
end
class Event::LOGINOK < Event
attr_reader :user, :session
def initialize(info)
info = info.split(",")
#>> $B|201629,zh99997,5da9e5fa,Level-1 (总经验:183),,20101118
info[3] =~ /Level-(\d)+ \(总经验:(\d+)\)/
$iduel.user = @user = User.new(info[0].to_i, info[1], $1.to_i, $2.to_i)
$iduel.session = @session = info[2]
$iduel.key = ($iduel.user.id - 0x186a0) ^ 0x22133
end
end
class Event::OLIF < Event
attr_reader :users
def initialize(info)
@users = info.split(',').collect do |user|
Iduel::User.new(user)
end
end
end
class Event::RMIF < Event
attr_reader :rooms
def initialize(info)
info = info.split("|")
@rooms = []
templist = @rooms
empty = false
info.each do |room|
if room == '~~'
empty = true
templist = []
else
room = room.split(",")
templist << if empty
Iduel::Room.new(room[0].to_i, room[1], Iduel::User.new(room[2]), nil, room[3]=="1", Iduel::Color[room[4].to_i], nil, room[6])
else
Iduel::Room.new(room[0].to_i, room[3], Iduel::User.new(room[1]), Iduel::User.new(room[2]), false, Iduel::Color[room[5].to_i], room[3])
end
end
end
@rooms = templist + @rooms
end
end
class Event::NOL < Event
def initialize(info)
super
@args = @args.collect do |user|
Iduel::User.new(user)
end
end
end
class Event::DOL < Event
def initialize(info)
super
@args = @args.collect do |user|
Iduel::User.new(user)
end
end
end
class Event::PCHAT < Event
attr_reader :user, :content
def initialize(info)
user, @content = info.split(",", 2)
@user = Iduel::User.new user
end
end
class Event::JOINROOMOK < Event
attr_reader :room
def initialize(id)
@room = Iduel::Room.new(id)
end
end
class Event::QROOMOK < Event
end
class Event::SingleRoomInfo < Event
def initialize(info)
id, x, player1, player2 = info.split(",", 4)
@room = Room.new(id)
@room.player1 = User.new(player1)
@room.player2 = User.new(player2)
end
end
#"Q"
#"273,1,zh99998(201448),zh99997(201629)"
class Event::WATCHROOMSTART < Event
def initialize(info)
id, name = info.split(",", 1)
@room = Iduel::Room.new(id.to_i, name, '', '', false, Color[0])#:name, :player1, :player2, :crypted, :color
end
end
class Event::UMSG < Event
attr_reader :action
def initialize(info)
@action = Action.parse info
p @action
end
end
class Event::WMSG < Event
def initialize(info)
#black_st(212671), [109] ┊墓地,苍岩┊
#p info
#p $1, $2
info =~ /(.+)\((\d+)\), \[(\d+)\] (.*)/m #cchenwor(211650), [27] ◎→<[效果怪兽][盟军·次世代鸟人] 1400 400>攻击8
@args = [$1, $2, $3, $4]
end
end
class Event::WATCHSTOP < Event
end
class Event::Error < Event
def initialize(info)
@title, @message = case info.to_i
when 0x00
["网络错误", "网络连接中断"]
when 0x65
["出错啦~", "服务器程序出现未知错误,请记录好出现错误的事件,并联系管理员。"]
when 0x66
["错误", "通信验证错误"]
when 0x67
["错误", "通信钥匙错误"]
when 0xc9
["登录失败", "错误的帐号名或密码"]
when 0xca
["登录失败", "你的账号还未激活"]
when 0xcb
["登录失败", "你的账号被系统封锁"]
when 0x12d
["错误", "房间已满"]
when 0x12e
["错误", "房间密码错误"]
when 0x12f
["错误", "你没有权限给房间上密码"]
when 0x130
["错误", "你已经加入房间,请不要重新加入"]
when 0x131
["加入房间", "你未达到房间要求的等级限制。"]
when 0x132
["观战错误", "所请求的房间无效,或未开始决斗"]
when 0x133
["观战错误", "你已经在该房间观战"]
when 0x134
["发送信息错误", "你还未加入房间"]
when 0x135
["错误", "请求的房间无效"]
end
#Exception.new(@message).raise
puts @title.encode! "GBK"
puts @message.encode! "GBK"
#system("pause")
end
end
class Event::UNKNOWN < Event
def initialize(*args)
#puts "Unknown Server Return:#{@last_info}:#{args.inspect}"
end
end
end
__END__
$conn = Iduel.new
......
class <<Action
require_relative 'action'
class Action
CardFilter = /(<\[.*?\]\[(?:.*?)\][\s\d]*>|一张怪兽卡|一张魔\/陷卡)/.to_s
PosFilter = /((?:手卡|场上|魔陷区|怪兽区|墓地|额外牌堆|除外区|卡组顶端|\(\d+\)){1,2})/.to_s
PositionFilter = /(|攻击表示|防守表示|里侧表示|背面守备表示)/.to_s
def parse_pos(pos)
def self.parse_pos(pos)
if index = pos.index("(")
index += 1
pos[index, pos.index(")")-index].to_i
......@@ -23,7 +24,7 @@ class <<Action
end
end
end
def parse_card(card)
def self.parse_card(card)
if index = card.rindex("[")
index += 1
Card.find(card[index, card.rindex("]")-index].to_sym)
......@@ -31,7 +32,7 @@ class <<Action
Card.find(nil)
end
end
def parse_position(position)
def self.parse_position(position)
case position
when "攻击表示"
:attack
......@@ -41,7 +42,7 @@ class <<Action
:set
end
end
def escape_pos(pos)
def self.escape_pos(pos)
case pos
when 0..5
"魔陷区(#{pos})"
......@@ -59,7 +60,7 @@ class <<Action
"卡组顶端"
end
end
def escape_position(position)
def self.escape_position(position)
case position
when :attack
"攻击表示"
......@@ -69,7 +70,7 @@ class <<Action
"里侧表示"
end
end
def escape_card(card)
def self.escape_card(card)
if [:通常魔法, :永续魔法, :装备魔法, :场地魔法, :通常陷阱, :永续陷阱, :反击陷阱].include? card.card_type
if card.position == :set
"一张魔/陷卡"
......@@ -84,7 +85,7 @@ class <<Action
end
end
end
def parse(str)
def self.parse(str)
str =~ /^\[\d+\] (.*)▊▊▊.*?$/m
from_player = false
case $1
......@@ -144,6 +145,14 @@ class <<Action
end
end
def escape
case self
when FirstToGo
"[#{@id}] ◎→[11年3月1日禁卡表]先攻"
when Reset
"[#{@id}] ◎→[11年3月1日禁卡表] Duel!!"
end
end
def run
$iduel.action self
end
end
\ No newline at end of file
class Iduel
Event = Class.new{@queue = []} #避开SDL::Event问题,所以没有用class Event
class Event
def self.push(event)
@queue << event
end
def self.poll
@queue.shift
end
def self.parse(info)
info =~ /^\$([A-Z])\|(.*)$/m
Event.push case $1
when "A"
Event::Error
when "B"
Event::LOGINOK
when "C"
Event::OLIF
when "F"
Event::JOINROOMOK
when "G"
Event::WATCHROOMSTART
when "J"
Event::UMSG
when "K"
Event::WMSG
when "M"
Event::QROOMOK #TODO
when "O"
Event::PCHAT
when "P"
Event::RMIF
when "Q"
Event::SingleRoomInfo
when "R"
Event::QROOMOK #卡表
else
p $1, $2
system("pause")
Event::UNKNOWN
end.new($2)
end
class LOGINOK < Event
attr_reader :user, :session
def initialize(info)
info = info.split(",")
#>> $B|201629,zh99997,5da9e5fa,Level-1 (总经验:183),,20101118
info[3] =~ /Level-(\d)+ \(总经验:(\d+)\)/
$iduel.user = @user = User.new(info[0].to_i, info[1], $1.to_i, $2.to_i)
$iduel.session = @session = info[2]
$iduel.key = ($iduel.user.id - 0x186a0) ^ 0x22133
end
end
class OLIF < Event
attr_reader :users
def initialize(info)
@users = info.split(',').collect do |user|
Iduel::User.new(user)
end
end
end
class RMIF < Event
attr_reader :rooms
def initialize(info)
info = info.split("|")
@rooms = []
templist = @rooms
empty = false
info.each do |room|
if room == '~~'
empty = true
templist = []
else
room = room.split(",")
templist << if empty
Iduel::Room.new(room[0].to_i, room[1], Iduel::User.new(room[2]), nil, room[3]=="1", Iduel::Color[room[4].to_i], nil, room[6])
else
Iduel::Room.new(room[0].to_i, room[3], Iduel::User.new(room[1]), Iduel::User.new(room[2]), false, Iduel::Color[room[5].to_i], room[3])
end
end
end
@rooms = templist + @rooms
end
end
class NOL < Event
def initialize(info)
super
@args = @args.collect do |user|
Iduel::User.new(user)
end
end
end
class DOL < Event
def initialize(info)
super
@args = @args.collect do |user|
Iduel::User.new(user)
end
end
end
class PCHAT < Event
attr_reader :user, :content
def initialize(info)
user, @content = info.split(",", 2)
@user = Iduel::User.new user
end
end
class JOINROOMOK < Event
attr_reader :room
def initialize(id)
@room = Iduel::Room.new(id)
end
end
class QROOMOK < Event
end
class SingleRoomInfo < Event
def initialize(info)
id, x, player1, player2 = info.split(",", 4)
@room = Room.new(id)
@room.player1 = User.new(player1)
@room.player2 = User.new(player2)
end
end
#"Q"
#"273,1,zh99998(201448),zh99997(201629)"
class WATCHROOMSTART < Event
def initialize(info)
id, name = info.split(",", 1)
@room = Iduel::Room.new(id.to_i, name, '', '', false, Color[0])#:name, :player1, :player2, :crypted, :color
end
end
class UMSG < Event
attr_reader :action
def initialize(info)
@action = Action.parse info
p @action
end
end
class WMSG < Event
def initialize(info)
#black_st(212671), [109] ┊墓地,苍岩┊
#p info
#p $1, $2
info =~ /(.+)\((\d+)\), \[(\d+)\] (.*)/m #cchenwor(211650), [27] ◎→<[效果怪兽][盟军·次世代鸟人] 1400 400>攻击8
@args = [$1, $2, $3, $4]
end
end
class WATCHSTOP < Event
end
class Error < Event
def initialize(info)
@title, @message = case info.to_i
when 0x00
["网络错误", "网络连接中断"]
when 0x65
["出错啦~", "服务器程序出现未知错误,请记录好出现错误的事件,并联系管理员。"]
when 0x66
["错误", "通信验证错误"]
when 0x67
["错误", "通信钥匙错误"]
when 0xc9
["登录失败", "错误的帐号名或密码"]
when 0xca
["登录失败", "你的账号还未激活"]
when 0xcb
["登录失败", "你的账号被系统封锁"]
when 0x12d
["错误", "房间已满"]
when 0x12e
["错误", "房间密码错误"]
when 0x12f
["错误", "你没有权限给房间上密码"]
when 0x130
["错误", "你已经加入房间,请不要重新加入"]
when 0x131
["加入房间", "你未达到房间要求的等级限制。"]
when 0x132
["观战错误", "所请求的房间无效,或未开始决斗"]
when 0x133
["观战错误", "你已经在该房间观战"]
when 0x134
["发送信息错误", "你还未加入房间"]
when 0x135
["错误", "请求的房间无效"]
end
#Exception.new(@message).raise
puts @title.encode! "GBK"
puts @message.encode! "GBK"
#system("pause")
end
end
class UNKNOWN < Event
def initialize(*args)
#puts "Unknown Server Return:#{@last_info}:#{args.inspect}"
end
end
end
end
\ No newline at end of file
......@@ -24,7 +24,6 @@ class Scene
def refresh_rect(x, y, width, height)
Surface.blit(@background,x,y,width,height,$screen,x,y)
yield
#p x,y,width, height
$screen.update_rect(x, y, width, height)
end
#--------------------------------------------------------------------------
......
......@@ -39,8 +39,11 @@ class Scene_Duel < Scene
Action.opponent_field = @opponent
$screen.update_rect(0,0,0,0)
post_start
end
def post_start
first_to_go
end
def change_phase(phase)
if phase == 5
@turn_player = !@turn_player
......@@ -51,6 +54,12 @@ class Scene_Duel < Scene
@phases_window.refresh
end
end
def reset
Action::Reset.new(true).run
end
def first_to_go
Action::FirstToGo.new(true).run
end
def handle(event)
case event
when Event::MouseMotion
......@@ -72,15 +81,24 @@ class Scene_Duel < Scene
@phases_window.index = @phase
end
end
else
super
end
when Event::KeyDown
case event.sym
when Key::F1
suffle
when Key::F2
draw
when Key::F5
reset
end
else
super
end
end
def handle_iduel(event)
case event
when Iduel::Event::UMSG
event.action.do
when Iduel::Event::Action
event.action.run
@player_field_window.refresh
end
end
......@@ -90,4 +108,11 @@ class Scene_Duel < Scene
handle_iduel(event)
end
end
def refresh_rect(x, y, width, height)
return unless $scene = self #线程的情况
p @background,$screen
Surface.blit(@background,x,y,width,height,$screen,x,y)
yield
$screen.update_rect(x, y, width, height)
end
end
\ No newline at end of file
......@@ -47,10 +47,16 @@ class Scene_Hall < Scene
when Key::RETURN
@active_window.clicked
when Key::F5
if room = @roomlist.list.find{|room|room.player1 == $iduel.user or room.player2 == $iduel.user}
if @roomlist and room = @roomlist.list.find{|room|room.player1 == $iduel.user or room.player2 == $iduel.user}
$iduel.qroom room
end
$iduel.upinfo
when Key::F12
if @roomlist and room = @roomlist.list.find{|room|room.player1 == $iduel.user or room.player2 == $iduel.user}
$iduel.qroom room
end
$iduel.close
$scene = Scene_Login.new
end
when Event::KeyUp
case event.sym
......@@ -155,7 +161,7 @@ def a
if room.full?
$iduel.watch room
else
$iduel.join room, "test"
$iduel.join room, "zh"
end
end
when :right
......
require_relative 'ugl/sprite'
require_relative 'ugl/graphics'
require_relative 'ugl/event'
require_relative 'ugl/color'
require_relative 'ugl/font'
require_relative 'ugl/image'
require_relative 'ugl/audio'
#require_relative 'ugl/textimage'
require_relative 'ugl/window'
#<SDL::Event::Active:0x2b134f0 @gain=false, @state=1>
#<SDL::Event::Active:0x2b13310 @gain=false, @state=2>
#<SDL::Event::Active:0x2b131c0 @gain=true, @state=1>
#<SDL::Event::Active:0x2b13040 @gain=false, @state=1>
#<SDL::Event::Active:0x2b12ed8 @gain=true, @state=6>
#<SDL::Event::Active:0x2b12d70 @gain=true, @state=1>
#<SDL::Event::Active:0x2b127b8 @gain=false, @state=1>
#<SDL::Event::Active:0x2b12668 @gain=true, @state=1>
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