From a45a2ecfe23ab2c194a08264a3f45cde34179698 Mon Sep 17 00:00:00 2001 From: E99p1ant Date: Mon, 1 Feb 2021 23:03:34 +0800 Subject: [PATCH] gamebox: reset gamebox (#110) --- go.mod | 4 ++-- go.sum | 4 ++++ internal/db/model.go | 7 ++++--- internal/dynamic_config/dynamic_config.go | 22 +++++++++++++++++----- internal/game/gamebox.go | 12 ++++++++++++ internal/route/router.go | 5 ++++- internal/utils/const.go | 2 ++ locales/en-US.yml | 1 + locales/zh-CN.yml | 1 + 9 files changed, 47 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 61e29ab..c2bdf23 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 6c5cc38..2150aa8 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/internal/db/model.go b/internal/db/model.go index bce077b..e39b99b 100644 --- a/internal/db/model.go +++ b/internal/db/model.go @@ -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 } diff --git a/internal/dynamic_config/dynamic_config.go b/internal/dynamic_config/dynamic_config.go index 010dbec..3117b51 100644 --- a/internal/dynamic_config/dynamic_config.go +++ b/internal/dynamic_config/dynamic_config.go @@ -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" @@ -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, @@ -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) } diff --git a/internal/game/gamebox.go b/internal/game/gamebox.go index f2a40e2..49c3730 100644 --- a/internal/game/gamebox.go +++ b/internal/game/gamebox.go @@ -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" @@ -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")) +} diff --git a/internal/route/router.go b/internal/route/router.go index e1913d5..d185e32 100644 --- a/internal/route/router.go +++ b/internal/route/router.go @@ -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" @@ -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), })) }) @@ -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)) diff --git a/internal/utils/const.go b/internal/utils/const.go index 932c1dd..abc957d 100644 --- a/internal/utils/const.go +++ b/internal/utils/const.go @@ -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" @@ -24,4 +25,5 @@ const ( // Config type STRING = iota BOOLEAN + SELECT ) diff --git a/locales/en-US.yml b/locales/en-US.yml index c65c65d..bb2e123 100644 --- a/locales/en-US.yml +++ b/locales/en-US.yml @@ -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!" diff --git a/locales/zh-CN.yml b/locales/zh-CN.yml index 9d974bb..be286c1 100644 --- a/locales/zh-CN.yml +++ b/locales/zh-CN.yml @@ -59,6 +59,7 @@ zh-CN: repeat: "存在重复添加的靶机,请检查" not_found: "靶机不存在!" auto_refresh_flag_error: "靶机 SSH 设置为空" + reset_success: "重置靶机信息成功" team: post_error: "添加队伍失败!"