Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dirs = [ "./locostats/lib", "./cinch/lib" ]
- dirs.each { |dir| $LOAD_PATH.unshift(dir) if File.directory?(dir) }
- require 'cinch'
- require 'digest/md5'
- require 'active_record'
- require 'locostats/activerecord/default'
- require 'locostats/activerecord/irc'
- require 'config.rb'
- class PsychoStatsPlugin
- include PsychoStats::GameStats
- include PsychoStats::Config
- include PsychoStats::Site
- include Cinch::Plugin
- def is_admin(user, check_register = true)
- return true if $DEBUG
- profile = Profile.find_by_irc(user.nick)
- return false if profile.nil?
- return false unless profile.user.accesslevel == 99
- # an sql check is faster then an is_registered
- return is_registered(user) if check_register
- true
- end
- def is_registered(user)
- return true if $DEBUG
- user.refresh
- user.authed?
- end
- match /rank (.+)/, method: :execute_id
- match /rank$/, method: :execute
- def rank(id)
- plr = Player.search(id)
- kd = sprintf "%.2f", plr.kills.to_f/plr.deaths
- "[#{plr.profile.cc}] #{id} rank is #{plr.rank} of #{Player.total_ranked} with #{plr.skill.to_i} points, kills: #{plr.kills} deaths: #{plr.deaths} KD: #{kd}"
- rescue
- "No player found with the id #{id}"
- end
- def execute_id(m, id)
- m.reply(rank(id))
- end
- def execute(m)
- m.reply(rank(m.user.nick))
- end
- match /status$/, method: :execute_status
- def status
- Server.where(:enabled => true).collect { |s| "#{s.host}:#{s.port.to_s}" }.join (", ")
- end
- def execute_status(m)
- m.reply(status)
- end
- match /server add (.+):(\d+)\s+(.+)/, method: :execute_server_add
- match /server add (.+)\s+(\d+)\s+(.+)/, method: :execute_server_add
- def server_add(host, port, password = nil)
- s = Server.where(:host => host, :port => port)
- if s.size > 0
- s = s.first
- return "Server already exists" if s.rcon == password
- s.rcon = password
- s.save!
- "Password changed"
- else
- server = Server.new
- server.host = host
- server.port = port
- server.rcon = password if !password.nil?
- server.querytype = "halflife"
- server.enabled = true
- server.idx = 10
- max = Server.maximum(:id)
- if max.nil?
- server.id = 1
- else
- server.id = max + 1
- end
- server.save!
- "Server successfully created"
- end
- rescue Exception => err
- "Couldn't add the server: #{err}"
- end
- def execute_server_add(m, host, port, password)
- return unless is_admin(m.user)
- m.reply(server_add(host, port, password))
- end
- match /server add (.+):(\d+)/, method: :execute_server_add_spec
- match /server add (.+)\s+(\d+)/, method: :execute_server_add_spec
- def execute_server_add_spec(m, host, port)
- return unless is_admin(m.user)
- m.reply(server_add(host ,port))
- end
- match /server del (\d+)/, method: :execute_server_del
- def server_del(num)
- return "Server successfully deleted" if Server.delete(num) == 1
- "No server with this index"
- rescue Exception => err
- m.reply "An error has occured: #{err}"
- end
- def execute_server_del(m, num)
- return unless is_admin(m.user)
- m.reply(server_del(num))
- end
- match /server del (.+):(\d)/, method: :execute_server_del_spec
- match /server del (.+)\s+(\d)/, method: :execute_server_del_spec
- def server_del_spec(server, port)
- # TODO: find doesn't work correctly, fix it
- s = Server.find(:first, :conditions => [ "host = ? AND port = ?", server, port.to_i ])
- Server.del(s.primary_key)
- rescue Exception => err
- "No such server present"
- end
- def execute_server_del_spec(m, server, port)
- return unless is_admin(m.user)
- m.reply(server_del_spec(server, port))
- end
- match /server list/, method: :server_list
- def server_list(m)
- authed = is_admin(m.user)
- servers = Server.find(:all)
- m.reply "Total #{servers.size} servers:"
- servers.each do |server|
- if !m.channel? and authed
- m.reply "##{server.id} #{server.host} #{server.port} #{server.rcon}"
- else
- m.reply "##{server.id} #{server.host} #{server.port}"
- end
- end
- end
- match /user available (.+)/, method: :available
- def available(m, player)
- return unless is_registered(m.user)
- player = Player.search(player)
- if !player.nil? and !player.profile.irc.nil?
- m.reply "This profile has already been taken"
- else
- m.reply "This profile is still available"
- end
- end
- match /user register \"(.+)\" \"(.+)\"/, method: :execute_register
- def execute_register(m, player, password)
- return unless is_registered(m.user)
- m.reply(register(m.user.nick, player, password))
- end
- def register(nick, playername, password)
- already = Profile.find_by_irc(nick)
- return "You have already a registered profile" if !already.nil?
- player = Player.search(playername)
- return "There is no such profile" if player.nil?
- return "This profile is already owned by you" if !player.user.nil? and player.profile.irc == nick
- return "This profile is already owned by #{player.profile.irc}" if !player.user.nil?
- user = User.create_irc(nick, password, player.profile)
- p user.profile
- "Error during registration process" if user.nil?
- "Successfully registered the username #{nick} with profile #{player.names.first.name}"
- rescue Exception => err
- "Error during registration process #{err}"
- end
- match /user drop$/, method: :drop
- def drop(m)
- return unless is_registered(m.user)
- profile = Profile.find_by_irc(m.user.nick)
- User.delete(profile.userid)
- profile.irc = nil
- profile.save!
- m.reply "Sucessfully dropped username"
- rescue
- m.reply "You are not registered"
- end
- match /check$/, method: :check
- def check(m)
- authed = is_registered(m.user)
- admin = is_admin(m.user, false)
- reply = []
- reply.push "authed" if authed
- reply.push "an admin" if admin
- if reply.size > 0
- m.reply "You are " + reply.join(", ")
- else
- m.reply "A normal user"
- end
- end
- end
- bot = Cinch::Bot.new do
- configure do |c|
- bot_config(c)
- c.plugins.plugins = [PsychoStatsPlugin]
- end
- end
- bot.start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement