UNPKG

@haelp/teto

Version:

A typescript-based controllable TETR.IO client.

1 lines 3.52 kB
{"version":3,"sources":["../../../src/engine/queue/index.ts"],"sourcesContent":["import { type BagType, type RngInnerFunction, rngMap } from \"./rng\";\nimport { Mino } from \"./types\";\n\nexport interface QueueInitializeParams {\n seed: number;\n type: BagType;\n minLength: number;\n}\n\nexport class Queue {\n seed: number;\n type: BagType;\n genFunction!: RngInnerFunction;\n value: Mino[];\n _minLength!: number;\n index: number;\n repopulateListener: ((pieces: Mino[]) => void) | null = null;\n constructor(options: QueueInitializeParams) {\n this.seed = options.seed;\n this.type = options.type;\n this.reset();\n this.value = [];\n this.minLength = options.minLength;\n\n this.index = 0;\n }\n\n reset(index = 0) {\n this.genFunction = rngMap[this.type](this.seed);\n this.value = [];\n this.index = 0;\n this.repopulate();\n for (let i = 0; i < index; i++) {\n this.shift();\n this.repopulate();\n }\n }\n\n onRepopulate(listener: NonNullable<typeof this.repopulateListener>) {\n this.repopulateListener = listener;\n }\n\n get minLength() {\n return this._minLength;\n }\n set minLength(val: number) {\n this._minLength = val;\n this.repopulate();\n }\n\n get next() {\n return this.value[0];\n }\n\n at(index: number) {\n return this.value.at(index);\n }\n\n shift() {\n const val = this.value.shift();\n this.index++;\n this.repopulate();\n return val;\n }\n\n private repopulate() {\n const added: Mino[] = [];\n while (this.value.length < this.minLength) {\n const newValues = this.genFunction();\n this.value.push(...newValues);\n added.push(...newValues);\n }\n\n if (this.repopulateListener && added.length) {\n this.repopulateListener(added);\n }\n }\n}\nexport * from \"./rng\";\nexport * from \"./types\";\n"],"names":["rngMap","Queue","seed","type","genFunction","value","_minLength","index","repopulateListener","options","reset","minLength","repopulate","i","shift","onRepopulate","listener","val","next","at","added","length","newValues","push"],"mappings":"AAAA,SAA8CA,MAAM,QAAQ,QAAQ;AASpE,OAAO,MAAMC;IACXC,KAAa;IACbC,KAAc;IACdC,YAA+B;IAC/BC,MAAc;IACdC,WAAoB;IACpBC,MAAc;IACdC,qBAAwD,KAAK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