Skip to content

Commit

Permalink
wip: ws of game notice
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Mar 30, 2024
1 parent 90a433b commit 1e1d21f
Show file tree
Hide file tree
Showing 29 changed files with 83 additions and 250 deletions.
9 changes: 7 additions & 2 deletions internal/casbin/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ var (
)

func InitCasbin() {
adapter, err := gormadapter.NewAdapterByDB(database.Db())
adapter, err := gormadapter.NewAdapterByDBWithCustomTable(
database.Db(),
&gormadapter.CasbinRule{},
"casbins",
)
cfg, err := embed.FS.ReadFile("configs/casbin.conf")
md, _ := model.NewModelFromString(string(cfg))
Enforcer, err = casbin.NewEnforcer(md, adapter)
if err != nil {
zap.L().Fatal("Casbin init failed", zap.Error(err))
}
_ = Enforcer.LoadPolicy()
Enforcer.ClearPolicy()
_ = Enforcer.SavePolicy()
initDefaultPolicy()
}
10 changes: 1 addition & 9 deletions internal/casbin/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ func initDefaultPolicy() {
{"admin", "/api/*", "PUT"},
{"admin", "/api/*", "DELETE"},

{"monitor", "/api/games", "POST"},
{"monitor", "/api/games/{id}", "PUT"},
{"monitor", "/api/games/{id}", "DELETE"},
{"monitor", "/api/games/{id}/challenges", "POST"},
{"monitor", "/api/games/{id}/teams/{team_id}", "PUT"},

{"user", "/api/", "GET"},
{"user", "/api/users/logout", "POST"},
{"user", "/api/users/{id}", "PUT"},
Expand All @@ -40,7 +34,6 @@ func initDefaultPolicy() {
{"guest", "/api/", "GET"},
{"guest", "/api/configs/", "GET"},
{"guest", "/api/users/", "GET"},
{"guest", "/api/users/token/{token}", "GET"},
{"guest", "/api/users/register", "POST"},
{"guest", "/api/users/login", "POST"},
{"guest", "/api/games/{id}/broadcast", "GET"},
Expand All @@ -51,8 +44,7 @@ func initDefaultPolicy() {

_, err = Enforcer.AddGroupingPolicies([][]string{
{"user", "guest"},
{"monitor", "user"},
{"admin", "monitor"},
{"admin", "user"},
})

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func NewGameController(appService *service.Service) IGameController {
// @Tags Game
// @Router /games/{id}/broadcast [get]
func (g *GameController) BroadCast(ctx *gin.Context) {
id := convertor.ToInt64D(ctx.Param("id"), 0)
id := convertor.ToUintD(ctx.Param("id"), 0)
if id != 0 {
hub.ServeGameHub(ctx.Writer, ctx.Request, id)
}
Expand Down
5 changes: 0 additions & 5 deletions internal/database/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ func initGroup() {
DisplayName: "Administrator",
Description: "The administrator has the highest authority.",
},
{
Name: "monitor",
DisplayName: "Monitor",
Description: "The monitor has the authority to control the games.",
},
{
Name: "user",
DisplayName: "User",
Expand Down
6 changes: 3 additions & 3 deletions internal/hub/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type GameHub struct {
M sync.RWMutex
}

var gameHubs = make(map[int64]*GameHub)
var gameHubs = make(map[uint]*GameHub)
var gameHubsLock sync.Mutex

func ServeGameHub(w http.ResponseWriter, r *http.Request, gameID int64) {
func ServeGameHub(w http.ResponseWriter, r *http.Request, gameID uint) {
gameHubsLock.Lock()
gameHub, ok := gameHubs[gameID]
if !ok {
Expand Down Expand Up @@ -72,7 +72,7 @@ func handleGameHub(gameHub *GameHub) {
}
}

func SendGameMsg(gameID int64, msg interface{}) {
func SendGameMsg(gameID uint, msg interface{}) {
gameHubsLock.Lock()
gameHub, ok := gameHubs[gameID]
gameHubsLock.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions internal/model/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ type Game struct {
func (g *Game) BeforeDelete(db *gorm.DB) (err error) {
db.Table("game_teams").Where("game_id = ?", g.ID).Delete(&GameTeam{})
db.Table("game_challenges").Where("game_id = ?", g.ID).Delete(&GameChallenge{})
db.Table("submissions").Where("game_id = ?", g.ID).Delete(&Submission{})
db.Table("notices").Where("game_id = ?", g.ID).Delete(&Notice{})
return nil
}
1 change: 1 addition & 0 deletions internal/model/game_challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ type GameChallenge struct {

func (g *GameChallenge) BeforeDelete(db *gorm.DB) (err error) {
db.Table("submissions").Where("game_id = ?", g.GameID).Where("challenge_id = ?", g.ChallengeID).Delete(&Submission{})
db.Table("notices").Where("game_id = ?", g.GameID).Where("challenge_id = ?", g.ChallengeID).Delete(&Notice{})
return nil
}
21 changes: 21 additions & 0 deletions internal/model/notice.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package model

import (
"github.com/elabosak233/cloudsdale/internal/hub"
"gorm.io/gorm"
)

type Notice struct {
ID uint `json:"id"` // The game event's id.
Type string `gorm:"type:varchar(16);not null;default:'notice'" json:"type,omitempty"` // The game event's type. (Such as "first_blood", "second_blood", "third_blood", "new_challenge", "new_hint", "normal")
Expand All @@ -15,3 +20,19 @@ type Notice struct {
CreatedAt int64 `gorm:"autoUpdateTime:milli" json:"created_at,omitempty"` // The game event's creation time.
UpdatedAt int64 `gorm:"autoUpdateTime:milli" json:"updated_at,omitempty"` // The game event's last update time.
}

func (n *Notice) AfterCreate(db *gorm.DB) (err error) {
result := db.Table("notices").
Preload("User", func(db *gorm.DB) *gorm.DB {
return db.Select([]string{"id", "username", "nickname", "email"})
}).
Preload("Team", func(db *gorm.DB) *gorm.DB {
return db.Select([]string{"id", "name", "email"})
}).
Preload("Challenge", func(db *gorm.DB) *gorm.DB {
return db.Select([]string{"id", "title"})
}).
First(n, n.ID)
hub.SendGameMsg(*(n.GameID), n)
return result.Error
}
9 changes: 0 additions & 9 deletions internal/model/response/category_response.go

This file was deleted.

34 changes: 0 additions & 34 deletions internal/model/response/challenge_response.go

This file was deleted.

8 changes: 0 additions & 8 deletions internal/model/response/flag_response.go

This file was deleted.

9 changes: 0 additions & 9 deletions internal/model/response/game_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ import (
"github.com/elabosak233/cloudsdale/internal/model"
)

type GameResponse struct {
model.Game
}

type GameSimpleResponse struct {
ID int64 `json:"id"`
Title string `json:"title"`
}

type GameChallengeResponse struct {
*model.Challenge
IsEnabled bool `json:"is_enabled"`
Expand Down
17 changes: 0 additions & 17 deletions internal/model/response/image_response.go

This file was deleted.

7 changes: 0 additions & 7 deletions internal/model/response/port_response.go

This file was deleted.

2 changes: 0 additions & 2 deletions internal/model/response/submission_response.go

This file was deleted.

27 changes: 0 additions & 27 deletions internal/model/response/team_response.go

This file was deleted.

24 changes: 9 additions & 15 deletions internal/model/response/user_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ import (
)

type UserResponse struct {
ID uint `json:"id"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Email string `json:"email"`
GroupID uint `json:"group_id"`
Group *model.Group `json:"group"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
Teams []*TeamSimpleResponse `json:"teams,omitempty"`
}

type UserSimpleResponse struct {
ID uint `xorm:"'id'" json:"id"`
Username string `xorm:"'username'" json:"username"`
Nickname string `xorm:"'nickname'" json:"nickname"`
ID uint `json:"id"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Email string `json:"email"`
GroupID uint `json:"group_id"`
Group *model.Group `json:"group"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
Teams []*model.Team `json:"teams,omitempty"`
}
5 changes: 2 additions & 3 deletions internal/repository/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package repository
import (
"github.com/elabosak233/cloudsdale/internal/model"
"github.com/elabosak233/cloudsdale/internal/model/request"
"github.com/elabosak233/cloudsdale/internal/model/response"
"gorm.io/gorm"
)

type IGameRepository interface {
Insert(game model.Game) (g model.Game, err error)
Update(game model.Game) (err error)
Delete(req request.GameDeleteRequest) (err error)
Find(req request.GameFindRequest) (games []response.GameResponse, count int64, err error)
Find(req request.GameFindRequest) (games []model.Game, count int64, err error)
}

type GameRepository struct {
Expand Down Expand Up @@ -39,7 +38,7 @@ func (t *GameRepository) Delete(req request.GameDeleteRequest) (err error) {
return result.Error
}

func (t *GameRepository) Find(req request.GameFindRequest) (games []response.GameResponse, count int64, err error) {
func (t *GameRepository) Find(req request.GameFindRequest) (games []model.Game, count int64, err error) {
applyFilters := func(q *gorm.DB) *gorm.DB {
if req.ID != 0 {
q = q.Where("id = ?", req.ID)
Expand Down
18 changes: 4 additions & 14 deletions internal/repository/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
)

type IInstanceRepository interface {
Insert(container model.Instance) (c model.Instance, err error)
FindByPodID(podIDs []uint) (containers []model.Instance, err error)
Insert(instance model.Instance) (i model.Instance, err error)
}

type InstanceRepository struct {
Expand All @@ -18,16 +17,7 @@ func NewInstanceRepository(db *gorm.DB) IInstanceRepository {
return &InstanceRepository{db: db}
}

func (t *InstanceRepository) Insert(container model.Instance) (c model.Instance, err error) {
result := t.db.Table("instances").Create(&container)
return container, result.Error
}

func (t *InstanceRepository) FindByPodID(podIDs []uint) (containers []model.Instance, err error) {
result := t.db.Table("instances").
Where("pod_id IN ?", podIDs).
Preload("Image").
Preload("Nats").
Find(&containers)
return containers, result.Error
func (t *InstanceRepository) Insert(instance model.Instance) (i model.Instance, err error) {
result := t.db.Table("instances").Create(&instance)
return instance, result.Error
}
Loading

0 comments on commit 1e1d21f

Please sign in to comment.