Fix: buildable
This commit is contained in:
		| @@ -2,14 +2,12 @@ package handlers | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"encoding/json" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"gralias/broker" | ||||
| 	"gralias/llmapi" | ||||
| 	"gralias/models" | ||||
| 	"gralias/wordloader" | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| func createRoom(ctx context.Context, req *models.RoomReq) (*models.Room, error) { | ||||
| @@ -20,7 +18,7 @@ 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 := repo.CreateRoom(ctx, room); err != nil { | ||||
| 	if err := repo.RoomCreate(ctx, room); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	return room, nil | ||||
| @@ -109,25 +107,24 @@ func notifyBotIfNeeded(room *models.Room) { | ||||
| // 	return nil | ||||
| // } | ||||
|  | ||||
| func getAllNames() []string { | ||||
| 	names := []string{} | ||||
| 	// will not scale | ||||
| 	wholeMemStore := memcache.GetAll() | ||||
| 	session := &models.Session{} | ||||
| 	// filter by key size only sessions | ||||
| 	for k, v := range wholeMemStore { | ||||
| 		// xid is 20 in len | ||||
| 		if len(k) != 20 { | ||||
| 			continue | ||||
| 		} | ||||
| 		if err := json.Unmarshal(v, &session); err != nil { | ||||
| 			log.Error("failed to unmarshal", "error", err) | ||||
| 			continue | ||||
| 		} | ||||
| 		names = append(names, session.Username) | ||||
| 	} | ||||
| 	return names | ||||
| } | ||||
| // func getAllNames() []string { | ||||
| // 	names := []string{} | ||||
| // 	// will not scale | ||||
| // 	session := &models.Session{} | ||||
| // 	// filter by key size only sessions | ||||
| // 	for _, name := range wholeMemStore { | ||||
| // 		// xid is 20 in len | ||||
| // 		if len(k) != 20 { | ||||
| // 			continue | ||||
| // 		} | ||||
| // 		if err := json.Unmarshal(v, &session); err != nil { | ||||
| // 			log.Error("failed to unmarshal", "error", err) | ||||
| // 			continue | ||||
| // 		} | ||||
| // 		names = append(names, session.Username) | ||||
| // 	} | ||||
| // 	return names | ||||
| // } | ||||
|  | ||||
| // can room exists without state? I think no | ||||
| func getFullInfoByCtx(ctx context.Context) (*models.FullInfo, error) { | ||||
| @@ -142,7 +139,7 @@ func getFullInfoByCtx(ctx context.Context) (*models.FullInfo, error) { | ||||
| 		return resp, nil | ||||
| 	} | ||||
| 	// room, err := getRoomByID(state.RoomID) | ||||
| 	room, err := repo.GetRoomByID(ctx, state.RoomID) | ||||
| 	room, err := repo.RoomGetByID(ctx, state.RoomID) | ||||
| 	if err != nil { | ||||
| 		log.Warn("failed to find room despite knowing room_id;", | ||||
| 			"room_id", state.RoomID) | ||||
| @@ -158,7 +155,7 @@ func getPlayerByCtx(ctx context.Context) (*models.Player, error) { | ||||
| 		log.Debug("no username in ctx") | ||||
| 		return &models.Player{}, errors.New("no username in ctx") | ||||
| 	} | ||||
| 	return repo.GetPlayerByName(username) | ||||
| 	return repo.PlayerGetByName(username) | ||||
| } | ||||
|  | ||||
| // // DEPRECATED | ||||
| @@ -249,41 +246,15 @@ func joinTeam(ctx context.Context, role, team string) (*models.FullInfo, error) | ||||
| // } | ||||
|  | ||||
| // get bots | ||||
| func listBots() map[string]map[string]string { | ||||
| 	cacheMap := memcache.GetAll() | ||||
| 	resp := make(map[string]map[string]string) | ||||
| 	// no way to know if room is public until unmarshal -_-; | ||||
| 	for key, value := range cacheMap { | ||||
| 		if strings.HasPrefix(key, models.CacheBotPredix) { | ||||
| 			botMap := make(map[string]string) | ||||
| 			if err := json.Unmarshal(value, &botMap); err != nil { | ||||
| 				log.Warn("failed to unmarshal bot", "error", err) | ||||
| 				continue | ||||
| 			} | ||||
| 			resp[botMap["bot_name"]] = botMap | ||||
| 		} | ||||
| func listBots() []models.Player { | ||||
| 	bots, err := repo.PlayerList(true) | ||||
| 	if err != nil { | ||||
| 		log.Error("failed to fetch bots from db", "error", err) | ||||
| 	} | ||||
| 	return resp | ||||
| 	return bots | ||||
| } | ||||
|  | ||||
| // get players | ||||
| func listPlayers() map[string]map[string]string { | ||||
| 	cacheMap := memcache.GetAll() | ||||
| 	resp := make(map[string]map[string]string) | ||||
| 	// no way to know if room is public until unmarshal -_-; | ||||
| 	for key, value := range cacheMap { | ||||
| 		if strings.HasPrefix(key, models.CacheStatePrefix) { | ||||
| 			playerMap := make(map[string]string) | ||||
| 			if err := json.Unmarshal(value, &playerMap); err != nil { | ||||
| 				log.Warn("failed to unmarshal player", "error", err) | ||||
| 				continue | ||||
| 			} | ||||
| 			resp[playerMap["Username"]] = playerMap | ||||
| 		} | ||||
| 	} | ||||
| 	return resp | ||||
| } | ||||
|  | ||||
| func notify(event, msg string) { | ||||
| 	Notifier.Notifier <- broker.NotificationEvent{ | ||||
| 		EventName: event, | ||||
| @@ -312,54 +283,54 @@ func loadCards(room *models.Room) { | ||||
|  | ||||
| func recoverBots() { | ||||
| 	bots := listBots() | ||||
| 	for botName, botMap := range bots { | ||||
| 		if err := recoverBot(botMap); err != nil { | ||||
| 			log.Warn("failed to recover bot", "botName", botName, "error", err) | ||||
| 	for _, bot := range bots { | ||||
| 		if err := recoverBot(bot); err != nil { | ||||
| 			log.Warn("failed to recover bot", "botName", bot.Username, "error", err) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func recoverBot(bm map[string]string) error { | ||||
| func recoverBot(bm models.Player) error { | ||||
| 	// check if room still exists | ||||
| 	if _, err := getRoomByID(bm["room_id"]); err != nil { | ||||
| 		return fmt.Errorf("no such room: %s; err: %w", bm["room_id"], err) | ||||
| 	if _, err := repo.RoomGetByID(context.Background(), bm.RoomID); err != nil { | ||||
| 		return fmt.Errorf("no such room: %s; err: %w", bm.RoomID, err) | ||||
| 	} | ||||
| 	log.Debug("recovering bot", "bot", bm) | ||||
| 	_, err := llmapi.NewBot(bm["role"], bm["team"], bm["bot_name"], bm["room_id"], cfg, true) | ||||
| 	_, err := llmapi.NewBot(string(bm.Role), string(bm.Team), bm.Username, bm.RoomID, cfg, true) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func recoverPlayers() { | ||||
| 	players := listPlayers() | ||||
| 	for playerName, playerMap := range players { | ||||
| 		if err := recoverPlayer(playerMap); err != nil { | ||||
| 			log.Warn("failed to recover player", "playerName", playerName, "error", err) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| // func recoverPlayers() { | ||||
| // 	players := listPlayers() | ||||
| // 	for playerName, playerMap := range players { | ||||
| // 		if err := recoverPlayer(playerMap); err != nil { | ||||
| // 			log.Warn("failed to recover player", "playerName", playerName, "error", err) | ||||
| // 		} | ||||
| // 	} | ||||
| // } | ||||
|  | ||||
| func recoverPlayer(pm map[string]string) error { | ||||
| 	// check if room still exists | ||||
| 	room, err := getRoomByID(pm["RoomID"]) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("no such room: %s; err: %w", pm["RoomID"], err) | ||||
| 	} | ||||
| 	log.Debug("recovering player", "player", pm) | ||||
| 	role, team, ok := room.GetPlayerByName(pm["Username"]) | ||||
| 	if !ok { | ||||
| 		return fmt.Errorf("failed to find player %s in the room %v", pm["Username"], room) | ||||
| 	} | ||||
| 	us := &models.UserState{ | ||||
| 		Username: pm["Username"], | ||||
| 		RoomID:   pm["RoomID"], | ||||
| 		Team:     team, | ||||
| 		Role:     role, | ||||
| 	} | ||||
| 	return saveState(pm["Username"], us) | ||||
| } | ||||
| // func recoverPlayer(pm map[string]string) error { | ||||
| // 	// check if room still exists | ||||
| // 	room, err := repo.RoomGetByID(context.Background(), pm["RoomID"]) | ||||
| // 	if err != nil { | ||||
| // 		return fmt.Errorf("no such room: %s; err: %w", pm["RoomID"], err) | ||||
| // 	} | ||||
| // 	log.Debug("recovering player", "player", pm) | ||||
| // 	role, team, ok := room.GetPlayerByName(pm["Username"]) | ||||
| // 	if !ok { | ||||
| // 		return fmt.Errorf("failed to find player %s in the room %v", pm["Username"], room) | ||||
| // 	} | ||||
| // 	us := &models.Player{ | ||||
| // 		Username: pm["Username"], | ||||
| // 		RoomID:   pm["RoomID"], | ||||
| // 		Team:     team, | ||||
| // 		Role:     role, | ||||
| // 	} | ||||
| // 	return saveState(pm["Username"], us) | ||||
| // } | ||||
|  | ||||
| // validateMove checks if it is players turn | ||||
| func validateMove(fi *models.FullInfo, ur models.UserRole) error { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder