<commit_before><template>
  <div>
    <tournament-preview v-if="!tournament.isStarted" :tournament="tournament" :user="user"></tournament-preview>
    <tournament-overview v-if="tournament.isStarted" :tournament="tournament" :user="user"></tournament-overview>
  </div>
</template>

<script>
import Tournament from '../models/Tournament'
import TournamentOverview from '../components/TournamentOverview'
import TournamentPreview from '../components/TournamentPreview'
import * as levels from "../models/Level"
import _ from 'lodash'

export default {
  name: 'Tournament',

  components: {
    TournamentOverview,
    TournamentPreview,
  },

  data () {
    return {
      tournament: new Tournament(),
      user: this.$root.user,
      levels: levels,
    }
  },

  computed: {
    runnerups: function () {
      let t = this.tournament

      if (!t.runnerups) {
        return []
      }

      return _.map(t.runnerups, (runnerupName) => {
        return _.find(t.players, { name: runnerupName })
      })
    }
  },

  methods: {
    start: function () {
      if (this.tournament) {
        this.api.start({ id: this.tournament.id }).then((res) => {
          console.log("start response:", res)
          let j = res.json()
          this.$route.router.go('/towerfall' + j.redirect)
        }, (err) => {
          console.error(`start for ${this.tournament} failed`, err)
        })
      } else {
        console.error("start called with no tournament")
      }
    },
    next: function () {
      if (this.tournament) {
        this.api.next({ id: this.tournament.id }).then((res) => {
          console.debug("next response:", res)
          let j = res.json()
          this.$route.router.go('/towerfall' + j.redirect)
        }, (err) => {
          console.error(`next for ${this.tournament} failed`, err)
        })
      } else {
        console.error("next called with no tournament")
      }
    }
  },

  created: function () {
    console.debug("Creating API resource")
    let customActions = {
      start: { method: "GET", url: "/api/towerfall{/id}/start/" },
      next: { method: "GET", url: "/api/towerfall{/id}/next/" },
      getData: { method: "GET", url: "/api/towerfall/tournament{/id}/" }
    }
    this.api = this.$resource("/api/towerfall", {}, customActions)
  },

  route: {
    data ({ to }) {
      // listen for tournaments from App
      this.$on(`tournament${to.params.tournament}`, (tournament) => {
        console.debug("New tournament from App:", tournament)
        this.$set('tournament', tournament)
      })

      // TODO perhaps use $root.tournaments again?
      return this.api.getData({ id: to.params.tournament }).then((res) => {
        let tournament = Tournament.fromObject(res.data.tournament)
        console.debug("loaded tournament", tournament)
        return {
          tournament: tournament
        }
      }, (error) => {
        console.error('error when getting tournament', error)
        return { tournament: new Tournament() }
      })
    }
  }
}
</script>

<style lang="scss">
</style>
<commit_msg>Set user and level properly on Tournament.vue<commit_after><template>
  <div>
    <tournament-preview v-if="!tournament.isStarted" :tournament="tournament" :user="user" :levels="levels"></tournament-preview>
    <tournament-overview v-if="tournament.isStarted" :tournament="tournament" :user="user" :levels="levels"></tournament-overview>
  </div>
</template>

<script>
import Tournament from '../models/Tournament'
import TournamentOverview from '../components/TournamentOverview'
import TournamentPreview from '../components/TournamentPreview'
import * as levels from "../models/Level"
import _ from 'lodash'

export default {
  name: 'Tournament',

  components: {
    TournamentOverview,
    TournamentPreview,
  },

  data () {
    return {
      tournament: new Tournament(),
      user: this.$root.user,
      levels: levels,
    }
  },

  computed: {
    runnerups: function () {
      let t = this.tournament

      if (!t.runnerups) {
        return []
      }

      return _.map(t.runnerups, (runnerupName) => {
        return _.find(t.players, { name: runnerupName })
      })
    }
  },

  methods: {
    start: function () {
      if (this.tournament) {
        this.api.start({ id: this.tournament.id }).then((res) => {
          console.log("start response:", res)
          let j = res.json()
          this.$route.router.go('/towerfall' + j.redirect)
        }, (err) => {
          console.error(`start for ${this.tournament} failed`, err)
        })
      } else {
        console.error("start called with no tournament")
      }
    },
    next: function () {
      if (this.tournament) {
        this.api.next({ id: this.tournament.id }).then((res) => {
          console.debug("next response:", res)
          let j = res.json()
          this.$route.router.go('/towerfall' + j.redirect)
        }, (err) => {
          console.error(`next for ${this.tournament} failed`, err)
        })
      } else {
        console.error("next called with no tournament")
      }
    }
  },

  created: function () {
    console.debug("Creating API resource")
    let customActions = {
      start: { method: "GET", url: "/api/towerfall{/id}/start/" },
      next: { method: "GET", url: "/api/towerfall{/id}/next/" },
      getData: { method: "GET", url: "/api/towerfall/tournament{/id}/" }
    }
    this.api = this.$resource("/api/towerfall", {}, customActions)
  },

  route: {
    data ({ to }) {
      // listen for tournaments from App
      this.$on(`tournament${to.params.tournament}`, (tournament) => {
        console.debug("New tournament from App:", tournament)
        this.$set('tournament', tournament)
      })

      // TODO perhaps use $root.tournaments again?
      return this.api.getData({ id: to.params.tournament }).then((res) => {
        let tournament = Tournament.fromObject(res.data.tournament)
        console.debug("loaded tournament", tournament)
        return {
          tournament: tournament,
          user: this.$root.user,
        }
      }, (error) => {
        console.error('error when getting tournament', error)
        return { tournament: new Tournament() }
      })
    }
  }
}
</script>

<style lang="scss">
</style>

{'fraction_non_alphanumeric': 0.16074856046065258, 'fraction_numerical': 0.0, 'mean_word_length': 2.458517699115044, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 43, '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': '104308', 'n_tokens_mistral': 1965, 'n_tokens_neox': 1863, 'n_words': 404}