go开发游戏开服程序

【go开发游戏开服程序】缥帙各舒散,前后互相逾。这篇文章主要讲述go开发游戏开服程序相关的知识,希望能为你提供帮助。
用go实现自动开服 1.创建游戏库,日志库 2.导入表 3.创建游戏程序 4.修改配置文件

package mainimport ( "bytes" "flag" "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" "io/ioutil" "os" "os/exec" "strconv" "strings" )//执行windows命令 func runInWindows(cmd string) (string, error) { result, err := exec.Command("cmd", "/c", cmd).Output() if err != nil { return "", err } return strings.TrimSpace(string(result)), err }//数据库连接 var GameDB *sqlx.DB//game库连接 var GameLogDB *sqlx.DB //log库连接 //game库连接方式 func init() { database, err := sqlx.Open("mysql", "gameuser:xxxx@tcp(10.41.3.138:3306)/") if err != nil { fmt.Println("open mysql failed,", err) return } GameDB = database defer GameDB.Close() // 注意这行代码要写在上面err判断的下面 }//log库连接方式 func init() { database, err := sqlx.Open("mysql", "gameuser:xxx@tcp(10.41.3.138:3306)/") if err != nil { fmt.Println("open mysql failed,", err) return } GameLogDB = database defer GameLogDB.Close() // 注意这行代码要写在上面err判断的下面 }//数据库创建授权 func createDb(plat string, zone int) { db, err := sqlx.Open("mysql", "gameuser:xxxx@tcp(10.41.3.138:3306)/") if err != nil { fmt.Println("数据库连接错误", err) } gamedb := fmt.Sprintf("game_sczz_%s_%s", plat, strconv.Itoa(zone)) gamelogdb := fmt.Sprintf("gamelog_sczz_%s_%s", plat, strconv.Itoa(zone)) _, dberr := db.Exec("CREATE DATABASE " + gamedb + " charset utf8 COLLATE utf8_general_ci") if dberr != nil { fmt.Println("游戏数据库创建错误???--> ", gamedb, dberr) return } else { fmt.Println("游戏数据库创建成功!!!--> ", gamedb) } _, dblogerr := db.Exec("CREATE DATABASE " + gamelogdb + " charset utf8 COLLATE utf8_general_ci") if dblogerr != nil { fmt.Println("日志数据库创建错误???--> ", gamelogdb, dblogerr) return } else { fmt.Println("日志数据库创建成功!!!--> ", gamelogdb) } }//数据库导入 func createTable(plat string, zone int) { gamedb := fmt.Sprintf("game_sczz_%s_%s", plat, strconv.Itoa(zone)) gamelogdb := fmt.Sprintf("gamelog_sczz_%s_%s", plat, strconv.Itoa(zone)) gamesqlcmd := fmt.Sprintf("mysql -h 10.41.3.138 -ugameuser-pxxx%s < gamedb.sql", (gamedb))//gamesql导入命令 gamelogsqlcmd := fmt.Sprintf("mysql -h10.41.3.138-ugameuser -pxxx %s < gamelogdb.sql", (gamelogdb)) //gamelog导入命令 fmt.Println("分别执行的导入命令", gamesqlcmd, gamelogsqlcmd) gamesqlresult, gamesqlerr := runInWindows(gamesqlcmd) if gamesqlerr != nil { fmt.Println("gamesql导入失败???", gamesqlerr) } else { fmt.Println("gamesql导入成功!!!") } fmt.Println(gamesqlresult) gamelogsqlresult, gamelogsqlerr := runInWindows(gamelogsqlcmd) if gamelogsqlerr != nil { fmt.Println("gamelogsql 导入失败???", gamelogsqlerr) } else { fmt.Println("gamelogsql导入成功!!!") } fmt.Println(gamelogsqlresult)}//设定game程序 func configGame(plat string, zone int) { path := fmt.Sprintf("game\\\\sczz_%s_%s /E /X /O /K /H /I > nul", plat, strconv.Itoa(zone)) fmt.Println("path is ", path) //server重命名sczz_maowang_1 gamecmd := fmt.Sprintf("xcopyserver %s ", path) //设定新的目录名称 fmt.Println("game xcopy command is ", gamecmd) gameresult, err := runInWindows(gamecmd) if err != nil { fmt.Println("game程序设定错误???")} else { fmt.Println("game程序设定成功!!!") } fmt.Println(gameresult)}//设定接口程序 func configInterface(plat string, zone int) { webdir := fmt.Sprintf("web\\\\web_sczz_%s", plat) _, err := os.Stat(webdir) if err != nil { os.Mkdir(webdir, 0766) } path := fmt.Sprintf("web\\\\web_sczz_%s\\\\s%s /E /X /O /K /H /I > nul", plat, strconv.Itoa(zone)) fmt.Println("interface path is ...", path) interfacecmd := fmt.Sprintf("xcopy interface %s", path) //设定新的目录名称 fmt.Println("interfacecmd is ....", interfacecmd) interfaceresult, err := runInWindows(interfacecmd) if err != nil { fmt.Println("interfacecmd程序设定错误???")} else { fmt.Println("game程序设定成功!!!") } fmt.Println(interfaceresult)}//判断搭建区服是否存在 func IsDir(plat string, zone int) bool { //判断区服是否存在 path := fmt.Sprintf("game/sczz_%s_%s", plat, strconv.Itoa(zone)) _, err := os.Stat(path) if err != nil { fmt.Println("游戏还么有创建,开始搭建区服!!!") return false } return true }//修改配置文件端口号 func modifyPort(plat string, zone int) { initport := 4000 rpcport := initport + zone gamedb := fmt.Sprintf("game_sczz_%s_%s", plat, strconv.Itoa(zone)) gamelogdb := fmt.Sprintf("gamelog_sczz_%s_ %s", plat, strconv.Itoa(zone)) configFile := fmt.Sprintf("web\\\\web_sczz_%s\\\\s%s\\\\Web.config", plat, strconv.Itoa(zone)) fmt.Println(rpcport, gamedb, gamelogdb, configFile) xmlParam, err := ioutil.ReadFile(configFile) if err != nil { fmt.Println("文件读取失败!!!") return } if !bytes.Contains(xmlParam, []byte("4001")) { fmt.Println("4001不存在,替换失败!!!") return } if !bytes.Contains(xmlParam, []byte("game_sczz_maowang_1")) { fmt.Println("game_sczz_maowang_1不存在,替换失败!!!") return } if !bytes.Contains(xmlParam, []byte("gamelog_sczz_maowang_1")) { fmt.Println("gamelog_sczz_maowang_1不存在,替换失败!!!") return }xmlParam = bytes.ReplaceAll(xmlParam, []byte("4001"), []byte(strconv.Itoa(rpcport))) ioutil.WriteFile(configFile, xmlParam, os.ModePerm)}//修改配置文件gamedb func modifyGamedb(plat string, zone int) { gamedb := fmt.Sprintf("game_sczz_%s_%s", plat, strconv.Itoa(zone)) configFile := fmt.Sprintf("web\\\\web_sczz_%s\\\\s%s\\\\Web.config", plat, strconv.Itoa(zone)) xmlParam2, err := ioutil.ReadFile(configFile) if err != nil { fmt.Println("文件读取失败!!!") return }if !bytes.Contains(xmlParam2, []byte("game_sczz_maowang_1")) { fmt.Println("game_sczz_maowang_1不存在,替换失败!!!") return } xmlParam2 = bytes.ReplaceAll(xmlParam2, []byte("game_sczz_maowang_1"), []byte(gamedb)) ioutil.WriteFile(configFile, xmlParam2, os.ModePerm) }//修改gamelogdb func modifyGameLogdb(plat string, zone int) { gamelogdb := fmt.Sprintf("gamelog_sczz_%s_%s", plat, strconv.Itoa(zone)) configFile := fmt.Sprintf("web\\\\web_sczz_%s\\\\s%s\\\\Web.config", plat, strconv.Itoa(zone)) xmlParam3, err := ioutil.ReadFile(configFile) if err != nil { fmt.Println("gamelog文件读取失败!!!") return } if !bytes.Contains(xmlParam3, []byte("gamelog_sczz_maowang_1")) { fmt.Println("gamelog_sczz_maowang_1不存在,替换失败!!!") return } xmlParam3 = bytes.ReplaceAll(xmlParam3, []byte("gamelog_sczz_maowang_1"), []byte(gamelogdb)) ioutil.WriteFile(configFile, xmlParam3, os.ModePerm) } func main() { var plat string var zone int flag.StringVar(& plat, "a", "maowang", "平台") flag.IntVar(& zone, "z", 1000, "区服") flag.Parse() if flag.NFlag() < 2 { fmt.Println("参数使用错误") return } fmt.Println("你输入的信息是:") fmt.Println("平台: ", plat) fmt.Println("区服: ", zone) var commit string fmt.Println("请输入确认信息yes,代表确认无误可以往下执行!!!") fmt.Scan(& commit) if commit != "yes" { return } //判断此区服是否搭建 if IsDir(plat, zone) { fmt.Println("区服已经创建过了,不需创建,退出!!!") return } //创建数据库 createDb(plat, zone) //创建表和存储过程 createTable(plat, zone) //设定game程序 configGame(plat, zone) //设定网关程序 configInterface(plat, zone) //modifyConfig(plat, zone) //修改配置文件选项 fmt.Println("开始修改配置文件:") modifyPort(plat, zone) modifyGamedb(plat, zone) modifyGameLogdb(plat, zone)}


    推荐阅读