Enha: model update
This commit is contained in:
		| @@ -8,6 +8,7 @@ import ( | ||||
| type RoomsRepo interface { | ||||
| 	ListRooms(ctx context.Context) ([]*models.Room, error) | ||||
| 	GetRoomByID(ctx context.Context, id string) (*models.Room, error) | ||||
| 	GetRoomExtended(ctx context.Context, id string) (*models.Room, error) | ||||
| 	CreateRoom(ctx context.Context, room *models.Room) error | ||||
| 	DeleteRoomByID(ctx context.Context, id string) error | ||||
| 	UpdateRoom(ctx context.Context, room *models.Room) error | ||||
| @@ -32,7 +33,7 @@ func (p *RepoProvider) GetRoomByID(ctx context.Context, id string) (*models.Room | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) CreateRoom(ctx context.Context, r *models.Room) error { | ||||
| 	_, err := p.DB.ExecContext(ctx, `INSERT INTO rooms (id, created_at, creator_name, team_turn, this_turn_limit, opened_this_turn, blue_counter, red_counter, red_turn, mime_done, is_running, round_time, is_over, team_won, room_pass) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, r.ID, r.CreatedAt, r.CreatorName, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsRunning, r.RoundTime, r.IsOver, r.TeamWon, r.RoomPass) | ||||
| 	_, err := p.DB.ExecContext(ctx, `INSERT INTO rooms (id, created_at, creator_name, team_turn, this_turn_limit, opened_this_turn, blue_counter, red_counter, red_turn, mime_done, , is_running, is_over, team_won, room_link) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, r.ID, r.CreatedAt, r.CreatorName, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsRunning, r.IsOver, r.TeamWon, r.RoomLink) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| @@ -42,6 +43,73 @@ func (p *RepoProvider) DeleteRoomByID(ctx context.Context, id string) error { | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) UpdateRoom(ctx context.Context, r *models.Room) error { | ||||
| 	_, err := p.DB.ExecContext(ctx, `UPDATE rooms SET team_turn = ?, this_turn_limit = ?, opened_this_turn = ?, blue_counter = ?, red_counter = ?, red_turn = ?, mime_done = ?, is_running = ?, round_time = ?, is_over = ?, team_won = ?, room_pass = ? WHERE id = ?`, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsRunning, r.RoundTime, r.IsOver, r.TeamWon, r.RoomPass, r.ID) | ||||
| 	_, err := p.DB.ExecContext(ctx, `UPDATE rooms SET team_turn = ?, this_turn_limit = ?, opened_this_turn = ?, blue_counter = ?, red_counter = ?, red_turn = ?, mime_done = ?,  = ?, is_running = ?, is_over = ?, team_won = ?, room_link = ? WHERE id = ?`, r.TeamTurn, r.ThisTurnLimit, r.OpenedThisTurn, r.BlueCounter, r.RedCounter, r.RedTurn, r.MimeDone, r.IsRunning, r.IsOver, r.TeamWon, r.RoomLink, r.ID) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (p *RepoProvider) GetRoomExtended(ctx context.Context, id string) (*models.Room, error) { | ||||
| 	room := &models.Room{} | ||||
| 	err := p.DB.GetContext(ctx, room, `SELECT * FROM rooms WHERE id = ?`, id) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	// Get players | ||||
| 	players := []*models.Player{} | ||||
| 	err = p.DB.SelectContext(ctx, &players, `SELECT * FROM players WHERE room_id = ?`, id) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	room.RedTeam.Color = string(models.UserTeamRed) | ||||
| 	room.BlueTeam.Color = string(models.UserTeamBlue) | ||||
| 	for _, player := range players { | ||||
| 		if player.Team == models.UserTeamRed { | ||||
| 			if player.Role == models.UserRoleMime { | ||||
| 				room.RedTeam.Mime = player.Username | ||||
| 			} else { | ||||
| 				room.RedTeam.Guessers = append(room.RedTeam.Guessers, player.Username) | ||||
| 			} | ||||
| 		} else if player.Team == models.UserTeamBlue { | ||||
| 			if player.Role == models.UserRoleMime { | ||||
| 				room.BlueTeam.Mime = player.Username | ||||
| 			} else { | ||||
| 				room.BlueTeam.Guessers = append(room.BlueTeam.Guessers, player.Username) | ||||
| 			} | ||||
| 		} | ||||
| 		if player.IsBot { | ||||
| 			if room.BotMap == nil { | ||||
| 				room.BotMap = make(map[string]models.BotPlayer) | ||||
| 			} | ||||
| 			room.BotMap[player.Username] = models.BotPlayer{ | ||||
| 				Role: player.Role, | ||||
| 				Team: player.Team, | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	// Get word cards | ||||
| 	wordCards := []*models.WordCard{} | ||||
| 	err = p.DB.SelectContext(ctx, &wordCards, `SELECT * FROM word_cards WHERE room_id = ?`, id) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	room.Cards = wordCards | ||||
|  | ||||
| 	// Get actions | ||||
| 	actions := []*models.Action{} | ||||
| 	err = p.DB.SelectContext(ctx, &actions, `SELECT * FROM actions WHERE room_id = ? ORDER BY created_at ASC`, id) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	room.ActionHistory = actions | ||||
|  | ||||
| 	// Get settings | ||||
| 	settings := &models.GameSettings{} | ||||
| 	err = p.DB.GetContext(ctx, settings, `SELECT * FROM settings WHERE room_id = ?`, id) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	room.Settings = *settings | ||||
|  | ||||
| 	return room, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Grail Finder
					Grail Finder