Refactor project structure and update configurations. Now first working version
- Updated `.air.conf` for Nix compatibility and simplified build commands. - Enhanced `.gitignore` to include `tmp` directory. - Improved `README.md` with clearer instructions and added language details. - Refined CSS styles for better UI consistency and added alert styles. - Upgraded `flake.nix` to use Go 1.24 and improved shell environment setup. - Modified authentication logic in `auth.go` for better user handling. - Updated `main.go` to dynamically set the listening port and improved logging. - Added new `routes.go` file for handling game entry and history. - Enhanced user models and added statistics tracking in `models.go`. - Improved template rendering and added user feedback messages in HTML templates. - Removed obsolete build error logs and binaries.
This commit is contained in:
56
src/utils.go
56
src/utils.go
@@ -16,12 +16,15 @@ func mustRandToken(n int) string {
|
||||
return hex.EncodeToString(b)
|
||||
}
|
||||
|
||||
func now() time.Time { return time.Now().UTC() }
|
||||
func now() time.Time {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
|
||||
var adjectives = []string{"swift", "brave", "mighty", "cheeky", "sneaky", "zippy", "bouncy", "crispy", "fuzzy", "spicy", "snappy", "jazzy", "spry", "bold", "witty"}
|
||||
var animals = []string{"otter", "panda", "falcon", "lynx", "badger", "tiger", "koala", "yak", "gecko", "eagle", "mamba", "fox", "yak", "whale", "rhino"}
|
||||
|
||||
func defaultUsername() string {
|
||||
// io-game style: adjective-animal-xxxx
|
||||
adjectives := []string{"swift", "brave", "mighty", "cheeky", "sneaky", "zippy", "bouncy", "crispy", "fuzzy", "spicy", "snappy", "jazzy", "spry", "bold", "witty"}
|
||||
animals := []string{"otter", "panda", "falcon", "lynx", "badger", "tiger", "koala", "yak", "gecko", "eagle", "mamba", "fox", "yak", "whale", "rhino"}
|
||||
// Inspired from IO games: adjective-animal-xx
|
||||
a := adjectives[int(time.Now().UnixNano())%len(adjectives)]
|
||||
an := animals[int(time.Now().UnixNano()/17)%len(animals)]
|
||||
return a + "-" + an + "-" + strings.ToLower(mustRandToken(2))
|
||||
@@ -72,8 +75,18 @@ func ensureUniqueUsernameAndSlug(u *User) error {
|
||||
}
|
||||
|
||||
func userByEmail(email string) (*User, error) {
|
||||
// Email must be lowercased and trimmed
|
||||
var u User
|
||||
tx := db.Where("email = ?", strings.ToLower(email)).First(&u)
|
||||
tx := db.Where("email = ?", email).First(&u)
|
||||
if tx.Error != nil {
|
||||
return nil, tx.Error
|
||||
}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func userByName(name string) (*User, error) {
|
||||
var u User
|
||||
tx := db.Where("username = ?", name).First(&u)
|
||||
if tx.Error != nil {
|
||||
return nil, tx.Error
|
||||
}
|
||||
@@ -121,3 +134,36 @@ func stripAfterDot(s string) string {
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func getStatsFromUser(u *User) stats {
|
||||
var gps []GamePlayer
|
||||
var games, wins, losses int
|
||||
db.Where("user_id = ?", u.ID).Find(&gps)
|
||||
gameMap := map[uint]string{}
|
||||
for _, gp := range gps {
|
||||
gameMap[gp.GameID] = gp.Side
|
||||
}
|
||||
|
||||
if len(gameMap) > 0 {
|
||||
var gamesL []Game
|
||||
ids := make([]uint, 0, len(gameMap))
|
||||
for gid := range gameMap {
|
||||
ids = append(ids, gid)
|
||||
}
|
||||
db.Find(&gamesL, ids)
|
||||
for _, g := range gamesL {
|
||||
games++
|
||||
if g.ScoreA == g.ScoreB {
|
||||
continue
|
||||
}
|
||||
if g.ScoreA > g.ScoreB && gameMap[g.ID] == "A" {
|
||||
wins++
|
||||
} else if g.ScoreB > g.ScoreA && gameMap[g.ID] == "B" {
|
||||
wins++
|
||||
} else {
|
||||
losses++
|
||||
}
|
||||
}
|
||||
}
|
||||
return stats{Games: games, Wins: wins, Losses: losses}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user