<commit_before>GameCommand = require('./game-command')
TokenPoker = require('./core')
_ = require('underscore')

module.exports = class BaseGame
  constructor: (@store, @round) ->
    @registry = new TokenPoker.HandRegistry([
      new TokenPoker.GroupedHand('Six of a Kind', '6', 10),
      new TokenPoker.GroupedHand('Five of a Kind', '51', 540),
      new TokenPoker.GroupedHand('Deuce Trips', '33', 900),
      new TokenPoker.GroupedHand('Full House', '42', 1350),
      new TokenPoker.StraightHand('Six Straight', 6, 3600),
      new TokenPoker.GroupedHand('Three Pair', '222', 10800),
      new TokenPoker.GroupedHand('Four of a Kind', '411', 10800),
      new TokenPoker.StraightHand('Five Straight', 5, 25184),
      new TokenPoker.GroupedHand('Crowded House', '321', 43200),
      new TokenPoker.GroupedHand('Three of a Kind', '3111', 100800),
      new TokenPoker.GroupedHand('Snowflake', '111111', 133216),
      new TokenPoker.GroupedHand('Two Pair', '2211', 226800),
      new TokenPoker.GroupedHand('One Pair', '21111', 442800)
    ])
    @matcher = new TokenPoker.HandMatcher(@registry)
    @randomProvider ||= new RandomProvider

    this.setCache(1)

  setCache: (minimumPlayers) ->
    @playCache = new PlayCache(this, minimumPlayers)

  sendCommand: (playerName, args) ->
    try
      commandResults = []
      # TODO: remove coupling into TimedRound class
      # smell - because we're caching plays, but now can no longer just
      # allow the Round instance to throw when it's too soon, so now
      # the Game class is having to have Round knowledge - which is wrong.
      # if we ever wanted a non-Timed round - this is wrong. We're
      # screwing with the nature of things.
      @round.throwIfNotRestartable() if @round.restartDelaySecondsLeft() > 0
      return if !this.matchedGameCommand(args)
      playerCommands = @playCache.sendCommand(playerName, args)
      this.ensureRoundStarted()
      for playerCommand in playerCommands
        [result, gameCommand] = this.matchedGameCommand(playerCommand.args)
        commandResults.push gameCommand.callback.call(this, playerCommand.playerName, result[1..]...)
      switch
        when commandResults.length > 1
          commandResults
        when commandResults.length == 1
          commandResults[0]
        else
          undefined
    catch error
      error

  matchedGameCommand: (args) ->
    for gameCommand in this.commands()
      if result = gameCommand.regexp.exec(args)
        return [result, gameCommand]

  ensureRoundStarted: ->
    this.startRound() if !@round.isStarted()

  startRound: ->
    @round.start()
    this.setAlarms() if this.setAlarms
    @winner = undefined
<commit_msg>BaseGame should check cmd BEFORE checking round.<commit_after>GameCommand = require('./game-command')
TokenPoker = require('./core')
_ = require('underscore')

module.exports = class BaseGame
  constructor: (@store, @round) ->
    @registry = new TokenPoker.HandRegistry([
      new TokenPoker.GroupedHand('Six of a Kind', '6', 10),
      new TokenPoker.GroupedHand('Five of a Kind', '51', 540),
      new TokenPoker.GroupedHand('Deuce Trips', '33', 900),
      new TokenPoker.GroupedHand('Full House', '42', 1350),
      new TokenPoker.StraightHand('Six Straight', 6, 3600),
      new TokenPoker.GroupedHand('Three Pair', '222', 10800),
      new TokenPoker.GroupedHand('Four of a Kind', '411', 10800),
      new TokenPoker.StraightHand('Five Straight', 5, 25184),
      new TokenPoker.GroupedHand('Crowded House', '321', 43200),
      new TokenPoker.GroupedHand('Three of a Kind', '3111', 100800),
      new TokenPoker.GroupedHand('Snowflake', '111111', 133216),
      new TokenPoker.GroupedHand('Two Pair', '2211', 226800),
      new TokenPoker.GroupedHand('One Pair', '21111', 442800)
    ])
    @matcher = new TokenPoker.HandMatcher(@registry)
    @randomProvider ||= new RandomProvider

    this.setCache(1)

  setCache: (minimumPlayers) ->
    @playCache = new PlayCache(this, minimumPlayers)

  sendCommand: (playerName, args) ->
    try
      commandResults = []
      return if !this.matchedGameCommand(args)
      # TODO: remove coupling into TimedRound class
      # smell - because we're caching plays, but now can no longer just
      # allow the Round instance to throw when it's too soon, so now
      # the Game class is having to have Round knowledge - which is wrong.
      # if we ever wanted a non-Timed round - this is wrong. We're
      # screwing with the nature of things.
      @round.throwIfNotRestartable() if @round.restartDelaySecondsLeft() > 0
      playerCommands = @playCache.sendCommand(playerName, args)
      this.ensureRoundStarted()
      for playerCommand in playerCommands
        [result, gameCommand] = this.matchedGameCommand(playerCommand.args)
        commandResults.push gameCommand.callback.call(this, playerCommand.playerName, result[1..]...)
      switch
        when commandResults.length > 1
          commandResults
        when commandResults.length == 1
          commandResults[0]
        else
          undefined
    catch error
      error

  matchedGameCommand: (args) ->
    for gameCommand in this.commands()
      if result = gameCommand.regexp.exec(args)
        return [result, gameCommand]

  ensureRoundStarted: ->
    this.startRound() if !@round.isStarted()

  startRound: ->
    @round.start()
    this.setAlarms() if this.setAlarms
    @winner = undefined

{'fraction_non_alphanumeric': 0.11394275161588181, 'fraction_numerical': 0.03804247460757156, 'mean_word_length': 3.2511773940345368, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '339738', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1584, 'n_words': 448}