Enha: model update
This commit is contained in:
		| @@ -20,42 +20,43 @@ func createRoom(ctx context.Context, req *models.RoomReq) (*models.Room, error) | ||||
| 	} | ||||
| 	room := req.CreateRoom(creator) | ||||
| 	room.RoomLink = cfg.BaseURL + "/room-join?id=" + room.ID | ||||
| 	if err := saveRoom(room); err != nil { | ||||
| 	if err := repo.CreateRoom(ctx, room); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return room, nil | ||||
| } | ||||
|  | ||||
| func saveRoom(room *models.Room) error { | ||||
| 	key := models.CacheRoomPrefix + room.ID | ||||
| 	data, err := json.Marshal(room) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	memcache.Set(key, data) | ||||
| 	// do I need last action here? since room save is kind of an action on itself | ||||
| 	// time.Now().Add(time.Hour).Sub(room.LastActionTS) | ||||
| 	anHour := int64(216000) // 60 * 60 * 60 | ||||
| 	memcache.Expire(key, anHour) | ||||
| 	return nil | ||||
| } | ||||
| // // DEPRECATED | ||||
| // func saveRoom(room *models.Room) error { | ||||
| // 	key := models.CacheRoomPrefix + room.ID | ||||
| // 	data, err := json.Marshal(room) | ||||
| // 	if err != nil { | ||||
| // 		return err | ||||
| // 	} | ||||
| // 	memcache.Set(key, data) | ||||
| // 	// do I need last action here? since room save is kind of an action on itself | ||||
| // 	// time.Now().Add(time.Hour).Sub(room.LastActionTS) | ||||
| // 	anHour := int64(216000) // 60 * 60 * 60 | ||||
| // 	memcache.Expire(key, anHour) | ||||
| // 	return nil | ||||
| // } | ||||
|  | ||||
| func getRoomByID(roomID string) (*models.Room, error) { | ||||
| 	roomBytes, err := memcache.Get(models.CacheRoomPrefix + roomID) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	resp := &models.Room{} | ||||
| 	if err := json.Unmarshal(roomBytes, &resp); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return resp, nil | ||||
| } | ||||
| // func getRoomByID(roomID string) (*models.Room, error) { | ||||
| // 	roomBytes, err := memcache.Get(models.CacheRoomPrefix + roomID) | ||||
| // 	if err != nil { | ||||
| // 		return nil, err | ||||
| // 	} | ||||
| // 	resp := &models.Room{} | ||||
| // 	if err := json.Unmarshal(roomBytes, &resp); err != nil { | ||||
| // 		return nil, err | ||||
| // 	} | ||||
| // 	return resp, nil | ||||
| // } | ||||
|  | ||||
| func removeRoom(roomID string) { | ||||
| 	key := models.CacheRoomPrefix + roomID | ||||
| 	memcache.RemoveKey(key) | ||||
| } | ||||
| // func removeRoom(roomID string) { | ||||
| // 	key := models.CacheRoomPrefix + roomID | ||||
| // 	memcache.RemoveKey(key) | ||||
| // } | ||||
|  | ||||
| // context | ||||
|  | ||||
| @@ -76,17 +77,17 @@ func getStateByCtx(ctx context.Context) (*models.UserState, error) { | ||||
| // 	repo.CreateRoom() | ||||
| // } | ||||
|  | ||||
| func saveFullInfo(fi *models.FullInfo) error { | ||||
| 	// INFO: no transactions; so case is possible where first object is updated but the second is not | ||||
| 	if err := saveState(fi.State.Username, fi.State); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	log.Debug("saved user state", "state", fi.State) | ||||
| 	if err := saveRoom(fi.Room); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| // func saveFullInfo(fi *models.FullInfo) error { | ||||
| // 	// INFO: no transactions; so case is possible where first object is updated but the second is not | ||||
| // 	if err := saveState(fi.State.Username, fi.State); err != nil { | ||||
| // 		return err | ||||
| // 	} | ||||
| // 	log.Debug("saved user state", "state", fi.State) | ||||
| // 	if err := saveRoom(fi.Room); err != nil { | ||||
| // 		return err | ||||
| // 	} | ||||
| // 	return nil | ||||
| // } | ||||
|  | ||||
| func notifyBotIfNeeded(room *models.Room) { | ||||
| 	if botName := room.WhichBotToMove(); botName != "" { | ||||
|   | ||||
| @@ -14,9 +14,9 @@ import ( | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	log      *slog.Logger | ||||
| 	cfg      *config.Config | ||||
| 	memcache cache.Cache | ||||
| 	log *slog.Logger | ||||
| 	cfg *config.Config | ||||
| 	// memcache cache.Cache | ||||
| 	Notifier *broker.Broker | ||||
| 	repo     repos.AllRepos | ||||
| ) | ||||
| @@ -26,7 +26,7 @@ func init() { | ||||
| 		Level:     slog.LevelDebug, | ||||
| 		AddSource: true, | ||||
| 	})) | ||||
| 	memcache = cache.MemCache | ||||
| 	// memcache = cache.MemCache | ||||
| 	cfg = config.LoadConfigOrDefault("") | ||||
| 	Notifier = broker.Notifier | ||||
| 	cache.MemCache.StartBackupRoutine(15 * time.Second) // Reduced backup interval | ||||
| @@ -91,17 +91,25 @@ func HandleExit(w http.ResponseWriter, r *http.Request) { | ||||
| 		creatorLeft = true | ||||
| 	} | ||||
| 	exitedRoom := fi.ExitRoom() | ||||
| 	if err := saveRoom(exitedRoom); err != nil { | ||||
| 		abortWithError(w, err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	// if err := saveRoom(exitedRoom); err != nil { | ||||
| 	// 	abortWithError(w, err.Error()) | ||||
| 	// 	return | ||||
| 	// } | ||||
| 	if creatorLeft { | ||||
| 		removeRoom(exitedRoom.ID) | ||||
| 		if err := repo.DeleteRoomByID(r.Context(), exitedRoom.ID); err != nil { | ||||
| 			log.Error("failed to remove room", "error", err) | ||||
| 		} | ||||
| 		// removeRoom(exitedRoom.ID) | ||||
| 		// TODO: notify users if creator left | ||||
| 		// and throw them away | ||||
| 		notify(models.NotifyRoomListUpdate, "") | ||||
| 	} | ||||
| 	if err := saveState(fi.State.Username, fi.State); err != nil { | ||||
| 	// scary to update the whole room | ||||
| 	if err := repo.UpdateRoom(r.Context(), exitedRoom); err != nil { | ||||
| 		abortWithError(w, err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	if err := repo.PlayerExitRoom(fi.State.Username); err != nil { | ||||
| 		abortWithError(w, err.Error()) | ||||
| 		return | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder