diff --git a/components/index.html b/components/index.html
index 4b0f94a..759c9f3 100644
--- a/components/index.html
+++ b/components/index.html
@@ -1,22 +1,26 @@
{{define "main"}}
-
+ Start of main temp
{{ if not . }}
- {{template "login"}}
-
+ login temp
+ {{template "login"}}
+ {{ else if ne .LinkLogin "" }}
+ got to linklogin
+ {{template "linklogin" .LinkLogin}}
{{ else if eq .State.RoomID "" }}
-
-
Hello {{.State.Username}}
-
-
-
-
-
- {{template "roomlist" .List}}
-
+ empty state roomid
+
+
Hello {{.State.Username}}
+
+
+
+
+
+ {{template "roomlist" .List}}
+
{{else}}
-
-
- {{template "room" .}}
-
+ else
+
+ {{template "room" .}}
+
{{end}}
{{end}}
diff --git a/components/linklogin.html b/components/linklogin.html
new file mode 100644
index 0000000..2898405
--- /dev/null
+++ b/components/linklogin.html
@@ -0,0 +1,16 @@
+{{define "linklogin"}}
+
+ You're about to join room#{{.}}; but first!
+
+
+{{end}}
diff --git a/handlers/auth.go b/handlers/auth.go
index 0f2172d..0507423 100644
--- a/handlers/auth.go
+++ b/handlers/auth.go
@@ -60,6 +60,7 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
abortWithError(w, msg)
return
}
+ roomID := r.PostFormValue("room_id")
// make sure username does not exists
cleanName := utils.RemoveSpacesFromStr(username)
// login user
@@ -75,19 +76,37 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
- // state := models.MakeTestState(cleanName)
- // state.State.Username = cleanName
userstate := models.InitState(cleanName)
fi := &models.FullInfo{
State: userstate,
}
- fi.List = listPublicRooms()
- // save state to cache
- if err := saveState(cleanName, userstate); err != nil {
- // if err := saveFullInfo(fi); err != nil {
- log.Error("failed to save state", "error", err)
- abortWithError(w, err.Error())
- return
+ // check if room_id provided and exists
+ if roomID != "" {
+ log.Debug("got room_id in login", "room_id", roomID)
+ room, err := getRoomByID(roomID)
+ if err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ room.PlayerList = append(room.PlayerList, fi.State.Username)
+ fi.State.RoomID = room.ID
+ fi.Room = room
+ fi.List = nil
+ // save full info instead
+ if err := saveFullInfo(fi); err != nil {
+ abortWithError(w, err.Error())
+ return
+ }
+ } else {
+ log.Debug("no room_id in login")
+ fi.List = listPublicRooms()
+ // save state to cache
+ if err := saveState(cleanName, userstate); err != nil {
+ // if err := saveFullInfo(fi); err != nil {
+ log.Error("failed to save state", "error", err)
+ abortWithError(w, err.Error())
+ return
+ }
}
tmpl.ExecuteTemplate(w, "base", fi)
}
diff --git a/handlers/game.go b/handlers/game.go
index f79799b..19fc4fa 100644
--- a/handlers/game.go
+++ b/handlers/game.go
@@ -49,55 +49,52 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "base", fi)
}
-func HandleRoomEnter(w http.ResponseWriter, r *http.Request) {
- // parse payload
- roomID := r.URL.Query().Get("id")
- if roomID == "" {
- // error
- return
- }
- // create a room
- room, err := getRoomByID(roomID)
- if err != nil {
- msg := "failed to find the room"
- log.Error(msg, "error", err, "room_id", roomID)
- abortWithError(w, msg)
- return
- }
- ctx := context.WithValue(r.Context(), "current_room", room.ID)
- ctx, err = updateRoomInSession(ctx, room.ID)
- if err != nil {
- msg := "failed to set current room to session"
- log.Error(msg, "error", err)
- abortWithError(w, msg)
- return
- }
- state, err := getStateByCtx(ctx)
- if err != nil {
- log.Error("failed to get state", "error", err)
- abortWithError(w, err.Error())
- return
- }
- state.RoomID = room.ID
- // update state
- if err := saveStateByCtx(ctx, state); err != nil {
- log.Error("failed to update state", "error", err)
- abortWithError(w, err.Error())
- return
- }
- // send msg of created room
- // h.Broker.Notifier <- broker.NotificationEvent{
- // EventName: models.MsgRoomListUpdate,
- // Payload: fmt.Sprintf("%s created a room named %s", r.CreatorName, r.RoomName),
- // }
- // return html
- tmpl, err := template.ParseGlob("components/*.html")
- if err != nil {
- abortWithError(w, err.Error())
- return
- }
- tmpl.ExecuteTemplate(w, "base", room)
-}
+// DEPRACATED: duplication of HandleJoinRoom
+// func HandleRoomEnter(w http.ResponseWriter, r *http.Request) {
+// // parse payload
+// roomID := r.URL.Query().Get("id")
+// if roomID == "" {
+// msg := "room id not provided"
+// log.Error(msg)
+// abortWithError(w, msg)
+// return
+// }
+// tmpl, err := template.ParseGlob("components/*.html")
+// if err != nil {
+// abortWithError(w, err.Error())
+// return
+// }
+// // create a room
+// room, err := getRoomByID(roomID)
+// if err != nil {
+// msg := "failed to find the room"
+// log.Error(msg, "error", err, "room_id", roomID)
+// abortWithError(w, msg)
+// return
+// }
+// state, err := getStateByCtx(r.Context())
+// // INFO: if non-loggined user join: prompt to login
+// if err != nil {
+// log.Error("failed to get state", "error", err)
+// // abortWithError(w, err.Error())
+// tmpl.ExecuteTemplate(w, "login", nil)
+// return
+// }
+// state.RoomID = room.ID
+// // update state
+// if err := saveStateByCtx(r.Context(), state); err != nil {
+// log.Error("failed to update state", "error", err)
+// abortWithError(w, err.Error())
+// return
+// }
+// // send msg of created room
+// // h.Broker.Notifier <- broker.NotificationEvent{
+// // EventName: models.MsgRoomListUpdate,
+// // Payload: fmt.Sprintf("%s created a room named %s", r.CreatorName, r.RoomName),
+// // }
+// // return html
+// tmpl.ExecuteTemplate(w, "base", room)
+// }
func HandleJoinTeam(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
@@ -239,11 +236,20 @@ func HandleJoinRoom(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
- fi, err := getFullInfoByCtx(r.Context())
+ tmpl, err := template.ParseGlob("components/*.html")
if err != nil {
abortWithError(w, err.Error())
return
}
+ fi, err := getFullInfoByCtx(r.Context())
+ if err != nil {
+ // INFO: if non-loggined user join: prompt to login
+ fi = &models.FullInfo{}
+ fi.LinkLogin = roomID
+ tmpl.ExecuteTemplate(w, "base", fi)
+ // abortWithError(w, err.Error())
+ return
+ }
room.PlayerList = append(room.PlayerList, fi.State.Username)
fi.State.RoomID = room.ID
fi.Room = room
@@ -252,12 +258,6 @@ func HandleJoinRoom(w http.ResponseWriter, r *http.Request) {
abortWithError(w, err.Error())
return
}
- // return html
- tmpl, err := template.ParseGlob("components/*.html")
- if err != nil {
- abortWithError(w, err.Error())
- return
- }
tmpl.ExecuteTemplate(w, "room", fi)
}
diff --git a/main.go b/main.go
index cba93e9..1484ec8 100644
--- a/main.go
+++ b/main.go
@@ -23,7 +23,7 @@ func ListenToRequests(port string) error {
mux.HandleFunc("GET /ping", handlers.HandlePing)
mux.HandleFunc("GET /", handlers.HandleHome)
mux.HandleFunc("POST /login", handlers.HandleFrontLogin)
- mux.HandleFunc("GET /room", handlers.HandleRoomEnter)
+ // mux.HandleFunc("GET /room", handlers.HandleRoomEnter)
mux.HandleFunc("POST /join-team", handlers.HandleJoinTeam)
mux.HandleFunc("GET /end-turn", handlers.HandleEndTurn)
mux.HandleFunc("POST /room-create", handlers.HandleCreateRoom)
diff --git a/models/main.go b/models/main.go
index 0b46359..8d89526 100644
--- a/models/main.go
+++ b/models/main.go
@@ -242,9 +242,10 @@ func (rr *RoomReq) CreateRoom(creator string) *Room {
// ====
type FullInfo struct {
- State *UserState
- Room *Room
- List []*Room
+ State *UserState
+ Room *Room
+ List []*Room
+ LinkLogin string // room_id
}
func (f *FullInfo) ExitRoom() *Room {