func (this *Database) ExecSql(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var sqlstr string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &sqlstr, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r sql.Result var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if r, err = sqlconn.Exec(sqlstr); err != nil { log.LogError("sql:", sqlstr) server.Check(app.Call(nil, callback, callbackparams, 0, err.Error())) return 0, nil } eff, _ := r.RowsAffected() server.Check(app.Call(nil, callback, callbackparams, eff, "")) return 0, nil }
func (pl *PlayerList) GetPlayerBase(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { r := server.NewMessageReader(msg) account, err := r.ReadString() if server.Check(err) { return 0, nil } role_name, err := r.ReadString() if server.Check(err) { return 0, nil } callback, err := r.ReadString() if server.Check(err) { return 0, nil } app := server.GetAppById(mailbox.App) if app == nil { server.Check(server.ErrAppNotFound) return 0, nil } if acc, ok := pl.accounts[account]; ok { pl := acc.GetPlayInfo(role_name) if pl != nil { server.Check(app.Call(&mailbox, callback, account, role_name, pl.BaseId)) return 0, nil } } server.Check(app.Call(&mailbox, callback, account, role_name, "")) return 0, nil }
func (this *Database) QuerySql(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var sqlstr string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &sqlstr, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r *sql.Rows var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if r, err = sqlconn.Query(sqlstr); err != nil { log.LogError(err) return 0, nil } defer r.Close() var cols []string cols, err = r.Columns() if err != nil { server.Check(app.Call(nil, callback, callbackparams, []share.DBRow{share.DBRow{"error": []byte(err.Error())}})) return 0, nil } result := make([]interface{}, len(cols)) for k := range result { result[k] = new([]byte) } arrresult := make([]share.DBRow, 0, 100) for r.Next() { err = r.Scan(result...) if err != nil { log.LogError("sql:", sqlstr) server.Check(app.Call(nil, callback, callbackparams, []share.DBRow{share.DBRow{"error": []byte(err.Error())}})) return 0, nil } mapresult := make(share.DBRow, len(cols)) for k, v := range cols { mapresult[v] = *result[k].(*[]byte) } arrresult = append(arrresult, mapresult) if len(arrresult) == 100 { break } } server.Check(app.Call(nil, callback, callbackparams, arrresult)) return 0, nil }
func (this *Database) UpdateRow(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var tbl string var values map[string]interface{} var condition string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &tbl, &values, &condition, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r sql.Result var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if condition != "" { condition = fmt.Sprintf("WHERE %s", condition) } args := make([]interface{}, 0, len(values)) var sql bytes.Buffer sql.WriteString("UPDATE `") sql.WriteString(tbl) sql.WriteString("` SET") split := " " for k, v := range values { sql.WriteString(split) split = ", " sql.WriteString("`") sql.WriteString(k) sql.WriteString("`") sql.WriteString("=?") args = append(args, v) } sqlstr := fmt.Sprintf("%s %s", sql.String(), condition) if r, err = sqlconn.Exec(sqlstr, args...); err != nil { log.LogError("sql:", sqlstr) if callback == "_" { log.LogError(err) return 0, nil } server.Check(app.Call(nil, callback, callbackparams, 0, err.Error())) return 0, nil } if callback == "_" { return 0, nil } eff, _ := r.RowsAffected() server.Check(app.Call(nil, callback, callbackparams, eff, "")) return 0, nil }
func (b *BaseProxy) AddPlayer(mailbox rpc.Mailbox, player share.PlayerInfo) error { base := server.GetAppById(mailbox.App) if base == nil { return server.ErrAppNotFound } ap := App.players.AddPlayer(mailbox, player.Entity) if ap == nil { return base.Call(&mailbox, "AreaBridge.AddPlayerBak", "create player failed") } ap.State = STATE_GAMING ap.Trans = server.Transform{player.Scene, Vector3{player.X, player.Y, player.Z}, player.Dir} App.EntryScene(ap.Entity) ap.State = STATE_GAMING return base.Call(&mailbox, "AreaBridge.AddPlayerBak", "ok") }
func (a *Account) LoadUser(mailbox rpc.Mailbox, info share.LoadUser) error { return a.process("LoadUser", func() error { bak := share.LoadUserBak{} app := server.GetAppById(mailbox.App) if app == nil { return server.ErrAppNotFound } c := db.DB.C(ROLEINFO) roleinfo := RoleInfo{} if err := c.Find(bson.M{"rolename": info.RoleName, "roleindex": info.Index}).One(&roleinfo); err != nil { return app.Call(&mailbox, "DbBridge.SelectUserBak", bak) } if roleinfo.Status == 1 { err := &s2c.Error{} err.ErrorNo = proto.Int32(share.ERROR_ROLE_USED) log.LogError("role inuse", info.RoleName) return server.MailTo(nil, &mailbox, "Login.Error", err) } //Loaduser data, err := LoadUser(db.DB, roleinfo.Uid, roleinfo.Entity) if err != nil { log.LogError(err) return err } c.Update(bson.M{"rolename": roleinfo.Rolename}, bson.M{"$set": bson.M{"lastlogintime": time.Now(), "status": 1, "serverid": mailbox.App}}) bak.Name = info.RoleName bak.Scene = roleinfo.Scene bak.X = roleinfo.Scene_x bak.Y = roleinfo.Scene_y bak.Z = roleinfo.Scene_z bak.Data = &data log.LogMessage("load role succeed:", info.RoleName) return app.Call(&mailbox, "DbBridge.SelectUserBak", bak) }) return nil }
func (this *Database) Count(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var tbl string var condition string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &tbl, &condition, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r *sql.Rows var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) } if condition != "" { condition = fmt.Sprintf("WHERE %s", condition) } sqlstr := fmt.Sprintf("SELECT COUNT(*) FROM `%s` %s LIMIT 1", tbl, condition) if r, err = sqlconn.Query(sqlstr); err != nil { log.LogError("sql:", sqlstr) return 0, nil } defer r.Close() if !r.Next() { server.Check(app.Call(nil, callback, -1)) return 0, nil } var count int32 if err = r.Scan(&count); err != nil { log.LogError(err) server.Check(app.Call(nil, callback, -1)) return 0, nil } server.Check(app.Call(nil, callback, callbackparams, count)) return 0, nil }
func (this *Database) DeleteRow(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var tbl string var condition string var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &tbl, &condition, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r sql.Result var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if condition != "" { condition = fmt.Sprintf(" WHERE %s", condition) } sqlstr := fmt.Sprintf("DELETE FROM `%s`%s", tbl, condition) if r, err = sqlconn.Exec(sqlstr); err != nil { log.LogError("sql:", sqlstr) if callback != "_" { server.Check(app.Call(nil, callback, callbackparams, 0, err.Error())) return 0, nil } } if callback == "_" { return 0, nil } eff, _ := r.RowsAffected() server.Check(app.Call(nil, callback, callbackparams, eff, "")) return 0, nil }
func (a *Account) CreateUser(mailbox rpc.Mailbox, info share.CreateUser) error { if info.SaveData.Data == nil { return errors.New("save data is nil") } return a.process("CreateUser", func() error { app := server.GetAppById(mailbox.App) if app == nil { return server.ErrAppNotFound } c := db.DB.C(ROLEINFO) result := RoleInfo{} if count, err := c.Find(bson.M{"account": info.Account}).Count(); err == nil { if count >= db.limit { errmsg := &s2c.Error{} errmsg.ErrorNo = proto.Int32(share.ERROR_ROLE_LIMIT) server.MailTo(&mailbox, &mailbox, "Role.Error", errmsg) return nil } } if err := c.Find(bson.M{"rolename": info.Name}).One(&result); err == nil { log.LogError("name conflict") err := &s2c.Error{} err.ErrorNo = proto.Int32(share.ERROR_NAME_CONFLIT) return server.MailTo(nil, &mailbox, "Role.Error", err) } uid := db.getNextSequence("userid") if uid == 0 { err := errors.New("uid is zero") log.LogError(err) return err } user := RoleInfo{ Uid: uid, Account: info.Account, Rolename: info.Name, Createtime: time.Now(), Lastlogintime: time.Time{}, Locktime: time.Time{}, Roleindex: int8(info.Index), Roleinfo: "", Entity: info.SaveData.Data.Typ, Deleted: 0, Locked: 0, Status: 0, Serverid: "", Scene: info.Scene, Scene_x: info.X, Scene_y: info.Y, Scene_z: info.Z, } if err := c.Insert(user); err != nil { log.LogError(err) return app.Call(&mailbox, "DbBridge.CreateRoleBack", err.Error()) } if err := SaveToDb(db.DB, uid, &info.SaveData); err != nil { c.Remove(bson.M{"_id": uid}) log.LogError(err) return app.Call(&mailbox, "DbBridge.CreateRoleBack", err.Error()) } log.LogMessage("player:", info.Name, " create succeed") return app.Call(&mailbox, "DbBridge.CreateRoleBack", "ok") }) }
func (a *Account) LoadUser(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { reader := server.NewMessageReader(msg) var info share.LoadUser if server.Check(reader.ReadObject(&info)) { return 0, nil } sqlconn := db.sql var r *sql.Rows var err error bak := share.LoadUserBak{} bak.Account = info.Account app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if r, err = sqlconn.Query("SELECT `uid`,`locktime`,`locked`,`entity`, `status`, `serverid`, `scene`, `scene_x`, `scene_y`, `scene_z`, `scene_dir`, `roleinfo`, `landtimes` FROM `role_info` WHERE `account`=? and `rolename`=? and `roleindex`=? LIMIT 1", info.Account, info.RoleName, info.Index); err != nil { server.Check(err) return 0, nil } if !r.Next() { log.LogError("user not found") r.Close() server.Check(app.Call(&mailbox, "DbBridge.SelectUserBak", bak)) return 0, nil } var uid uint64 var ltime mysql.NullTime var locked int var ent string var status int var serverid string var scene string var x, y, z, dir float32 var roleinfo string var landtimes int32 err = r.Scan(&uid, <ime, &locked, &ent, &status, &serverid, &scene, &x, &y, &z, &dir, &roleinfo, &landtimes) if err != nil { log.LogError("scan user failed") r.Close() return 0, nil } r.Close() if status == 1 { server.Check(app.Call(&mailbox, "DbBridge.RoleInUse", serverid)) return 0, nil } data, err := LoadUser(sqlconn, uid, ent) if server.Check(err) { server.Check(err) return 0, nil } if _, err = sqlconn.Exec("UPDATE `role_info` set `lastlogintime`=?,`status`=?,`serverid`=?,`landtimes`=`landtimes`+1 WHERE `account`=? and `rolename`=? LIMIT 1", time.Now().Format("2006-01-02 15:04:05"), 1, app.Name, info.Account, info.RoleName); err != nil { log.LogError(err) return 0, nil } data.RoleInfo = roleinfo bak.Account = info.Account bak.Name = info.RoleName bak.Scene = scene bak.X = x bak.Y = y bak.Z = z bak.Dir = dir bak.LandTimes = landtimes bak.Data = &data server.Check(app.Call(&mailbox, "DbBridge.SelectUserBak", bak)) return 0, nil }
func (a *Account) CreateUser(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { reader := server.NewMessageReader(msg) var info share.CreateUser if server.Check(reader.ReadObject(&info)) { return 0, nil } if info.SaveData.Data == nil { log.LogError("save data is nil") return 0, nil } app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } sqlconn := db.sql var r *sql.Rows var err error if r, err = sqlconn.Query("SELECT count(*) FROM `role_info` WHERE `account`=?", info.Account); err != nil { log.LogError(err) server.Check(app.Call(&mailbox, "DbBridge.CreateRoleBack", err.Error())) return 0, nil } var count int if r.Next() { r.Scan(&count) } r.Close() if count >= db.limit { errmsg := &s2c.Error{} errmsg.ErrorNo = proto.Int32(share.ERROR_ROLE_LIMIT) server.MailTo(&mailbox, &mailbox, "Role.Error", errmsg) return 0, nil } if db.nameunique { //名称是否唯一 if r, err = sqlconn.Query("SELECT `uid` FROM `role_info` WHERE `account`=? and `rolename`=? LIMIT 1", info.Account, info.Name); err != nil { log.LogError(err) server.Check(app.Call(&mailbox, "DbBridge.CreateRoleBack", err.Error())) return 0, nil } if r.Next() { log.LogError("name conflict") r.Close() errmsg := &s2c.Error{} errmsg.ErrorNo = proto.Int32(share.ERROR_NAME_CONFLIT) server.Check(server.MailTo(&mailbox, &mailbox, "Role.Error", errmsg)) return 0, nil } r.Close() } uid, err := sqlconn.GetUid("userid") if err != nil { log.LogError(err) server.Check(app.Call(&mailbox, "DbBridge.CreateRoleBack", err.Error())) return 0, nil } if err = CreateUser(sqlconn, uid, info.Account, info.Name, info.Index, info.Scene, info.X, info.Y, info.Z, info.Dir, info.SaveData.Data.Typ, &info.SaveData); err != nil { log.LogError(err) server.Check(app.Call(&mailbox, "DbBridge.CreateRoleBack", err.Error())) return 0, nil } server.Check(server.GetLocalApp().Call(&mailbox, "Account.GetUserInfo", info.Account)) return 0, nil //return app.Call(&mailbox, "DbBridge.CreateRoleBack", "ok") }
func (this *Database) QueryRows(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var tbl string var condition string var orderby string var index, count int32 var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &tbl, &condition, &orderby, &index, &count, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r *sql.Rows var err error if count <= 0 { log.LogError("count must above zero") return 0, nil } app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } if condition != "" { condition = fmt.Sprintf(" WHERE %s", condition) } if orderby != "" { orderby = fmt.Sprintf(" ORDER BY %s", orderby) } sqlstr := fmt.Sprintf("SELECT * FROM `%s`%s%s LIMIT %d, %d", tbl, condition, orderby, index, count) if r, err = sqlconn.Query(sqlstr); err != nil { log.LogError(err) return 0, nil } defer r.Close() var cols []string cols, err = r.Columns() if err != nil { server.Check(app.Call(nil, callback, callbackparams, []share.DBRow{share.DBRow{"error": []byte(err.Error())}}, index, count)) return 0, nil } result := make([]interface{}, len(cols)) for k := range result { result[k] = new([]byte) } arrresult := make([]share.DBRow, 0, count) for r.Next() { err = r.Scan(result...) if err != nil { log.LogError("sql:", sqlstr) server.Check(app.Call(nil, callback, callbackparams, []share.DBRow{share.DBRow{"error": []byte(err.Error())}}, index, count)) return 0, nil } mapresult := make(share.DBRow, len(cols)) for k, v := range cols { mapresult[v] = *result[k].(*[]byte) } arrresult = append(arrresult, mapresult) } server.Check(app.Call(nil, callback, callbackparams, arrresult, index, count)) return 0, nil }
func (this *Database) InsertRows(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) { var tbl string var keys []string var values []interface{} var callback string var callbackparams share.DBParams if server.Check(server.ParseArgs(msg, &tbl, &keys, &values, &callback, &callbackparams)) { return 0, nil } sqlconn := db.sql var r sql.Result var err error app := server.GetAppById(mailbox.App) if app == nil { log.LogError(server.ErrAppNotFound) return 0, nil } var sql bytes.Buffer sql.WriteString("INSERT INTO `") sql.WriteString(tbl) sql.WriteString("`(") split := " " for _, v := range keys { sql.WriteString(split) split = ", " sql.WriteString("`") sql.WriteString(v) sql.WriteString("`") } sql.WriteString(") VALUES") split = " " if len(values)%len(keys) != 0 { log.LogError("length is not match") return 0, nil } rows := len(values) / len(keys) for j := 0; j < rows; j++ { sql.WriteString(split) sql.WriteString("(") split = " " for i := 0; i < len(keys); i++ { sql.WriteString(split) sql.WriteString("?") split = ", " } split = ", " sql.WriteString(")") } if r, err = sqlconn.Exec(sql.String(), values...); err != nil { log.LogError("sql:", sql.String()) if callback == "_" { log.LogError(err) return 0, nil } server.Check(app.Call(nil, callback, callbackparams, 0, err.Error())) return 0, nil } if callback == "_" { return 0, nil } eff, _ := r.RowsAffected() server.Check(app.Call(nil, callback, callbackparams, eff, "")) return 0, nil }