summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/alert.rb2
-rw-r--r--app/models/bracket.rb7
-rw-r--r--app/models/game.rb6
-rw-r--r--app/models/game_setting.rb13
-rw-r--r--app/models/match.rb93
-rw-r--r--app/models/pm.rb14
-rw-r--r--app/models/remote_username.rb14
-rw-r--r--app/models/server.rb36
-rw-r--r--app/models/session.rb39
-rw-r--r--app/models/team.rb4
-rw-r--r--app/models/tournament.rb130
-rw-r--r--app/models/tournament_setting.rb4
-rw-r--r--app/models/tournament_stage.rb42
-rw-r--r--app/models/user.rb204
14 files changed, 602 insertions, 6 deletions
diff --git a/app/models/alert.rb b/app/models/alert.rb
index 0516355..9876711 100644
--- a/app/models/alert.rb
+++ b/app/models/alert.rb
@@ -1,3 +1,3 @@
class Alert < ActiveRecord::Base
- belongs_to :author
+ belongs_to :author, class_name: "User"
end
diff --git a/app/models/bracket.rb b/app/models/bracket.rb
index e8d9c5a..7e22f0d 100644
--- a/app/models/bracket.rb
+++ b/app/models/bracket.rb
@@ -1,4 +1,11 @@
class Bracket < ActiveRecord::Base
belongs_to :user
belongs_to :tournament
+ has_many :bracket_matches
+
+ def create_matches
+ tournament.stages.first.matches_ordered.each do |m|
+ bracket_matches.create(match: m)
+ end
+ end
end
diff --git a/app/models/game.rb b/app/models/game.rb
index 13520ac..c5cb32a 100644
--- a/app/models/game.rb
+++ b/app/models/game.rb
@@ -1,3 +1,7 @@
class Game < ActiveRecord::Base
- belongs_to :parent
+ belongs_to :parent, class_name: "Game"
+ has_many :children, class_name: "Game"
+ has_many :game_settings
+
+ alias_attribute :settings, :game_settings
end
diff --git a/app/models/game_setting.rb b/app/models/game_setting.rb
index bff8d97..e701cae 100644
--- a/app/models/game_setting.rb
+++ b/app/models/game_setting.rb
@@ -1,3 +1,16 @@
class GameSetting < ActiveRecord::Base
belongs_to :game
+
+ alias_attribute :value, :default
+
+ def self.types
+ return {
+ :text_short => 0,
+ :text_long => 1,
+ :pick_one_radio => 2,
+ :pick_several => 3,
+ :true_false => 4,
+ :pick_one_dropdown => 5,
+ }
+ end
end
diff --git a/app/models/match.rb b/app/models/match.rb
index b5f539b..3cbe0da 100644
--- a/app/models/match.rb
+++ b/app/models/match.rb
@@ -1,4 +1,95 @@
class Match < ActiveRecord::Base
belongs_to :tournament_stage
- belongs_to :winner
+ has_many :statistics
+ has_and_belongs_to_many :teams
+
+ belongs_to :winner, class_name: "Team"
+
+ def setup()
+ end
+
+ def finished?
+ ok = true
+ tournament_stage.scoring_method.stats_needed.each do |stat|
+ ok &= statistics.where(match: self, name: stat).nil?
+ end
+ ok
+ end
+
+ def win?(player)
+ winner.players.include? player
+ end
+
+ def users
+ ret = []
+ self.teams.each{|t| ret.concat(t.users)}
+ return ret
+ end
+
+ def stats_from(sampling_class)
+ figure_sampling_methods.map{|stat,klass| (sampling_class==klass) ? stat : nil}.select{|s| not s.nil?}
+ end
+
+ def handle_sampling(user, params)
+ method_classes.each do |klass|
+ klass.new(self).handle_user_interaction(user, params)
+ end
+ end
+
+ def render_sampling(user)
+ require 'set'
+ html = ''
+
+ method_classes.each do |klass|
+ html += '<div>'
+ html += klass.new(self).render_user_interaction(user)
+ html += '</div>'
+ end
+
+ return html.html_safe
+ end
+
+ private
+ def figure_sampling_methods
+ if @sampling_methods.nil?
+ data = {}
+ needed = self.tournament_stage.scoring.stats_needed
+ methods_names = self.tournament_stage.tournament.sampling_methods
+ methods_names.each do |method_name|
+ method_class = "Sampling::#{sampling_name.camelcase}".constantize
+ if method_class.works_with(self.tournament_stage.tournament.game)
+ needed.each do |stat|
+ data[stat] ||= {}
+ data[stat][method] = method.can_get?(user, stat)
+ end
+ end
+ end
+
+ needed.each do |stat|
+ max_val = nil
+ max_pri = 0
+ data[stat].each do |method,priority|
+ if priority > max_pri
+ max_val = method
+ max_pri = priority
+ end
+ end
+ data[stat] = max_val
+ end
+ @sampling_methods = data
+ end
+ return @sampling_methods
+ end
+
+ def method_classes
+ if @method_classes.nil?
+ data = Set.new
+ figure_sampling_methods.each do |stat,method|
+ data.push(method)
+ end
+ @method_classes = data
+ end
+ return @method_classes
+ end
+
end
diff --git a/app/models/pm.rb b/app/models/pm.rb
index 0e60f3e..8b06181 100644
--- a/app/models/pm.rb
+++ b/app/models/pm.rb
@@ -1,5 +1,15 @@
class Pm < ActiveRecord::Base
- belongs_to :author
- belongs_to :recipient
+ belongs_to :author, class_name: "User"
+ belongs_to :recipient, class_name: "User"
belongs_to :conversation
+
+ def name
+ return current_user.name
+ end
+
+=begin
+ def mailboxer_email(email)
+ return current_user.email
+ end
+=end
end
diff --git a/app/models/remote_username.rb b/app/models/remote_username.rb
index c477f8a..8c1ce26 100644
--- a/app/models/remote_username.rb
+++ b/app/models/remote_username.rb
@@ -1,4 +1,16 @@
class RemoteUsername < ActiveRecord::Base
belongs_to :game
belongs_to :user
-end
+
+ def value
+ begin
+ return JSON.parse(self.json_value)
+ rescue
+ return {}
+ end
+ end
+
+ def value=(v)
+ self.json_value = v.to_json
+ end
+end
diff --git a/app/models/server.rb b/app/models/server.rb
index 120f0fa..5ba7524 100644
--- a/app/models/server.rb
+++ b/app/models/server.rb
@@ -1,2 +1,38 @@
class Server < ActiveRecord::Base
+ def default_user_abilities
+ @abilities ||= User::Abilities.new(DefaultUser.new(self))
+ end
+ def default_user_abilities=(new)
+ new.each do |k,v|
+ if v == "0"
+ v = false
+ end
+ default_user_abilities[k] = v
+ end
+ end
+ class DefaultUser
+ def initialize(server)
+ @server = server
+ end
+ def can?(action)
+ bit = User.permission_bits[action]
+ if bit.nil?
+ return false
+ else
+ return (@server.default_user_permissions & bit != 0)
+ end
+ end
+ def add_ability(action)
+ bit = User.permission_bits[action.to_sym]
+ unless bit.nil?
+ @server.default_user_permissions |= bit
+ end
+ end
+ def remove_ability(action)
+ bit = User.permission_bits[action.to_sym]
+ unless bit.nil?
+ @server.default_user_permissions &= ~ bit
+ end
+ end
+ end
end
diff --git a/app/models/session.rb b/app/models/session.rb
index a5fd26e..f5e642b 100644
--- a/app/models/session.rb
+++ b/app/models/session.rb
@@ -1,3 +1,42 @@
class Session < ActiveRecord::Base
belongs_to :user
+
+ ##
+ # Create a random remember token for the user. This will be
+ # changed every time the user creates a new session.
+ #
+ # If you want this value, hang on to it; the raw value is
+ # discarded afterward.
+ #
+ # By changing the cookie every new session, any hijacked sessions
+ # (where the attacker steals a cookie to sign in as a certain
+ # user) will expire the next time the user signs back in.
+ #
+ # The random string is of length 16 composed of A-Z, a-z, 0-9
+ # This is the browser's cookie value.
+ def create_token()
+ t = SecureRandom.urlsafe_base64
+ self.token = Session.hash_token(t)
+ t
+ end
+
+ ##
+ # Encrypt the remember token.
+ # This is the encrypted version of the cookie stored on
+ # the database.
+ #
+ # The reasoning for storing a hashed token is so that even if
+ # the database is compromised, the attacker won't be able to use
+ # the remember tokens to sign in.
+ def Session.hash_token(token)
+ # SHA-1 (Secure Hash Algorithm) is a US engineered hash
+ # function that produces a 20 byte hash value which typically
+ # forms a hexadecimal number 40 digits long.
+ # The reason I am not using the Bcrypt algorithm is because
+ # SHA-1 is much faster and I will be calling this on
+ # every page a user accesses.
+ #
+ # https://en.wikipedia.org/wiki/SHA-1
+ Digest::SHA1.hexdigest(token.to_s)
+ end
end
diff --git a/app/models/team.rb b/app/models/team.rb
index fa7ba9e..828d168 100644
--- a/app/models/team.rb
+++ b/app/models/team.rb
@@ -1,2 +1,6 @@
class Team < ActiveRecord::Base
+ has_and_belongs_to_many :matches
+ has_and_belongs_to_many :users
+
+ alias_attribute :players, :users
end
diff --git a/app/models/tournament.rb b/app/models/tournament.rb
index dcdb8d5..a569213 100644
--- a/app/models/tournament.rb
+++ b/app/models/tournament.rb
@@ -1,3 +1,133 @@
class Tournament < ActiveRecord::Base
belongs_to :game
+ has_many :tournament_stages
+ has_many :brackets
+ has_many :tournament_settings
+ has_and_belongs_to_many :players, class_name: "User", association_foreign_key: "player_id", join_table: "players_tournaments"
+ has_and_belongs_to_many :hosts, class_name: "User", association_foreign_key: "host_id", join_table: "hosts_tournaments"
+
+ alias_attribute :stages, :tournament_stages
+
+ # Settings #################################################################
+
+ def settings
+ @settings ||= Settings.new(self)
+ end
+
+ def settings=(setting)
+ setting.each do |key, value|
+ value = false if value == "0"
+ settings[key] = value
+ end
+ end
+
+ class Settings
+ def initialize(tournament)
+ @tournament = tournament
+ end
+
+ def [](setting_name)
+ tournament_setting = @tournament.tournament_settings.find{|s|s.name==setting_name}
+ if tournament_setting.nil?
+ return nil
+ else
+ return tournament_setting.value
+ end
+ end
+
+ def []=(setting_name, val)
+ tournament_setting = @tournament.tournament_settings.find{|s|s.name==setting_name}
+ if tournament_setting.nil?
+ game_setting = @tournament.game.settings.find_by_name(setting_name)
+ @tournament.tournament_settings.build(name: setting_name, value: val,
+ vartype: game_setting.vartype,
+ type_opt: game_setting.type_opt,
+ description: game_setting.description,
+ display_order: game_setting.display_order)
+ else
+ tournament_setting.value = val
+ end
+ end
+
+ def keys
+ @tournament.tournament_settings.all.collect { |x| x.name }
+ end
+
+ def empty?() keys.empty? end
+ def count() keys.count end
+ def length() count end
+ def size() count end
+
+ def method_missing(name, *args)
+ if name.to_s.ends_with?('=')
+ self[name.to_s.sub(/=$/, '').to_s] = args.first
+ else
+ return self[name.to_s]
+ end
+ end
+ end
+
+ # Misc. ####################################################################
+
+ def open?
+ return true
+ end
+
+ # Joining/Leaving ##########################################################
+
+ def joinable_by?(user)
+ return (open? and user.can?(:join_tournament) and !players.include?(user))
+ end
+
+ def join(user)
+ unless joinable_by?(user)
+ return false
+ end
+ players.push(user)
+ end
+
+ def leave(user)
+ if players.include?(user) && status == 0
+ players.delete(user)
+ end
+ end
+
+ # Configured methods #######################################################
+
+ def scoring
+ @scoring ||= "Scoring::#{self.scoring_method.camelcase}".constantize
+ end
+
+ def sampling
+ @sampling ||= "Sampling::#{self.sampling_method.camelcase}".constantize
+ end
+
+ # YISSSSSS
+
+ def scoring_methods
+ make_methods "scoring"
+ end
+
+ def sampling_methods
+ make_methods("sampling").select do |name|
+ "Sampling::#{name.camelcase}".constantize.works_with?(self.game)
+ end
+ end
+
+ def scheduling_methods
+ make_methods "scheduling"
+ end
+
+ def seeding_methods
+ make_methods "seeding"
+ end
+
+ private
+ def make_methods(dir)
+ @@methods ||= {}
+ if @@methods[dir].nil? or Rails.env.development?
+ @@methods[dir] = Dir.glob("#{Rails.root}/lib/#{dir}/*.rb").map{|filename| File.basename(filename, ".rb") }
+ end
+ return @@methods[dir]
+ end
end
diff --git a/app/models/tournament_setting.rb b/app/models/tournament_setting.rb
index b3e6ace..9efaaea 100644
--- a/app/models/tournament_setting.rb
+++ b/app/models/tournament_setting.rb
@@ -1,3 +1,7 @@
class TournamentSetting < ActiveRecord::Base
belongs_to :tournament
+
+ def self.types
+ GameSetting.types
+ end
end
diff --git a/app/models/tournament_stage.rb b/app/models/tournament_stage.rb
index 205c8cc..caf5a8a 100644
--- a/app/models/tournament_stage.rb
+++ b/app/models/tournament_stage.rb
@@ -1,3 +1,45 @@
class TournamentStage < ActiveRecord::Base
belongs_to :tournament
+ has_many :matches
+
+ # A 1-indexed hash of matches
+ def matches_ordered
+ h = {}
+ i = 1
+ self.matches.order(:id).each do |m|
+ h[i] = m
+ i += 1
+ end
+ return h
+ end
+
+ def create_matches
+ scheduling.create_matches
+ end
+
+ def to_svg(highlight_user)
+ return scheduling.graph(highlight_user)
+ end
+
+ def seed
+ return seeding.seed.pair(matches, players)
+ end
+
+ # Accessors to the configured methods
+
+ def scoring
+ @scoring ||= tournament.scoring
+ end
+
+ def sampling
+ @sampling ||= tournament.sampling
+ end
+
+ def scheduling
+ @scheduling ||= "Scheduling::#{self.scheduling_method.camelcase}".constantize.new(self)
+ end
+
+ def seeding
+ @seeding ||= "Seeding::#{self.seeding_method.camelcase}".constantize
+ end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 4a57cf0..b2c7862 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,2 +1,206 @@
class User < ActiveRecord::Base
+ before_save :default_values
+
+ has_and_belongs_to_many :tournaments_played, class_name: "Tournament", foreign_key: "player_id", join_table: "players_tournaments"
+ has_and_belongs_to_many :tournaments_hosted, class_name: "Tournament", foreign_key: "host_id", join_table: "hosts_tournaments"
+ has_and_belongs_to_many :teams
+ has_many :sessions
+ has_many :statistics
+ has_many :remote_usernames
+
+ apply_simple_captcha
+
+ acts_as_messageable
+
+ def mailboxer_email(object)
+ return nil
+ end
+
+ before_save { self.email = email.downcase }
+ before_save { self.user_name = user_name }
+
+ def default_values
+ self.permissions ||= Server.first.default_user_permissions
+ end
+
+ def set_remote_username(game, data)
+ remote = self.remote_usernames.where(:game => game).first
+ if remote.nil?
+ self.remote_usernames.create(game: game, value: data)
+ else
+ remote.value = data
+ remote.save
+ end
+ end
+
+ def get_remote_username(game)
+ obj = self.remote_usernames.where(:game => game).first
+ if obj.nil?
+ if game.parent.nil?
+ return nil
+ else
+ return get_remote_username(game.parent)
+ end
+ else
+ return obj.value
+ end
+ end
+
+ def self.permission_bits
+ return {
+ :create_tournament => (2**1),
+ :edit_tournament => (2**2),
+ :join_tournament => (2**3),
+ :delete_tournament => (2**4),
+
+ :create_game => (2**5),
+ :edit_game => (2**6),
+ :delete_game => (2**7),
+
+ :create_user => (2**8),
+ :edit_user => (2**9),
+ :delete_user => (2**10),
+
+ :create_alert => (2**11),
+ :edit_alert => (2**12),
+ :delete_alert => (2**13),
+
+ :create_pm => (2**14),
+ :edit_pm => (2**15),
+ :delete_pm => (2**16),
+
+ :create_session => (2**17),
+ :delete_session => (2**18),
+
+ :edit_permissions => (2**19),
+ :edit_server => (2**20),
+
+ :create_bracket => (2**21),
+ :edit_bracket => (2**22),
+ :delete_bracket => (2**23)
+ }
+ end
+
+ def can?(action)
+ bit = User.permission_bits[action]
+ if bit.nil?
+ return false
+ else
+ return (self.permissions & bit != 0)
+ end
+ end
+
+ def add_ability(action)
+ bit = User.permission_bits[action.to_sym]
+ unless bit.nil?
+ self.permissions |= bit
+ end
+ end
+
+ def remove_ability(action)
+ bit = User.permission_bits[action.to_sym]
+ unless bit.nil?
+ self.permissions &= ~ bit
+ end
+ end
+
+
+ # A representation of the permission bits as a mock-array.
+ def abilities
+ @abilities ||= Abilities.new(self)
+ end
+ def abilities=(new)
+ new.each do |k,v|
+ if v == "0"
+ v = false
+ end
+ abilities[k] = v
+ end
+ end
+
+ # A thin array-like wrapper around the permission bits to make it
+ # easy to modify them using a form.
+ class Abilities
+ def initialize(user)
+ @user = user
+ end
+ def [](ability)
+ return @user.can?(ability)
+ end
+ def []=(ability, val)
+ if val
+ @user.add_ability(ability)
+ else
+ @user.remove_ability(ability)
+ end
+ end
+ def keys
+ User.permission_bits.keys
+ end
+ def method_missing(name, *args)
+ if name.to_s.ends_with?('=')
+ self[name.to_s.sub(/=$/, '').to_sym] = args.first
+ else
+ return self[name.to_sym]
+ end
+ end
+ end
+
+ # VAILD_EMAIL is the regex used to validate a user given email.
+ VALID_EMAIL_REG = /\A\S+@\S+\.\S+\z/i
+
+ # VALID_USER_NAME checks to make sure a user's user_name
+ # is in the proper format.
+ VALID_USER_NAME_REG = /\A[a-zA-Z0-9 _\-]+\z/
+
+ # The following lines put a user account through a series of
+ # validations in order to make sure all of their information
+ # is in the proper format.
+ #
+ # validates :symbol_to_be_validated
+ #
+ # - presence: determines whether or not a symbol is filled or not
+ # - length: ensures there is a length limit on the symbol
+ # - format: checks the format of given information to ensure
+ # validity
+ validates(:name, presence: true, length: { maximum: 50 })
+ validates(:email, presence: true, format: {with:
+ VALID_EMAIL_REG},
+ uniqueness: { case_sensitive: false })
+ validates(:user_name, presence: true, length:{maximum: 50},
+ format: {with: VALID_USER_NAME_REG },
+ uniqueness: {case_sensitive: false })
+
+ # Instead of adding password and password_confirmation
+ # attributes, requiring the presence of a password,
+ # requiring that pw and pw_com match, and add an authenticate
+ # method to compare an encrypted password to the
+ # password_digest to authenticate users, I can just add
+ # has_secure_password which does all of this for me.
+ has_secure_password
+
+ validates :password, length: { minimum: 6 }
+
+ class NilUser
+ def nil?
+ return true
+ end
+ def can?(action)
+ case action
+ when :create_user
+ return true
+ when :create_session
+ return true
+ else
+ return false
+ end
+ end
+ def method_missing(name, *args)
+ # Throw an error if User doesn't have this method
+ super unless User.new.respond_to?(name)
+ # User has this method -- return a blank value
+ # 'false' if the method ends with '?'; 'nil' otherwise.
+ name.to_s.ends_with?('?') ? false : nil
+ end
+ end
end