Enha: use of db methods

This commit is contained in:
Grail Finder
2025-07-02 12:55:50 +03:00
parent b66f9c4c06
commit 9973546aad
6 changed files with 89 additions and 78 deletions

View File

@ -60,34 +60,34 @@ func createRoom(ctx context.Context, req *models.RoomReq) (*models.Room, error)
// context
func getStateByCtx(ctx context.Context) (*models.UserState, error) {
username, ok := ctx.Value(models.CtxUsernameKey).(string)
if !ok {
log.Debug("no username in ctx")
return &models.UserState{}, errors.New("no username in ctx")
}
us, err := loadState(username)
if err != nil {
return &models.UserState{}, err
}
return us, nil
}
// func getStateByCtx(ctx context.Context) (*models.UserState, error) {
// username, ok := ctx.Value(models.CtxUsernameKey).(string)
// if !ok {
// log.Debug("no username in ctx")
// return &models.UserState{}, errors.New("no username in ctx")
// }
// us, err := loadState(username)
// if err != nil {
// return &models.UserState{}, err
// }
// return us, nil
// }
// func dbCreate(fi *models.FullInfo) 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 := repo.PlayerUpdate(fi.State); err != nil {
return err
}
log.Debug("saved user state", "state", fi.State)
if err := repo.RoomUpdate(context.Background(), fi.Room); err != nil {
return err
}
return nil
}
func notifyBotIfNeeded(room *models.Room) {
if botName := room.WhichBotToMove(); botName != "" {
@ -229,24 +229,24 @@ func joinTeam(ctx context.Context, role, team string) (*models.FullInfo, error)
}
// get all rooms
func listRooms(allRooms bool) []*models.Room {
cacheMap := memcache.GetAll()
publicRooms := []*models.Room{}
// no way to know if room is public until unmarshal -_-;
for key, value := range cacheMap {
if strings.HasPrefix(key, models.CacheRoomPrefix) {
room := &models.Room{}
if err := json.Unmarshal(value, &room); err != nil {
log.Warn("failed to unmarshal room", "error", err)
continue
}
if room.IsPublic || allRooms {
publicRooms = append(publicRooms, room)
}
}
}
return publicRooms
}
// func listRooms(allRooms bool) []*models.Room {
// cacheMap := memcache.GetAll()
// publicRooms := []*models.Room{}
// // no way to know if room is public until unmarshal -_-;
// for key, value := range cacheMap {
// if strings.HasPrefix(key, models.CacheRoomPrefix) {
// room := &models.Room{}
// if err := json.Unmarshal(value, &room); err != nil {
// log.Warn("failed to unmarshal room", "error", err)
// continue
// }
// if room.IsPublic || allRooms {
// publicRooms = append(publicRooms, room)
// }
// }
// }
// return publicRooms
// }
// get bots
func listBots() map[string]map[string]string {