<commit_before>import { ImageFile } from './core/file-storage/image-file';
import { SyncObject, SyncVar } from './core/synchronize-object/decorator';
import { Network } from './core/system';
import { DataElement } from './data-element';
import { PeerCursor } from './peer-cursor';
import { TabletopObject } from './tabletop-object';

export enum DiceType {
  D2,
  D4,
  D6,
  D8,
  D10,
  D10_10TIMES,
  D12,
  D20
}

@SyncObject('dice-symbol')
export class DiceSymbol extends TabletopObject {
  @SyncVar() face: string = '0';
  @SyncVar() owner: string = '';
  @SyncVar() rotate: number = 0;

  get name(): string { return this.getCommonValue('name', ''); }
  set name(name: string) { this.setCommonValue('name', name); }
  get size(): number { return this.getCommonValue('size', 1); }
  set size(size: number) { this.setCommonValue('size', size); }

  get faces(): string[] { return this.imageDataElement.children.map(element => (element as DataElement).name); }
  get imageFile(): ImageFile {
    return this.isVisible ?
      this.getImageFile(this.face)
      : this.faces.length
        ? this.getImageFile(this.faces[0])
        : null;
  }

  get ownerName(): string {
    let object = PeerCursor.findByUserId(this.owner);
    return object ? object.name : '';
  }
  get hasOwner(): boolean { return 0 < this.owner.length; }
  get isMine(): boolean { return Network.peerContext.userId === this.owner; }
  get isVisible(): boolean { return !this.hasOwner || this.isMine; }

  diceRoll(): string {
    let faces = this.faces;
    this.face = 0 < faces.length ? faces[Math.floor(Math.random() * faces.length)] : '';
    return this.face;
  }

  setDicetype(type: DiceType) {
    this.makeDiceFace(type);
  }

  private makeDiceFace(type: DiceType, identifierSuffix?: string): DataElement[] {
    let sided: number = 1;
    let faces: DataElement[] = [];
    let faceGeneratorFunc: (index: number) => string = index => (index + 1) + '';

    switch (type) {
      case DiceType.D2:
        sided = 2;
        break;
      case DiceType.D4:
        sided = 4;
        break;
      case DiceType.D6:
        sided = 6;
        break;
      case DiceType.D8:
        sided = 8;
        break;
      case DiceType.D10_10TIMES:
        faceGeneratorFunc = index => (index + 1) + '0';
      case DiceType.D10:
        sided = 10;
        break;
      case DiceType.D12:
        sided = 12;
        break;
      case DiceType.D20:
        sided = 20;
        break;
      default:
        sided = 2;
        break;
    }

    for (let i = 0; i < sided; i++) {
      let faceName = faceGeneratorFunc(i);
      let identifier = identifierSuffix != null ? faceName + '_' + identifierSuffix : null;
      faces.push(DataElement.create(faceName, '', { type: 'image' }, identifier));
    }

    this.imageDataElement.children.forEach(element => element.destroy());
    faces.forEach(element => this.imageDataElement.appendChild(element));
    this.face = faces[0].name;

    return faces;
  }

  static create(name: string, type: DiceType, size: number,
    identifier?: string): DiceSymbol {
    let object: DiceSymbol = identifier ? new DiceSymbol(identifier) : new DiceSymbol();

    object.createDataElements();
    object.commonDataElement.appendChild(DataElement.create('name', name, {}, 'name_' + object.identifier));
    object.commonDataElement.appendChild(DataElement.create('size', size, {}, 'size_' + object.identifier));

    object.makeDiceFace(type, object.identifier);
    object.initialize();
    return object;
  }
}<commit_msg>style: コード整形<commit_after>import { ImageFile } from './core/file-storage/image-file';
import { SyncObject, SyncVar } from './core/synchronize-object/decorator';
import { Network } from './core/system';
import { DataElement } from './data-element';
import { PeerCursor } from './peer-cursor';
import { TabletopObject } from './tabletop-object';

export enum DiceType {
  D2,
  D4,
  D6,
  D8,
  D10,
  D10_10TIMES,
  D12,
  D20
}

@SyncObject('dice-symbol')
export class DiceSymbol extends TabletopObject {
  @SyncVar() face: string = '0';
  @SyncVar() owner: string = '';
  @SyncVar() rotate: number = 0;

  get name(): string { return this.getCommonValue('name', ''); }
  set name(name: string) { this.setCommonValue('name', name); }
  get size(): number { return this.getCommonValue('size', 1); }
  set size(size: number) { this.setCommonValue('size', size); }

  get faces(): string[] { return this.imageDataElement.children.map(element => (element as DataElement).name); }
  get imageFile(): ImageFile {
    return this.isVisible ?
      this.getImageFile(this.face)
      : this.faces.length
        ? this.getImageFile(this.faces[0])
        : null;
  }

  get ownerName(): string {
    let object = PeerCursor.findByUserId(this.owner);
    return object ? object.name : '';
  }
  get hasOwner(): boolean { return 0 < this.owner.length; }
  get isMine(): boolean { return Network.peerContext.userId === this.owner; }
  get isVisible(): boolean { return !this.hasOwner || this.isMine; }

  diceRoll(): string {
    let faces = this.faces;
    this.face = 0 < faces.length ? faces[Math.floor(Math.random() * faces.length)] : '';
    return this.face;
  }

  setDicetype(type: DiceType) {
    this.makeDiceFace(type);
  }

  private makeDiceFace(type: DiceType, identifierSuffix?: string): DataElement[] {
    let sided: number = 1;
    let faces: DataElement[] = [];
    let faceGeneratorFunc: (index: number) => string = index => (index + 1) + '';

    switch (type) {
      case DiceType.D2:
        sided = 2;
        break;
      case DiceType.D4:
        sided = 4;
        break;
      case DiceType.D6:
        sided = 6;
        break;
      case DiceType.D8:
        sided = 8;
        break;
      case DiceType.D10_10TIMES:
        faceGeneratorFunc = index => (index + 1) + '0';
      case DiceType.D10:
        sided = 10;
        break;
      case DiceType.D12:
        sided = 12;
        break;
      case DiceType.D20:
        sided = 20;
        break;
      default:
        sided = 2;
        break;
    }

    for (let i = 0; i < sided; i++) {
      let faceName = faceGeneratorFunc(i);
      let identifier = identifierSuffix != null ? faceName + '_' + identifierSuffix : null;
      faces.push(DataElement.create(faceName, '', { type: 'image' }, identifier));
    }

    this.imageDataElement.children.forEach(element => element.destroy());
    faces.forEach(element => this.imageDataElement.appendChild(element));
    this.face = faces[0].name;

    return faces;
  }

  static create(name: string, type: DiceType, size: number, identifier?: string): DiceSymbol {
    let object: DiceSymbol = identifier ? new DiceSymbol(identifier) : new DiceSymbol();

    object.createDataElements();
    object.commonDataElement.appendChild(DataElement.create('name', name, {}, 'name_' + object.identifier));
    object.commonDataElement.appendChild(DataElement.create('size', size, {}, 'size_' + object.identifier));

    object.makeDiceFace(type, object.identifier);
    object.initialize();
    return object;
  }
}
{'fraction_non_alphanumeric': 0.14439258944986566, 'fraction_numerical': 0.014425116673737802, 'mean_word_length': 3.0090702947845807, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '1049979', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2229, 'n_words': 527}