From 5b8432bfec8aeb58f06978034b71bebbde034434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20R=C3=A9thelyi?= <rethelyibalint@gmail.com> Date: Tue, 21 Sep 2021 20:44:09 +0200 Subject: [PATCH] can resume the game --- main.go | 3 -- route/route.go | 58 ++++++++++++++--------- stations.yaml | 26 +++++----- web/router.go | 100 +++++++++++++++++++++------------------ web/templates/error.html | 4 +- 5 files changed, 104 insertions(+), 87 deletions(-) diff --git a/main.go b/main.go index a5e6df6..c038ae8 100644 --- a/main.go +++ b/main.go @@ -11,9 +11,6 @@ import ( func main() { rand.Seed(time.Now().UnixNano()) route.ReadFile() - route.CreateBaseStations() - //r := route.CreateNewRoute() - //fmt.Println(r) e := web.Router.Run(":8080") if e != nil { diff --git a/route/route.go b/route/route.go index 00ae041..27ab340 100644 --- a/route/route.go +++ b/route/route.go @@ -13,10 +13,8 @@ type Station struct { } type File struct { - First Station - Last Station - Middle []Station - Stories []string + Stations map[string]*Station + Stories []string } type User struct { @@ -24,22 +22,20 @@ type User struct { Name string Point int Group *Group + + LastVisitedURL string } type Group struct { Id uuid.UUID Name string Point int - Route []Station + Route []*Station Members []User } -var file File -var ( - Last Station - First Station -) -var Middle []Station +var Stations map[string]*Station +var Stories []string func ReadFile() { b, err := ioutil.ReadFile("stations.yaml") @@ -47,24 +43,40 @@ func ReadFile() { panic(err) } + var file File err = yaml.Unmarshal(b, &file) if err != nil { panic(err) } -} -func CreateBaseStations() { - First = file.First - Last = file.Last - Middle = file.Middle + Stations = file.Stations + Stories = file.Stories } -func CreateNewRoute() []Station { - tmp := Middle - var route []Station - route = append(route, First) - rand.Shuffle(len(tmp), func(i, j int) { tmp[i], tmp[j] = tmp[j], tmp[i] }) - route = append(route, tmp...) - route = append(route, Last) +func CreateNewRoute() []*Station { + route := make([]*Station, len(Stations)) + route[0] = Stations["first"] + route[len(Stations)-1] = Stations["last"] + + keys := make([]string, len(Stations)-2) + i := 0 + for k := range Stations { + switch k { + case "first", "last": + default: + keys[i] = k + i++ + } + } + rand.Shuffle(len(keys), func(i, j int) { + keys[i], keys[j] = keys[j], keys[i] + }) + + i = 1 + for _, k := range keys { + route[i] = Stations[k] + i++ + } + return route } diff --git a/stations.yaml b/stations.yaml index 7d1cc36..76c4f2b 100644 --- a/stations.yaml +++ b/stations.yaml @@ -1,18 +1,14 @@ -first: - id: "first" - name: "egy" - -last: - id: "last" - name: "uccso" - -middle: - - id: "9881300f-175b-4809-b8fb-d915b2c900ac" - name: "rand1" - - id: "2ab06fbd-0606-42a9-abc7-4163221d3f1f" - name: "rand2" - - id: "7023030f-e22f-4c82-93ae-4c847b3ac964" - name: "rand3" +stations: + first: + name: egy + 9881300f-175b-4809-b8fb-d915b2c900ac: + name: rand1 + 2ab06fbd-0606-42a9-abc7-4163221d3f1f: + name: rand2 + 7023030f-e22f-4c82-93ae-4c847b3ac964: + name: rand3 + last: + name: uccso stories: - "elso" diff --git a/web/router.go b/web/router.go index e97e876..9cc88bd 100644 --- a/web/router.go +++ b/web/router.go @@ -2,7 +2,6 @@ package web import ( "errors" - "fmt" "git.sch.bme.hu/blintmester/keresett-a-feri/route" "github.com/gin-gonic/gin" "github.com/google/uuid" @@ -12,7 +11,6 @@ import ( var Router = gin.Default() type StationURI struct { - //Name string `uri:"name" binding:"required"` Id string `uri:"uuid" binding:"required"` } @@ -47,72 +45,84 @@ func mainPage(c *gin.Context) { }) } -func findExistingOrCreateNewGroup(arr []route.Group, b string) (route.Group, bool) { - for _, a := range arr { +func getGroup(b string) (*route.Group, bool) { + for i, a := range groups { if a.Name == b { - return a, true + return groups[i], true } } - return route.Group{}, false + return nil, false } -var groups []route.Group +func getUser(cookie string) (*route.User, bool) { + if cookie != "" { + for i, group := range groups { + for j, member := range group.Members { + if member.Id.String() == cookie { + return &groups[i].Members[j], true + } + } + } + } + return nil, false +} + +var groups []*route.Group func login(c *gin.Context) { - groupName := c.PostForm("groupName") - group, ok := findExistingOrCreateNewGroup(groups, groupName) + + cookie, _ := c.Cookie("uuid") + user, ok := getUser(cookie) if !ok { - group = route.Group{ + groupName := c.PostForm("group") + group, ok := getGroup(groupName) + if !ok { + group = &route.Group{ + Id: uuid.New(), + Name: groupName, + Point: 0, + Route: route.CreateNewRoute(), + } + groups = append(groups, group) + } + + user = &route.User{ Id: uuid.New(), - Name: groupName, + Name: c.PostForm("name"), Point: 0, - Route: route.CreateNewRoute(), + Group: group, } - groups = append(groups, group) - } + group.Members = append(group.Members, *user) - user := route.User{ - Id: uuid.New(), - Name: c.PostForm("name"), - Point: 0, - Group: &group, + c.SetCookie("uuid", user.Id.String(), 7200, "/", "", false, true) } - group.Members = append(group.Members, user) - - c.Redirect(http.StatusMovedPermanently, "/station/first") + if user.LastVisitedURL == "" { + user.LastVisitedURL = "first" + } + c.Redirect(http.StatusMovedPermanently, "/station/"+user.LastVisitedURL) } func routeChooser(c *gin.Context) { + cookie, _ := c.Cookie("uuid") + user, ok := getUser(cookie) + if !ok { + c.Redirect(http.StatusMovedPermanently, "/") + return + } + var stationUri StationURI if err := c.ShouldBindUri(&stationUri); err != nil { - c.Error(err) + _ = c.Error(err) return } - var station route.Station - - if stationUri.Id == route.First.Id { - fmt.Println("First station") - station = route.First - } else if stationUri.Id == route.Last.Id { - fmt.Println("Last station") - station = route.Last - } else { - found := false - for _, stat := range route.Middle { - if stationUri.Id == stat.Id { - found = true - fmt.Println(stat.Id) - station = stat - break - } - } - if !found { - c.Error(errors.New("wrong uuid")) - return - } + station, ok := route.Stations[stationUri.Id] + if !ok { + _ = c.Error(errors.New("wrong uuid")) + return } + user.LastVisitedURL = stationUri.Id c.HTML(http.StatusOK, "station.html", gin.H{ "Name": station.Name, }) diff --git a/web/templates/error.html b/web/templates/error.html index 006ca05..9395334 100644 --- a/web/templates/error.html +++ b/web/templates/error.html @@ -8,8 +8,10 @@ <h1>Something's fucked...</h1> <h2>Errors:</h2> +<ul> {{- range $key, $error := .errors }} -<il>{{ $error }}</il> + <li>{{ $error }}</li> {{- end }} +</ul> </body> </html> \ No newline at end of file -- GitLab