func TestGetRoundResult(t *testing.T) {

	db, err := openDB()
	if err != nil {
		log.Fatalln("Open database failed:", err)
	}

	defer db.Close()

	first := steward.RoundResult{ID: -1, TeamID: 10, Round: 1,
		AttackScore: 30, DefenceScore: 40}
	second := steward.RoundResult{ID: -1, TeamID: first.TeamID,
		Round: first.Round + 1, AttackScore: 130, DefenceScore: 140}

	_, err = steward.AddRoundResult(db.db, first)
	if err != nil {
		log.Fatalln("Add round result failed:", err)
	}

	_, err = steward.AddRoundResult(db.db, second)
	if err != nil {
		log.Fatalln("Add round result failed:", err)
	}

	last_res, err := steward.GetLastResult(db.db, second.TeamID)
	if err != nil {
		log.Fatalln("Get last result failed:", err)
	}

	res, err := steward.GetRoundResult(db.db, second.TeamID, second.Round)
	if err != nil {
		log.Fatalln("Get round result failed:", err)
	}

	if res != last_res {
		log.Fatalln("Last result != round result", res, last_res)
	}

	attack_sum := first.AttackScore + second.AttackScore
	defence_sum := first.DefenceScore + second.DefenceScore

	if res.AttackScore != attack_sum {
		log.Fatalln("Invalid attack score value", res.AttackScore,
			attack_sum)
	}

	if res.DefenceScore != defence_sum {
		log.Fatalln("Invalid defence score value", res.DefenceScore,
			defence_sum)
	}
}
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
}
func TestAddRoundResult(t *testing.T) {

	db, err := openDB()
	if err != nil {
		log.Fatalln("Open database failed:", err)
	}

	defer db.Close()

	first := steward.RoundResult{ID: -1, TeamID: 10, Round: 1,
		AttackScore: 30, DefenceScore: 40}
	second := steward.RoundResult{ID: -1, TeamID: first.TeamID,
		Round: first.Round + 1, AttackScore: 130, DefenceScore: 140}

	_, err = steward.AddRoundResult(db.db, first)
	if err != nil {
		log.Fatalln("Add round result failed:", err)
	}

	_, err = steward.AddRoundResult(db.db, second)
	if err != nil {
		log.Fatalln("Add round result failed:", err)
	}
}