From ce047f964032d4d1a1e254e6ab734f3e241eafa1 Mon Sep 17 00:00:00 2001 From: CalebRose Date: Mon, 15 Apr 2024 07:25:51 -0700 Subject: [PATCH] djiscord command changes --- controller/DiscordController.go | 26 +++++++++++++++++++- main.go | 2 ++ managers/DiscordManager.go | 24 +++++++++++++++++++ managers/PlayerManager.go | 42 +++++++++++++++++++++------------ managers/RequestsManager.go | 8 ++++--- models/CollegePlayerResponse.go | 6 +++++ repository/SaveCommands.go | 30 +++++++++++++++++++++++ structs/BaseTeam.go | 5 ++++ structs/GameResultsResponse.go | 2 ++ 9 files changed, 126 insertions(+), 19 deletions(-) diff --git a/controller/DiscordController.go b/controller/DiscordController.go index 3dc5e40..3b02bdb 100644 --- a/controller/DiscordController.go +++ b/controller/DiscordController.go @@ -157,7 +157,7 @@ func GetNFLPlayer(w http.ResponseWriter, r *http.Request) { panic("User did not provide a first name") } - player := managers.GetCollegePlayerViaDiscord(id) + player := managers.GetNFLPlayerViaDiscord(id) json.NewEncoder(w).Encode(player) } @@ -232,3 +232,27 @@ func GetNFLGameStreams(w http.ResponseWriter, r *http.Request) { streams := managers.GetNFLPlayByPlayStreamData(timeslot, week) json.NewEncoder(w).Encode(streams) } + +// GetCollegeGamesByConference +func AssignDiscordIDtoCollegeTeam(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + teamID := vars["teamID"] + discordID := vars["discordID"] + if len(teamID) == 0 { + panic("User did not provide conference name") + } + + managers.AssignDiscordIDToCollegeTeam(teamID, discordID) +} + +// GetCollegeGamesByConference +func AssignDiscordIDtoNFLTeam(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + teamID := vars["teamID"] + discordID := vars["discordID"] + if len(teamID) == 0 { + panic("User did not provide conference name") + } + + managers.AssignDiscordIDToNFLTeam(teamID, discordID) +} diff --git a/main.go b/main.go index 0fde0cc..f85bdbb 100644 --- a/main.go +++ b/main.go @@ -273,7 +273,9 @@ func handleRequests() { myRouter.HandleFunc("/teams/ds/college/week/team/{week}/{team}/", controller.GetWeeklyTeamStatsByTeamAbbrAndWeek).Methods("GET") myRouter.HandleFunc("/teams/ds/college/season/team/{season}/{team}/", controller.GetSeasonTeamStatsByTeamAbbrAndSeason).Methods("GET") myRouter.HandleFunc("/ds/cfb/player/{id}", controller.GetCollegePlayer).Methods("GET") + myRouter.HandleFunc("/ds/cfb/assign/discord/{teamID}/{discordID}", controller.AssignDiscordIDtoCollegeTeam).Methods("GET") myRouter.HandleFunc("/ds/nfl/player/{id}", controller.GetNFLPlayer).Methods("GET") + myRouter.HandleFunc("/ds/nfl/assign/discord/{teamID}/{discordID}", controller.AssignDiscordIDtoNFLTeam).Methods("GET") myRouter.HandleFunc("/ds/cfb/croots/class/{teamID}/", controller.GetRecruitingClassByTeamID).Methods("GET") myRouter.HandleFunc("/ds/cfb/croot/{id}", controller.GetRecruitViaDiscord).Methods("GET") myRouter.HandleFunc("/schedule/ds/current/week/{league}/", controller.GetCurrentWeekGamesByLeague).Methods("GET") diff --git a/managers/DiscordManager.go b/managers/DiscordManager.go index 4716cbc..80d86bd 100644 --- a/managers/DiscordManager.go +++ b/managers/DiscordManager.go @@ -4,6 +4,8 @@ import ( "strconv" "sync" + "github.com/CalebRose/SimFBA/dbprovider" + "github.com/CalebRose/SimFBA/repository" "github.com/CalebRose/SimFBA/structs" "github.com/CalebRose/SimFBA/util" ) @@ -304,10 +306,12 @@ func GetCFBPlayByPlayStreamData(timeslot, week string, isFBS bool) []structs.Str HomeTeamCoach: game.HomeTeamCoach, HomeTeamRank: game.HomeTeamRank, HomeLabel: homeTeam.TeamName + " " + homeTeam.Mascot, + HomeTeamDiscordID: homeTeam.DiscordID, AwayTeamID: uint(game.AwayTeamID), AwayTeam: game.AwayTeam, AwayTeamCoach: game.AwayTeam, AwayTeamRank: game.AwayTeamRank, + AwayTeamDiscordID: awayTeam.DiscordID, AwayLabel: awayTeam.TeamName + " " + awayTeam.Mascot, HomeOffensiveScheme: homeGameplan.OffensiveScheme, HomeDefensiveScheme: homeGameplan.DefensiveScheme, @@ -423,3 +427,23 @@ func GetNFLPlayByPlayStreamData(timeslot, week string) []structs.StreamResponse return streams } + +func AssignDiscordIDToCollegeTeam(tID, dID string) { + db := dbprovider.GetInstance().GetDB() + + team := GetTeamByTeamID(tID) + + team.AssignDiscordID(dID) + + repository.SaveCollegeTeamRecord(team, db) +} + +func AssignDiscordIDToNFLTeam(tID, dID string) { + db := dbprovider.GetInstance().GetDB() + + team := GetNFLTeamByTeamID(tID) + + team.AssignDiscordID(dID) + + repository.SaveNFLTeamRecord(team, db) +} diff --git a/managers/PlayerManager.go b/managers/PlayerManager.go index a3a526f..555afc4 100644 --- a/managers/PlayerManager.go +++ b/managers/PlayerManager.go @@ -110,16 +110,38 @@ func GetCollegePlayerByCollegePlayerId(CollegePlayerId string) structs.CollegePl return CollegePlayer } -func GetCollegePlayerViaDiscord(id string) models.CollegePlayerCSV { +func GetCollegePlayerViaDiscord(id string) models.DiscordPlayerResponse { db := dbprovider.GetInstance().GetDB() + ts := GetTimestamp() - var CollegePlayer structs.CollegePlayer + seasonID := strconv.Itoa(ts.CollegeSeasonID) + var collegePlayer structs.CollegePlayer - db.Where("id = ?", id).Find(&CollegePlayer) + db.Preload("SeasonStats", "season_id = ?", seasonID).Where("id = ?", id).Find(&collegePlayer) - collegePlayerResponse := models.MapPlayerToCSVModel(CollegePlayer) + collegePlayerResponse := models.MapPlayerToCSVModel(collegePlayer) - return collegePlayerResponse + return models.DiscordPlayerResponse{ + Player: collegePlayerResponse, + CollegeStats: collegePlayer.SeasonStats, + } +} + +func GetNFLPlayerViaDiscord(id string) models.DiscordPlayerResponse { + db := dbprovider.GetInstance().GetDB() + ts := GetTimestamp() + + seasonID := strconv.Itoa(ts.NFLSeasonID) + var nflPlayer structs.NFLPlayer + + db.Preload("SeasonStats", "season_id = ?", seasonID).Where("id = ?", id).Find(&nflPlayer) + + nflPlayerResponse := models.MapNFLPlayerToCSVModel(nflPlayer) + + return models.DiscordPlayerResponse{ + Player: nflPlayerResponse, + NFLStats: nflPlayer.SeasonStats, + } } func GetCollegePlayerByIdAndWeek(id, week string) models.CollegePlayerCSV { @@ -188,16 +210,6 @@ func SetRedshirtStatusForPlayer(playerId string) structs.CollegePlayer { return player } -func GetNFLPlayerViaDiscord(id string) structs.NFLPlayer { - db := dbprovider.GetInstance().GetDB() - - var nflPlayer structs.NFLPlayer - - db.Where("id = ?", id).Find(&nflPlayer) - - return nflPlayer -} - func GetAllNFLDraftees() []models.NFLDraftee { db := dbprovider.GetInstance().GetDB() diff --git a/managers/RequestsManager.go b/managers/RequestsManager.go index 962cdd8..50a9952 100644 --- a/managers/RequestsManager.go +++ b/managers/RequestsManager.go @@ -6,6 +6,7 @@ import ( "strconv" "github.com/CalebRose/SimFBA/dbprovider" + "github.com/CalebRose/SimFBA/repository" "github.com/CalebRose/SimFBA/structs" ) @@ -226,8 +227,9 @@ func RemoveUserFromTeam(teamId string) { coach.SetAsInactive() team.RemoveUserFromTeam() + team.AssignDiscordID("") - db.Save(&team) + repository.SaveCollegeTeamRecord(team, db) db.Save(&coach) @@ -238,7 +240,7 @@ func RemoveUserFromTeam(teamId string) { for _, game := range seasonalGames { if game.Week >= timestamp.CollegeWeek { game.UpdateCoach(int(team.ID), "AI") - db.Save(&game) + repository.SaveCFBGameRecord(game, db) } } @@ -253,7 +255,7 @@ func RemoveUserFromTeam(teamId string) { recruitingProfile.ActivateAI() } - db.Save(&recruitingProfile) + repository.SaveRecruitingTeamProfile(recruitingProfile, db) newsLog := structs.NewsLog{ TeamID: 0, diff --git a/models/CollegePlayerResponse.go b/models/CollegePlayerResponse.go index 3ad324b..63310f5 100644 --- a/models/CollegePlayerResponse.go +++ b/models/CollegePlayerResponse.go @@ -32,3 +32,9 @@ type NFLPlayerResponse struct { Stats structs.NFLPlayerStats SeasonStats structs.NFLPlayerSeasonStats } + +type DiscordPlayerResponse struct { + Player CollegePlayerCSV + CollegeStats structs.CollegePlayerSeasonStats + NFLStats structs.NFLPlayerSeasonStats +} diff --git a/repository/SaveCommands.go b/repository/SaveCommands.go index dbfc5c6..4a5e68e 100644 --- a/repository/SaveCommands.go +++ b/repository/SaveCommands.go @@ -86,3 +86,33 @@ func SaveRecruitProfile(profile structs.RecruitPlayerProfile, db *gorm.DB) { log.Panicln("Could not save team profile") } } + +func SaveCollegeTeamRecord(team structs.CollegeTeam, db *gorm.DB) { + team.CollegeCoach = structs.CollegeCoach{} + team.RecruitingProfile = structs.RecruitingTeamProfile{} + team.TeamStats = nil + team.TeamStandings = nil + team.TeamRecord = structs.CollegeTeamRecords{} + team.TeamGameplan = structs.CollegeGameplan{} + team.TeamDepthChart = structs.CollegeTeamDepthChart{} + team.TeamSeasonStats = structs.CollegeTeamSeasonStats{} + err := db.Save(&team).Error + if err != nil { + log.Panicln("Could not save team profile") + } +} + +func SaveNFLTeamRecord(team structs.NFLTeam, db *gorm.DB) { + team.Capsheet = structs.NFLCapsheet{} + team.Contracts = nil + team.TeamStats = nil + team.Standings = nil + team.DraftPicks = nil + team.TeamGameplan = structs.NFLGameplan{} + team.TeamDepthChart = structs.NFLDepthChart{} + team.TeamSeasonStats = nil + err := db.Save(&team).Error + if err != nil { + log.Panicln("Could not save team profile") + } +} diff --git a/structs/BaseTeam.go b/structs/BaseTeam.go index 953c670..0cf076a 100644 --- a/structs/BaseTeam.go +++ b/structs/BaseTeam.go @@ -17,6 +17,7 @@ type BaseTeam struct { ColorOne string ColorTwo string ColorThree string + DiscordID string } func (bt *BaseTeam) RemoveUserFromTeam() { @@ -26,3 +27,7 @@ func (bt *BaseTeam) RemoveUserFromTeam() { func (bt *BaseTeam) AssignUserToTeam(user string) { bt.Coach = user } + +func (bt *BaseTeam) AssignDiscordID(id string) { + bt.DiscordID = id +} diff --git a/structs/GameResultsResponse.go b/structs/GameResultsResponse.go index 7f75c0b..4b2c1e4 100644 --- a/structs/GameResultsResponse.go +++ b/structs/GameResultsResponse.go @@ -78,6 +78,7 @@ type StreamResponse struct { HomeLabel string HomeTeam string HomeTeamCoach string + HomeTeamDiscordID string HomeTeamRank uint HomeOffensiveScheme string HomeDefensiveScheme string @@ -85,6 +86,7 @@ type StreamResponse struct { AwayLabel string AwayTeam string AwayTeamCoach string + AwayTeamDiscordID string AwayTeamRank uint AwayOffensiveScheme string AwayDefensiveScheme string