Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #======================================================================
- # |=| FiFO Client
- #======================================================================
- # This class is used for every client
- # the socket moves here and also the SQL
- #=====================================================================
- # Initial Author: Kono
- # Re-Written By: Xolitude
- # DATE : 17:22 Thur, November, 6, 2008
- #=====================================================================
- class Client
- attr_accessor :sock
- attr_accessor :sql
- attr_accessor :clients
- attr_accessor :map_id
- attr_accessor :name
- attr_accessor :level
- attr_accessor :netid
- attr_accessor :hp
- attr_accessor :mp
- attr_accessor :gold
- attr_accessor :username
- attr_accessor :event
- attr_accessor :agi
- attr_accessor :pdef
- attr_accessor :eva
- attr_accessor :mdef
- attr_accessor :states
- attr_accessor :damage
- attr_accessor :gold
- attr_accessor :loggedin
- attr_accessor :playerid
- attr_reader :id # ID
- attr_reader :x # map x-coordinate (logical)
- attr_reader :y # map y-coordinate (logical)
- attr_reader :real_x # map x-coordinate (real * 128)
- attr_reader :real_y # map y-coordinate (real * 128)
- attr_reader :tile_id # tile ID (invalid if 0)
- attr_reader :character_name # character file name
- attr_reader :character_hue # character hue
- attr_reader :opacity # opacity level
- attr_reader :blend_type # blending method
- attr_reader :direction # direction
- attr_reader :pattern # pattern
- attr_reader :move_route_forcing # forced move route flag
- attr_reader :through # through
- attr_accessor :animation_id # animation ID
- attr_accessor :transparent # transparent flag
- def initialize
- @sock = nil
- @sql = nil
- @clients = {}
- @last_msg = ""
- @playerid = 0
- @can_handle = true
- @servername = MESSAGE1
- @map_id = 0
- @name = ""
- @character_name = ""
- @character_index = 1
- @level = -1
- @netid = -1
- @hp = -1
- @mp = -1
- @gold = 0
- @username = ""
- @move_tom = true
- @event = self
- @eva = 0
- @agi = 0
- @pdef = 0
- @mdef = 0
- @states = []
- @damage = 0
- @gold = 0
- @ver = 1.7
- @group = "Standard"
- @pm = ""
- @id = 0
- @x = 0
- @y = 0
- @real_x = 0
- @real_y = 0
- @tile_id = 0
- @character_name = ""
- @character_hue = 0
- @opacity = 255
- @blend_type = 0
- @direction = 2
- @pattern = 0
- @move_route_forcing = false
- @through = false
- @animation_id = 0
- @transparent = false
- @original_direction = 2
- @original_pattern = 0
- @move_type = 0
- @move_speed = 4
- @move_frequency = 6
- @move_route = nil
- @move_route_index = 0
- @original_move_route = nil
- @original_move_route_index = 0
- @walk_anime = true
- @step_anime = false
- @direction_fix = false
- @always_on_top = false
- @anime_count = 0
- @stop_count = 0
- @jump_count = 0
- @jump_peak = 0
- @wait_count = 0
- @locked = false
- @prelock_direction = 0
- @loggedin = 0
- @switches = Game_Switches.new
- @items = {} # Inventory item hash (item ID)
- @weapons = {} # Inventory item hash (weapon ID)
- @armors = {} # Inventory item hash (armor ID)
- end
- def login(name,password)
- Mysql::query
- @select = @sql.query("SELECT * FROM users WHERE name='#{name}'")
- @hash = @select.fetch_hash
- if @select.num_rows() < 1
- return "not exist"
- end
- if @hash['password'] != password
- return "wrong password"
- end
- if @hash['banned'].to_i == 1
- return "banned"
- end
- if @hash['loggedin'].to_i == 1
- return "logged in"
- end
- @loggedin = 1
- @group = @hash['group']
- puts @group
- @netid = @hash['id'].to_i
- @sql.query("UPDATE users SET loggedin=1 WHERE name='#{name}'")
- return "ok"
- end
- def register(user,pass)
- @check = @sql.query("SELECT loggedin FROM users WHERE name='#{user}'")
- if @check.num_rows() >= 1
- return "exist"
- end
- @sql.query("INSERT INTO users(name,password) VALUES('#{user}','#{pass}')")
- return "done"
- end
- def handle(clients_list, msg)
- @clients = clients_list
- @last_msg = msg
- @msg = msg
- case @msg
- when /<0>'e'<\/0>/ # authnificate
- send("<0 0>'e' n=#{SERVERNAME}</0>\n")
- when /<login (.*)>(.*)<\/login>/
- puts $1 + " " + $2
- result = login($1, $2)
- if result=="not exist"
- send("<login>'wu'</login>\n")
- elsif result=="wrong password"
- send("<login>'wp'</login>\n")
- elsif result=="logged in"
- send("<login>'wu'</login>\n")
- elsif result=="ok"
- send("<login>'allow'</login>\n")
- send("<1>#{@netid}</1>\n")
- send("<2>#{$1}</2>\n")
- end
- when /<register>(.*);(.*);(.*);(.*);(.*)<\/register>/
- register($1, $2, $3, $4, $5)
- if stats == "exist"
- send("<register>'exist'</register>\n")
- else
- send("<reges>'ok'</reges>\n")
- end
- when /<chareq>/
- handle_characters if @can_handle
- when /<new>(.*)<\/new>/
- create_character($1)
- when /<5 c=(.*)>(.*)<\/5>/
- @ingame = true
- $clients.values.each { |client|
- if client.netid.to_i == $1.to_i
- client.send("<5 #{@netid}>#{$2}</5>\n");
- break;
- end
- }
- when /<5>(.*)<\/5>/
- eval($1)
- @ingame = true
- sendmap("<5 #{@netid}>#{$1}</5>\n")
- when /<ver>(.*)<\/ver>/
- @ver = $1
- send("<ver>#{@ver}</ver>\n")
- when /<1>(.*)<\/1>/
- send("<1>#{@netid}</1>\n")
- when /<2>(.*)<\/2>/
- send("<2>#{@name}</1>\n")
- when /<check>(.*)<\/check>/
- send("<check>#{@group}</check>\n")
- when /<mod>(.*)<\/mod>/
- send("<mod>#{MOTD}</mod>\n")
- when /<exist>(.*)<\/exist>/
- @exist = @sql.query("SELECT banned FROM users WHERE name='#{$1}'")
- if @exist.num_rows() == 1
- send("<exist>1</exist>\n")
- else
- send("<exist>0</exist>\n")
- end
- when /<switch>(.*) = (.*)<\/switch>/
- switch_interaction($1.to_i, eval($2))
- when /<chat>(.*)<\/chat>/
- sendall("<chat>#{$1}</chat>\n",true)
- when /<getpm>(.*)<\/getpm>/
- send("<getpm>#{$1}</getpm>\n")
- when /<21a>(.*)/
- send("<21a>'Confirmed'</21a>\n")
- when /<21>(.*)<\/21>/
- sendall("<21>#{$1}</21>\n")
- when /<9>(.*)<\/9>/
- remove
- else
- if DEBUG_MSG
- puts "FiFO: Unhandled Message Receieved: #{@msg}"
- end
- end
- end
- def send(msg)
- sock.send(msg,0)
- end
- def remove
- if @ingame
- save
- sendall("<9>#{@netid}</9>\n")
- $clients.delete(self)
- end
- end
- def gain_item(itemid, type, howmany)
- number = item_number(item)
- case item
- when RPG::Item
- @items[id] = [[number + n, 0].max, 99].min
- when RPG::Weapon
- @weapons[item.id] = [[number + n, 0].max, 99].min
- when RPG::Armor
- @armors[item.id] = [[number + n, 0].max, 99].min
- end
- end
- def save_items
- for item in @items.keys
- exist = @sql.query("SELECT * FROM inventory WHERE player_id = '#{@playerid}' AND item_id = '#{item.id.to_i}'")
- if exist.num_rows >= 1
- @sql.query("UPDATE inventory SET quantity = '#{@items[item].to_i}' WHERE player_id = '#{@playerid}' AND item_id = '#{item.id.to_i}'")
- else
- @sql.query("INSERT INTO inventory(item_id, quantity, player_id) VALUES('#{item.id.to_i}','#{@items[item].to_i}','#{@playerid}')")
- end
- end
- end
- def send_items(itemid, quantity)
- sel = @sql.query("SELECT * FROM inventory WHERE player_id = '#{@playerid}'")
- sel.each_hash do |item|
- send("<item #{item['item_id'].to_i}>#{item['quantity']}</item>\n")
- end
- end
- def sendall(data,including = false)
- # Sending to all
- $clients.values.each do |client|
- next if (client.sock == @sock and !including) or client.netid==-1
- next if client.sock == nil
- next if client.sock.closed?
- next if client.map_id != @map_id
- client.send(data)
- end
- end
- def ic
- return
- end
- def start(id)
- return
- end
- def sendmap(msg, including = false)
- for client in $clients.values
- next if client.sock == nil
- next if client.sock.closed?
- next if (client.sock == @sock and !including) or client.netid == -1
- next if client.map_id != map_id
- client.send(msg)
- end
- end
- def handle_characters
- @can_handle = false
- selection = @sql.query("SELECT * FROM players WHERE userid = '#{@netid}'")
- if selection.num_rows() >= 1
- send_character(selection)
- else
- send_empty_character
- end
- end
- def send_empty_character
- send("<character>'new'</character>\n")
- @can_handle = false # Validate
- end
- def send_character(information)
- fh = information.fetch_hash
- character = fh.to_custom_string
- puts character
- send("<character>#{character}</character>\n")
- @playerid = fh['id'].to_i
- load_switches
- @can_handle = false # Validate
- end
- def create_character(information)
- @can_handle = false
- check = @sql.query("SELECT id FROM players WHERE userid = '#{@netid}'")
- if check.num_rows() >= 1
- return
- end
- @data = eval(information)
- @sql.query("INSERT INTO players(userid,hp,mp,x,y,level,map_id,character_name,character_index,armor1_id,armor2_id,armor3_id,armor4_id,weapon_id) VALUES ('#{@netid}','#{@data['hp'].to_i}','#{@data['mp'].to_i}','#{@data['x'].to_i}','#{@data['y'].to_i}','#{@data['level'].to_i}','#{@data['map_id'].to_i}','#{@data['character_name']}','#{@data['character_index']}','#{@data['armor1_id'].to_i}','#{@data['armor2_id'].to_i}','#{@data['armor3_id'].to_i}','#{@data['armor4_id'].to_i}','#{@data['weapon_id'].to_i}')")
- handle_characters
- end
- def save
- begin
- selection = @sql.query("SELECT id FROM players WHERE userid = '#{@netid}'")
- if selection.num_rows() >= 1
- @sql.query("UPDATE players SET hp = '#{@hp}',mp = '#{@mp}',x = '#{@x}',y = '#{@y}',level = '#{@level.to_i}',map_id = '#{@map_id}',character_name = '#{@character_name}',character_index = '#{@character_index}',armor1_id = '#{@armor1_id.to_i}',armor2_id = '#{@armor2_id.to_i}',armor3_id = '#{@armor3_id.to_i}',armor4_id = '#{@armor4_id.to_i}',weapon_id = '#{@weapon_id.to_i}', gold = #{@gold.to_i} WHERE userid = '#{@netid}'")
- end
- rescue Exception
- puts $!
- puts $!.backtrace.join("\n")
- end
- end
- def switch_interaction(switchid, active)
- return if @playerid.nil? or @playerid == 0
- switch_exist = @sql.query("SELECT active FROM switches WHERE player_id = '#{@playerid}' AND switch_id = '#{switchid.to_i}'")
- if switch_exist.num_rows() >= 1 # If exist
- @sql.query("UPDATE switches SET active = '#{active == true ? 1 : 0}' WHERE player_id = '#{@playerid}' AND switch_id = '#{switchid.to_i}'")
- else
- @sql.query("INSERT INTO switches(switch_id,player_id,active) VALUES ('#{switchid.to_i}','#{@playerid.to_i}','#{active == true ? 1 : 0}')")
- end
- @switches[switchid.to_i] = active
- end
- def send_switch(switchid, active)
- send("<switch #{switchid.to_i}>#{active}</switch>\n")
- end
- def load_switches
- sel = @sql.query("SELECT * FROM switches WHERE player_id = '#{@playerid}'")
- sel.each_hash do |switch|
- @switches[switch['switch_id'].to_i] = switch['active'].to_i == 1 ? true : false
- send_switch(switch['switch_id'].to_i, switch['active'].to_i == 1 ? true : false)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement