diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..639900d
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..d5de91a
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..f48c1e9
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,346 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true
+ DEFINITION_ORDER
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1540985236499
+
+
+ 1540985482306
+
+
+
+
+
+ 1540806299000
+
+ https://jiratinf16b4.it.dh-karlsruhe.de:8443/browse/TURNIERE-63
+
+
+
+ 1540985482338
+
+
+
+
+
+
+
+
+
+
+
+ dfe9df9ddfdcdfc1dff4dfc2dffddf80dfcbdfc6dfe8df89df9bdfe9df93dfc1df93df80dffcdfc0dfd0dfdddf9ddfe2dfdedfc2dfe5df92dff4dfdddf8fdf89dfcfdf99dfc5
+
+
+ malaber
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 1.8
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/models/group.rb b/app/models/group.rb
new file mode 100644
index 0000000..46710b6
--- /dev/null
+++ b/app/models/group.rb
@@ -0,0 +1,4 @@
+class Group < ApplicationRecord
+ belongs_to :matches
+ belongs_to :teams
+end
diff --git a/app/models/group_stage.rb b/app/models/group_stage.rb
new file mode 100644
index 0000000..326fd4e
--- /dev/null
+++ b/app/models/group_stage.rb
@@ -0,0 +1,2 @@
+class GroupStage < ApplicationRecord
+end
diff --git a/app/models/match.rb b/app/models/match.rb
new file mode 100644
index 0000000..ea72ed7
--- /dev/null
+++ b/app/models/match.rb
@@ -0,0 +1,2 @@
+class Match < ApplicationRecord
+end
diff --git a/app/models/playoff_stage.rb b/app/models/playoff_stage.rb
new file mode 100644
index 0000000..24bfaf9
--- /dev/null
+++ b/app/models/playoff_stage.rb
@@ -0,0 +1,2 @@
+class PlayoffStage < ApplicationRecord
+end
diff --git a/app/models/team.rb b/app/models/team.rb
new file mode 100644
index 0000000..48a6c83
--- /dev/null
+++ b/app/models/team.rb
@@ -0,0 +1,2 @@
+class Team < ApplicationRecord
+end
diff --git a/app/services/group_stage.rb b/app/services/group_stage.rb
new file mode 100644
index 0000000..e0b4d5d
--- /dev/null
+++ b/app/services/group_stage.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+module Turniere
+ class GroupStage
+ def self.generate_playoffs(teams, _tournament)
+ stage_count = calculate_required_stage_count(teams.size)
+ generate_matches(teams)
+ end
+
+ def self.calculate_required_stage_count(number_of_teams)
+ if number_of_teams.zero? || number_of_teams == 1
+ 0
+ else
+ Math.log(Turniere::Utils.previous_power_of_two(number_of_teams)) / Math.log(2)
+ end
+ end
+ end
+end
diff --git a/app/services/matches.rb b/app/services/matches.rb
new file mode 100644
index 0000000..39aead7
--- /dev/null
+++ b/app/services/matches.rb
@@ -0,0 +1,33 @@
+module Turniere
+ class Matches
+ def self.generate_matches(teams)
+ if teams.size == 1
+ return #TODO error with only one team
+ end
+ neededGames = 0
+ if (Turniere::Utils.po2?(teams.size())
+ neededGames = teams.size() / 2
+ else
+ neededGames = teams.size() - Turniere::Utils.previous_power_of_two(teams.size()) / 2
+ end
+
+ lastPos = 0
+ matches = []
+ i = 0
+
+ while i < neededGames
+ match = Match(teams[2 * i], teams[( 2 * i ) + 1], 0, 0, :not_startet, i, false)
+ matches.insert match
+ i++
+ end
+
+ lastPos = i + 1
+
+ while teams.size() != 0
+ match = Match(teams[2 * i], teams[( 2 * i ) + 1], 0, 0, Match, i, false)
+ matches.insert match
+ end
+ return lastPos
+ end
+ end
+end
\ No newline at end of file
diff --git a/db/migrate/20181111174658_create_groups.rb b/db/migrate/20181111174658_create_groups.rb
new file mode 100644
index 0000000..f645ff3
--- /dev/null
+++ b/db/migrate/20181111174658_create_groups.rb
@@ -0,0 +1,12 @@
+class CreateGroups < ActiveRecord::Migration[5.2]
+ def change
+ create_table :groups do |t|
+ t.integer :id
+ t.references :matches, foreign_key: true
+ t.references :teams, foreign_key: true
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20181111174759_create_group_stages.rb b/db/migrate/20181111174759_create_group_stages.rb
new file mode 100644
index 0000000..fa3650c
--- /dev/null
+++ b/db/migrate/20181111174759_create_group_stages.rb
@@ -0,0 +1,11 @@
+class CreateGroupStages < ActiveRecord::Migration[5.2]
+ def change
+ create_table :group_stages do |t|
+ t.integer :id
+ t.reference :groups
+ t.integer :playoff_size
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20181111174944_create_matches.rb b/db/migrate/20181111174944_create_matches.rb
new file mode 100644
index 0000000..5e8bbe1
--- /dev/null
+++ b/db/migrate/20181111174944_create_matches.rb
@@ -0,0 +1,16 @@
+class CreateMatches < ActiveRecord::Migration[5.2]
+ def change
+ create_table :matches do |t|
+ t.integer :id
+ t.reference :team_1
+ t.reference :team_2
+ t.integer :score_team_1
+ t.integer :score_team_2
+ t.integer :state
+ t.integer :position
+ t.boolean :is_group_match
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20181111175026_create_playoff_stages.rb b/db/migrate/20181111175026_create_playoff_stages.rb
new file mode 100644
index 0000000..5ca4d03
--- /dev/null
+++ b/db/migrate/20181111175026_create_playoff_stages.rb
@@ -0,0 +1,11 @@
+class CreatePlayoffStages < ActiveRecord::Migration[5.2]
+ def change
+ create_table :playoff_stages do |t|
+ t.integer :id
+ t.integer :level
+ t.reference :matches
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/migrate/20181111175127_create_teams.rb b/db/migrate/20181111175127_create_teams.rb
new file mode 100644
index 0000000..07f1f27
--- /dev/null
+++ b/db/migrate/20181111175127_create_teams.rb
@@ -0,0 +1,13 @@
+class CreateTeams < ActiveRecord::Migration[5.2]
+ def change
+ create_table :teams do |t|
+ t.integer :id
+ t.string :name
+ t.integer :group_score
+ t.integer :group_points_scored
+ t.integer :group_points_recieved
+
+ t.timestamps
+ end
+ end
+end
diff --git a/test/fixtures/group_stages.yml b/test/fixtures/group_stages.yml
new file mode 100644
index 0000000..1264878
--- /dev/null
+++ b/test/fixtures/group_stages.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ id: 1
+ groups:
+ playoff_size: 1
+
+two:
+ id: 1
+ groups:
+ playoff_size: 1
diff --git a/test/fixtures/groups.yml b/test/fixtures/groups.yml
new file mode 100644
index 0000000..5b6bef5
--- /dev/null
+++ b/test/fixtures/groups.yml
@@ -0,0 +1,13 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ id: 1
+ matches: one
+ teams: one
+ name: MyString
+
+two:
+ id: 1
+ matches: two
+ teams: two
+ name: MyString
diff --git a/test/fixtures/matches.yml b/test/fixtures/matches.yml
new file mode 100644
index 0000000..4c29833
--- /dev/null
+++ b/test/fixtures/matches.yml
@@ -0,0 +1,21 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ id: 1
+ team_1:
+ team_2:
+ score_team_1: 1
+ score_team_2: 1
+ state: 1
+ position: 1
+ is_group_match: false
+
+two:
+ id: 1
+ team_1:
+ team_2:
+ score_team_1: 1
+ score_team_2: 1
+ state: 1
+ position: 1
+ is_group_match: false
diff --git a/test/fixtures/playoff_stages.yml b/test/fixtures/playoff_stages.yml
new file mode 100644
index 0000000..207e071
--- /dev/null
+++ b/test/fixtures/playoff_stages.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ id: 1
+ level: 1
+ matches:
+
+two:
+ id: 1
+ level: 1
+ matches:
diff --git a/test/fixtures/teams.yml b/test/fixtures/teams.yml
new file mode 100644
index 0000000..67463c3
--- /dev/null
+++ b/test/fixtures/teams.yml
@@ -0,0 +1,15 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ id: 1
+ name: MyString
+ group_score: 1
+ group_points_scored: 1
+ group_points_recieved: 1
+
+two:
+ id: 1
+ name: MyString
+ group_score: 1
+ group_points_scored: 1
+ group_points_recieved: 1
diff --git a/test/models/group_stage_test.rb b/test/models/group_stage_test.rb
new file mode 100644
index 0000000..d9d6197
--- /dev/null
+++ b/test/models/group_stage_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class GroupStageTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
diff --git a/test/models/group_test.rb b/test/models/group_test.rb
new file mode 100644
index 0000000..778eb0c
--- /dev/null
+++ b/test/models/group_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class GroupTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
diff --git a/test/models/match_test.rb b/test/models/match_test.rb
new file mode 100644
index 0000000..14436b1
--- /dev/null
+++ b/test/models/match_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class MatchTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
diff --git a/test/models/playoff_stage_test.rb b/test/models/playoff_stage_test.rb
new file mode 100644
index 0000000..c1da24e
--- /dev/null
+++ b/test/models/playoff_stage_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class PlayoffStageTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
diff --git a/test/models/team_test.rb b/test/models/team_test.rb
new file mode 100644
index 0000000..8b101cb
--- /dev/null
+++ b/test/models/team_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TeamTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
diff --git a/turniere-backend.iml b/turniere-backend.iml
new file mode 100644
index 0000000..1382e87
--- /dev/null
+++ b/turniere-backend.iml
@@ -0,0 +1,31 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file