Skip to content

Commit d2866d6

Browse files
committed
feat: add random wall brushing function (needs help with imports)
- Add random brushing algorithm structure - Need assistance fixing import path error at app.go:9 - Function ready for review and guidance
1 parent 455abfa commit d2866d6

File tree

2 files changed

+396
-0
lines changed

2 files changed

+396
-0
lines changed

app.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9+
<<<<<<< HEAD
10+
"math/rand"
11+
=======
12+
>>>>>>> 455abfa291290798fb93a0f80cec2d4013175c15
913
"net/http"
1014
"os"
1115
"os/exec"
@@ -48,6 +52,28 @@ type ContributionDay struct {
4852
Count int `json:"count"`
4953
}
5054

55+
<<<<<<< HEAD
56+
// RandomPaintRequest 随机刷墙请求参数
57+
type RandomPaintRequest struct {
58+
StartDate string `json:"startDate"` // 格式: "2024-01-01"
59+
EndDate string `json:"endDate"` // 格式: "2024-12-31"
60+
Density float64 `json:"density"` // 0.0-1.0,活跃天数比例
61+
MinPerDay int `json:"minPerDay"` // 每天最少提交数
62+
MaxPerDay int `json:"maxPerDay"` // 每天最多提交数
63+
ExcludeWeekend bool `json:"excludeWeekend"` // 是否排除周末
64+
RandomSeed int64 `json:"randomSeed"` // 随机种子,0表示使用时间戳
65+
}
66+
67+
// RandomPaintResponse 随机刷墙响应
68+
type RandomPaintResponse struct {
69+
Contributions []ContributionDay `json:"contributions"`
70+
TotalDays int `json:"totalDays"` // 总天数
71+
ActiveDays int `json:"activeDays"` // 活跃天数
72+
TotalCommits int `json:"totalCommits"` // 总提交数
73+
}
74+
75+
=======
76+
>>>>>>> 455abfa291290798fb93a0f80cec2d4013175c15
5177
type GenerateRepoRequest struct {
5278
Year int `json:"year"`
5379
GithubUsername string `json:"githubUsername"`
@@ -477,6 +503,108 @@ func (a *App) ImportContributions() (*ImportContributionsResponse, error) {
477503
return &ImportContributionsResponse{Contributions: contributions}, nil
478504
}
479505

506+
<<<<<<< HEAD
507+
// GenerateRandomContributions 生成随机贡献数据
508+
func (a *App) GenerateRandomContributions(req RandomPaintRequest) (*RandomPaintResponse, error) {
509+
// 参数验证
510+
startDate, err := time.Parse("2006-01-02", req.StartDate)
511+
if err != nil {
512+
return nil, fmt.Errorf("invalid start date: %w", err)
513+
}
514+
515+
endDate, err := time.Parse("2006-01-02", req.EndDate)
516+
if err != nil {
517+
return nil, fmt.Errorf("invalid end date: %w", err)
518+
}
519+
520+
if startDate.After(endDate) {
521+
return nil, fmt.Errorf("start date must be before end date")
522+
}
523+
524+
if req.Density < 0 || req.Density > 1 {
525+
return nil, fmt.Errorf("density must be between 0 and 1")
526+
}
527+
528+
if req.MinPerDay < 0 {
529+
return nil, fmt.Errorf("minPerDay must be positive")
530+
}
531+
532+
if req.MaxPerDay < req.MinPerDay {
533+
return nil, fmt.Errorf("maxPerDay must be greater than or equal to minPerDay")
534+
}
535+
536+
// 初始化随机数生成器
537+
var seed int64
538+
if req.RandomSeed == 0 {
539+
seed = time.Now().UnixNano()
540+
} else {
541+
seed = req.RandomSeed
542+
}
543+
rng := rand.New(rand.NewSource(seed))
544+
545+
// 生成贡献数据
546+
contributions := []ContributionDay{}
547+
totalCommits := 0
548+
activeDays := 0
549+
550+
currentDate := startDate
551+
for !currentDate.After(endDate) {
552+
// 检查是否排除周末
553+
if req.ExcludeWeekend {
554+
weekday := currentDate.Weekday()
555+
if weekday == time.Saturday || weekday == time.Sunday {
556+
currentDate = currentDate.AddDate(0, 0, 1)
557+
continue
558+
}
559+
}
560+
561+
dateStr := currentDate.Format("2006-01-02")
562+
563+
// 根据密度决定今天是否有贡献
564+
if rng.Float64() <= req.Density {
565+
// 生成当天的提交次数
566+
var commitsToday int
567+
if req.MaxPerDay == req.MinPerDay {
568+
commitsToday = req.MinPerDay
569+
} else {
570+
commitsToday = req.MinPerDay + rng.Intn(req.MaxPerDay-req.MinPerDay+1)
571+
}
572+
573+
// 确保至少有一次提交
574+
if commitsToday < 1 {
575+
commitsToday = 1
576+
}
577+
578+
contributions = append(contributions, ContributionDay{
579+
Date: dateStr,
580+
Count: commitsToday,
581+
})
582+
583+
totalCommits += commitsToday
584+
activeDays++
585+
}
586+
587+
currentDate = currentDate.AddDate(0, 0, 1)
588+
}
589+
590+
// 按日期排序
591+
sort.Slice(contributions, func(i, j int) bool {
592+
return contributions[i].Date < contributions[j].Date
593+
})
594+
595+
// 计算总天数
596+
totalDays := int(endDate.Sub(startDate).Hours()/24) + 1
597+
598+
return &RandomPaintResponse{
599+
Contributions: contributions,
600+
TotalDays: totalDays,
601+
ActiveDays: activeDays,
602+
TotalCommits: totalCommits,
603+
}, nil
604+
}
605+
606+
=======
607+
>>>>>>> 455abfa291290798fb93a0f80cec2d4013175c15
480608
func sanitiseRepoName(input string) string {
481609
input = strings.TrimSpace(input)
482610
if input == "" {

0 commit comments

Comments
 (0)