// 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) } }
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) }
func SetPlayerName(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) seasonName := r.FormValue("SeasonName") seasonYear := r.FormValue("SeasonYear") playerId := r.FormValue("PlayerId") newPlayerName := r.FormValue("NewPlayerName") c.Infof("Replaceing '%v' with '%v' for season '%v' '%v'", playerId, newPlayerName, seasonName, seasonYear) var season *model.Season if seasonName == "" || seasonYear == "" { c.Infof("Lookup season") tmpSeason := api.GetActiveSeasonWithContext(c) season = &tmpSeason } else { season = api.LoadSeasonByNameYear(c, seasonName, seasonYear) } players := season.GetPlayers(c) targetIndex := playerIndex(players, playerId) replacePlayer := players[targetIndex] replacePlayer.Name = newPlayerName season.Schedule = []byte(strings.Replace(string(season.Schedule), playerId, newPlayerName, -1)) season.Conferences = []byte(strings.Replace(string(season.Conferences), playerId, newPlayerName, -1)) pkey := model.PlayerKey(c, season.Name, season.Year, newPlayerName) season.Players[targetIndex] = pkey err := datastore.RunInTransaction(c, func(c appengine.Context) error { model.SavePlayer(c, season, replacePlayer) err := model.SaveSeason(c, *season) return err }, nil) if err != nil { panic(err) } }
// Handles updating a game by a non-admin user 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 := LoadSeasonById(c, seasonId) var weeks []model.Week err := json.Unmarshal(season.Schedule, &weeks) if err != nil { panic(err) } game := &(weeks[weekNumber-1].Games[gameIndex]) if len(game.WinnerId) == 0 { if len(winnerName) != 0 { now := time.Now() deadline := now.Add(time.Duration(4*24) * time.Hour) game.DisputeDeadline = deadline.Unix() } } 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) } }
func updateGameDispute(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") isDisputed, err := strconv.ParseBool(r.FormValue("isDisputed")) if err != nil { panic(err) } season := LoadSeasonById(c, seasonId) const emailMessage = ` A dispute was submitted for the game between %s and %s. Winner: %s ` const emailSubject = ` "%s -- Automated Dispute Notification" ` var emailList []string emailList = []string{"dungeongod" + "@" + "gmail" + ".com"} msg := &mail.Message{ Sender: "", To: emailList, Subject: fmt.Sprintf(emailSubject, season.Name), Body: fmt.Sprintf(emailMessage, player1Name, player2Name, winnerName), } if err := mail.Send(c, msg); err != nil { c.Errorf("Couldn't send email: %v", err) } var weeks []model.Week err2 := json.Unmarshal(season.Schedule, &weeks) if err2 != nil { panic(err2) } game := &(weeks[weekNumber-1].Games[gameIndex]) game.IsDisputed = isDisputed c.Infof("Disputing 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) } }
// 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) }
func createSeason(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) decoder := json.NewDecoder(r.Body) var s model.Season err := decoder.Decode(&s) if err != nil { log.Printf("Unexpected error decoding json data: %v", err) panic(err) } err = model.SaveSeason(c, s) if err != nil { log.Printf("Unexpected error saving season: %v", err) panic(err) } }
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) }
// 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) }