Ejemplo n.º 1
0
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)
	}
}
Ejemplo n.º 2
0
func collectTeamResult(db *sql.DB, team steward.Team,
	services []steward.Service) (tr TeamResult, err error) {

	tr.ID = team.ID
	tr.Name = team.Name

	rr, err := steward.GetLastResult(db, team.ID)
	if err != nil {
		// At game start, no result exist
		rr = steward.RoundResult{AttackScore: 0, DefenceScore: 0}
	}

	tr.Attack = rr.AttackScore
	tr.Defence = rr.DefenceScore

	advisory, err := steward.GetAdvisoryScore(db, team.ID)
	if err != nil {
		tr.Advisory = 0
	} else {
		tr.Advisory = advisory
	}

	round, err := steward.CurrentRound(db)
	if err != nil {
		// At game start, no round exist
		return tr, nil
	}

	for _, svc := range services {
		s := steward.Status{round.ID, team.ID, svc.ID, -1}
		state, err := steward.GetState(db, s)
		if err != nil {
			// Try to get status from previous round
			s.Round--
			state, err = steward.GetState(db, s)
			if err != nil {
				state = steward.StatusDown
			}
		}

		tr.Status = append(tr.Status, state)
	}

	return
}