func TestGetCapturedFlags(t *testing.T) {

	db, err := openDB()

	defer db.Close()

	round := 1
	team_id := 1

	flg1 := steward.Flag{ID: 1, Flag: "f", Round: round, TeamID: team_id,
		ServiceID: 1, Cred: "1:2"}
	flg2 := steward.Flag{ID: 2, Flag: "b", Round: round, TeamID: team_id,
		ServiceID: 1, Cred: "1:2"}

	err = steward.AddFlag(db.db, flg1)
	if err != nil {
		log.Fatalln("Add flag failed:", err)
	}

	err = steward.AddFlag(db.db, flg2)
	if err != nil {
		log.Fatalln("Add flag failed:", err)
	}

	err = steward.CaptureFlag(db.db, flg1.ID, 20)
	err = steward.CaptureFlag(db.db, flg2.ID, 30)

	flags1, err := steward.GetCapturedFlags(db.db, round, 20)
	if err != nil {
		log.Fatalln("Get captured flags failed:", err)
	}

	if len(flags1) != 1 {
		log.Fatalln("Get captured flags more/less than added")
	}

	flags2, err := steward.GetCapturedFlags(db.db, round, 30)
	if err != nil {
		log.Fatalln("Get captured flags failed:", err)
	}

	if len(flags2) != 1 {
		log.Fatalln("Get captured flags more/less than added")
	}

	if flags1[0] != flg1 || flags2[0] != flg2 {
		log.Fatalln("Getted flags invalid", flags1[0], flg1, flags2[0], flg2)
	}
}
Example #2
0
// CountRound count round result
func CountRound(db *sql.DB, round int, teams []steward.Team,
	services []steward.Service) (err error) {

	roundRes := make(map[steward.Team]steward.RoundResult)

	for _, team := range teams {

		res := steward.RoundResult{TeamID: team.ID, Round: round}

		def, err := CountDefenceResult(db, round, team.ID, services)
		if err != nil {
			return err
		}

		res.DefenceScore = def * 2

		roundRes[team] = res
	}

	perService := 1.0 / float64(len(services))

	for _, team := range teams {

		cflags, err := steward.GetCapturedFlags(db, round, team.ID)
		if err != nil {
			return err
		}

		for _, flag := range cflags {

			attackedTeam, err := steward.GetTeam(db, flag.TeamID)
			if err != nil {
				return err
			}

			res := roundRes[attackedTeam]
			res.DefenceScore -= perService
			if res.DefenceScore < 0 {
				res.DefenceScore = 0
			}
			roundRes[attackedTeam] = res

			attackRes := roundRes[team]
			attackRes.AttackScore += perService
			roundRes[team] = attackRes
		}
	}

	for _, res := range roundRes {
		_, err := steward.AddRoundResult(db, res)
		if err != nil {
			return err
		}

	}

	return
}