Commit 3d02ed93 authored by 神楽坂玲奈's avatar 神楽坂玲奈

用户信息初步,头像下载优化

parent 2342d619
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
......@@ -10,13 +10,15 @@ class Iduel
attr_accessor :user
attr_accessor :room_id
attr_accessor :key
attr_accessor :rooms
def initialize
require 'socket'
require 'digest/md5'
require 'open-uri'
require_relative 'iduel_action'
require_relative 'iduel_event'
require_relative 'iduel_user'
require_relative 'iduel_room'
@conn = TCPSocket.open(Server, Port)
@conn.set_encoding "GBK"
Thread.abort_on_exception = true
......@@ -70,78 +72,4 @@ class Iduel
def quit
send(11, @key, checknum("ULO", "#{@session}"))
end
end
class Iduel::User
@@all = []
attr_accessor :id, :name, :level, :exp
class << self
alias old_new new
def new(id, name = "", level = nil, exp = nil)
if id.is_a? String and id =~ /(.*)\((\d+)\)/
id = $2.to_i
name=$1
else
id = id.to_i
end
user = @@all.find{|user| user.id == id }
if user
user.name = name if name
user.level = level if level
user.exp = exp if exp
user
else
user = old_new(id, name, level, exp)
@@all << user
user
end
end
end
def initialize(id, name = "", level = nil, exp = nil)
@id = id
@name = name
@level = level
@exp = exp
end
def avatar(size = :small)
cache = "graphics/avatars/#{@id}_#{size}.png"
Thread.new do
open("http://www.duelcn.com/uc_server/avatar.php?uid=#{id-100000}&size=#{size}", 'rb') do |io|
open(cache, 'wb') {|c|c.write io.read}
end rescue Thread.exit
yield Surface.load cache
end rescue p("http://www.duelcn.com/uc_server/avatar.php?uid=#{id-100000}&size=#{size}") if block_given?
Surface.load cache rescue Surface.load "graphics/avatars/noavatar_#{size}.gif"
end
end
class Iduel::Room
@@all = []
attr_accessor :id, :name, :player1, :player2, :private, :color
class << self
alias old_new new
def new(id, *args)
id = id.to_i
room = @@all.find{|room| room.id == id }
if room
room
else
room = old_new(id, *args)
@@all << room
room
end
end
end
def initialize(id, name, player1, player2, private, color, session = nil, forbid = nil)
@id =id
@name = name
@player1 = player1
@player2 = player2
@private = private
@color = color
@forbid = forbid
@session = session
end
alias full? player2
alias private? private
end
\ No newline at end of file
......@@ -80,6 +80,7 @@ class Iduel::Event::RMIF < Iduel::Event
end
end
@rooms = templist + @rooms
$iduel.rooms = @rooms
end
end
class Iduel::Event::NOL < Iduel::Event
......@@ -119,6 +120,7 @@ class Iduel::Event::SingleRoomInfo < Iduel::Event
@room = Iduel::Room.new(id)
@room.player1 = Iduel::User.new(player1)
@room.player2 = Iduel::User.new(player2)
$iduel.rooms << @room unless $iduel.rooms.include? @room
end
end
#"Q"
......
class Iduel::Room
@@all = []
attr_accessor :id, :name, :player1, :player2, :private, :color
class << self
alias old_new new
def new(id, *args)
id = id.to_i
room = @@all.find{|room| room.id == id }
if room
room
else
room = old_new(id, *args)
@@all << room
room
end
end
end
def initialize(id, name, player1, player2, private, color, session = nil, forbid = nil)
@id =id
@name = name
@player1 = player1
@player2 = player2
@private = private
@color = color
@forbid = forbid
@session = session
end
alias full? player2
alias private? private
end
\ No newline at end of file
class Iduel::User
@@all = []
attr_accessor :id, :name, :level, :exp
class << self
alias old_new new
def new(id, name = "", level = nil, exp = nil)
if id.is_a? String and id =~ /(.*)\((\d+)\)/
id = $2.to_i
name=$1
else
id = id.to_i
end
user = @@all.find{|user| user.id == id }
if user
user.name = name if name
user.level = level if level
user.exp = exp if exp
user
else
user = old_new(id, name, level, exp)
@@all << user
user
end
end
end
def initialize(id, name = "", level = nil, exp = nil)
@id = id
@name = name
@level = level
@exp = exp
end
def avatar(size = :small)
cache = "graphics/avatars/#{@id}_#{size}.png"
result = Surface.load(cache) rescue Surface.load("graphics/avatars/loading_#{size}.gif")
if block_given?
Thread.new do
open("http://www.duelcn.com/uc_server/avatar.php?uid=#{id-100000}&size=#{size}", 'rb') {|io|open(cache, 'wb') {|c|c.write io.read}} rescue cache = "graphics/avatars/noavatar_#{size}.gif"
yield Surface.load cache
end
yield result
else
result
end
end
def status
room = $iduel.rooms.find{|room|room.player1 == @user or room.player2 == @user}
result = case
when room.nil?
"等待中"
when room.player2
"房间#{room.id}决斗中"
else
"房间#{room.id}等待中"
end
result
end
end
\ No newline at end of file
#encoding: UTF-8
alias gbk_puts puts
def puts(*args)
gbk_puts(*(args.collect{|item|item.encode "GBK"}))
end
alias gbk_puts puts
def puts(*args)
gbk_puts(*(args.collect{|item|item.encode "UTF-8"}))
end
def p(*args)
print(args.collect{|item|item.inspect.encode "GBK"}.join("\n")+"\n") rescue print(args.join("\n")+"\n")
end
def p(*args)
print(args.collect{|item|item.inspect.encode "UTF-8"}.join("\n")+"\n") rescue print(args.join("\n")+"\n")
end
require 'sdl'
include SDL
......@@ -17,7 +17,7 @@ $config = YAML.load_file("config.yml") rescue YAML.load_file("data/config_defaul
SDL.init(INIT_VIDEO | INIT_AUDIO)
WM::set_caption("iDuel - 享受决斗", "graphics/system/iDuelPanel_32512.ico")
WM::icon = Surface.load("graphics/system/iDuelPanel_32512.ico")
#WM::icon = Surface.load("graphics/system/iDuelPanel_32512.ico")
style = HWSURFACE
style |= FULLSCREEN if $config["fullscreen"]
$screen = Screen.open($config["width"], $config["height"], 0, style)
......@@ -28,7 +28,9 @@ SDL::Mixer.open(Mixer::DEFAULT_FREQUENCY,Mixer::DEFAULT_FORMAT,Mixer::DEFAULT_CH
#SDL::Mixer.open
require_relative 'scene'
require_relative 'window'
require_relative 'window_list'
require_relative 'window_user'
require_relative 'scene_title'
$scene = Scene_Title.new
......
......@@ -21,8 +21,8 @@ class Scene
#--------------------------------------------------------------------------
def start
end
def refresh_rect(x, y, width, height)
Surface.blit(@background,x,y,width,height,$screen,x,y)
def refresh_rect(x, y, width, height, background=@background, ox=0,oy=0)
Surface.blit(background,x+ox,y+oy,width,height,$screen,x,y)
yield
$screen.update_rect(x, y, width, height)
end
......
......@@ -32,7 +32,7 @@ class Scene_Hall < Scene
case event
when Event::MouseMotion
[@playerlist, @roomlist].each do |window|
if window .include? event.x, event.y
if window.include? event.x, event.y
@active_window = window
@active_window.mousemoved(event.x, event.y)
break
......
......@@ -15,7 +15,7 @@ class Scene_Watch < Scene_Duel
Iduel::Event::DOL.callback do |event|
p event
end
Iduel::Event::RMIF.callback do |event|
Iduel::Event::RMIF.callback do |event|66
p event
end
Iduel::Event::WMSG.callback do |event|
......
class Window
attr_reader :x, :y, :width, :height
def initialize(x, y, width, height)
@x = x
@y = y
@width = width
@height = height
end
def refresh
end
def include?(x,y)
x > @x && x < @x + @width && y > @y && y < @y + @height
end
def dispose
$scene.refresh_rect(@x, @y, @width, @height)
end
end
\ No newline at end of file
......@@ -5,15 +5,11 @@
#  大厅内房间列表
#==============================================================================
class Window_List
attr_reader :x, :y, :width, :height
class Window_List < Window
attr_reader :list
attr_reader :index
def initialize(x, y, width, height)
@x = x
@y = y
@width = width
@height = height
super(x,y,width, height)
@o_index = 0
@item_max = 0
@column_max = 1
......@@ -59,8 +55,6 @@ class Window_List
def clicked
$scene.refresh_rect(*item_rect(@index)){draw_item(@index, 2)} if @index
end
def include?(x,y)
x > @x && x < @x + @width && y > @y && y < @y + @height
end
end
......@@ -11,9 +11,18 @@ class Window_PlayerList < Window_List
super(x,y,272,16*34)
@font = TTF.open("fonts/WenQuanYi Micro Hei.ttf", WLH)
@color = [0x03, 0x11, 0x22]
@color_over = [0x03, 0x11, 0x22, 200,200,255]
@color_click = [200,200,255, 0x03, 0x11, 0x22]
end
def draw_item(index, status=0)
@font.draw_blended_utf8($screen, @list[index].name, @x, @y+index*WLH, *@color)
case status
when 0
@font.draw_blended_utf8($screen, @list[index].name, @x, @y+index*WLH, *@color)
when 1
@font.draw_shaded_utf8($screen, @list[index].name, @x, @y+index*WLH, *@color_over)
when 2
@font.draw_shaded_utf8($screen, @list[index].name, @x, @y+index*WLH, *@color_click)
end
end
def item_rect(index)
[@x, @y+WLH*index, @width, WLH]
......@@ -24,8 +33,14 @@ class Window_PlayerList < Window_List
@height = @item_max * WLH
refresh
end
def clicked
$scene.refresh_rect(*item_rect(@index)){draw_item(@index, 2)} if @index
@userwindow = Window_User.new(100,100,@list[@index])
end
def mousemoved(x,y)
return unless include?(x,y)
self.index = (y - @y) / WLH
if (y-@y) / 24 < @item_max
self.index = (y - @y) / WLH
end
end
end
\ No newline at end of file
class Window_User < Window
def initialize(x, y, user)
@background = Surface.load "graphics/hall/user.png"
@boarder = Surface.load "graphics/hall/avatar_boader.png"
super(x,y,@background.w,@background.h)
@font = TTF.open('fonts/WenQuanYi Micro Hei.ttf', 16)
@user = user
refresh
end
def refresh(x=@x, y=@y, width=@width, height=@height)
$scene.refresh_rect(@x, @y, @width, @height, @background, -@x, -@y) do
Surface.blit(@background, 0,0,0,0,$screen,@x,@y)
@thread = @user.avatar(:middle) do |avatar|
$scene.refresh_rect(@x+12, @y+12, @boarder.w, @boarder.h, @background, -@x, -@y) do
Surface.blit(avatar, 0,0,0,0,$screen,@x+24,@y+24)
Surface.blit(@boarder, 0,0,0,0,$screen,@x+12,@y+12)
end
end
@font.draw_blended_utf8($screen, @user.name, @x+172, @y+24, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, "id: #{@user.id}" , @x+172, @y+24+16*2, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, @user.status , @x+172, @y+24+16*3, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, "发送消息" , @x+172, @y+24+16*4+8, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, "查看资料" , @x+172, @y+24+16*5+8, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, "加为好友" , @x+172, @y+24+16*6+8, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, "加入游戏" , @x+172, @y+24+16*7+8, 0x00,0x00,0x00)
end
end
def clicked
end
def dispose
@thread.exit
super
end
end
\ No newline at end of file
......@@ -5,45 +5,31 @@
# 游戏大厅显示用户信息的类
#==============================================================================
class Window_UserInfo
class Window_UserInfo < Window
def initialize(x, y, user)
@boarder = Surface.load "graphics/hall/avatar_boader.png"
@x = x
@y = y
@width = 240
@height= @boarder.h
super(x,y,240,@boarder.h)
@font = TTF.open('fonts/WenQuanYi Micro Hei.ttf', 16)
Surface.blit(user.avatar(:middle) do |newest_avatar|
$scene.refresh_rect(@x, @y, @boarder.w, @boarder.h) do
if $scene.is_a? Scene_Hall
Surface.blit(newest_avatar, 0,0,0,0,$screen,@x+12,@y+12)
Surface.blit(@boarder, 0,0,0,0,$screen,@x,@y)
end
end
end, 0,0,0,0,$screen,@x+12,@y+12)
Surface.blit(@boarder, 0,0,0,0,$screen,@x,@y)
@font.draw_blended_utf8($screen, user.name, @x+160, @y+12, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, "Level: #{user.level}" , @x+160, @y+12+16*2, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, "总经验: #{user.exp}", @x+160, @y+12+16*3, 0x00,0x00,0x00)
@user = user
refresh
end
def refresh
#p "-------------Read start-----------"
#@user.avatar{|avatar| self.contents[1] = avatar; p "-------------read end-----------" }
#self.
#se
#self.contents[1] = @user.avatar(:middle)
#p @user
#contents[0].clear
#@list.each_with_index do |player, index|
#contents[0].draw_text(player.name, 0, 16*index)
#end
def refresh(x=@x, y=@y, width=@width, height=@height)
@thread = @user.avatar(:middle) do |avatar|
$scene.refresh_rect(@x, @y, @boarder.w, @boarder.h) do
Surface.blit(avatar, 0,0,0,0,$screen,@x+12,@y+12)
Surface.blit(@boarder, 0,0,0,0,$screen,@x,@y)
end
end
@font.draw_blended_utf8($screen, @user.name, @x+160, @y+12, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, "id: #{@user.id}" , @x+160, @y+12+16*2, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, "Level: #{@user.level}" , @x+160, @y+12+16*3, 0x00,0x00,0x00)
@font.draw_blended_utf8($screen, "总经验: #{@user.exp}", @x+160, @y+12+16*4, 0x00,0x00,0x00)
end
def dispose
@thread.exit
super
end
end
#<Iduel::User:0x46b6438 @id="201629", @name="zh99997", @credit="Level-1 (\u603B\u7ECF\u9A8C:183)">
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.ruby.rubyproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/ruby-project/1">
<name>iduel</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
<root id="spec.src.dir"/>
</test-roots>
</data>
</configuration>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.ruby.rubyproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/ruby-project/1">
<name>iduel</name>
<source-roots>
<root id="src.dir"/>
</source-roots>
<test-roots>
<root id="test.src.dir"/>
<root id="spec.src.dir"/>
</test-roots>
</data>
</configuration>
</project>
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