forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alliance_team.go
57 lines (49 loc) · 1.8 KB
/
alliance_team.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Model and datastore CRUD methods for an alliance-team association.
package main
type AllianceTeam struct {
Id int
AllianceId int
PickPosition int
TeamId int
}
func (database *Database) CreateAllianceTeam(allianceTeam *AllianceTeam) error {
return database.allianceTeamMap.Insert(allianceTeam)
}
func (database *Database) GetTeamsByAlliance(allianceId int) ([]AllianceTeam, error) {
var allianceTeams []AllianceTeam
err := database.allianceTeamMap.Select(&allianceTeams,
"SELECT * FROM alliance_teams WHERE allianceid = ? ORDER BY pickposition", allianceId)
return allianceTeams, err
}
func (database *Database) SaveAllianceTeam(allianceTeam *AllianceTeam) error {
_, err := database.allianceTeamMap.Update(allianceTeam)
return err
}
func (database *Database) DeleteAllianceTeam(allianceTeam *AllianceTeam) error {
_, err := database.allianceTeamMap.Delete(allianceTeam)
return err
}
func (database *Database) TruncateAllianceTeams() error {
return database.allianceTeamMap.TruncateTables()
}
func (database *Database) GetAllAlliances() ([][]AllianceTeam, error) {
alliances := make([][]AllianceTeam, 0)
var allianceTeams []AllianceTeam
err := database.allianceTeamMap.Select(&allianceTeams,
"SELECT * FROM alliance_teams ORDER BY allianceid, pickposition")
if err == nil {
// Format the sorted list of teams into a two-dimensional slice.
currentAllianceId := -1
for _, allianceTeam := range allianceTeams {
if allianceTeam.AllianceId != currentAllianceId {
currentAllianceId = allianceTeam.AllianceId
alliances = append(alliances, make([]AllianceTeam, 0))
}
alliances[len(alliances)-1] = append(alliances[len(alliances)-1], allianceTeam)
}
}
return alliances, err
}