This commit is contained in:
2025-05-31 04:17:55 +07:00
parent 49999e9e70
commit 42dc516e29
22 changed files with 513 additions and 180 deletions
+4
View File
@@ -44,6 +44,10 @@ func (s *Server) AddAction(ctx context.Context, req *proto.AddActionReq) (*proto
return s.services.AddAction(ctx, req)
}
func (s *Server) GetGame(ctx context.Context, req *proto.GetGameReq) (*proto.GetGameRsp, error) {
return s.services.GetGame(ctx, req)
}
func (s *Server) GameStart(ctx context.Context, req *proto.GameStartReq) (*proto.GameStartRsp, error) {
return s.services.GameStart(ctx, req)
}
+36 -3
View File
@@ -30,13 +30,17 @@ func NewRepository() (*Repository, error) {
if err != nil {
return nil, err
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS games (id INTEGER PRIMARY KEY AUTOINCREMENT, state TEXT, startAt TIMESTAMP, endAt TIMESTAMP);")
if err != nil {
return nil, err
}
return &Repository{db: db}, nil
}
func (r *Repository) GetTeams(ctx context.Context) ([]*models.Team, error) {
rows, err := r.db.Query("select id, name, password from teams")
if err != nil {
panic(err)
return nil, err
}
defer rows.Close()
teams := []*models.Team{}
@@ -69,7 +73,7 @@ func (r *Repository) AddTeams(ctx context.Context, teams []*models.Team) ([]*mod
func (r *Repository) GetActions(ctx context.Context, teamId int64) ([]*models.Action, error) {
rows, err := r.db.Query("select id, place from actions where teamId = $1", teamId)
if err != nil {
panic(err)
return nil, err
}
defer rows.Close()
actions := []*models.Action{}
@@ -132,7 +136,7 @@ func (r *Repository) AddApplications(ctx context.Context, actions []*models.Acti
func (r *Repository) GetApplications(ctx context.Context, teamId int64, state string) ([]*models.Application, error) {
rows, err := r.db.Query("select id, name from applications where teamId = $1 and state = $2", teamId, state)
if err != nil {
panic(err)
return nil, err
}
defer rows.Close()
applications := []*models.Application{}
@@ -157,3 +161,32 @@ func (r *Repository) GiveApplications(ctx context.Context, teamId int64, applica
}
return nil
}
func (r *Repository) GetGame(ctx context.Context) (string, error) {
rows, err := r.db.Query("select state from games limit 1")
if err != nil {
return "", err
}
defer rows.Close()
state := ""
if rows.Next() {
err := rows.Scan(&state)
if err != nil {
return "", err
}
return state, nil
}
state = "NEW"
_, err = r.db.Exec("insert into games (state) values ($1)", state)
if err != nil {
return "", err
}
return state, nil
}
func (r *Repository) GameUpdateState(ctx context.Context, state string) error {
_, err := r.db.Exec("update games set state = $1", state)
return err
}
+18 -4
View File
@@ -40,12 +40,26 @@ func (s *Services) GiveApplications(ctx context.Context, req *proto.GiveApplicat
return &proto.GiveApplicationsRsp{}, nil
}
func (s *Services) GameStop(ctx context.Context, req *proto.GameStopReq) (*proto.GameStopRsp, error) {
panic("unimplemented")
func (s *Services) GetGame(ctx context.Context, _ *proto.GetGameReq) (*proto.GetGameRsp, error) {
state, err := s.repository.GetGame(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return &proto.GetGameRsp{State: state}, nil
}
func (s *Services) GameStart(ctx context.Context, req *proto.GameStartReq) (*proto.GameStartRsp, error) {
panic("unimplemented")
func (s *Services) GameStart(ctx context.Context, _ *proto.GameStartReq) (*proto.GameStartRsp, error) {
if err := s.repository.GameUpdateState(ctx, "RUN"); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return &proto.GameStartRsp{}, nil
}
func (s *Services) GameStop(ctx context.Context, req *proto.GameStopReq) (*proto.GameStopRsp, error) {
if err := s.repository.GameUpdateState(ctx, "STOP"); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return &proto.GameStopRsp{}, nil
}
func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*proto.AddActionRsp, error) {