Advertisement
Guest User

Untitled

a guest
May 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.03 KB | None | 0 0
  1. #======================================================================
  2. # Action_Handler
  3. #----------------------------------------------------------------------
  4. # Handles all basic actions.
  5. #======================================================================
  6.  
  7. class Action_Handler
  8.  
  9. # setting all accessible variables
  10. attr_reader :current
  11.  
  12. #----------------------------------------------------------------------
  13. # Initialization.
  14. #----------------------------------------------------------------------
  15. def initialize(client)
  16. @client = client
  17. @current = Action.new
  18. end
  19. #----------------------------------------------------------------------
  20. # Tries to register this client.
  21. # username - username
  22. # password - password's hash value
  23. # Returns: Success result of the register try.
  24. #----------------------------------------------------------------------
  25. def try_register(username, password)
  26. # try to find user
  27. check = RMXOS.server.sql.query("SELECT COUNT(*) AS count FROM users WHERE username = '#{RMXOS.fix_string(username)}'")
  28. hash = check.fetch_hash
  29. # user already exists
  30. return RMXOS::RESULT_FAIL if hash['count'].to_i > 0
  31. # get user count
  32. check = RMXOS.server.sql.query("SELECT COUNT(*) AS count FROM users")
  33. hash = check.fetch_hash
  34. RMXOS.server.sql.query("START TRANSACTION")
  35. # if this is the first user
  36. if hash['count'].to_i == 0
  37. # register new user as admin
  38. RMXOS.server.sql.query("INSERT INTO users (username, password, usergroup) VALUES ('#{RMXOS.fix_string(username)}', '#{password}', #{RMXOS::GROUP_ADMIN})")
  39. else
  40. # register new user
  41. RMXOS.server.sql.query("INSERT INTO users (username, password, usergroup) VALUES ('#{RMXOS.fix_string(username)}', '#{password}', #{RMXOS::GROUP_PLAYER})")
  42. end
  43. check = RMXOS.server.sql.query("SELECT user_id FROM users WHERE username = '#{RMXOS.fix_string(username)}'")
  44. hash = check.fetch_hash
  45. RMXOS.server.sql.query("INSERT INTO user_data (user_id, lastlogin) VALUES (#{hash['user_id']}, '#{RMXOS.get_sqltime(Time.now.getutc)}')")
  46. RMXOS.server.sql.query("COMMIT")
  47. return RMXOS::RESULT_SUCCESS
  48. end
  49. #----------------------------------------------------------------------
  50. # Tries to log in this client.
  51. # username - username
  52. # password - password's hash value
  53. # Returns: Success result.
  54. #----------------------------------------------------------------------
  55. def try_login(username, password)
  56. # find this user
  57. check = RMXOS.server.sql.query("SELECT user_id, usergroup, banned FROM users WHERE username = " +
  58. "'#{RMXOS.fix_string(username)}' AND password = '#{password}'")
  59. # either username or password is incorrect
  60. return RMXOS::RESULT_FAIL if check.num_rows == 0
  61. hash = check.fetch_hash
  62. # this user is banned
  63. return RMXOS::RESULT_DENIED if hash['banned'] != '0'
  64. user_id = hash['user_id'].to_i
  65. # this user is already logged in
  66. return RMXOS::RESULT_ALTFAIL if $clients.any? {|client| client.player.user_id == user_id}
  67. # get user main data
  68. @client.player.user_id = user_id
  69. @client.player.username = username
  70. @client.player.usergroup = hash['usergroup'].to_i
  71. # log last login time
  72. RMXOS.server.sql.query("UPDATE user_data SET lastlogin = '#{RMXOS.get_sqltime(Time.now.getutc)}' WHERE user_id = #{@client.player.user_id}")
  73. # find all buddies
  74. self.setup_buddies
  75. # get other user data
  76. check = RMXOS.server.sql.query("SELECT guild_id FROM user_data WHERE user_id = #{user_id}")
  77. hash = check.fetch_hash
  78. # set all guild related data if player is in a guild
  79. self.setup_guild_data(hash['guild_id'].to_i) if hash['guild_id'] != nil
  80. # notify if new PMs in the inbox
  81. check = RMXOS.server.sql.query("SELECT COUNT(*) AS count FROM inbox WHERE recipient_id = #{@client.player.user_id} AND unread = 1")
  82. hash = check.fetch_hash
  83. @client.send("CHT#{RMXOS::Data::ColorInfo}\t0\t#{RMXOS::Data::NewPMs}") if hash['count'].to_i > 0
  84. # notify if inbox is full
  85. check = RMXOS.server.sql.query("SELECT COUNT(*) AS count FROM inbox WHERE recipient_id = #{@client.player.user_id}")
  86. hash = check.fetch_hash
  87. if hash['count'].to_i >= INBOX_SIZE
  88. @client.send("CHT#{RMXOS::Data::ColorInfo}\t0\t#{RMXOS::Data::InboxFull}")
  89. end
  90. return RMXOS::RESULT_SUCCESS
  91. end
  92. #----------------------------------------------------------------------
  93. # Changes the user password.
  94. # oldpass - old encrypted password
  95. # newpass - new encrypted password
  96. # Returns: Action result of this action.
  97. #----------------------------------------------------------------------
  98. def try_password_change(oldpass, newpass)
  99. check = RMXOS.server.sql.query("SELECT password FROM users WHERE user_id = #{@client.player.user_id}")
  100. hash = check.fetch_hash
  101. # password check
  102. return RMXOS::RESULT_FAIL if oldpass != hash['password']
  103. # prepare yes/no action
  104. @current.type = Action::TYPE_PASSWORD_CHANGE
  105. @current.data = [@client.player.user_id, newpass, @client.player.username]
  106. return RMXOS::RESULT_SUCCESS
  107. end
  108. #----------------------------------------------------------------------
  109. # Executes YES on the last action.
  110. #----------------------------------------------------------------------
  111. def execute_yes
  112. # check action type
  113. case @current.type
  114. when Action::TYPE_PASSWORD_CHANGE then self.execute_password_change
  115. when Action::TYPE_GUILD_PASSWORD_CHANGE then self.execute_guild_password_change
  116. when Action::TYPE_GUILD_DISBAND then self.execute_guild_disband
  117. when Action::TYPE_GUILD_TRANSFER then self.execute_guild_transfer
  118. when Action::TYPE_GUILD_JOIN then self.execute_guild_join
  119. when Action::TYPE_GUILD_LEAVE then self.execute_guild_leave
  120. when Action::TYPE_TRADE_REQUEST then self.execute_trade_request
  121. when Action::TYPE_BUDDY_ADD then self.execute_buddy_add
  122. when Action::TYPE_BUDDY_REMOVE then self.execute_buddy_remove
  123. when Action::TYPE_PM_DELETE then self.execute_pm_delete
  124. when Action::TYPE_PM_DELETE_ALL then self.execute_pm_delete_all
  125. else
  126. # no request message
  127. @client.send("CHT#{RMXOS::Data::ColorInfo}\t0\t#{RMXOS::Data::NoRequest}")
  128. end
  129. # clear current
  130. @current.clear
  131. end
  132. #----------------------------------------------------------------------
  133. # Executes NO on the last action.
  134. #----------------------------------------------------------------------
  135. def execute_no
  136. name = nil
  137. # check action type
  138. case @current.type
  139. when Action::TYPE_PASSWORD_CHANGE, Action::TYPE_GUILD_PASSWORD_CHANGE
  140. # player confirmation message
  141. message1 = RMXOS::Data::PasswordNoChange
  142. when Action::TYPE_GUILD_DISBAND
  143. # player confirmation message
  144. message1 = RMXOS::Data::GuildNoDisband.sub('GUILD') {@client.player.guildname}
  145. when Action::TYPE_GUILD_TRANSFER
  146. # player confirmation message
  147. message1 = RMXOS::Data::GuildNoTransfer
  148. # answer to requester
  149. message2 = RMXOS::Data::GuildNoTransferPlayer.sub('PLAYER') {@client.player.username}
  150. name = @client.player.guildleader
  151. when Action::TYPE_GUILD_JOIN
  152. # player confirmation message
  153. message1 = RMXOS::Data::GuildNoJoin
  154. # answer to requester
  155. message2 = RMXOS::Data::GuildNoJoinPlayer.sub('PLAYER') {@client.player.username}
  156. name = @current.data[1]
  157. when Action::TYPE_GUILD_LEAVE
  158. if @current.data[1] == @client.player.username
  159. # player confirmation message
  160. message1 = RMXOS::Data::GuildNoLeave.sub('GUILD') {@client.player.guildname}
  161. else
  162. # confirmation message for no removal from guild
  163. message1 = RMXOS::Data::GuildNoRemove.sub('PLAYER') {@current.data[1]}
  164. end
  165. when Action::TYPE_TRADE_REQUEST
  166. # player confirmation message
  167. message1 = RMXOS::Data::TradeNoRequest
  168. # answer to requester
  169. message2 = RMXOS::Data::TradeNoRequestPlayer.sub('PLAYER') {@client.player.username}
  170. name = @current.data[0]
  171. when Action::TYPE_BUDDY_ADD
  172. # player confirmation message
  173. message1 = RMXOS::Data::BuddyNoAdd
  174. # answer to requester
  175. message2 = RMXOS::Data::BuddyNoAddPlayer.sub('PLAYER') {@client.player.username}
  176. name = @current.data[0]
  177. when Action::TYPE_BUDDY_REMOVE
  178. # player confirmation message
  179. message1 = RMXOS::Data::BuddyNoRemovePlayer.sub('PLAYER') {@current.data[0]}
  180. when Action::TYPE_PM_DELETE, Action::TYPE_PM_DELETE_ALL
  181. # player confirmation message
  182. message1 = RMXOS::Data::PMNoDeletion
  183. else
  184. # no request message
  185. message1 = RMXOS::Data::NoRequest
  186. end
  187. # send player confirmation message
  188. @client.send("CHT#{RMXOS::Data::ColorInfo}\t0\t#{message1}")
  189. # send answer to requester if requester exists
  190. @client.sender.send_to_name(name, "CHT#{RMXOS::Data::ColorInfo}\t0\t#{message2}") if name != nil
  191. # clear action
  192. @current.clear
  193. end
  194. #----------------------------------------------------------------------
  195. # Executes a password change.
  196. #----------------------------------------------------------------------
  197. def execute_password_change
  198. # change password
  199. user_id, newpass, username = @current.data
  200. RMXOS.server.sql.query("UPDATE users SET password = '#{newpass}' WHERE user_id = #{user_id}")
  201. @client.send("CHT#{RMXOS::Data::ColorOK}\t0\t#{RMXOS::Data::PasswordChanged}")
  202. # log this action if action log is turned on
  203. if RMXOS.server.options.log_actions
  204. RMXOS.server.log(@client.player, 'Action', "user password change: #{user_id} (#{username})")
  205. end
  206. end
  207. #----------------------------------------------------------------------
  208. # Broadcasts disconnection of this client by kicking.
  209. #----------------------------------------------------------------------
  210. def execute_kick
  211. @client.send('KCK')
  212. @client.terminate
  213. $clients.delete(@client)
  214. end
  215. #----------------------------------------------------------------------
  216. # Sets up buddies.
  217. #----------------------------------------------------------------------
  218. def setup_buddies
  219. @client.player.buddies = []
  220. # get all buddy IDs
  221. check = RMXOS.server.sql.query("SELECT * FROM buddy_list WHERE user1_id = #{@client.player.user_id} OR user2_id = #{@client.player.user_id}")
  222. check.num_rows.times {
  223. hash = check.fetch_hash
  224. if hash['user1_id'].to_i == @client.player.user_id
  225. @client.player.buddies.push(hash['user2_id'].to_i)
  226. else
  227. @client.player.buddies.push(hash['user1_id'].to_i)
  228. end
  229. }
  230. # convert buddy IDs
  231. @client.player.buddies.each_index {|i|
  232. check = RMXOS.server.sql.query("SELECT username FROM users WHERE user_id = #{@client.player.buddies[i]}")
  233. hash = check.fetch_hash
  234. @client.player.buddies[i] = hash['username']
  235. }
  236. end
  237. #----------------------------------------------------------------------
  238. # Sets up all guild related data.
  239. # guild_id - ID of the guild
  240. #----------------------------------------------------------------------
  241. def setup_guild_data(guild_id)
  242. # get guild data
  243. check = RMXOS.server.sql.query("SELECT guildname, leader_id FROM guilds WHERE guild_id = #{guild_id}")
  244. hash = check.fetch_hash
  245. @client.player.guildname = hash['guildname']
  246. # get guild leader name
  247. if hash['leader_id'].to_i != @client.player.user_id
  248. check = RMXOS.server.sql.query("SELECT username FROM users WHERE user_id = #{hash['leader_id']}")
  249. hash = check.fetch_hash
  250. @client.player.guildleader = hash['username']
  251. else
  252. # this player is the guild leader
  253. @client.player.guildleader = @client.player.username
  254. end
  255. # get guild member count
  256. check = RMXOS.server.sql.query("SELECT username FROM users JOIN user_data ON users.user_id = " +
  257. "user_data.user_id WHERE guild_id = #{guild_id}")
  258. @client.player.guildmembers = []
  259. check.num_rows.times {
  260. hash = check.fetch_hash
  261. @client.player.guildmembers.push(hash['username'])
  262. }
  263. end
  264. #----------------------------------------------------------------------
  265. # Checks if this client has trades enabled.
  266. # user_id - user ID of the other client
  267. # Returns: True or False.
  268. #----------------------------------------------------------------------
  269. def can_trade?(user_id)
  270. check = RMXOS.server.sql.query("SELECT notrade FROM user_data WHERE user_id = #{user_id}")
  271. # user does not exist, can't trade
  272. return false if check.num_rows == 0
  273. hash = check.fetch_hash
  274. return (hash['notrade'] == '0')
  275. end
  276.  
  277. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement