func OnlineCountryServer(w http.ResponseWriter, req *http.Request) { defer req.Body.Close() min := int((GetUnixTime())/60) - 1 if last_online_country_min == min && last_online_country_out_string != "" { io.WriteString(w, last_online_country_out_string+"\n") logging.Debug("quest online num:%s,%s,%d,%s", req.RemoteAddr, req.URL.Path, min, last_online_country_out_string) return } last_online_country_min = min rows, res, err := db_login.Query("SELECT zone FROM zoneInfo") if err != nil { logging.Error("select err:%s", err.Error()) return } zones := make(map[int]int) for _, row := range rows { zoneid := res.Map("zone") id := row.Int(zoneid) zones[(id<<16)+3] = 0 zones[(id<<16)+4] = 0 zones[(id<<16)+5] = 0 zones[(id<<16)+6] = 0 } rows, res, err = db_monitor.Query("select * from COUNTRYONLINENUM_TODAY where timestamp_min = " + strconv.Itoa(min)) if err != nil { logging.Error("select err:%s", err.Error()) } if len(rows) == 0 { min = int((GetUnixTime())/60) - 2 rows, res, err = db_monitor.Query("select * from COUNTRYONLINENUM_TODAY where timestamp_min = " + strconv.Itoa(min)) if err != nil { logging.Error("select err:%s", err.Error()) } } last_online_country_out_string = "" for _, row := range rows { zoneid := res.Map("ZONEID") country := res.Map("COUNTRY") onlinenum := res.Map("ONLINENUM") zones[int(((row.Int(zoneid)&0X0000FFFF)<<16)+row.Int(country))] = row.Int(onlinenum) } for id, num := range zones { if num > 0 { last_online_country_out_string += strconv.Itoa(int(id>>16)) + "\\" + strconv.Itoa(int(int16(id))) + "\\" + strconv.Itoa(num) + ";" } } io.WriteString(w, last_online_country_out_string+"\n") logging.Debug("quest online num:%s,%s,%d,%s", req.RemoteAddr, req.URL.Path, min, last_online_country_out_string) }
func GetZonenameByZoneid(zoneid uint32) (int, string) { query_string := "select GAME,NAME from zoneInfo where zone = " + strconv.Itoa(int(zoneid)) rows, res, err := db.Query(query_string) if err != nil { logging.Error("select err:%s,%s", err.Error(), zoneid) return 0, "" } if len(rows) == 0 { return 3055, "none" } game := res.Map("GAME") name := res.Map("NAME") convert, err1 := iconv.Open("GB2312", "UTF-8") for _, row := range rows { gamename := row.Str(name) if err1 == nil { var err2 error gamename, err2 = convert.ConvString(row.Str(name)) if err2 != nil { gamename = row.Str(name) } } return row.Int(game), gamename } return 0, "" }
func (self *GameLoginBw) Connect() *net.TCPConn { cfg := config.NewConfig() if err := cfg.LoadFromFile(config.GetConfigStr("loginServerList"), "LoginServerList"); err != nil { logging.Error("init game err,%s", err.Error()) return nil } addr := cfg.GetConfigStr("ip") + ":" + cfg.GetConfigStr("port") raddr, _ := net.ResolveTCPAddr("tcp", addr) conn, err := net.DialTCP("tcp", nil, raddr) if err != nil { logging.Error("conn err:%s,1%s", addr, err.Error()) return nil } logging.Debug("new connection:%s", conn.RemoteAddr()) return conn }
func HandleGonghuiKeyCard(w http.ResponseWriter, req *http.Request) { defer req.Body.Close() var keystring string var count int rows, res, err := db_monitor.Query("SELECT COUNT(USERID) AS COUNT FROM SDO_GONGHUI_CARD_USER WHERE KEYSTRING = '" + req.FormValue("KeyString") + "'") if err != nil { logging.Error("select err:%s", err.Error()) return } if len(rows) != 0 { COUNT := res.Map("COUNT") count = rows[0].Int(COUNT) } ret := SdoRetCardCount{ Return_code: 0, Return_message: "", Data: SdoRetCardCountData{ KeyString: keystring, Count: uint(count), }, } b, _ := json.Marshal(ret) w.Write(b) logging.Debug("gonghui keystring:%s", req.FormValue("sndaId")) }
func HandleMaxlevel(w http.ResponseWriter, req *http.Request) { defer req.Body.Close() maxlevel := 0 sndaid, _ := strconv.Atoi(req.FormValue("sndaId")) rows, res, err := db_monitor.Query("SELECT MAXLEVEL FROM USERMAXLEVEL WHERE ACCOUNT = 'S:" + req.FormValue("sndaId") + ":" + req.FormValue("sndaId") + "'") if err != nil { logging.Error("select err:%s", err.Error()) return } if len(rows) != 0 { level := res.Map("MAXLEVEL") maxlevel = rows[0].Int(level) } ret := SdoRet{ Return_code: 0, Return_message: "", Data: SdoRetData{ SndaId: uint(sndaid), MaxLevel: maxlevel, }, } b, _ := json.Marshal(ret) w.Write(b) logging.Debug("maxlevel:%s,%d", req.FormValue("sndaId"), maxlevel) }
//get platname by urlstring usl master as /game/plat func GetPlatNameByUrl(url string) string { if ok, err := regexp.MatchString("^/.*/.*/.*$", url); ok == false || err != nil { logging.Error("url string err:%s", url) return "" } return strings.Split(url, "/")[2] }
func OnlineServer(w http.ResponseWriter, req *http.Request) { defer req.Body.Close() min := int((GetUnixTime())/60) - 1 if last_online_min == min && last_online_out_string != "" { io.WriteString(w, last_online_out_string+"\n") logging.Debug("quest online num:%s,%s,%d,%s", req.RemoteAddr, req.URL.Path, min, last_online_out_string) return } last_online_min = min rows, res, err := db_login.Query("SELECT game,zone,name FROM zoneInfo") if err != nil { logging.Error("select err:%s", err.Error()) return } zones := make(map[int]int) for _, row := range rows { zoneid := res.Map("zone") gameid := res.Map("game") name := res.Map("name") id := row.Int(zoneid) zones[id] = 0 zoneid_map[uint32(row.Int(gameid)<<16+row.Int(zoneid))] = row.Str(name) } rows, res, err = db_monitor.Query("select * from ONLINENUM_TODAY where timestamp_min = " + strconv.Itoa(min)) if err != nil { logging.Error("select err:%s", err.Error()) } if len(rows) == 0 { min = int((GetUnixTime())/60) - 2 rows, res, err = db_monitor.Query("select * from ONLINENUM_TODAY where timestamp_min = " + strconv.Itoa(min)) if err != nil { logging.Error("select err:%s", err.Error()) } } last_online_out_string = "" for _, row := range rows { zoneid := res.Map("zone_id") onlinenum := res.Map("online_number") zones[int(int16(row.Int(zoneid)))] = row.Int(onlinenum) } for id, num := range zones { last_online_out_string += strconv.Itoa(int(int16(id))) + "\\" + strconv.Itoa(num) + ";" } io.WriteString(w, last_online_out_string+"\n") logging.Debug("quest online num:%s,%s,%d,%s", req.RemoteAddr, req.URL.Path, min, last_online_out_string) }
func Billing(game string, server_id string, myaccid uint32, charid uint32, moneynum uint32) error { if gamefun, ok := billMap[game]; ok == true { zoneid, _ := strconv.Atoi(server_id) return gamefun.Billing(uint32(zoneid), myaccid, charid, moneynum) } else { logging.Error("not game func for game:%s", game) } return nil }
func AddLoginToken(game string, server_id string, myaccount string, myaccid uint32, isAdult uint32, token string, w http.ResponseWriter, platname string, req *http.Request, err_url string) error { if gamefun, ok := loginMap[game]; ok == true { zoneid, _ := strconv.Atoi(server_id) return gamefun.Login(uint32(zoneid), myaccount, myaccid, isAdult, token, w, platname, req, err_url) } else { logging.Error("not game func for game:%s", game) } return nil }
func addMyAccount(channel, account, accountid string) (string, uint32, error) { query_string := "insert into ACCOUNT (CHANNEL,ACCOUNT,ACCOUNTID) values( '" + channel + "' , '" + account + "' , " + accountid + ")" _, res, err := db.Query(query_string) if err != nil { logging.Error("insert err:%s", err.Error()) return "", 0, err } logging.Info("new account,%s,%d", channel+":"+account+":"+accountid, uint32(res.InsertId())) return channel + ":" + account + ":" + accountid, uint32(res.InsertId()), nil }
func HandleGonghuiUserCard(w http.ResponseWriter, req *http.Request) { defer req.Body.Close() sndaid, _ := strconv.Atoi(req.FormValue("sndaId")) maxlevel := 0 keystring := "" userid := 0 name := "" accid := 0 plat_accid := 0 zone := 0 rows, res, err := db_monitor.Query("SELECT KEYSTRING,USERID,NAME,`ZONE`,ACCID,PLAT_ACCID FROM SDO_GONGHUI_CARD_USER WHERE PLAT_ACCID = " + req.FormValue("sndaId")) if err != nil { logging.Error("select err:%s", err.Error()) return } convert, err1 := iconv.Open("GB2312", "UTF-8") if len(rows) != 0 { KEYSTRING := res.Map("KEYSTRING") USERID := res.Map("USERID") NAME := res.Map("NAME") ZONE := res.Map("ZONE") ACCID := res.Map("ACCID") PLAT_ACCID := res.Map("PLAT_ACCID") keystring = rows[0].Str(KEYSTRING) userid = rows[0].Int(USERID) if err1 == nil { var err2 error name, err2 = convert.ConvString(rows[0].Str(NAME)) if err2 != nil { name = rows[0].Str(NAME) } } else { name = rows[0].Str(NAME) } accid = rows[0].Int(ACCID) zone = rows[0].Int(ZONE) plat_accid = rows[0].Int(PLAT_ACCID) } ret := SdoRetCard{ Return_code: 0, Return_message: "", Data: SdoRetCardData{ SndaId: uint(sndaid), KeyString: keystring, Charid: uint(userid), Name: name, Accid: uint(accid), Zone: uint(zone), Plat_accid: uint(plat_accid), }, } b, _ := json.Marshal(ret) w.Write(b) logging.Debug("gonghui card:%s,%d", req.FormValue("sndaId"), maxlevel) }
func GetMyAccountByAccountId(channel, accountid string) (uint32, error) { if accountid == "" { accountid = "0" } query_string := "select accid from ACCOUNT where accountid = '" + accountid + "' and channel = '" + channel + "'" row, res, err := db.QueryFirst(query_string) if err != nil { logging.Error("select err:%s", err.Error()) return 0, err } if len(row) == 0 { return 0, nil } return uint32(row.Int(res.Map("accid"))), nil }
func GetAccidBySndaId(sndastr string) (uint32, uint32) { sndaid, _ := strconv.Atoi(sndastr) accid := uint32(0) rows, res, err := db_account.Query("SELECT ACCID FROM ACCOUNT WHERE ACCOUNTID = " + strconv.Itoa(int(sndaid))) if err != nil { logging.Error("select err:%s", err.Error()) return accid, uint32(sndaid) } if len(rows) != 0 { id := res.Map("ACCID") accid = uint32(rows[0].Int(id)) } logging.Debug("accid:%d,%d", sndaid, accid) return accid, uint32(sndaid) }
func GetAllZoneCharNameByAccid(server_id string, myaccid uint32) []CharName { query_string := "select ID,NAME from CHARNAME where ACCID = " + strconv.Itoa(int(myaccid)) + " and ZONE = " + server_id rows, res, err := db_checkname.Query(query_string) if err != nil { logging.Error("select err:%s", err.Error()) return nil } map_id := res.Map("ID") map_name := res.Map("NAME") names := make([]CharName, len(rows)) for i, row := range rows { names[i].CharId = row.Str(map_id) names[i].CharName = row.Str(map_name) } return names }
func main() { flag.Parse() config.SetConfig("config", *flag.String("config", "config.xml", "config xml file for start")) config.SetConfig("logfilename", *flag.String("logfilename", "/log/logfilename.log", "log file name")) config.SetConfig("deamon", *flag.String("deamon", "false", "need run as demo")) config.SetConfig("port", *flag.String("port", "8000", "http port ")) config.SetConfig("log", *flag.String("log", "debug", "logger level ")) config.LoadFromFile(config.GetConfigStr("config"), "global") if err := config.LoadFromFile(config.GetConfigStr("config"), "SdoOnlineServer"); err != nil { fmt.Println(err) return } logger, err := logging.NewTimeRotationHandler(config.GetConfigStr("logfilename"), "060102-15") if err != nil { fmt.Println(err) return } logger.SetLevel(logging.DEBUG) logging.AddHandler("SDO", logger) mysqlurl := config.GetConfigStr("mysql") if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil { logging.Error("mysql config syntax err:%s", mysqlurl) return } logging.Info("server starting...") mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1) mysqlurl = strings.Replace(mysqlurl, "@", ":", 1) mysqlurl = strings.Replace(mysqlurl, "/", ":", 1) mysqlurls := strings.Split(mysqlurl, ":") config.SetConfig("dbname", mysqlurls[4]) db_login = mysql.New("tcp", "", mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4]) if err != nil { logging.Error("db connect error:%s", err.Error()) return } mysqlurl = config.GetConfigStr("mysql_monitor") if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil { logging.Error("mysql config syntax err:%s", mysqlurl) return } mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1) mysqlurl = strings.Replace(mysqlurl, "@", ":", 1) mysqlurl = strings.Replace(mysqlurl, "/", ":", 1) mysqlurls = strings.Split(mysqlurl, ":") config.SetConfig("dbname", mysqlurls[4]) db_monitor = mysql.New("tcp", "", mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4]) if err != nil { logging.Error("db connect error:%s", err.Error()) return } mysqlurl = config.GetConfigStr("mysql_account") if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil { logging.Error("mysql config syntax err:%s", mysqlurl) return } mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1) mysqlurl = strings.Replace(mysqlurl, "@", ":", 1) mysqlurl = strings.Replace(mysqlurl, "/", ":", 1) mysqlurls = strings.Split(mysqlurl, ":") config.SetConfig("dbname", mysqlurls[4]) db_account = mysql.New("tcp", "", mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4]) if err != nil { logging.Error("db connect error:%s", err.Error()) return } http.HandleFunc("/online", OnlineServer) http.HandleFunc("/online/country", OnlineCountryServer) http.HandleFunc("/gjia/maxlevel", HandleMaxlevel) http.HandleFunc("/gjia/isprecreate", HandleIsPrecreate) http.HandleFunc("/gjia/isonline", HandleIsOnline) http.HandleFunc("/card/gonghuiuser", HandleGonghuiUserCard) http.HandleFunc("/card/gonghuikey", HandleGonghuiKeyCard) err = http.ListenAndServe(":"+config.GetConfigStr("port"), nil) if err != nil { logging.Error("ListenAndServe:%s", err.Error()) } logging.Info("server stop...") }
func main() { flag.Parse() config.SetConfig("config", *flag.String("config", "config.xml", "config xml file for start")) config.SetConfig("logfilename", *flag.String("logfilename", "/log/authserver.log", "log file name")) config.SetConfig("daemon", *flag.String("daemon", "false", "need run as daemon")) config.SetConfig("port", *flag.String("port", "8000", "http port ")) config.SetConfig("log", *flag.String("log", "debug", "logger level ")) config.SetConfig("loginServerList", *flag.String("loginServerList", "loginServerList.xml", "server list config")) config.SetConfig("bw_plugin", *flag.String("bw_plugin", "game/bw/", "bw_plugin dir")) config.LoadFromFile(config.GetConfigStr("config"), "global") if err := config.LoadFromFile(config.GetConfigStr("config"), "AuthServer"); err != nil { fmt.Println(err) return } if err := config.LoadFromFile(config.GetConfigStr("key"), "Key"); err != nil { fmt.Println(err) return } logger, err := logging.NewTimeRotationHandler(config.GetConfigStr("logfilename"), "060102-15") if err != nil { fmt.Println(err) return } logger.SetLevel(logging.DEBUG) logging.AddHandler("AU", logger) if config.GetConfigStr("daemon") == "true" { logging.DisableStdout() } logging.Info("server starting...") mysqlurl := config.GetConfigStr("mysql") if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil { logging.Error("mysql config syntax err:%s", mysqlurl) return } mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1) mysqlurl = strings.Replace(mysqlurl, "@", ":", 1) mysqlurl = strings.Replace(mysqlurl, "/", ":", 1) mysqlurls := strings.Split(mysqlurl, ":") config.SetConfig("dbname", mysqlurls[4]) db.InitDatabase(mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4]) mysqlurl = config.GetConfigStr("mysql_checkname") if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil { logging.Error("mysql config syntax err:%s", mysqlurl) return } mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1) mysqlurl = strings.Replace(mysqlurl, "@", ":", 1) mysqlurl = strings.Replace(mysqlurl, "/", ":", 1) mysqlurls = strings.Split(mysqlurl, ":") db.InitCheckNameDatabase(mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4]) plat.InitPlat() game.InitLogin() game.InitBill() //addr, err := net.InterfaceByIndex(0) ifname, err := net.InterfaceByName(config.GetConfigStr("ifname")) if err != nil { fmt.Println("GetLocalIp Err:", err) logging.Debug("GetLocalIp Err:%s", err.Error()) } addrs, err := ifname.Addrs() if err != nil { fmt.Println("GetLocalIp Err:", err) logging.Debug("GetLocalIp Err:%s", err.Error()) } else { fmt.Println(addrs[0].String(), addrs[0].Network(), strings.Split(addrs[0].String(), "/")[0]) } //err = http.ListenAndServe(strings.Split(addrs[0].String(), "/")[0]+":"+config.GetConfigStr("port"), nil) err = http.ListenAndServe(":"+config.GetConfigStr("port"), nil) if err != nil { fmt.Println(err) logging.Debug("ListenAndServe:%s", err.Error()) } logging.Info("server stop...") }