Feat: which bot to move
This commit is contained in:
		| @@ -6,6 +6,7 @@ import ( | |||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"golias/broker" | 	"golias/broker" | ||||||
|  | 	"golias/llmapi" | ||||||
| 	"golias/models" | 	"golias/models" | ||||||
| 	"golias/utils" | 	"golias/utils" | ||||||
| 	"golias/wordloader" | 	"golias/wordloader" | ||||||
| @@ -121,6 +122,10 @@ func loadState(username string) (*models.UserState, error) { | |||||||
| 	return resp, nil | 	return resp, nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func loadBot(botName, roomID string) (*llmapi.Bot, error) { | ||||||
|  | 	return nil, nil | ||||||
|  | } | ||||||
|  |  | ||||||
| func getAllNames() []string { | func getAllNames() []string { | ||||||
| 	names := []string{} | 	names := []string{} | ||||||
| 	// will not scale | 	// will not scale | ||||||
|   | |||||||
| @@ -85,7 +85,7 @@ func HandleShowColor(w http.ResponseWriter, r *http.Request) { | |||||||
| 		fi.Room.IsRunning = false | 		fi.Room.IsRunning = false | ||||||
| 		fi.Room.IsOver = true | 		fi.Room.IsOver = true | ||||||
| 		fi.Room.TeamWon = oppositeColor | 		fi.Room.TeamWon = oppositeColor | ||||||
| 	case "white", oppositeColor: | 	case "white", string(oppositeColor): | ||||||
| 		// end turn | 		// end turn | ||||||
| 		fi.Room.TeamTurn = oppositeColor | 		fi.Room.TeamTurn = oppositeColor | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -3,6 +3,7 @@ package handlers | |||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
|  | 	"fmt" | ||||||
| 	"golias/models" | 	"golias/models" | ||||||
| 	"html/template" | 	"html/template" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| @@ -146,9 +147,9 @@ func HandleEndTurn(w http.ResponseWriter, r *http.Request) { | |||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	// check if one who pressed it is from the team who has the turn | 	// check if one who pressed it is from the team who has the turn | ||||||
| 	if fi.Room.TeamTurn != string(fi.State.Team) { | 	if fi.Room.TeamTurn != fi.State.Team { | ||||||
| 		err = errors.New("unexpected team turn:" + fi.Room.TeamTurn) | 		msg := fmt.Sprintln("unexpected team turn:" + fi.Room.TeamTurn) | ||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, msg) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	fi.Room.ChangeTurn() | 	fi.Room.ChangeTurn() | ||||||
| @@ -162,6 +163,17 @@ func HandleEndTurn(w http.ResponseWriter, r *http.Request) { | |||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  | 	if botName := fi.Room.WhichBotToMove(); botName != "" { | ||||||
|  | 		// get bot from memcache | ||||||
|  | 		bot, err := loadBot(botName, fi.Room.ID) | ||||||
|  | 		if err != nil { | ||||||
|  | 			log.Error("failed to load bot", "bot_name", botName, "room_id", fi.Room.ID) | ||||||
|  | 			abortWithError(w, err.Error()) | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 		// send signal to bot | ||||||
|  | 		bot.SignalsCh <- true | ||||||
|  | 	} | ||||||
| 	notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "") | 	notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "") | ||||||
| 	tmpl.ExecuteTemplate(w, "base", fi) | 	tmpl.ExecuteTemplate(w, "base", fi) | ||||||
| } | } | ||||||
| @@ -203,6 +215,17 @@ func HandleStartGame(w http.ResponseWriter, r *http.Request) { | |||||||
| 		abortWithError(w, err.Error()) | 		abortWithError(w, err.Error()) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  | 	if botName := fi.Room.WhichBotToMove(); botName != "" { | ||||||
|  | 		// get bot from memcache | ||||||
|  | 		bot, err := loadBot(botName, fi.Room.ID) | ||||||
|  | 		if err != nil { | ||||||
|  | 			log.Error("failed to load bot", "bot_name", botName, "room_id", fi.Room.ID) | ||||||
|  | 			abortWithError(w, err.Error()) | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 		// send signal to bot | ||||||
|  | 		bot.SignalsCh <- true | ||||||
|  | 	} | ||||||
| 	// to update only the room that should be updated | 	// to update only the room that should be updated | ||||||
| 	notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "") | 	notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "") | ||||||
| 	// notify(models.NotifyBacklogPrefix+fi.Room.ID, "game started") | 	// notify(models.NotifyBacklogPrefix+fi.Room.ID, "game started") | ||||||
|   | |||||||
| @@ -80,6 +80,11 @@ func NewBot(role, team, name, roomID string, cfg *config.Config) (*Bot, error) { | |||||||
| 		return nil, errors.New("cannot join after game started") | 		return nil, errors.New("cannot join after game started") | ||||||
| 	} | 	} | ||||||
| 	room.PlayerList = append(room.PlayerList, name) | 	room.PlayerList = append(room.PlayerList, name) | ||||||
|  | 	bp := models.BotPlayer{ | ||||||
|  | 		Role: models.StrToUserRole(role), | ||||||
|  | 		Team: models.StrToUserTeam(team), | ||||||
|  | 	} | ||||||
|  | 	room.BotMap[name] = bp | ||||||
| 	switch team { | 	switch team { | ||||||
| 	case "red": | 	case "red": | ||||||
| 		if role == "mime" { | 		if role == "mime" { | ||||||
|   | |||||||
| @@ -47,6 +47,11 @@ type Action struct { | |||||||
| 	Number     string // for clue | 	Number     string // for clue | ||||||
| } | } | ||||||
|  |  | ||||||
|  | type BotPlayer struct { | ||||||
|  | 	Role UserRole // gueeser | mime | ||||||
|  | 	Team UserTeam | ||||||
|  | } | ||||||
|  |  | ||||||
| type Room struct { | type Room struct { | ||||||
| 	ID        string    `json:"id" db:"id"` | 	ID        string    `json:"id" db:"id"` | ||||||
| 	CreatedAt time.Time `json:"created_at" db:"created_at"` | 	CreatedAt time.Time `json:"created_at" db:"created_at"` | ||||||
| @@ -56,11 +61,12 @@ type Room struct { | |||||||
| 	CreatorName   string   `json:"creator_name"` | 	CreatorName   string   `json:"creator_name"` | ||||||
| 	PlayerList    []string `json:"player_list"` | 	PlayerList    []string `json:"player_list"` | ||||||
| 	ActionHistory []Action | 	ActionHistory []Action | ||||||
| 	TeamTurn      string | 	TeamTurn      UserTeam | ||||||
| 	RedTeam       Team | 	RedTeam       Team | ||||||
| 	BlueTeam      Team | 	BlueTeam      Team | ||||||
| 	Cards         []WordCard | 	Cards         []WordCard | ||||||
| 	WCMap         map[string]WordColor | 	WCMap         map[string]WordColor | ||||||
|  | 	BotMap        map[string]BotPlayer // key is bot name | ||||||
| 	Result        uint8                // 0 for unknown; 1 is win for red; 2 if for blue; | 	Result        uint8                // 0 for unknown; 1 is win for red; 2 if for blue; | ||||||
| 	BlueCounter   uint8 | 	BlueCounter   uint8 | ||||||
| 	RedCounter    uint8 | 	RedCounter    uint8 | ||||||
| @@ -73,19 +79,48 @@ type Room struct { | |||||||
| 	RoundTime int32  `json:"round_time"` | 	RoundTime int32  `json:"round_time"` | ||||||
| 	// ProgressPct uint32 `json:"progress_pct"` | 	// ProgressPct uint32 `json:"progress_pct"` | ||||||
| 	IsOver  bool | 	IsOver  bool | ||||||
| 	TeamWon string // blue | red | 	TeamWon UserTeam // blue | red | ||||||
| } | } | ||||||
|  |  | ||||||
| func (r *Room) GetOppositeTeamColor() string { | // WhichBotToMove returns bot name that have to move or empty string | ||||||
|  | func (r *Room) WhichBotToMove() string { | ||||||
|  | 	if !r.IsRunning { | ||||||
|  | 		return "" | ||||||
|  | 	} | ||||||
| 	switch r.TeamTurn { | 	switch r.TeamTurn { | ||||||
| 	case "red": | 	case UserTeamBlue: | ||||||
| 		return "blue" | 		if !r.MimeDone { | ||||||
| 	case "blue": | 			_, ok := r.BotMap[r.BlueTeam.Mime] | ||||||
| 		return "red" | 			if ok { | ||||||
|  | 				return r.BlueTeam.Mime | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		// check gussers | ||||||
|  | 	case UserTeamRed: | ||||||
|  | 		if !r.MimeDone { | ||||||
|  | 			_, ok := r.BotMap[r.RedTeam.Mime] | ||||||
|  | 			if ok { | ||||||
|  | 				return r.RedTeam.Mime | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		// check gussers | ||||||
|  | 	default: | ||||||
|  | 		// how did we got here? | ||||||
|  | 		return "" | ||||||
| 	} | 	} | ||||||
| 	return "" | 	return "" | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (r *Room) GetOppositeTeamColor() UserTeam { | ||||||
|  | 	switch r.TeamTurn { | ||||||
|  | 	case "red": | ||||||
|  | 		return UserTeamBlue | ||||||
|  | 	case "blue": | ||||||
|  | 		return UserTeamRed | ||||||
|  | 	} | ||||||
|  | 	return UserTeamNone | ||||||
|  | } | ||||||
|  |  | ||||||
| func (r *Room) UpdateCounter() { | func (r *Room) UpdateCounter() { | ||||||
| 	redCounter := uint8(0) | 	redCounter := uint8(0) | ||||||
| 	blueCounter := uint8(0) | 	blueCounter := uint8(0) | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								todos.md
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								todos.md
									
									
									
									
									
								
							| @@ -6,6 +6,7 @@ | |||||||
| - invite link; | - invite link; | ||||||
| - login with invite link; | - login with invite link; | ||||||
| - add html icons of whos turn it is (like an image of big ? when mime is thinking); | - add html icons of whos turn it is (like an image of big ? when mime is thinking); | ||||||
|  | - there two places for bot to check if its its move: start-game; end-turn; | ||||||
|  |  | ||||||
| #### sse points | #### sse points | ||||||
| - clue sse update; | - clue sse update; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder