Skip to content

Commit

Permalink
fix: game join problems
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Jun 2, 2024
1 parent ae9e6a1 commit a68662f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
20 changes: 16 additions & 4 deletions internal/model/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,21 @@ func (t *Team) AfterCreate(db *gorm.DB) (err error) {
}

func (t *Team) AfterUpdate(db *gorm.DB) (err error) {
db.Table("user_teams").Create(&UserTeam{
TeamID: t.ID,
UserID: t.CaptainID,
})
var userTeams []UserTeam
db.Table("user_teams").Where("team_id = ?", t.ID).Find(&userTeams)

flag := true
for _, userTeam := range userTeams {
if userTeam.UserID == t.CaptainID {
flag = false
}
}

if flag {
db.Table("user_teams").Create(&UserTeam{
TeamID: t.ID,
UserID: t.CaptainID,
})
}
return nil
}
12 changes: 8 additions & 4 deletions internal/service/game_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,18 @@ func (g *GameTeamService) Create(req request.GameTeamCreateRequest) (err error)
return errors.New("invalid team member count")
}

gameTeams, _, err := g.gameTeamRepository.Find(model.GameTeam{})
gameTeams, _, err := g.gameTeamRepository.Find(model.GameTeam{
GameID: req.ID,
})
for _, gameTeam := range gameTeams {
if gameTeam.TeamID == team.ID && gameTeam.GameID == game.ID {
return errors.New("team already exists")
}
for _, u := range team.Users {
if u.ID == req.UserID {
return errors.New("user already exists")
for _, u := range gameTeam.Team.Users {
for _, tu := range team.Users {
if tu.ID == u.ID {
return errors.New("user already exists")
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/widgets/GameNoticeArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function GameNoticeArea() {
socket.onmessage = (event) => {
const n = JSON.parse(event.data);
setNotices((notices) => {
return [...notices, n];
return [n, ...notices];
});
};

Expand Down
31 changes: 25 additions & 6 deletions web/src/pages/games/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default function Page() {

const [game, setGame] = useState<Game>();
const [gameTeams, setGameTeams] = useState<Array<GameTeam>>([]);
const [canEnter, setCanEnter] = useState(false);

const startedAt = dayjs(Number(game?.started_at) * 1000).format(
"YYYY/MM/DD HH:mm:ss"
Expand Down Expand Up @@ -91,6 +92,20 @@ export default function Page() {
});
}

useEffect(() => {
if (gameTeams) {
for (const gameTeam of gameTeams) {
if (gameTeam?.is_allowed) {
for (const user of gameTeam?.team?.users || []) {
if (user?.id === authStore?.user?.id) {
setCanEnter(true);
}
}
}
}
}
}, [gameTeams]);

useEffect(() => {
getGame();
getGameTeams();
Expand Down Expand Up @@ -133,12 +148,16 @@ export default function Page() {
>
查看榜单
</Button>
<Button onClick={() => applyOpen()}>
报名参赛
</Button>
<Button onClick={() => enter()}>
进入比赛
</Button>
{!canEnter && (
<Button onClick={() => applyOpen()}>
报名参赛
</Button>
)}
{canEnter && (
<Button onClick={() => enter()}>
进入比赛
</Button>
)}
</Group>
</Stack>
</Stack>
Expand Down

0 comments on commit a68662f

Please sign in to comment.