Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/configure amount of matches #100

Merged
merged 13 commits into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,73 @@ <h3>Settings</h3>
<fieldset class="fieldset">
<legend>Tournament</legend>
<h5>{{settings['tournamentStage'].display}}</h5>
<div class="input-group">
<select class="input-group-field" [(ngModel)]="settings['tournamentStage'].value"
[disabled]="settings['tournamentStage'].options == undefined">
<option *ngFor="let option of settings['tournamentStage'].options" value="{{option}}">
{{option}}
</option>
</select>
<div class="input-group-button">
<div class="grid-x grid-margin-x">
<div class="cell medium-4">
<select class="input-group-field" [(ngModel)]="settings['tournamentStage'].value"
[disabled]="settings['tournamentStage'].options == undefined">
<option *ngFor="let option of settings['tournamentStage'].options"
value="{{option}}">
{{option}}
</option>
</select>
</div>
<div class="cell medium-2">
<button class="button" (click)="save('tournamentStage')"
[disabled]="settings['tournamentStage'].options == undefined"><i
class="fa fa-save"></i>&nbsp; Save
</button>
</div>
</div>

<div class="cell medium-2">
<h5>Number of rounds per stage</h5>
<label>
This setting is used when you import a partial schedule file or a team list.
</label>
</div>

<div class="grid-x grid-margin-x">
<div class="cell medium-6">
<div class="grid-y">
<div class="cell medium-1">
<div class="grid-x grid-margin-x">
<div class="cell medium-4">
{{settings['numberOfPracticeRounds'].display}}
</div>
<div class="cell medium-4">
{{settings['numberOfRankingRounds'].display}}
</div>
</div>
</div>
<div class="cell medium-1">
<div class="grid-x grid-margin-x">
<div class="cell medium-4">
<input type="number" [(ngModel)]="settings['numberOfPracticeRounds'].value"
min="{{MIN_AMOUNT_OF_ROUNDS}}" max="{{MAX_AMOUNT_OF_ROUNDS}}">
</div>
<div class="cell medium-4">
<input type="number" [(ngModel)]="settings['numberOfRankingRounds'].value"
min="{{MIN_AMOUNT_OF_ROUNDS}}" max="{{MAX_AMOUNT_OF_ROUNDS}}">
</div>
<div class="cell medium-2">
<button class="button" (click)="saveNumberOFRounds()" [disabled]=""><i
class="fa fa-save"></i>&nbsp; Save
</button>
</div>

</div>
</div>
</div>
</div>
</div>

<h5>Delete Tournament Data</h5>
<label>Press Delete to delete Teams and Matches. <br/> Note: You must first delete all scoring data.</label>
<label>Press Delete to delete Teams and Matches. <br/> Note: You must first delete all scoring
data.</label>
<button class="button alert" data-open="model-delete"
(click)="setDeleteModel(tournamentDataService)" [disabled]="!hasDataInDb">Delete
</button>

</fieldset>

<fieldset class="fieldset">
Expand All @@ -39,7 +86,9 @@ <h5>{{settings['tournamentTitle'].display}}</h5>
<div class="input-group">
<input type="text" [(ngModel)]="settings['tournamentTitle'].value" class="input-group-field">
<div class="input-group-button">
<button class="button" (click)="save('tournamentTitle')"><i class="fa fa-save"></i>&nbsp; Save</button>
<button class="button" (click)="save('tournamentTitle')"><i class="fa fa-save"></i>&nbsp;
Save
</button>
</div>
</div>
</fieldset>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Notifications} from "../../shared/services/notifications.service";
import {st} from "../../../../node_modules/@angular/core/src/render3";
import {DeleteService} from "../../shared/services/delete-service.service";
import {TournamentDataService} from "../../shared/services/tournament-data.service";
import {forkJoin} from "rxjs";

@Component({
selector: 'app-tournament-settings',
Expand All @@ -16,6 +17,9 @@ export class TournamentSettingsComponent implements OnInit {
public loading: boolean = true;
public hasDataInDb = false;

public MIN_AMOUNT_OF_ROUNDS = 0;
public MAX_AMOUNT_OF_ROUNDS = 5

constructor(private tournamentSettingsService: TournamentSettingsService,
public tournamentDataService: TournamentDataService,
private notification: Notifications,
Expand All @@ -24,7 +28,7 @@ export class TournamentSettingsComponent implements OnInit {

ngOnInit() {
this.reload();
this.tournamentDataService.dataReload.subscribe(()=>{
this.tournamentDataService.dataReload.subscribe(() => {
this.reload()
})
}
Expand All @@ -43,6 +47,16 @@ export class TournamentSettingsComponent implements OnInit {
display: 'Stage',
value: settings['tournamentStage'],
name: 'tournamentStage'
},
numberOfPracticeRounds: {
display: 'Practice rounds',
value: settings['numberOfPracticeRounds'],
name: 'numberOfPracticeRounds'
},
numberOfRankingRounds: {
display: 'Ranking rounds',
value: settings['numberOfRankingRounds'],
name: 'numberOfRankingRounds'
}
}
this.tournamentSettingsService.getStages().subscribe(
Expand All @@ -66,19 +80,42 @@ export class TournamentSettingsComponent implements OnInit {
this.tournamentSettingsService.saveSetting(setting, this.settings[setting].value).subscribe(
taltaub22 marked this conversation as resolved.
Show resolved Hide resolved
response => {
this.loading = false;
this.notification.success("Setting saved successfully")
this.notification.success(`${this.settings[setting].display} saved successfully`)
taltaub22 marked this conversation as resolved.
Show resolved Hide resolved
},
err => {
this.notification.error("Oh no! Something went wrong while trying to save...")
taltaub22 marked this conversation as resolved.
Show resolved Hide resolved
})
}

setDeleteModel(model){
saveNumberOFRounds() {
if (this.doesValueBetween(this.settings['numberOfPracticeRounds'].value, this.MIN_AMOUNT_OF_ROUNDS, this.MAX_AMOUNT_OF_ROUNDS) &&
this.doesValueBetween(this.settings['numberOfRankingRounds'].value, this.MIN_AMOUNT_OF_ROUNDS, this.MAX_AMOUNT_OF_ROUNDS)) {
forkJoin(this.tournamentSettingsService.saveSetting('numberOfPracticeRounds', this.settings['numberOfPracticeRounds'].value),
this.tournamentSettingsService.saveSetting('numberOfRankingRounds', this.settings['numberOfRankingRounds'].value))
.subscribe(
response => {
this.notification.success(`Number of rounds saved successfully`)
},
error => {
this.notification.error("Oh no! Something went wrong while trying to save number of rounds")
}
)
} else {
this.notification.error(`Valid rounds per stage is between ${this.MIN_AMOUNT_OF_ROUNDS} and ${this.MAX_AMOUNT_OF_ROUNDS}`)
}

}

doesValueBetween(value, min, max) {
return value >= min && value <= max;
}

setDeleteModel(model) {
this.deleteModalsService.setDeleteModel(model);
}

haveDataInDb(){
this.tournamentDataService.hasData().subscribe(result=>{
haveDataInDb() {
this.tournamentDataService.hasData().subscribe(result => {
this.hasDataInDb = result
})
}
Expand Down
21 changes: 18 additions & 3 deletions server/logic/settings_logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ const SETTING_COLLECTION_NAME = 'settings'
function setDefaultSettings () {
const defaultSettings = {
'tournamentStage': 'practice',
'tournamentTitle': 'World Festival Houston 2019'
'tournamentTitle': 'World Festival Houston 2019',
'numberOfPracticeRounds': 1,
'numberOfRankingRounds': 3
}

db.connection().then(connection => {
connection.db().collection(SETTING_COLLECTION_NAME).findOne().then(response => {
if (!response) {
connection.db().collection(SETTING_COLLECTION_NAME).findOne({}).then(response => {
const promises = []
if (response) {
for (const setting of Object.keys(defaultSettings)) {
idanstark42 marked this conversation as resolved.
Show resolved Hide resolved
if (!response[setting]) {
const toSet = {}
toSet[setting] = defaultSettings[setting]
promises.push(connection.db().collection(SETTING_COLLECTION_NAME).updateOne({}, {
$set: toSet
}))
}
}
return Promise.all(promises)
idanstark42 marked this conversation as resolved.
Show resolved Hide resolved
} else {
return connection.db().collection(SETTING_COLLECTION_NAME).insert(defaultSettings)
}
})
Expand Down
72 changes: 38 additions & 34 deletions server/routers/matchTeam_router.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict'
const express = require('express')

const db = require('../utilities/mongo_connection')
const settings = require('../logic/settings_logic')

const MsLogger = require('@first-lego-league/ms-logger').Logger()

const RANDOM_ID_LENGTH = 25
Expand All @@ -12,10 +15,10 @@ exports.getRouter = function () {
db.connection().then(connection => {
connection.db().collection('matches').find({ 'matchTeams.teamNumber': parseInt(req.params.team) }).toArray().then(data => {
if (!data || data.length === 0) {
res.send(getDefaultMatchesForTeam(parseInt(req.params.team)))
return
return getDefaultStages().then(stages => {
res.send(getDefaultMatchesForTeam(parseInt(req.params.team), stages))
taltaub22 marked this conversation as resolved.
Show resolved Hide resolved
})
}

res.send(data)
})
}).catch(err => {
Expand All @@ -28,44 +31,45 @@ exports.getRouter = function () {
return router
}

function getDefaultMatchesForTeam (teamNumber) {
const practice = {
'_id': createRandomId(RANDOM_ID_LENGTH),
'matchId': 1,
'stage': 'practice',
'matchTeams': [
{
'teamNumber': teamNumber,
'tableId': null
}
]
}
function getDefaultMatchesForTeam (teamNumber, stages) {
const matches = []

const ranking = {
'_id': createRandomId(RANDOM_ID_LENGTH),
'matchId': 1,
'stage': 'ranking',
'matchTeams': [
{
for (const stage in stages) {
for (let i = 1; i <= stages[stage].matchAmount; i++) {
const match = {}
match._id = createRandomId(RANDOM_ID_LENGTH)
match.stage = stages[stage].stageName
match.matchId = i
match.matchTeams = [{
'teamNumber': teamNumber,
'tableId': null
}
]
}]
matches.push(match)
}
}

let matches = []
matches.push(practice)
return matches
}

for (let i = 1; i <= 3; i++) {
let match = {}
match._id = createRandomId(RANDOM_ID_LENGTH)
match.matchId = i
match.stage = ranking.stage
match.matchTeams = ranking.matchTeams
matches.push(match)
}
function getDefaultStages () {
const stages = [
{
stageName: 'practice',
matchAmount: 1
},
{
stageName: 'ranking',
matchAmount: 3
}
]

return matches
return Promise.all([settings.getSetting('numberOfPracticeRounds'),
settings.getSetting('numberOfRankingRounds')])
.then(data => {
stages.filter(x => x.stageName == 'practice')[0].matchAmount = data[0]
stages.filter(x => x.stageName == 'ranking')[0].matchAmount = data[1]
return stages
})
}

function createRandomId (length) {
Expand Down