Advertisement
Guest User

Client_DFOnline

a guest
Apr 1st, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.79 KB | None | 0 0
  1. #======================================================================
  2. # |=| FiFO Client
  3. #======================================================================
  4. # This class is used for every client
  5. # the socket moves here and also the SQL
  6. #=====================================================================
  7. # Initial Author: Kono
  8. # Re-Written By: Xolitude
  9. # DATE : 17:22 Thur, November, 6, 2008
  10. #=====================================================================
  11. class Client
  12.  
  13. attr_accessor :sock
  14. attr_accessor :sql
  15. attr_accessor :clients
  16. attr_accessor :map_id
  17. attr_accessor :name
  18. attr_accessor :level
  19. attr_accessor :netid
  20. attr_accessor :hp
  21. attr_accessor :mp
  22. attr_accessor :gold
  23. attr_accessor :username
  24. attr_accessor :event
  25. attr_accessor :agi
  26. attr_accessor :pdef
  27. attr_accessor :eva
  28. attr_accessor :mdef
  29. attr_accessor :states
  30. attr_accessor :damage
  31. attr_accessor :gold
  32. attr_accessor :loggedin
  33. attr_accessor :playerid
  34. attr_reader :id # ID
  35. attr_reader :x # map x-coordinate (logical)
  36. attr_reader :y # map y-coordinate (logical)
  37. attr_reader :real_x # map x-coordinate (real * 128)
  38. attr_reader :real_y # map y-coordinate (real * 128)
  39. attr_reader :tile_id # tile ID (invalid if 0)
  40. attr_reader :character_name # character file name
  41. attr_reader :character_hue # character hue
  42. attr_reader :opacity # opacity level
  43. attr_reader :blend_type # blending method
  44. attr_reader :direction # direction
  45. attr_reader :pattern # pattern
  46. attr_reader :move_route_forcing # forced move route flag
  47. attr_reader :through # through
  48. attr_accessor :animation_id # animation ID
  49. attr_accessor :transparent # transparent flag
  50.  
  51. def initialize
  52. @sock = nil
  53. @sql = nil
  54. @clients = {}
  55. @last_msg = ""
  56. @playerid = 0
  57. @can_handle = true
  58. @servername = MESSAGE1
  59. @map_id = 0
  60. @name = ""
  61. @character_name = ""
  62. @character_index = 1
  63. @level = -1
  64. @netid = -1
  65. @hp = -1
  66. @mp = -1
  67. @gold = 0
  68. @username = ""
  69. @move_tom = true
  70. @event = self
  71. @eva = 0
  72. @agi = 0
  73. @pdef = 0
  74. @mdef = 0
  75. @states = []
  76. @damage = 0
  77. @gold = 0
  78. @ver = 1.7
  79. @group = "Standard"
  80. @pm = ""
  81. @id = 0
  82. @x = 0
  83. @y = 0
  84. @real_x = 0
  85. @real_y = 0
  86. @tile_id = 0
  87. @character_name = ""
  88. @character_hue = 0
  89. @opacity = 255
  90. @blend_type = 0
  91. @direction = 2
  92. @pattern = 0
  93. @move_route_forcing = false
  94. @through = false
  95. @animation_id = 0
  96. @transparent = false
  97. @original_direction = 2
  98. @original_pattern = 0
  99. @move_type = 0
  100. @move_speed = 4
  101. @move_frequency = 6
  102. @move_route = nil
  103. @move_route_index = 0
  104. @original_move_route = nil
  105. @original_move_route_index = 0
  106. @walk_anime = true
  107. @step_anime = false
  108. @direction_fix = false
  109. @always_on_top = false
  110. @anime_count = 0
  111. @stop_count = 0
  112. @jump_count = 0
  113. @jump_peak = 0
  114. @wait_count = 0
  115. @locked = false
  116. @prelock_direction = 0
  117. @loggedin = 0
  118. @switches = Game_Switches.new
  119.  
  120. @items = {} # Inventory item hash (item ID)
  121. @weapons = {} # Inventory item hash (weapon ID)
  122. @armors = {} # Inventory item hash (armor ID)
  123.  
  124. end
  125.  
  126. def login(name,password)
  127. Mysql::query
  128. @select = @sql.query("SELECT * FROM users WHERE name='#{name}'")
  129. @hash = @select.fetch_hash
  130. if @select.num_rows() < 1
  131. return "not exist"
  132. end
  133. if @hash['password'] != password
  134. return "wrong password"
  135. end
  136. if @hash['banned'].to_i == 1
  137. return "banned"
  138. end
  139. if @hash['loggedin'].to_i == 1
  140. return "logged in"
  141. end
  142. @loggedin = 1
  143. @group = @hash['group']
  144. puts @group
  145. @netid = @hash['id'].to_i
  146. @sql.query("UPDATE users SET loggedin=1 WHERE name='#{name}'")
  147. return "ok"
  148. end
  149.  
  150. def register(user,pass)
  151. @check = @sql.query("SELECT loggedin FROM users WHERE name='#{user}'")
  152. if @check.num_rows() >= 1
  153. return "exist"
  154. end
  155. @sql.query("INSERT INTO users(name,password) VALUES('#{user}','#{pass}')")
  156. return "done"
  157. end
  158.  
  159. def handle(clients_list, msg)
  160. @clients = clients_list
  161. @last_msg = msg
  162. @msg = msg
  163. case @msg
  164. when /<0>'e'<\/0>/ # authnificate
  165. send("<0 0>'e' n=#{SERVERNAME}</0>\n")
  166. when /<login (.*)>(.*)<\/login>/
  167. puts $1 + " " + $2
  168. result = login($1, $2)
  169. if result=="not exist"
  170. send("<login>'wu'</login>\n")
  171. elsif result=="wrong password"
  172. send("<login>'wp'</login>\n")
  173. elsif result=="logged in"
  174. send("<login>'wu'</login>\n")
  175. elsif result=="ok"
  176. send("<login>'allow'</login>\n")
  177. send("<1>#{@netid}</1>\n")
  178. send("<2>#{$1}</2>\n")
  179. end
  180. when /<register>(.*);(.*);(.*);(.*);(.*)<\/register>/
  181. register($1, $2, $3, $4, $5)
  182. if stats == "exist"
  183. send("<register>'exist'</register>\n")
  184. else
  185. send("<reges>'ok'</reges>\n")
  186. end
  187. when /<chareq>/
  188. handle_characters if @can_handle
  189. when /<new>(.*)<\/new>/
  190. create_character($1)
  191. when /<5 c=(.*)>(.*)<\/5>/
  192. @ingame = true
  193. $clients.values.each { |client|
  194. if client.netid.to_i == $1.to_i
  195. client.send("<5 #{@netid}>#{$2}</5>\n");
  196. break;
  197. end
  198. }
  199. when /<5>(.*)<\/5>/
  200. eval($1)
  201. @ingame = true
  202. sendmap("<5 #{@netid}>#{$1}</5>\n")
  203. when /<ver>(.*)<\/ver>/
  204. @ver = $1
  205. send("<ver>#{@ver}</ver>\n")
  206. when /<1>(.*)<\/1>/
  207. send("<1>#{@netid}</1>\n")
  208. when /<2>(.*)<\/2>/
  209. send("<2>#{@name}</1>\n")
  210. when /<check>(.*)<\/check>/
  211. send("<check>#{@group}</check>\n")
  212. when /<mod>(.*)<\/mod>/
  213. send("<mod>#{MOTD}</mod>\n")
  214. when /<exist>(.*)<\/exist>/
  215. @exist = @sql.query("SELECT banned FROM users WHERE name='#{$1}'")
  216. if @exist.num_rows() == 1
  217. send("<exist>1</exist>\n")
  218. else
  219. send("<exist>0</exist>\n")
  220. end
  221. when /<switch>(.*) = (.*)<\/switch>/
  222. switch_interaction($1.to_i, eval($2))
  223. when /<chat>(.*)<\/chat>/
  224. sendall("<chat>#{$1}</chat>\n",true)
  225. when /<getpm>(.*)<\/getpm>/
  226. send("<getpm>#{$1}</getpm>\n")
  227. when /<21a>(.*)/
  228. send("<21a>'Confirmed'</21a>\n")
  229. when /<21>(.*)<\/21>/
  230. sendall("<21>#{$1}</21>\n")
  231. when /<9>(.*)<\/9>/
  232. remove
  233. else
  234. if DEBUG_MSG
  235. puts "FiFO: Unhandled Message Receieved: #{@msg}"
  236. end
  237. end
  238. end
  239.  
  240. def send(msg)
  241. sock.send(msg,0)
  242. end
  243.  
  244. def remove
  245. if @ingame
  246. save
  247. sendall("<9>#{@netid}</9>\n")
  248. $clients.delete(self)
  249. end
  250. end
  251.  
  252. def gain_item(itemid, type, howmany)
  253. number = item_number(item)
  254. case item
  255. when RPG::Item
  256. @items[id] = [[number + n, 0].max, 99].min
  257. when RPG::Weapon
  258. @weapons[item.id] = [[number + n, 0].max, 99].min
  259. when RPG::Armor
  260. @armors[item.id] = [[number + n, 0].max, 99].min
  261. end
  262. end
  263.  
  264. def save_items
  265. for item in @items.keys
  266. exist = @sql.query("SELECT * FROM inventory WHERE player_id = '#{@playerid}' AND item_id = '#{item.id.to_i}'")
  267. if exist.num_rows >= 1
  268. @sql.query("UPDATE inventory SET quantity = '#{@items[item].to_i}' WHERE player_id = '#{@playerid}' AND item_id = '#{item.id.to_i}'")
  269. else
  270. @sql.query("INSERT INTO inventory(item_id, quantity, player_id) VALUES('#{item.id.to_i}','#{@items[item].to_i}','#{@playerid}')")
  271. end
  272. end
  273. end
  274.  
  275. def send_items(itemid, quantity)
  276. sel = @sql.query("SELECT * FROM inventory WHERE player_id = '#{@playerid}'")
  277. sel.each_hash do |item|
  278. send("<item #{item['item_id'].to_i}>#{item['quantity']}</item>\n")
  279. end
  280. end
  281.  
  282. def sendall(data,including = false)
  283. # Sending to all
  284. $clients.values.each do |client|
  285. next if (client.sock == @sock and !including) or client.netid==-1
  286. next if client.sock == nil
  287. next if client.sock.closed?
  288. next if client.map_id != @map_id
  289. client.send(data)
  290. end
  291. end
  292.  
  293. def ic
  294. return
  295. end
  296.  
  297. def start(id)
  298. return
  299. end
  300.  
  301. def sendmap(msg, including = false)
  302. for client in $clients.values
  303. next if client.sock == nil
  304. next if client.sock.closed?
  305. next if (client.sock == @sock and !including) or client.netid == -1
  306. next if client.map_id != map_id
  307. client.send(msg)
  308. end
  309. end
  310.  
  311. def handle_characters
  312. @can_handle = false
  313. selection = @sql.query("SELECT * FROM players WHERE userid = '#{@netid}'")
  314. if selection.num_rows() >= 1
  315. send_character(selection)
  316. else
  317. send_empty_character
  318. end
  319. end
  320.  
  321. def send_empty_character
  322. send("<character>'new'</character>\n")
  323. @can_handle = false # Validate
  324. end
  325.  
  326. def send_character(information)
  327. fh = information.fetch_hash
  328. character = fh.to_custom_string
  329. puts character
  330. send("<character>#{character}</character>\n")
  331. @playerid = fh['id'].to_i
  332. load_switches
  333. @can_handle = false # Validate
  334. end
  335.  
  336. def create_character(information)
  337. @can_handle = false
  338. check = @sql.query("SELECT id FROM players WHERE userid = '#{@netid}'")
  339. if check.num_rows() >= 1
  340. return
  341. end
  342. @data = eval(information)
  343. @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}')")
  344. handle_characters
  345. end
  346.  
  347. def save
  348. begin
  349. selection = @sql.query("SELECT id FROM players WHERE userid = '#{@netid}'")
  350. if selection.num_rows() >= 1
  351. @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}'")
  352. end
  353. rescue Exception
  354. puts $!
  355. puts $!.backtrace.join("\n")
  356. end
  357. end
  358.  
  359. def switch_interaction(switchid, active)
  360. return if @playerid.nil? or @playerid == 0
  361. switch_exist = @sql.query("SELECT active FROM switches WHERE player_id = '#{@playerid}' AND switch_id = '#{switchid.to_i}'")
  362. if switch_exist.num_rows() >= 1 # If exist
  363. @sql.query("UPDATE switches SET active = '#{active == true ? 1 : 0}' WHERE player_id = '#{@playerid}' AND switch_id = '#{switchid.to_i}'")
  364. else
  365. @sql.query("INSERT INTO switches(switch_id,player_id,active) VALUES ('#{switchid.to_i}','#{@playerid.to_i}','#{active == true ? 1 : 0}')")
  366. end
  367. @switches[switchid.to_i] = active
  368. end
  369.  
  370. def send_switch(switchid, active)
  371. send("<switch #{switchid.to_i}>#{active}</switch>\n")
  372. end
  373.  
  374. def load_switches
  375. sel = @sql.query("SELECT * FROM switches WHERE player_id = '#{@playerid}'")
  376. sel.each_hash do |switch|
  377. @switches[switch['switch_id'].to_i] = switch['active'].to_i == 1 ? true : false
  378. send_switch(switch['switch_id'].to_i, switch['active'].to_i == 1 ? true : false)
  379. end
  380. end
  381.  
  382. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement