refactor: basic screen
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<main class="main">
|
||||
<app-screen-edit [players]="players"></app-screen-edit>
|
||||
<app-screen-basic [players]="players"></app-screen-basic>
|
||||
</main>
|
||||
|
||||
<router-outlet />
|
||||
|
||||
@@ -1,41 +1,28 @@
|
||||
<div class="row justify-content-md-center">
|
||||
<h1>Please select the number of teams:</h1>
|
||||
<div class="btn-toolbar mb-3 col justify-content-center" role="toolbar">
|
||||
<div class="btn-group me-2" id="numTeamsSelector" role="group"
|
||||
value="2">
|
||||
<input value="2" [(ngModel)]="numTeamsSelectorValue" type="radio" class="btn-check" name="btnradio" id="btnradio1" autocomplete="off" checked>
|
||||
<label class="btn btn-outline-primary" for="btnradio1">Two</label>
|
||||
<div class="justify-content-md-center">
|
||||
<div class="btn-toolbar mb-3 col justify-content-center" role="toolbar">
|
||||
<div class="btn-group me-2" id="numTeamsSelector" role="group"
|
||||
value="2">
|
||||
<input value="2" [(ngModel)]="numTeamsSelectorValue" type="radio" class="btn-check" name="btnradio" id="btnradio1" autocomplete="off" checked>
|
||||
<label class="btn btn-outline-primary" for="btnradio1">Two</label>
|
||||
|
||||
<input value="3" [(ngModel)]="numTeamsSelectorValue" type="radio" class="btn-check" name="btnradio" id="btnradio2" autocomplete="off">
|
||||
<label class="btn btn-outline-primary" for="btnradio2">Three</label>
|
||||
<input value="3" [(ngModel)]="numTeamsSelectorValue" type="radio" class="btn-check" name="btnradio" id="btnradio2" autocomplete="off">
|
||||
<label class="btn btn-outline-primary" for="btnradio2">Three</label>
|
||||
|
||||
<input value="n" [(ngModel)]="numTeamsSelectorValue" type="radio" class="btn-check" name="btnradio" id="btnradio3" autocomplete="off">
|
||||
<label class="btn btn-outline-primary" for="btnradio3">n</label>
|
||||
<input value="n" [(ngModel)]="numTeamsSelectorValue" type="radio" class="btn-check" name="btnradio" id="btnradio3" autocomplete="off">
|
||||
<label class="btn btn-outline-primary" for="btnradio3">n</label>
|
||||
</div>
|
||||
<div *ngIf="numTeamsSelectorValue === 'n'" id="nTeamsText" class="col-md-auto">
|
||||
<label for="nTeamsField">n Teams</label>
|
||||
<input type="number" pattern="\d*" [(ngModel)]="nTeamsValue" id="nTeamsField" class="form-control" >
|
||||
</div>
|
||||
</div>
|
||||
<div *ngIf="numTeamsSelectorValue === 'n'" id="nTeamsText" class="col-md-auto">
|
||||
<label for="nTeamsField">n Teams</label>
|
||||
<input type="number" pattern="\d*" [(ngModel)]="nTeamsValue" id="nTeamsField" class="form-control" >
|
||||
</div>
|
||||
</div>
|
||||
<h1>Random Teams</h1>
|
||||
|
||||
<form>
|
||||
<div class="container">
|
||||
<label for="playerNames">Names</label>
|
||||
<textarea [(ngModel)]="playerNamesValue"
|
||||
class="form-control mb-3"
|
||||
rows="18"
|
||||
cols="30"
|
||||
style="text-align: left;"
|
||||
name="playerNames"
|
||||
#playerNames
|
||||
id="playerNames"
|
||||
></textarea>
|
||||
</div>
|
||||
<button type="button" (click)="onButtonGenerate(playerNames.value)" class="btn btn-primary">Generate</button>
|
||||
<button type="button" (click)="onButtonGenerate()" class="btn btn-primary">Generate</button>
|
||||
</form>
|
||||
|
||||
<table class="table table-striped custom-table">
|
||||
<caption *ngIf="duplicateNames.length >0">Removed: {{ duplicateNames }}</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Size</th>
|
||||
|
||||
@@ -5,6 +5,7 @@ import { RouterOutlet, ActivatedRoute } from '@angular/router';
|
||||
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Player } from '../model';
|
||||
import { iter } from '../util';
|
||||
|
||||
@Component({
|
||||
selector: 'app-screen-basic',
|
||||
@@ -14,46 +15,31 @@ import { Player } from '../model';
|
||||
})
|
||||
export class ScreenBasicComponent {
|
||||
@Input() players!: Player[];
|
||||
playerNamesValue = "";
|
||||
numTeamsSelectorValue = "2";
|
||||
numTeamsSelected = 2;
|
||||
nTeamsValue = "4";
|
||||
teamsArray: string[][] = [];
|
||||
duplicateNames: string[] = [];
|
||||
|
||||
onButtonGenerate(textinput: string): void{
|
||||
onButtonGenerate(): void{
|
||||
if(this.numTeamsSelectorValue === 'n'){
|
||||
this.numTeamsSelected = Number(this.nTeamsValue);
|
||||
}
|
||||
else{
|
||||
this.numTeamsSelected = Number(this.numTeamsSelectorValue);
|
||||
}
|
||||
let nameslist = this.playerNamesValue
|
||||
.split('\n')
|
||||
.map(function(str){return str.trim();})
|
||||
.filter(function(str){return str}); // boolean interpretation is same as non-empty
|
||||
// remove duplicates by using a Set
|
||||
let namesset = new Set(nameslist);
|
||||
let names = [...namesset];
|
||||
|
||||
|
||||
let teams = Array.from({ length: this.numTeamsSelected }, () => []);
|
||||
// clone array here
|
||||
let localPlayers: Player[] = Object.assign([],this.players);
|
||||
|
||||
let nameslen = names.length;
|
||||
function* iter(list: any[]){
|
||||
let index = 0;
|
||||
while(true){
|
||||
yield list[index % list.length];
|
||||
index++;
|
||||
}
|
||||
}
|
||||
let nameslen = localPlayers.length;
|
||||
let iterator = iter(teams);
|
||||
for(let i =0; i < nameslen; i++){
|
||||
let index = Math.floor(Math.random()* names.length);
|
||||
let n = names[index];
|
||||
names.splice(index,1);
|
||||
let index = Math.floor(Math.random()* localPlayers.length);
|
||||
let n = localPlayers[index];
|
||||
localPlayers.splice(index,1);
|
||||
let team = iterator.next().value;
|
||||
team.push(n);
|
||||
team.push(n.name);
|
||||
|
||||
}
|
||||
this.teamsArray = teams;
|
||||
|
||||
7
src/app/util.ts
Normal file
7
src/app/util.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export function* iter(list: any[]){
|
||||
let index = 0;
|
||||
while(true){
|
||||
yield list[index % list.length];
|
||||
index++;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user