Feat: player stats [WIP]
This commit is contained in:
		
							
								
								
									
										42
									
								
								repos/player_stats.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								repos/player_stats.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| package repos | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"gralias/models" | ||||
|  | ||||
| 	"github.com/jmoiron/sqlx" | ||||
| ) | ||||
|  | ||||
| type PlayerStatsRepo interface { | ||||
| 	GetPlayerStats(ctx context.Context, username string) (*models.PlayerStats, error) | ||||
| 	UpdatePlayerStats(ctx context.Context, stats *models.PlayerStats) error | ||||
| 	CreatePlayerStats(ctx context.Context, username string) error | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) GetPlayerStats(ctx context.Context, username string) (*models.PlayerStats, error) { | ||||
| 	stats := &models.PlayerStats{} | ||||
| 	err := sqlx.GetContext(ctx, p.DB, stats, "SELECT * FROM player_stats WHERE player_username = ?", username) | ||||
| 	return stats, err | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) UpdatePlayerStats(ctx context.Context, stats *models.PlayerStats) error { | ||||
| 	_, err := p.DB.NamedExecContext(ctx, `UPDATE player_stats SET | ||||
| 		games_played = :games_played, | ||||
| 		games_won = :games_won, | ||||
| 		games_lost = :games_lost, | ||||
| 		opened_opposite_words = :opened_opposite_words, | ||||
| 		opened_white_words = :opened_white_words, | ||||
| 		opened_black_words = :opened_black_words, | ||||
| 		mime_winrate = :mime_winrate, | ||||
| 		guesser_winrate = :guesser_winrate, | ||||
| 		played_as_mime = :played_as_mime, | ||||
| 		played_as_guesser = :played_as_guesser | ||||
| 	WHERE player_username = :player_username`, stats) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) CreatePlayerStats(ctx context.Context, username string) error { | ||||
| 	db := getDB(ctx, p.DB) | ||||
| 	_, err := db.ExecContext(ctx, "INSERT INTO player_stats (player_username) VALUES (?)", username) | ||||
| 	return err | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder