- {{range .Room.Cards}}
+ {{range .Cards}}
{{template "cardword" .}}
{{end}}
diff --git a/components/index.html b/components/index.html
index d937cc1..f5ac36e 100644
--- a/components/index.html
+++ b/components/index.html
@@ -1,11 +1,11 @@
{{define "main"}}
- {{ if eq .Username "" }}
+ {{ if not . }}
{{template "login"}}
- {{ else if eq .Room.ID "" }}
+ {{ else if eq .State.RoomID "" }}
diff --git a/components/room.html b/components/room.html
index 403a55d..5f21717 100644
--- a/components/room.html
+++ b/components/room.html
@@ -1,6 +1,6 @@
{{define "room"}}
-
Hello {{.Username}}
+
Hello {{.State.Username}}
@@ -11,6 +11,6 @@
- {{template "cardtable" .}}
+ {{template "cardtable" .Room}}
{{end}}
diff --git a/go.mod b/go.mod
index 55a533b..c765c78 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,5 @@ go 1.24
require (
github.com/BurntSushi/toml v1.5.0
- honnef.co/go/tools v0.6.1
+ github.com/rs/xid v1.6.0
)
-
-require golang.org/x/tools v0.30.0 // indirect
diff --git a/go.sum b/go.sum
index fda7250..6cdd3ff 100644
--- a/go.sum
+++ b/go.sum
@@ -1,6 +1,4 @@
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
-golang.org/x/tools v0.30.0 h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=
-golang.org/x/tools v0.30.0/go.mod h1:c347cR/OJfw5TI+GfX7RUPNMdDRRbjvYTS0jPyvsVtY=
-honnef.co/go/tools v0.6.1 h1:R094WgE8K4JirYjBaOpz/AvTyUu/3wbmAoskKN/pxTI=
-honnef.co/go/tools v0.6.1/go.mod h1:3puzxxljPCe8RGJX7BIy1plGbxEOZni5mR2aXe3/uk4=
+github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
+github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
diff --git a/handlers/actions.go b/handlers/actions.go
index 0b22ded..75c9553 100644
--- a/handlers/actions.go
+++ b/handlers/actions.go
@@ -8,7 +8,25 @@ import (
)
func createRoom(ctx context.Context, req *models.RoomReq) (*models.Room, error) {
- return nil, nil
+ creator, ok := ctx.Value(models.CtxUsernameKey).(string)
+ if !ok {
+ err := errors.New("failed to extract user from ctx")
+ return nil, err
+ }
+ room := req.CreateRoom(creator)
+ if err := saveRoom(room); err != nil {
+ return nil, err
+ }
+ return room, nil
+}
+
+func saveRoom(room *models.Room) error {
+ data, err := json.Marshal(room)
+ if err != nil {
+ return err
+ }
+ memcache.Set(models.CacheRoomPrefix+room.ID, data)
+ return nil
}
func getRoomByID(roomID string) (*models.Room, error) {
@@ -86,3 +104,19 @@ func getAllNames() []string {
}
return names
}
+
+func getFullInfoByCtx(ctx context.Context) (*models.FullInfo, error) {
+ state, err := getStateByCtx(ctx)
+ if err != nil {
+ return nil, err
+ }
+ room, err := getRoomByID(state.RoomID)
+ if err != nil {
+ return nil, err
+ }
+ resp := &models.FullInfo{
+ State: state,
+ Room: room,
+ }
+ return resp, nil
+}
diff --git a/handlers/auth.go b/handlers/auth.go
index d3bf939..f096b1e 100644
--- a/handlers/auth.go
+++ b/handlers/auth.go
@@ -80,9 +80,9 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
// }
// state := models.InitState(cleanName)
state := models.MakeTestState()
- state.Username = cleanName
+ state.State.Username = cleanName
// save state to cache
- saveState(cleanName, state)
+ saveState(cleanName, state.State)
tmpl.ExecuteTemplate(w, "base", state)
}
diff --git a/handlers/game.go b/handlers/game.go
index 8be29bf..639aa2a 100644
--- a/handlers/game.go
+++ b/handlers/game.go
@@ -79,3 +79,26 @@ func HandleRoomEnter(w http.ResponseWriter, r *http.Request) {
}
tmpl.ExecuteTemplate(w, "base", room)
}
+
+func HandleJoinTeam(w http.ResponseWriter, r *http.Request) {
+ // parse payload
+ team := r.URL.Query().Get("team")
+ role := r.URL.Query().Get("role")
+ if team == "" || role == "" {
+ msg := "missing team or role"
+ log.Error(msg)
+ abortWithError(w, msg)
+ // error
+ return
+ }
+ // get username
+ // get state
+ // get room
+ // return html
+ tmpl, err := template.ParseGlob("components/*.html")
+ if err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ tmpl.ExecuteTemplate(w, "base", nil)
+}
diff --git a/handlers/handlers.go b/handlers/handlers.go
index 7c70fcf..f82c1b7 100644
--- a/handlers/handlers.go
+++ b/handlers/handlers.go
@@ -2,7 +2,6 @@ package handlers
import (
"golias/config"
- "golias/models"
"golias/pkg/cache"
"html/template"
"log/slog"
@@ -63,13 +62,8 @@ func HandleHome(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
- userState, _ := getStateByCtx(r.Context())
- if userState == nil {
- userState = &models.UserState{}
+ fi, _ := getFullInfoByCtx(r.Context())
+ if err := tmpl.ExecuteTemplate(w, "base", fi); err != nil {
+ log.Error("failed to exec templ;", "error", err, "templ", "base")
}
- // if err != nil {
- // abortWithError(w, err.Error())
- // return
- // }
- tmpl.ExecuteTemplate(w, "base", userState)
}
diff --git a/main.go b/main.go
index 389bd06..11c742f 100644
--- a/main.go
+++ b/main.go
@@ -23,6 +23,7 @@ func ListenToRequests(port string) error {
mux.HandleFunc("GET /", handlers.HandleHome)
mux.HandleFunc("POST /login", handlers.HandleFrontLogin)
mux.HandleFunc("GET /room", handlers.HandleRoomEnter)
+ mux.HandleFunc("GET /join-team", handlers.HandleJoinTeam)
//elements
mux.HandleFunc("GET /room/createform", handlers.HandleShowCreateForm)
mux.HandleFunc("GET /room/hideform", handlers.HandleHideCreateForm)
diff --git a/models/main.go b/models/main.go
index b66f943..ddf6435 100644
--- a/models/main.go
+++ b/models/main.go
@@ -1,6 +1,10 @@
package models
-import "time"
+import (
+ "time"
+
+ "github.com/rs/xid"
+)
type WordColor string
@@ -28,10 +32,10 @@ func StrToWordColor(s string) WordColor {
}
type Room struct {
- ID string `json:"id" db:"id"`
- CreatedAt time.Time `json:"created_at" db:"created_at"`
- RoomName string `json:"room_name"`
- RoomPass string `json:"room_pass"`
+ ID string `json:"id" db:"id"`
+ CreatedAt time.Time `json:"created_at" db:"created_at"`
+ // RoomName string `json:"room_name"`
+ RoomPass string `json:"room_pass"`
RoomLink string
CreatorName string `json:"creator_name"`
PlayerList []string `json:"player_list"`
@@ -70,3 +74,22 @@ type RoomReq struct {
RoomName string `json:"room_name" form:"room_name"`
// GameSettings
}
+
+func (rr *RoomReq) CreateRoom(creator string) *Room {
+ roomID := xid.New().String()
+ return &Room{
+ // RoomName: ,
+ RoomPass: rr.RoomPass,
+ ID: roomID,
+ CreatedAt: time.Now(),
+ PlayerList: []string{creator},
+ CreatorName: creator,
+ }
+}
+
+// ====
+
+type FullInfo struct {
+ State *UserState
+ Room *Room
+}
diff --git a/models/state.go b/models/state.go
index 70944ce..a164b78 100644
--- a/models/state.go
+++ b/models/state.go
@@ -42,12 +42,12 @@ func StrToUserRole(s string) UserRole {
type UserState struct {
Username string
- Room Room
+ RoomID string
Team UserTeam
Role UserRole
}
-func MakeTestState() *UserState {
+func MakeTestState() *FullInfo {
cards := []WordCard{
{Word: "hamster", Color: "blue"},
{Word: "child", Color: "red"},
@@ -75,17 +75,21 @@ func MakeTestState() *UserState {
{Word: "tomato", Color: "red"},
{Word: "cloud", Color: "white"},
}
- room := Room{
+ room := &Room{
ID: "test-id",
CreatedAt: time.Now(),
CreatorName: "test-name",
Cards: cards,
}
- return &UserState{
+ us := &UserState{
Username: "test-name",
Team: UserTeamNone,
Role: UserRoleNone,
- Room: room,
+ RoomID: "test-id",
+ }
+ return &FullInfo{
+ State: us,
+ Room: room,
}
}