package fic import ( "time" ) // UnlockedChallenges disables dependancy requirement between challenges. var UnlockedChallenges bool // Team represents a group of players, come to solve our challenges. type Team struct { Id int64 `json:"id"` Name string `json:"name"` Color uint32 `json:"color"` } // GetTeams returns a list of registered Team from the database. func GetTeams() ([]Team, error) { if rows, err := DBQuery("SELECT id_team, name, color FROM teams"); err != nil { return nil, err } else { defer rows.Close() var teams = make([]Team, 0) for rows.Next() { var t Team if err := rows.Scan(&t.Id, &t.Name, &t.Color); err != nil { return nil, err } teams = append(teams, t) } if err := rows.Err(); err != nil { return nil, err } return teams, nil } } // GetTeam retrieves a Team from its identifier. func GetTeam(id int64) (Team, error) { var t Team if err := DBQueryRow("SELECT id_team, name, color FROM teams WHERE id_team = ?", id).Scan(&t.Id, &t.Name, &t.Color); err != nil { return t, err } return t, nil } // GetTeamBySerial retrieves a Team from one of its associated certificates. func GetTeamBySerial(serial int64) (Team, error) { var t Team if err := DBQueryRow("SELECT T.id_team, T.name, T.color FROM certificates C INNER JOIN teams T ON T.id_team = C.id_team WHERE id_cert = ?", serial).Scan(&t.Id, &t.Name, &t.Color); err != nil { return t, err } return t, nil } // CreateTeam creates and fills a new struct Team and registers it into the database. func CreateTeam(name string, color uint32) (Team, error) { if res, err := DBExec("INSERT INTO teams (name, color) VALUES (?, ?)", name, color); err != nil { return Team{}, err } else if tid, err := res.LastInsertId(); err != nil { return Team{}, err } else { return Team{tid, name, color}, nil } } // Update applies modifications back to the database. func (t Team) Update() (int64, error) { if res, err := DBExec("UPDATE teams SET name = ?, color = ? WHERE id_team = ?", t.Name, t.Color, t.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err } else { return nb, err } } // Delete the challenge from the database func (t Team) Delete() (int64, error) { if _, err := DBExec("DELETE FROM team_members WHERE id_team = ?", t.Id); err != nil { return 0, err } else if res, err := DBExec("DELETE FROM teams WHERE id_team = ?", t.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err } else { return nb, err } } // HasAccess checks if the Team has access to the given challenge. func (t Team) HasAccess(e Exercice) bool { if e.Depend == nil || UnlockedChallenges { return true } else { ed := Exercice{} ed.Id = *e.Depend s, _ := t.HasSolved(ed) return s } } // NbTry retrieves the number of attempts made by the Team to the given challenge. func NbTry(t *Team, e Exercice) int { var cnt *int if t != nil { DBQueryRow("SELECT COUNT(*) FROM exercice_tries WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&cnt) } else { DBQueryRow("SELECT COUNT(*) FROM exercice_tries WHERE id_exercice = ?", e.Id).Scan(&cnt) } if cnt == nil { return 0 } else { return *cnt } } // HasHint checks if the Team has revealed the given Hint. func (t Team) HasHint(h EHint) (bool) { var tm *time.Time DBQueryRow("SELECT MIN(time) FROM team_hints WHERE id_team = ? AND id_hint = ?", t.Id, h.Id).Scan(&tm) return tm != nil } // OpenHint registers to the database that the Team has now revealed. func (t Team) OpenHint(h EHint) (error) { _, err := DBExec("INSERT INTO team_hints (id_team, id_hint, time) VALUES (?, ?, ?)", t.Id, h.Id, time.Now()) return err } // CountTries gets the amount of attempts made by the Team and retrieves the time of the latest attempt. func (t Team) CountTries(e Exercice) (int64, time.Time) { var nb *int64 var tm *time.Time if DBQueryRow("SELECT COUNT(id_exercice), MAX(time) FROM exercice_tries WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&nb, &tm); tm == nil { return 0, time.Unix(0, 0) } else if nb == nil { return 0, *tm } else { return *nb, *tm } } // LastTryDist retrieves the distance to the correct answers, for the given challenge. // The distance is the number of bad responses given in differents MCQs. func (t Team) LastTryDist(e Exercice) int64 { var nb *int64 if DBQueryRow("SELECT nbdiff FROM exercice_tries WHERE id_team = ? AND id_exercice = ? ORDER BY time DESC LIMIT 1", t.Id, e.Id).Scan(&nb); nb == nil { return 0 } else { return *nb } } // HasSolved checks if the Team already has validated the given challenge. // Note that the function also returns the effective validation timestamp. func (t Team) HasSolved(e Exercice) (bool, time.Time) { var tm *time.Time if DBQueryRow("SELECT MIN(time) FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&tm); tm == nil { return false, time.Unix(0, 0) } else { return true, *tm } } // GetSolvedRank returns the number of teams that solved the challenge before the Team. func (t Team) GetSolvedRank(e Exercice) (nb int64, err error) { if rows, errr := DBQuery("SELECT id_team FROM exercice_solved WHERE id_exercice = ? ORDER BY time ASC", e.Id); err != nil { return nb, errr } else { for rows.Next() { var tid int64 if err = rows.Scan(&tid); err != nil { return } nb += 1 if t.Id == tid { break } } return } } // HasPartiallySolved checks if the Team already has unlocked the given key and returns the validation's timestamp. func (t Team) HasPartiallySolved(k Key) (tm *time.Time) { DBQueryRow("SELECT MIN(time) FROM key_found WHERE id_team = ? AND id_key = ?", t.Id, k.Id).Scan(&tm) return } // HasPartiallyRespond checks if the Team already has unlocked the given MCQ and returns the validation's timestamp. func (t Team) HasPartiallyRespond(m MCQ) (tm *time.Time) { DBQueryRow("SELECT MIN(time) FROM mcq_found WHERE id_team = ? AND id_mcq = ?", t.Id, m.Id).Scan(&tm) return }