Skip to content

Commit

Permalink
Tournament should work, create Sorting class, few edits here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
vokymir committed May 11, 2024
1 parent 0dca7e6 commit 071d003
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 29 deletions.
23 changes: 22 additions & 1 deletion TScripts/Match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Match {
return this.id;
}

getTeams(): [Team, Team] {
return [this.teamL, this.teamR];
}

getPointsToSet(): number {
return this.pointsToSet;
}
Expand Down Expand Up @@ -250,7 +254,7 @@ class Match {
}

/**
* Save
* Save info about the Match in session storage so we can create Log when saving the results.
*/
startOfMatch() {
this.setTimeOfStart();
Expand All @@ -271,6 +275,23 @@ class Match {
BrowserStorage.saveToSessionStorage("previousInfo", previousInfo);
}

/**
* Hard reset everything to 0.
*/
reset() {
this.setPoints(this.teamL, 0);
this.setPoints(this.teamR, 0);
this.teamLsets = 0;
this.teamRsets = 0;
this.teamLpointsTotal = 0;
this.teamRpointsTotal = 0;
this.finished = false;
}

/**
* Create Log and save it to History. [optional] Also ask user to leave for Tournament page.
* @param askToLeave Should the UI asks user to leave the Match page and go to Tournament?
*/
save(askToLeave: boolean = true) {
this.setTimeOfEnd();

Expand Down
12 changes: 12 additions & 0 deletions TScripts/Sorting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Class responsible for any kind of ordering of the matches.
* Two groups of sorting: straightforward and optimized.
* Optimized takes into account
*/
class Sorting {

static roundRobin(teams: Team[]): Match[] {

return []
}
}
184 changes: 156 additions & 28 deletions TScripts/Tournament.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
class Tournament {
private teams: Team[] = [];
private teamIDsNotToInclude: number[] = [];
private matchesPassed: Match[] = [];
private matchesPassedValid: Match[] = [];

Expand All @@ -15,81 +16,208 @@ class Tournament {

constructor() { }

addTeam(teamName: string, team: Team = new Team(this.teams.length + 1, teamName)): void {
/**
* Add a Team to the Tournament. Make sure
* @param teamName Name of the new team (can be edited later), doesn't have to be unique.
* @param team Better not to give if possible. If the team already exists make sure it doesn't have conflicting ID. If no Team given, auto-create new with valid ID. Quite frankly, it could be faster (when having a lot of teams and more importantly removed teams) to give it a Team with valid ID, but not as significantly and it isn't as bullet-proof, as it just doesn't add the team.
* @returns
*/
addTeam(teamName: string, team: Team | undefined = undefined): void {

const teamID = team.getId();
// if equal, it was created automaticaly and definitely is in no conflict
if (teamID != this.teams.length) {
// check if not duplicate
if (team == undefined) {
let chooseID = 1;

// choose valid ID for team
// make sure it is bigger than any teams ID
for (let i = 0; i < this.teams.length; i++) {
const currID = this.teams[i].getId();
if (currID > chooseID) {
chooseID = currID + 1;
}
}

// make sure it is bigger than any removed teams ID
for (let i = 0; i < this.teamIDsNotToInclude.length; i++) {
const currID = this.teamIDsNotToInclude[i];
if (currID > chooseID) {
chooseID = currID + 1;
}
}

team = new Team(chooseID, teamName);
}
else {
const teamID = team.getId();

// check if team not duplicate
for (let i = 0; i < this.teams.length; i++) {
if (teamID == this.teams[i].getId()) {
console.log("Cannot add two teams with the same ID.");
UI.duplicateTeamID(teamID);
return;
}
}
}
// add team

// add team to Teams and reOrder tournament
this.teams.push(team);
this.setOrder();
}


/**
* Remove Team from the tournament, remove matches Team was in from valid passed matches and re-order the tournament.
* Team remain in History and also in memory, as long as any Match with this Team is in passed matches (non-valid).
* @param team Team to remove
*/
removeTeam(team: Team) {
const teamID = team.getId();

for (let i = 0; i < this.teams.length; i++) {
if (teamID == this.teams[i].getId()) {
this.teams.splice(i, 1);
this.validatePassedMatchesAfterRemovingTeam(team);
this.setOrder();
return;
}
}
}

getTeams() {
/**
* Make sure only matches that matter are in Valid Passed Matches array, after REMOVING a TEAM from the tournament.
* @param team Team removed.
*/
validatePassedMatchesAfterRemovingTeam(team: Team) {
const before: Match[] = this.getPassedMatchesValid();
let after: Match[] = [];
const teamID = team.getId();

}
before.forEach(match => {
const teams = match.getTeams();
const teamLID = teams[0].getId();
const teamRID = teams[1].getId();

addPassedMatch(match: Match) {
if (teamID != teamLID && teamID != teamRID) {
after.push(match);
}
});

this.matchesPassedValid = after;
}

getPassedMatches() {
/**
* Make sure only matches that matter are in Valid Passed Matches array, after changing the order of matches.
* Pretty unoptimized, O(n^2), don't run too often.
*/
validatePassedMatches() {
const before = this.matchesPassedValid;
let after = [];
const order = this.order;

before.forEach(match => {
order.forEach(orderedMatch => {
if (orderedMatch.getID() == match.getID()) {
after.push(match);
return;
}
})
})
}

getTeams(): Team[] {
return this.teams;
}

addPassedMatchValid(match: Match) {
addPassedMatch(match: Match) {
this.matchesPassed.push(match);
}

getPassedMatches(): Match[] {
return this.matchesPassed;
}

setPassedMatchesValid(matches: Match[]) {
/**
* Only adding when match ordered and not already saved.
* @param match Match to add to Valid Passed Matches.
*/
addPassedMatchValid(match: Match) {
// control if valid, only than add
const matchID = match.getID();

// check if not already in valid passed matches
this.matchesPassedValid.forEach(passedMatch => {
if (passedMatch.getID() == matchID) {
console.log("Match is already in valid Matches.")
return;
}
});

// add to valid passed matches if in ordered matches
this.getOrder().forEach(orderedMatch => {
if (orderedMatch.getID() == matchID) {
this.matchesPassedValid.push(match);
return;
}
})
}

getPassedMatchesValid() {

getPassedMatchesValid(): Match[] {
return this.matchesPassedValid;
}

setOrderMethod(method: OrderMethod) {

if (this.orderMethod == method) {
return;
}
this.orderMethod = method;
this.setOrder();
}

getOrderMethod() {

getOrderMethod(): OrderMethod {
return this.orderMethod;
}

/**
* Order the matches accordingly to Order Method.
*/
setOrder() {
// take into account the order method
let order;
switch (this.getOrderMethod()) {
case OrderMethod.ROUND_ROBIN:
order = Sorting.roundRobin(this.teams);
break;
}
this.order = order;
this.validatePassedMatches();
}

getOrder() {

getOrder(): Match[] {
return this.order;
}

/**
* If given notwhole number, ceiling.
* @param points Whole number greater than 0.
*/
setPointsToSet(points: number) {

if (points > 0) {
this.pointsToSet = Math.ceil(points);
}
}

getPointsToSet() {

getPointsToSet(): number {
return this.pointsToSet;
}

/**
* If given notwhole number, ceiling.
* @param sets Whole number greater than 0.
*/
setSetsToWin(sets: number) {

if (sets > 0) {
this.setsToWin = Math.ceil(sets);
}
}

getSetsToWin() {

getSetsToWin(): number {
return this.setsToWin;
}
}
9 changes: 9 additions & 0 deletions TScripts/UI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ class UI {
static afterWinMatch(team: Team) {

}

/**
* Warn user about this and try to explain.
* @param id ID which is being added for the second time.
*/
static duplicateTeamID(id: number) {
console.log("Cannot add two teams with the same ID.\nThe invalid id is: " + id);
// pop-up to explain to user
}
}
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
<script src="Scripts/LogEvent.js"></script>
<script src="Scripts/OrderMethod.js"></script>
<script src="Scripts/StorageManager.js"></script>
<script src="Scripts/Sorting.js"></script>

</html>

0 comments on commit 071d003

Please sign in to comment.