Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
gamebox: reset gamebox (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhan005 authored Feb 1, 2021
1 parent fc1ea6e commit a45a2ec
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 11 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ require (
github.com/theplant/cldr v0.0.0-20190423050709-9f76f7ce4ee8 // indirect
github.com/theplant/htmltestingutils v0.0.0-20190423050759-0e06de7b6967 // indirect
github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61 // indirect
github.com/vidar-team/Cardinal_frontend v0.7.2
github.com/vidar-team/Cardinal_manager_frontend v0.7.2
github.com/vidar-team/Cardinal_frontend v0.7.3
github.com/vidar-team/Cardinal_manager_frontend v0.7.3
github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd // indirect
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd
golang.org/x/text v0.3.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ github.com/vidar-team/Cardinal_frontend v0.7.0 h1:7RcqGZcLy6bMXMBZvIbIoVC08mzdBF
github.com/vidar-team/Cardinal_frontend v0.7.0/go.mod h1:wD4qPD/Z+T3eT0SiOQe9262VY6xSyWz12UQXmkH9uiI=
github.com/vidar-team/Cardinal_frontend v0.7.2 h1:BvaBbfw+nw0fOdhTIAz7qPTqLqFdVLQd/ggP4XYWX4o=
github.com/vidar-team/Cardinal_frontend v0.7.2/go.mod h1:wD4qPD/Z+T3eT0SiOQe9262VY6xSyWz12UQXmkH9uiI=
github.com/vidar-team/Cardinal_frontend v0.7.3 h1:9lyBHUfnGIoegcIpen3n8S99cYYc+lS7hSW5MMV4O60=
github.com/vidar-team/Cardinal_frontend v0.7.3/go.mod h1:wD4qPD/Z+T3eT0SiOQe9262VY6xSyWz12UQXmkH9uiI=
github.com/vidar-team/Cardinal_manager_frontend v0.7.0 h1:ETcUmcSkh8cZYV2dSKioRM4bnAhnTNjEFKwD8yW4nCQ=
github.com/vidar-team/Cardinal_manager_frontend v0.7.0/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk=
github.com/vidar-team/Cardinal_manager_frontend v0.7.1-1 h1:vCxLOT54YmYf8hB9QdqB7VVVOhxdTIMpcLUE33E8FcE=
Expand All @@ -331,6 +333,8 @@ github.com/vidar-team/Cardinal_manager_frontend v0.7.1 h1:HgqOwST8hmFHD1w9zSQy3c
github.com/vidar-team/Cardinal_manager_frontend v0.7.1/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk=
github.com/vidar-team/Cardinal_manager_frontend v0.7.2 h1:DjSV6rZGl3M8o8xcBY6agNxRlgc5r71Gvimja/viBIE=
github.com/vidar-team/Cardinal_manager_frontend v0.7.2/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk=
github.com/vidar-team/Cardinal_manager_frontend v0.7.3 h1:FyHJlb1NV0GgIEDvXxtTwUMWu40vpfldEh9ZSmWFBqE=
github.com/vidar-team/Cardinal_manager_frontend v0.7.3/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
Expand Down
7 changes: 4 additions & 3 deletions internal/db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ type WebHook struct {
type DynamicConfig struct {
gorm.Model `json:"-"`

Key string
Value string
Kind int8
Key string
Value string
Kind int8
Options string
}
22 changes: 17 additions & 5 deletions internal/dynamic_config/dynamic_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dynamic_config

import (
"github.com/gin-gonic/gin"

"github.com/vidar-team/Cardinal/internal/db"
"github.com/vidar-team/Cardinal/internal/locales"
"github.com/vidar-team/Cardinal/internal/utils"
Expand All @@ -16,21 +17,32 @@ func Init() {
initConfig(utils.FLAG_SUFFIX_CONF, "}", utils.STRING)
initConfig(utils.ANIMATE_ASTEROID, utils.BOOLEAN_FALSE, utils.BOOLEAN)
initConfig(utils.SHOW_OTHERS_GAMEBOX, utils.BOOLEAN_FALSE, utils.BOOLEAN)
initConfig(utils.DEFAULT_LANGUAGE, "zh-CN", utils.SELECT, "zh-CN|en-US")
}

// initConfig set the default value of the given key.
// Always used in installation.
func initConfig(key string, value string, kind int8) {
func initConfig(key string, value string, kind int8, option ...string) {
var opt string
if len(option) != 0 {
opt = option[0]
}

db.MySQL.Model(&db.DynamicConfig{}).FirstOrCreate(&db.DynamicConfig{
Key: key,
Value: value,
Kind: kind,
Key: key,
Value: value,
Kind: kind,
Options: opt,
}, "`key` = ?", key)
}

// Set update the config by insert a new record into database, for we can make a config version control soon.
// Then refresh the config in struct.
func Set(key string, value string) {
if key == utils.DATBASE_VERSION {
return
}

db.MySQL.Model(&db.DynamicConfig{}).Where("`key` = ?", key).Update(&db.DynamicConfig{
Key: key,
Value: value,
Expand Down Expand Up @@ -77,6 +89,6 @@ func GetConfig(c *gin.Context) (int, interface{}) {
// GetAllConfig is the HTTP handler used to return the all the configs.
func GetAllConfig(c *gin.Context) (int, interface{}) {
var config []db.DynamicConfig
db.MySQL.Model(&db.DynamicConfig{}).Find(&config)
db.MySQL.Model(&db.DynamicConfig{}).Where("`key` != ?", utils.DATBASE_VERSION).Find(&config)
return utils.MakeSuccessJSON(config)
}
12 changes: 12 additions & 0 deletions internal/game/gamebox.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"

"github.com/vidar-team/Cardinal/internal/db"
"github.com/vidar-team/Cardinal/internal/dynamic_config"
"github.com/vidar-team/Cardinal/internal/locales"
Expand Down Expand Up @@ -223,3 +224,14 @@ func GetOthersGameBox(c *gin.Context) (int, interface{}) {
func CleanGameBoxStatus() {
db.MySQL.Model(&db.GameBox{}).Update(map[string]interface{}{"is_down": false, "is_attacked": false})
}

func ResetAllGameBoxes(c *gin.Context) (int, interface{}) {
db.MySQL.Model(&db.AttackAction{}).Delete(&db.AttackAction{})
db.MySQL.Model(&db.DownAction{}).Delete(&db.DownAction{})

CleanGameBoxStatus()
SetRankList()

logger.New(logger.IMPORTANT, "manager_operate", string(locales.I18n.T(c.GetString("lang"), "gamebox.reset_success")))
return utils.MakeSuccessJSON(locales.I18n.T(c.GetString("lang"), "gamebox.reset_success"))
}
5 changes: 4 additions & 1 deletion internal/route/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/gin-contrib/cors"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"

"github.com/vidar-team/Cardinal/conf"
"github.com/vidar-team/Cardinal/frontend"
"github.com/vidar-team/Cardinal/internal/asteroid"
Expand Down Expand Up @@ -57,7 +58,8 @@ func Init() *gin.Engine {

api.GET("/base", func(c *gin.Context) {
c.JSON(utils.MakeSuccessJSON(gin.H{
"Title": dynamic_config.Get(utils.TITLE_CONF),
"Title": dynamic_config.Get(utils.TITLE_CONF),
"Language": dynamic_config.Get(utils.DEFAULT_LANGUAGE),
}))
})

Expand Down Expand Up @@ -124,6 +126,7 @@ func Init() *gin.Engine {
// TODO: i18n
c.JSON(utils.MakeSuccessJSON("刷新 Flag 操作已执行,请在数据大屏查看是否有错误信息"))
})
managerRouter.GET("/gameboxes/reset", __(game.ResetAllGameBoxes))

// Team
managerRouter.GET("/teams", __(team.GetAllTeams))
Expand Down
2 changes: 2 additions & 0 deletions internal/utils/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
FLAG_SUFFIX_CONF = "flag_suffix"
ANIMATE_ASTEROID = "animate_asteroid"
SHOW_OTHERS_GAMEBOX = "show_others_gamebox"
DEFAULT_LANGUAGE = "default_language"

BOOLEAN_TRUE = "true"
BOOLEAN_FALSE = "false"
Expand All @@ -24,4 +25,5 @@ const (
// Config type
STRING = iota
BOOLEAN
SELECT
)
1 change: 1 addition & 0 deletions locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ en-US:
repeat: "Duplicated Gamebox Found!"
not_found: "Gamebox Not Found!"
auto_refresh_flag_error: "Missing GameBox SSH!"
reset_success: "Reset gameboxes successfully!"

team:
post_error: "Add Team Failed!"
Expand Down
1 change: 1 addition & 0 deletions locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ zh-CN:
repeat: "存在重复添加的靶机,请检查"
not_found: "靶机不存在!"
auto_refresh_flag_error: "靶机 SSH 设置为空"
reset_success: "重置靶机信息成功"

team:
post_error: "添加队伍失败!"
Expand Down

0 comments on commit a45a2ec

Please sign in to comment.