<commit_before>angular.module("MagicStick.controllers").controller "SeasonManageController", [
  "$scope"
  "User"
  "$http"
  "season"
  "toastr"
  ($scope, User, $http, season, toastr) ->
    getReason = (res) ->
      ": " + unless res?
        "Try again later."
      else if res.error?
        res.error
      else if res.errors?
        res.errors
      else
        "An error occurred (#{res})."
    $scope.season = season
    $scope.comment =
      comment: ""
      savedComment: ""
    $scope.bestOfOptions = [
      {id: 1, name: "one"}
      {id: 3, name: "three"}
      {id: 5, name: "five"}
      {id: 7, name: "seven"}
      {id: 11, name: "eleven"}
    ]
    $scope.isCurrentUser = (user) -> user?.username is User.username
    $scope.userLabel = (user) ->
      user.username + if user.name? then " (#{user.name})" else ""
    $scope.seasonOwner = -> $scope.isCurrentUser(season?.owner)
    $scope.seasonParticipant = ->
      _.find season?.members, {'username': User.username}
    $scope.matchStatusAlias = (status) ->
      if status is false
        "Loss"
      else if status is true
        "Win"
      else if not status?
        "Not Played"
      else
        "??"
    $scope.matchIsComplete = (match) ->
      match.user_season_match?.length > 0 and \
      _.any(match.user_season_match, ((userMatch) -> userMatch?.won?))
    $scope.updateJoinType = ->
      return unless season?
      $scope.updatingSeason = yes
      to = ""
      if season.invite_only
        season.allow_auto_join = no
        to = "invite only"
      else
        season.allow_auto_join = yes
        to = "anyone can join"
      $http.put("/api/match/seasons/#{season.id}/join-mode", {
        season:
          invite_only: season.invite_only
          allow_auto_join: season.allow_auto_join
      }).then (res) ->
          toastr.success "Updated join mode to '#{to}'"
        .catch (err) ->
          toastr.error "Couldn't update join mode to '#{to}': #{getReason err}"
        .finally ->
          $scope.updatingSeason = no
    $scope.createSeasonGrouping = (groupingName) ->
      $http.post("/api/match/seasons/#{season.id}/match-groups", {
        name: groupingName
      }).success (newGroup) ->
          toastr.success "Grouping successfully created"
          season.season_match_groups.push newGroup
          $scope.newGroupingNameError = null
          $scope.newGroupingName = null
        .error (data) ->
          $scope.newGroupingNameError =
            data?.errors?.name ? \
            data?.errors["[:season_id, :name]"] ? \
            ["is invalid"]
    $scope.removeSeasonGrouping = (group) ->
      $http.delete("/api/match/seasons/#{season.id}/match-groups/#{group.id}")
        .success ->
          toastr.success "Successfully deleted match group"
          _.remove season.season_match_groups,
            (nextGroup) -> nextGroup.id is group.id
        .error (reason) ->
          toastr.error \
            "Failed to remove season match grouping #{getReason reason}"
    $scope.joinSeason = -> $scope.addSeasonMember User.clone()
    $scope.addSeasonMember = (newMember) ->
      if _.find season.members, {'username': newMember.username}
        $scope.newMember = null
        toastr.info "User already present in season"
        return
      isCurrentUser = $scope.isCurrentUser newMember
      $http.put("/api/match/seasons/#{season.id}/members/#{newMember.id}")
        .success ->
          if isCurrentUser
            toastr.success "Joined the season"
          else
            toastr.success "User added to season"
          season.members.push newMember
          $scope.newMember = null
        .error (reason) ->
          toastr.error \
            "Failed to add #{newMember.username} to season #{getReason reason}"
    $scope.findMembersMatching = (text) ->
      $http.get("/api/auth/users", {
        params:
          limitted: true
          matching: text
      }).then (response) -> response.data.users
    $scope.removeSeasonMember = (member) ->
      isCurrentUser = $scope.isCurrentUser member
      if isCurrentUser && (
        !confirm "Do you really want to leave the season? \
        All of your match records will be deleted."
      )
        return
      else if !isCurrentUser && !(confirm "Do you really want to remove \
        #{member.username} from the season? \
        All of their match records will be deleted."
      )
        return
      $http.delete("/api/match/seasons/#{season.id}/members/#{member.id}")
        .success ->
          if isCurrentUser
            toastr.success "Left the season"
          else
            toastr.success "User removed from the season"
          _.remove season.members, (nextMember) -> nextMember.id is member.id
        .error (reason) ->
          if isCurrentUser
            toastr.error "Failed to leave season #{getReason reason}"
          else
            toastr.error \
              "Failed to remove user #{member.username} from season \
              #{getReason reason}"
    $scope.newMatch =
      scheduled_for: new Date()
      best_of: $scope.bestOfOptions[1].id
    $scope.newMatchError = null
    $scope.createMatch = ->
      return unless $scope.newMatch.season_match_group_id?
      newMatchSlug = angular.copy $scope.newMatch
      $http.post("/api/match/seasons/#{season.id}" + \
        "/match-groups/#{newMatchSlug.season_match_group_id}/matches", {
          match: newMatchSlug
      }).success (newMatch) ->
          toastr.success "Match #{newMatch.id} successfully created"
          group = findGroup newMatchSlug.season_match_group_id
          group.matches ?= []
          group.matches.push newMatch if group?
        .error (data) ->
          toastr.error "Couldn't create match"
          $scope.newMatchError = getReason data
    $scope.deleteMatch = (groupId, matchId) ->
      $http.delete(matchPath(groupId, matchId))
        .success ->
          toastr.success "Match #{matchId} successfully deleted"
          group = findGroup groupId
          return unless group?
          _.remove group.matches, ((item) -> item.id is matchId)
        .error (data) ->
          toastr.error "Couldn't delete match: #{getReason data}"
    $scope.addNextMatchMember = (nextMatchMember, groupId, matchId) ->
      member = angular.copy nextMatchMember
      $http.put("#{matchPath(groupId, matchId)}/members/#{member.id}")
        .success ->
          toastr.success "Successfully added user to match"
          refreshMatchMembers groupId, matchId
        .error (data) ->
          toastr.error "Couldn't add user to match: " + \<commit_msg>fix(match-create): initialize empty user season match and match groups<commit_after>angular.module("MagicStick.controllers").controller "SeasonManageController", [
  "$scope"
  "User"
  "$http"
  "season"
  "toastr"
  ($scope, User, $http, season, toastr) ->
    getReason = (res) ->
      ": " + unless res?
        "Try again later."
      else if res.error?
        res.error
      else if res.errors?
        res.errors
      else
        "An error occurred (#{res})."
    $scope.season = season
    $scope.comment =
      comment: ""
      savedComment: ""
    $scope.bestOfOptions = [
      {id: 1, name: "one"}
      {id: 3, name: "three"}
      {id: 5, name: "five"}
      {id: 7, name: "seven"}
      {id: 11, name: "eleven"}
    ]
    $scope.isCurrentUser = (user) -> user?.username is User.username
    $scope.userLabel = (user) ->
      user.username + if user.name? then " (#{user.name})" else ""
    $scope.seasonOwner = -> $scope.isCurrentUser(season?.owner)
    $scope.seasonParticipant = ->
      _.find season?.members, {'username': User.username}
    $scope.matchStatusAlias = (status) ->
      if status is false
        "Loss"
      else if status is true
        "Win"
      else if not status?
        "Not Played"
      else
        "??"
    $scope.matchIsComplete = (match) ->
      match.user_season_match?.length > 0 and \
      _.any(match.user_season_match, ((userMatch) -> userMatch?.won?))
    $scope.updateJoinType = ->
      return unless season?
      $scope.updatingSeason = yes
      to = ""
      if season.invite_only
        season.allow_auto_join = no
        to = "invite only"
      else
        season.allow_auto_join = yes
        to = "anyone can join"
      $http.put("/api/match/seasons/#{season.id}/join-mode", {
        season:
          invite_only: season.invite_only
          allow_auto_join: season.allow_auto_join
      }).then (res) ->
          toastr.success "Updated join mode to '#{to}'"
        .catch (err) ->
          toastr.error "Couldn't update join mode to '#{to}': #{getReason err}"
        .finally ->
          $scope.updatingSeason = no
    $scope.createSeasonGrouping = (groupingName) ->
      $http.post("/api/match/seasons/#{season.id}/match-groups", {
        name: groupingName
      }).success (newGroup) ->
          toastr.success "Grouping successfully created"
          newGroup.matches ?= []
          season.season_match_groups.push newGroup
          $scope.newGroupingNameError = null
          $scope.newGroupingName = null
        .error (data) ->
          $scope.newGroupingNameError =
            data?.errors?.name ? \
            data?.errors["[:season_id, :name]"] ? \
            ["is invalid"]
    $scope.removeSeasonGrouping = (group) ->
      $http.delete("/api/match/seasons/#{season.id}/match-groups/#{group.id}")
        .success ->
          toastr.success "Successfully deleted match group"
          _.remove season.season_match_groups,
            (nextGroup) -> nextGroup.id is group.id
        .error (reason) ->
          toastr.error \
            "Failed to remove season match grouping #{getReason reason}"
    $scope.joinSeason = -> $scope.addSeasonMember User.clone()
    $scope.addSeasonMember = (newMember) ->
      if _.find season.members, {'username': newMember.username}
        $scope.newMember = null
        toastr.info "User already present in season"
        return
      isCurrentUser = $scope.isCurrentUser newMember
      $http.put("/api/match/seasons/#{season.id}/members/#{newMember.id}")
        .success ->
          if isCurrentUser
            toastr.success "Joined the season"
          else
            toastr.success "User added to season"
          season.members.push newMember
          $scope.newMember = null
        .error (reason) ->
          toastr.error \
            "Failed to add #{newMember.username} to season #{getReason reason}"
    $scope.findMembersMatching = (text) ->
      $http.get("/api/auth/users", {
        params:
          limitted: true
          matching: text
      }).then (response) -> response.data.users
    $scope.removeSeasonMember = (member) ->
      isCurrentUser = $scope.isCurrentUser member
      if isCurrentUser && (
        !confirm "Do you really want to leave the season? \
        All of your match records will be deleted."
      )
        return
      else if !isCurrentUser && !(confirm "Do you really want to remove \
        #{member.username} from the season? \
        All of their match records will be deleted."
      )
        return
      $http.delete("/api/match/seasons/#{season.id}/members/#{member.id}")
        .success ->
          if isCurrentUser
            toastr.success "Left the season"
          else
            toastr.success "User removed from the season"
          _.remove season.members, (nextMember) -> nextMember.id is member.id
        .error (reason) ->
          if isCurrentUser
            toastr.error "Failed to leave season #{getReason reason}"
          else
            toastr.error \
              "Failed to remove user #{member.username} from season \
              #{getReason reason}"
    $scope.newMatch =
      scheduled_for: new Date()
      best_of: $scope.bestOfOptions[1].id
    $scope.newMatchError = null
    $scope.createMatch = ->
      return unless $scope.newMatch.season_match_group_id?
      newMatchSlug = angular.copy $scope.newMatch
      $http.post("/api/match/seasons/#{season.id}" + \
        "/match-groups/#{newMatchSlug.season_match_group_id}/matches", {
          match: newMatchSlug
      }).success (newMatch) ->
          toastr.success "Match #{newMatch.id} successfully created"
          group = findGroup newMatchSlug.season_match_group_id
          return unless group?
          group.matches ?= []
          newMatch.user_season_match ?= []
          group.matches.push newMatch
        .error (data) ->
          toastr.error "Couldn't create match"
          $scope.newMatchError = getReason data
    $scope.deleteMatch = (groupId, matchId) ->
      $http.delete(matchPath(groupId, matchId))
        .success ->
          toastr.success "Match #{matchId} successfully deleted"
          group = findGroup groupId
          return unless group?
          _.remove group.matches, ((item) -> item.id is matchId)
        .error (data) ->
          toastr.error "Couldn't delete match: #{getReason data}"
    $scope.addNextMatchMember = (nextMatchMember, groupId, matchId) ->
      member = angular.copy nextMatchMember
      $http.put("#{matchPath(groupId, matchId)}/members/#{member.id}")
        .success ->
          toastr.success "Successfully added user to match"
          refreshMatchMembers groupId, matchId
        .error (data) ->
          toastr.error "Couldn't add user to match: " + \
{'fraction_non_alphanumeric': 0.12498131260278068, 'fraction_numerical': 0.0011959934220361788, 'mean_word_length': 2.1208304175414043, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5894419', 'n_tokens_mistral': 3897, 'n_tokens_neox': 3899, 'n_words': 931}