Feat: create room
This commit is contained in:
		| @@ -4,15 +4,13 @@ | |||||||
|     Create a room <br/> |     Create a room <br/> | ||||||
|     or<br/> |     or<br/> | ||||||
|     <button button class="justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" hx-get="/room/hideform" hx-target=".create-room-div" >Hide Form</button> |     <button button class="justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" hx-get="/room/hideform" hx-target=".create-room-div" >Hide Form</button> | ||||||
|     <form hx-post="/room/create" hx-target="#ancestor"> |     <form hx-post="/room-create" hx-target="#ancestor"> | ||||||
| 	<label For="room_name">Room Name</label><br/> |  | ||||||
| 	<input type="text" id="room_name" name="room_name" class="text-center text-black" value={utils.MakeDefaultRoomName(utils.GetUsername(c))}/><br/> |  | ||||||
| 	<label For="game_time">Game Time:</label><br/> | 	<label For="game_time">Game Time:</label><br/> | ||||||
| 	<input type="number" id="game_time" name="game_time" class="text-center text-black" value="300"/><br/> | 	<input type="number" id="game_time" name="game_time" class="text-center text-white" value="300"/><br/> | ||||||
| 	<label For="language">Language:</label><br/> | 	<label For="language">Language:</label><br/> | ||||||
| 	<input type="text" id="language" name="language" class="text-center text-black" value="en"/><br/> | 	<input type="text" id="language" name="language" class="text-center text-white" value="en"/><br/> | ||||||
| 	<label For="password">Password:</label><br/> | 	<label For="password">Password:</label><br/> | ||||||
| 	<input type="text" id="password" name="room_pass" class="text-center text-black" value="" placeholder="Leave empty for open room"/><br/> | 	<input type="text" id="password" name="room_pass" class="text-center text-white" value="" placeholder="Leave empty for open room"/><br/> | ||||||
|     <button button class="justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" type="submit" >Create Room</button> |     <button button class="justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" type="submit" >Create Room</button> | ||||||
|     </form> |     </form> | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -75,6 +75,10 @@ func saveFullInfo(fi *models.FullInfo) error { | |||||||
| 	if err := saveState(fi.State.Username, fi.State); err != nil { | 	if err := saveState(fi.State.Username, fi.State); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  | 	// can room be nil? | ||||||
|  | 	// if fi.Room == nil { | ||||||
|  | 	// 	return nil | ||||||
|  | 	// } | ||||||
| 	if err := saveRoom(fi.Room); err != nil { | 	if err := saveRoom(fi.Room); err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| @@ -126,19 +130,24 @@ func getAllNames() []string { | |||||||
| 	return names | 	return names | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // can room exists without state? I think no | ||||||
| func getFullInfoByCtx(ctx context.Context) (*models.FullInfo, error) { | func getFullInfoByCtx(ctx context.Context) (*models.FullInfo, error) { | ||||||
|  | 	resp := &models.FullInfo{} | ||||||
| 	state, err := getStateByCtx(ctx) | 	state, err := getStateByCtx(ctx) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
|  | 	resp.State = state | ||||||
|  | 	if state.RoomID == "" { | ||||||
|  | 		return resp, nil | ||||||
|  | 	} | ||||||
| 	room, err := getRoomByID(state.RoomID) | 	room, err := getRoomByID(state.RoomID) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | 		log.Warn("failed to find room despite knowing room_id;", | ||||||
|  | 			"room_id", state.RoomID) | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	resp := &models.FullInfo{ | 	resp.Room = room | ||||||
| 		State: state, |  | ||||||
| 		Room:  room, |  | ||||||
| 	} |  | ||||||
| 	return resp, nil | 	return resp, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -72,16 +72,20 @@ func HandleFrontLogin(w http.ResponseWriter, r *http.Request) { | |||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	state := models.MakeTestState(cleanName) | 	// state := models.MakeTestState(cleanName) | ||||||
| 	state.State.Username = cleanName | 	// state.State.Username = cleanName | ||||||
|  | 	userstate := models.InitState(cleanName) | ||||||
|  | 	fi := &models.FullInfo{ | ||||||
|  | 		State: userstate, | ||||||
|  | 	} | ||||||
| 	// save state to cache | 	// save state to cache | ||||||
| 	// if err := saveState(cleanName, state.State); err != nil { | 	if err := saveState(cleanName, userstate); err != nil { | ||||||
| 	if err := saveFullInfo(state); err != nil { | 		// if err := saveFullInfo(fi); err != nil { | ||||||
| 		log.Error("failed to save state", "error", err) | 		log.Error("failed to save state", "error", err) | ||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	tmpl.ExecuteTemplate(w, "base", state) | 	tmpl.ExecuteTemplate(w, "base", fi) | ||||||
| } | } | ||||||
|  |  | ||||||
| func makeCookie(username string, remote string) (*http.Cookie, error) { | func makeCookie(username string, remote string) (*http.Cookie, error) { | ||||||
| @@ -152,7 +156,7 @@ func cacheSetSession(key string, session *models.Session) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| func updateRoomInSession(ctx context.Context, roomID string) (context.Context, error) { | func updateRoomInSession(ctx context.Context, roomID string) (context.Context, error) { | ||||||
| 	s, ok := ctx.Value("session").(models.Session) | 	s, ok := ctx.Value(models.CtxSessionKey).(*models.Session) | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		return context.TODO(), errors.New("failed to extract session from ctx") | 		return context.TODO(), errors.New("failed to extract session from ctx") | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -23,8 +23,16 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	ctx := context.WithValue(r.Context(), "current_room", room.ID) | 	ctx := context.WithValue(r.Context(), "current_room", room.ID) | ||||||
| 	ctx, err = updateRoomInSession(ctx, room.ID) | 	fi, err := getFullInfoByCtx(ctx) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | 		msg := "failed to get full info from ctx" | ||||||
|  | 		log.Error(msg, "error", err) | ||||||
|  | 		abortWithError(w, msg) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	fi.State.RoomID = room.ID | ||||||
|  | 	fi.Room = room | ||||||
|  | 	if err := saveFullInfo(fi); err != nil { | ||||||
| 		msg := "failed to set current room to session" | 		msg := "failed to set current room to session" | ||||||
| 		log.Error(msg, "error", err) | 		log.Error(msg, "error", err) | ||||||
| 		abortWithError(w, msg) | 		abortWithError(w, msg) | ||||||
| @@ -41,7 +49,7 @@ func HandleCreateRoom(w http.ResponseWriter, r *http.Request) { | |||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	tmpl.ExecuteTemplate(w, "base", nil) | 	tmpl.ExecuteTemplate(w, "base", fi) | ||||||
| } | } | ||||||
|  |  | ||||||
| func HandleRoomEnter(w http.ResponseWriter, r *http.Request) { | func HandleRoomEnter(w http.ResponseWriter, r *http.Request) { | ||||||
|   | |||||||
| @@ -41,6 +41,7 @@ func LogRequests(next http.Handler) http.Handler { | |||||||
|  |  | ||||||
| func GetSession(next http.Handler) http.Handler { | func GetSession(next http.Handler) http.Handler { | ||||||
| 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||||
|  | 		// TODO: move | ||||||
| 		cookieName := "session_token" | 		cookieName := "session_token" | ||||||
| 		sessionCookie, err := r.Cookie(cookieName) | 		sessionCookie, err := r.Cookie(cookieName) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								main.go
									
									
									
									
									
								
							| @@ -25,6 +25,7 @@ func ListenToRequests(port string) error { | |||||||
| 	mux.HandleFunc("GET /room", handlers.HandleRoomEnter) | 	mux.HandleFunc("GET /room", handlers.HandleRoomEnter) | ||||||
| 	mux.HandleFunc("POST /join-team", handlers.HandleJoinTeam) | 	mux.HandleFunc("POST /join-team", handlers.HandleJoinTeam) | ||||||
| 	mux.HandleFunc("GET /end-turn", handlers.HandleEndTurn) | 	mux.HandleFunc("GET /end-turn", handlers.HandleEndTurn) | ||||||
|  | 	mux.HandleFunc("POST /room-create", handlers.HandleCreateRoom) | ||||||
| 	//elements | 	//elements | ||||||
| 	mux.HandleFunc("GET /room/createform", handlers.HandleShowCreateForm) | 	mux.HandleFunc("GET /room/createform", handlers.HandleShowCreateForm) | ||||||
| 	mux.HandleFunc("GET /room/hideform", handlers.HandleHideCreateForm) | 	mux.HandleFunc("GET /room/hideform", handlers.HandleHideCreateForm) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder