Skip to content

Commit

Permalink
restructuration + rewrite TreeBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
IceMaD committed Feb 18, 2016
1 parent 7d84b55 commit 0e53f95
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 101 deletions.
6 changes: 2 additions & 4 deletions app/ts/Components/TournamentAppComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {ROUTER_DIRECTIVES, RouteConfig} from "angular2/router";
import {TeamHolderService} from "../Services/TeamHolderService";
import {FlashService} from "../Services/FlashService";
import {FlashMessageModel} from "../Models/FlashMessageModel";
import {HomepageView} from "../Views/HomepageView";
import {TeamManagementView} from "../Views/TeamManagementView";
import {TreeView} from "../Views/TreeView";

Expand All @@ -16,10 +15,9 @@ import {TreeView} from "../Views/TreeView";

@RouteConfig([

{path: '/', name: 'Homepage', component: HomepageView, useAsDefault: true},
{path: '/team-management', name: 'TeamManagement', component: TeamManagementView},
{path: '/', name: 'TeamManagement', component: TeamManagementView, useAsDefault: true},
{path: '/tree', name: 'TreeComponent', component: TreeView},
{path: '/*path', redirectTo:['Homepage']},
{path: '/*path', redirectTo:['TeamManagement']},

])

Expand Down
30 changes: 11 additions & 19 deletions app/ts/Models/NodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ export class NodeModel {
public last: boolean = false;
public status: string;
public highlighted: boolean;
public name: string;
public children: NodeModel[]= [];
public team: TeamModel;

constructor(
public name: string,
public children?: NodeModel[],
public team?: TeamModel
) {
constructor() {
this._id = IdService.getUniqueId();
}

Expand All @@ -29,23 +28,16 @@ export class NodeModel {
TeamHolderService.win(this);
}

traverseToParentOf(node: NodeModel): NodeModel|boolean {

for (let i = 0; i < this.children.length; i++) {
let child:NodeModel = this.children[i];

if (child.id === node.id) {
return this;
}
setTeam(team: TeamModel): this {
this.team = team;

let matchInChild = child.traverseToParentOf(node);
return this;
}

if (matchInChild) {
return matchInChild;
}
}
addChild(node: NodeModel): this {
this.children.push(node);

return false;
return this;
}

allChildrenHaveTeam():boolean {
Expand Down
76 changes: 12 additions & 64 deletions app/ts/Services/TeamHolderService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {TeamModel} from "../Models/TeamModel";
import {NodeModel} from "../Models/NodeModel";
import {TreeManager} from "./TreeManager";

export class TeamHolderService {

Expand All @@ -15,69 +16,18 @@ export class TeamHolderService {
new TeamModel('Monkeys'),
];

public static tree: NodeModel;
private static _nodes: NodeModel[];
private static _tree: NodeModel;

static get teams():TeamModel[] {
return this._teamList;
}

/**
* Build a tree
*
* @returns {NodeModel}
*/
static buildTree(): NodeModel {

let turnCount: number = Math.ceil(Math.log(this._teamList.length)/Math.log(2));
let nodes: { [turn: string]: { [group: string]: NodeModel } } = {t1:{}};
this._nodes = [];

/* Generate First Turn */
for (let i:number = 0; i < this._teamList.length; i = i+2) {
let turn = 't'+1;
let group = 'g'+(i/2+1);

let nodeTeam1 = new NodeModel('team ' + i, [], this._teamList[i]);
let nodeTeam2 = new NodeModel('team ' + (i+1), [], this._teamList[i+1]);

let node: NodeModel = new NodeModel(turn + group, [nodeTeam1, nodeTeam2]);
this._nodes.push(node);
this._nodes.push(nodeTeam1);
this._nodes.push(nodeTeam2);
nodes[turn][group] = node;
static get tree():NodeModel {
if (!(this._tree instanceof NodeModel)) {
this._tree = TreeManager.buildTree(this._teamList);
}

/* Generate Other turns */
for (let t:number = 2; t < turnCount+1; t++) {

nodes['t'+t] = {};

/* Group count for turn t */
let groupCount = Math.pow(2, turnCount - t);

for (let g:number = 1; g <= groupCount; g++) {

/* Store Previous turn */
let prevTurn = nodes['t'+(t-1)];
let turn = 't'+t;
let group = 'g'+g;

let node: NodeModel = new NodeModel(turn + group, [
prevTurn['g'+(g*2-1)],
prevTurn['g'+(g*2)],
]);

this._nodes.push(node);
nodes[turn][group] = node;
}
}

this.tree = nodes['t' + turnCount]['g1'];
this.tree.last = true;

this._nodes.push(this.tree);
return this.tree;
return this._tree;
}

static addTeam(name: string): TeamHolderService {
Expand Down Expand Up @@ -108,7 +58,7 @@ export class TeamHolderService {
}

static win(node: NodeModel): boolean {
let wonPlace: NodeModel|boolean = this.tree.traverseToParentOf(node);
let wonPlace: NodeModel|boolean = TreeManager.findParentOf(node);

if (wonPlace instanceof NodeModel && !wonPlace.team && wonPlace.allChildrenHaveTeam()) {
wonPlace.setWinTeam(node.team);
Expand All @@ -120,18 +70,16 @@ export class TeamHolderService {
}

static highlight(team: TeamModel):void {
for (let i:number = 0; i < this._nodes.length; i++) {
let node:NodeModel = this._nodes[i];

TreeManager.forEachNode(function(node: NodeModel) {
if (node.team && node.team.id === team.id) {
node.highlighted = true;
}
}
})
}

static unHighlight():void {
for (let i:number = 0; i < this._nodes.length; i++) {
this._nodes[i].highlighted = false;
}
TreeManager.forEachNode(function(node: NodeModel) {
node.highlighted = false;
})
}
}
64 changes: 64 additions & 0 deletions app/ts/Services/TreeManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {NodeModel} from "../Models/NodeModel";
import {TeamModel} from "../Models/TeamModel";

export class TreeManager {

private static _nodes: NodeModel[] = [];

static buildTree(teamList: TeamModel[]): NodeModel {

let countOf = {
teams: teamList.length,
nodes: (teamList.length * 2) - 1,
};

let nodes: NodeModel[] = [];

for (let i:number = 0; i < countOf.nodes; i++) {
let node = new NodeModel();
nodes.push(node);

if (nodes.length > 1) {
let positionOfParent: number = Math.floor(nodes.length/2) - 1;
let parent: NodeModel = nodes[positionOfParent];
parent.addChild(node);
}

if (countOf.nodes - i <= countOf.teams) {
node.setTeam(teamList[i+1 - countOf.teams]);
}
}

this._nodes = this._nodes.concat(nodes);

return nodes[0];
}

static findParentOf(nodeTofind: NodeModel): NodeModel|boolean {

// For each node
for (let i:number = 0; i < this._nodes.length; i++) {
let node = this._nodes[i];

// For each children of the node
for (let j = 0; j < node.children.length; j++) {
let child:NodeModel = node.children[j];

// If the child match, return the parent
if (child.id === nodeTofind.id) {
return node;
}
}
}

return false;
}

static forEachNode(callback: Function): void {
for (let i:number = 0; i < this._nodes.length; i++) {
let node: NodeModel = TreeManager._nodes[i];

callback(node);
}
}
}
12 changes: 0 additions & 12 deletions app/ts/Views/HomepageView.ts

This file was deleted.

4 changes: 2 additions & 2 deletions app/ts/Views/TreeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export class TreeView {
ngOnInit() {
if (!TeamHolderService.isFilled()) {
this._flashService.push('Vous devez renseigner 2, 4, 8, 16 ou 32 équipes');
this._router.navigate(['TeamManagement'])
this._router.navigate(['TeamManagement']);

return;
}

this.teams = TeamHolderService.teams;
this.tree = TeamHolderService.buildTree();
this.tree = TeamHolderService.tree;
}
}

0 comments on commit 0e53f95

Please sign in to comment.