All files / src/model scorer.ts

100% Statements 20/20
100% Branches 4/4
100% Functions 6/6
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11922x                                                                                                                                                     22x           21x 1x   21x 4x   21x 1x   21x 21x 21x 21x 21x 9x 8x           1x 1x       53x       3x       5x      
import { NumberLabel, NumberLabelImpl, NumberLabelOptions } from './numberLabel'
 
/**
 * {@link Scorer} の状態.
 *
 * 'enabled': 得点の変更を許可します.
 *
 * 'disabled': 得点を変更しようとしても無視します.
 */
export type ScorerStatus = 'enabled' | 'disabled'
 
/**
 * プレイヤーの得点を制御、描画します.
 */
export interface Scorer extends NumberLabel {
  /**
   * 得点を取得します. (0点以上、小数点以下切り捨て)
   */
  readonly value: number
 
  /**
   * 得点を取得します. (負の値あり、小数点以下あり)
   */
  readonly rawValue: number
 
  /**
   * 得点の変更を受け付けるか状態を取得します
   */
  readonly status: ScorerStatus
 
  /**
   * 得点を記録しても、サーバーには送信しない設定か取得します.
   */
  readonly refrainsSendingScore: boolean
 
  /**
   * ライブラリ利用者が自由に使えるフィールドです.
   */
  vars?: unknown
 
  /**
   * 加点します.
   *
   * @param score 加点する値
   */
  add (score: number): void
 
  /**
   * 引数の値に得点を設定します.
   *
   * @param score 設定する値
   */
  set (score: number): void
 
  /**
   * 得点を設定できるようにします.
   */
  enable (): void
 
  /**
   * 得点を設定しようとしても無視します.
   */
  disable (): void
 
  /**
   * 得点をサーバーに常時送るようにします.
   *
   * サーバーへの送信を抑止(refrainsSendingScore)している場合、
   * 本メソッドを呼び出して抑止を解除する必要があります.
   */
  keepSendingScore (): void
}
 
export type ScorerOptions = Omit<NumberLabelOptions, 'value' | 'textAlign'> & { refrainsSendingScore: boolean, vars: unknown }
 
export class ScorerImpl extends NumberLabelImpl implements Scorer {
  vars?: unknown
  private game: g.Game
  private _refrainsSendingScore: boolean
 
  constructor ({ scene, font, digit, prefix, suffix, refrainsSendingScore, vars }: ScorerOptions) {
    if (!scene.game.vars) {
      scene.game.vars = { gameState: { score: 0 } }
    }
    if (!('gameState' in scene.game.vars)) {
      scene.game.vars.gameState = { score: 0 }
    }
    if (!('score' in scene.game.vars.gameState)) {
      scene.game.vars.gameState.score = 0
    }
    super({ scene, value: scene.game.vars.gameState.score as number, font, digit, prefix, suffix, textAlign: 'right' })
    this.game = scene.game
    this._refrainsSendingScore = refrainsSendingScore
    this.vars = vars
    this.onValue.add(() => {
      if (!this._refrainsSendingScore) {
        this.game.vars.gameState.score = this.value
      }
    })
  }
 
  keepSendingScore () {
    this._refrainsSendingScore = false
    this.game.vars.gameState.score = this.value
  }
 
  override get value (): number {
    return Math.max(Math.floor(super.value), 0)
  }
 
  get rawValue (): number {
    return super.value
  }
 
  get refrainsSendingScore (): boolean {
    return this._refrainsSendingScore
  }
}