Esempio n. 1
0
// Handles updating a game
func updateGame(w http.ResponseWriter, r *http.Request, seasonId string, weekNumber int, gameIndex int) {
	c := appengine.NewContext(r)
	winnerName := r.FormValue("winnerName")
	player1Name := r.FormValue("player1Name")
	player2Name := r.FormValue("player2Name")
	season := api.LoadSeasonById(c, seasonId)
	var weeks []model.Week
	err := json.Unmarshal(season.Schedule, &weeks)
	if err != nil {
		panic(err)
	}
	game := &(weeks[weekNumber-1].Games[gameIndex])
	game.WinnerId = winnerName
	game.PlayerIds[0] = player1Name
	game.PlayerIds[1] = player2Name
	c.Infof("Updating game %v, %v: %v", weekNumber, gameIndex, weeks)
	newSchedule, err := json.Marshal(weeks)
	if err != nil {
		panic(err)
	}
	season.Schedule = newSchedule
	err = model.SaveSeason(c, *season)
	if err != nil {
		panic(err)
	}
}
Esempio n. 2
0
func addGame(w http.ResponseWriter, r *http.Request, seasonId string, weekNumber int) {
	c := appengine.NewContext(r)
	winnerName := r.FormValue("winnerName")
	player1Name := r.FormValue("player1Name")
	player2Name := r.FormValue("player2Name")
	season := api.LoadSeasonById(c, seasonId)
	var weeks []model.Week
	err := json.Unmarshal(season.Schedule, &weeks)
	if err != nil {
		panic(err)
	}
	playerIds := make([]string, 2)
	playerIds[0] = player1Name
	playerIds[1] = player2Name
	newGame := model.Game{
		PlayerIds: playerIds,
		WinnerId:  winnerName,
	}
	weeks[weekNumber-1].Games = append(weeks[weekNumber-1].Games, newGame)
	newSchedule, err := json.Marshal(weeks)
	if err != nil {
		panic(err)
	}
	season.Schedule = newSchedule
	model.SaveSeason(c, *season)
}
Esempio n. 3
0
// handles adding a week
func addWeek(w http.ResponseWriter, r *http.Request, seasonId string) {
	c := appengine.NewContext(r)
	season := api.LoadSeasonById(c, seasonId)

	playDate := r.FormValue("playDate")
	var scenarios []int

	if r.FormValue("scenarios") != "" {
		scenarioStrings := strings.Split(r.FormValue("scenarios"), ",")

		scenarios = make([]int, len(scenarioStrings))
		for i, v := range scenarioStrings {
			scenario, err := strconv.Atoi(v)
			if err != nil {
				panic(err)
			}
			scenarios[i] = scenario
		}
	}

	var weeks []model.Week
	err := json.Unmarshal(season.Schedule, &weeks)
	if err != nil {
		panic(err)
	}

	var playDateTime *time.Time
	if playDate == "" {
		playDateTime = nil
	} else {
		playDateTimeP, err := time.Parse("2006-01-02", playDate)
		playDateTime = &playDateTimeP
		if err != nil {
			panic(err)
		}
	}

	newWeek := model.Week{
		PlayDate:  playDateTime,
		Scenarios: scenarios,
		Games:     make([]model.Game, 0),
	}
	weeks = append(weeks, newWeek)
	newData, err := json.Marshal(weeks)
	if err != nil {
		panic(err)
	}
	season.Schedule = newData
	model.SaveSeason(c, *season)
}
Esempio n. 4
0
func deleteWeek(w http.ResponseWriter, r *http.Request, seasonId string, weekNumber int) {
	c := appengine.NewContext(r)
	season := api.LoadSeasonById(c, seasonId)
	var weeks []model.Week
	err := json.Unmarshal(season.Schedule, &weeks)
	if err != nil {
		panic(err)
	}
	weekIndex := weekNumber - 1
	weeks = append(weeks[:weekIndex], weeks[weekIndex+1:]...)
	newSchedule, err := json.Marshal(weeks)
	if err != nil {
		panic(err)
	}
	season.Schedule = newSchedule
	model.SaveSeason(c, *season)
}
Esempio n. 5
0
// handles updating the scenarios and playdates for a week
func updateWeek(w http.ResponseWriter, r *http.Request, seasonId string, weekNumber int) {
	c := appengine.NewContext(r)
	season := api.LoadSeasonById(c, seasonId)

	playDate := r.FormValue("playDate")
	var scenarios []int

	if r.FormValue("scenarios") != "" {
		scenarioStrings := strings.Split(r.FormValue("scenarios"), ",")

		scenarios = make([]int, len(scenarioStrings))
		for i, v := range scenarioStrings {
			scenario, err := strconv.Atoi(v)
			if err != nil {
				panic(err)
			}
			scenarios[i] = scenario
		}
	}

	var weeks []model.Week
	err := json.Unmarshal(season.Schedule, &weeks)
	if err != nil {
		panic(err)
	}

	var playDateTime *time.Time
	if playDate == "" {
		playDateTime = nil
	} else {
		playDateTimeP, err := time.Parse("2006-01-02 -0700 MST", playDate+" -0700 MST")
		playDateTime = &playDateTimeP
		if err != nil {
			panic(err)
		}
	}
	weeks[weekNumber-1].PlayDate = playDateTime
	weeks[weekNumber-1].Scenarios = scenarios
	newData, err := json.Marshal(weeks)
	if err != nil {
		panic(err)
	}
	season.Schedule = newData
	model.SaveSeason(c, *season)
}