Exemple #1
0
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
}
Exemple #2
0
func (s *Sync) SyncPlayer(src rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	infos := make(map[string]interface{})
	if server.Check(r.ReadObject(&infos)) {
		return 0, nil
	}
	mb := infos["mailbox"].(rpc.Mailbox)
	info := infos["data"].(*EntityInfo)
	playerid := info.ObjId
	player := App.GetEntity(playerid)
	if player == nil || player.ObjType() != PLAYER {
		log.LogError("sync player player not found", playerid)
		return 0, nil
	}
	for k := range s.childs {
		delete(s.childs, k)
	}
	for k := range s.newobj {
		delete(s.newobj, k)
	}
	s.getAllChilds(player)
	s.sync(info)
	for k := range s.childs { //剩余的表示需要删除
		App.Destroy(k)
	}

	server.Check(server.MailTo(&mb, &src, "BaseProxy.SyncPlayerBak", s.newobj))
	return 0, nil
}
Exemple #3
0
func (d *DbBridge) CreateRoleBack(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	errstr, err := r.ReadString()
	if server.Check(err) {
		return 0, nil
	}
	if errstr != "ok" {
		server.Check(server.Error(nil, &mailbox, "Role.Error", share.ERROR_CREATE_ROLE_ERROR))
		return 0, nil
	}

	player := App.Players.GetPlayer(mailbox.Id)
	if player == nil {
		log.LogError("player not found, id:", mailbox.Id)
		//角色没有找到
		return 0, nil
	}

	if player.State != STATE_LOGGED {
		log.LogError("player state not logged")
		return 0, nil
	}

	server.Check(d.getUserInfo(mailbox, player.Account))
	return 0, nil
}
Exemple #4
0
func (a *AreaBridge) AddPlayerBak(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	res, err := r.ReadString()
	if server.Check(err) {
		return 0, nil
	}
	if res == "ok" {
		player := App.Players.GetPlayer(mailbox.Id)
		if player == nil {
			log.LogFatalf("can not be nil")
			return 0, nil
		}

		if player.State != STATE_ENTERAREA { //可能客户端已经断开了,则让玩家下线
			player.Leave()
			return 0, nil
		}

		player.EnterScene()
		return 0, nil
	} else {
		err := &s2c.Error{}
		err.ErrorNo = proto.Int32(share.ERROR_ROLE_ENTERAREA_ERROR)
		server.Check(server.MailTo(nil, &mailbox, "error", err))
		return 0, nil
	}

}
Exemple #5
0
func (s *Session) GetBaseAndId(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	user, err := r.ReadString()
	if server.Check(err) {
		return 0, nil
	}
	s.l.Lock()
	defer s.l.Unlock()

	s.serial++
	if s.serial < 0 {
		s.serial = 0
	}
	bases := server.GetAppIdsByType("base")
	sort.Sort(sort.StringSlice(bases))
	if len(bases) > 0 {
		idx := s.serial % len(bases)
		baseid := bases[idx]
		s.id++
		if base := server.GetAppByName(baseid); base != nil {
			server.Check(base.Call(&mailbox, "Login.AddClient", user))
			return 0, nil
		}

		log.LogError(server.ErrNotFoundApp)
		return 0, nil
	}

	log.LogError(server.ErrNotFoundApp)
	return 0, nil
}
Exemple #6
0
func (d *DbBridge) RoleInUse(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	serverid, err := r.ReadString()
	if server.Check(err) {
		return 0, nil
	}
	player := App.Players.GetPlayer(mailbox.Id)
	if player == nil {
		return 0, nil
	}

	if serverid == App.Name {
		server.Check(App.Players.SwitchPlayer(player))
		return 0, nil
	}

	app := server.GetAppByName(serverid)
	if app == nil {
		log.LogError(server.ErrAppNotFound)
		return 0, nil
	}

	app.Call(&mailbox, "Login.SwitchPlayer", player.Account)
	return 0, nil
}
Exemple #7
0
func (a *Account) ClearStatus(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	serverid, err := r.ReadString()
	if server.Check(err) {
		return 0, nil
	}

	server.Check2(db.sql.Exec("UPDATE `role_info` SET `status`=?, `serverid`=? WHERE `status`=? and `serverid`=?", 0, "", 1, serverid))
	log.LogMessage("clear server:", serverid)
	return 0, nil
}
Exemple #8
0
func (pl *PlayerList) UpdatePlayer(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
	}
	base_id, err := r.ReadString()
	if server.Check(err) {
		return 0, nil
	}

	if base_id == "" {
		if acc, ok := pl.accounts[account]; ok {
			pl := acc.GetPlayInfo(role_name)
			if pl != nil {
				delete(acc.roles, account)
				log.LogMessage("remove ", account, ",", role_name)
			}
		}

		return 0, nil
	}

	if acc, ok := pl.accounts[account]; ok {
		p := acc.GetPlayInfo(role_name)
		if p != nil {
			log.LogMessage("update ", account, ",", role_name, " base:", base_id)
			p.BaseId = base_id
			return 0, nil
		}

		p = &PlayerInfo{role_name, base_id}
		acc.roles[role_name] = p
		log.LogMessage("add ", account, ",", role_name, " base:", base_id)
		return 0, nil
	}

	p := &PlayerInfo{role_name, base_id}
	acc := &AccountInfo{}
	acc.roles = make(map[string]*PlayerInfo, MAX_ROLES)
	acc.roles[role_name] = p
	pl.accounts[account] = acc
	log.LogMessage("add ", account, ",", role_name, " base:", base_id)
	return 0, nil
}
Exemple #9
0
func (d *DbBridge) UpdateUserInfo(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	infos := share.UpdateUserBak{}
	if server.Check(r.ReadObject(&infos)) {
		return 0, nil
	}
	for _, info := range infos.Infos {
		if e := App.GetEntity(info.ObjId); e != nil {
			e.SetDbId(info.DBId)
			continue
		}

		log.LogWarning("update user info failed, object not found,", info.ObjId)
	}
	return 0, nil
}
Exemple #10
0
func (a *Account) ClearPlayerStatus(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	var info share.ClearUser
	if server.Check(r.ReadObject(&info)) {
		return 0, nil
	}

	sqlconn := db.sql
	_, err := sqlconn.Exec("UPDATE `role_info` SET `status`=?, `serverid`=? WHERE `account`=? and `rolename`=?", 0, "", info.Account, info.Name)
	if err != nil {
		log.LogError(err)
		return 0, nil
	}
	log.LogMessage("player clear status,", info.Name)
	return 0, nil
}
Exemple #11
0
func (l *Login) SwitchPlayer(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	user, err := r.ReadString()
	if server.Check(err) {
		return 0, nil
	}
	serial := rand.Int31()
	log.LogMessage("client serial:", serial)
	l.Cached[user] = cacheUser{serial, time.Now()}

	ret := &s2c.Loginsucceed{}
	ret.Host = proto.String(App.ClientHost)
	ret.Port = proto.Int32(int32(App.ClientPort))
	ret.Key = proto.Int32(serial)
	server.Check(server.MailTo(nil, &mailbox, "Login.SwitchBase", ret))
	return 0, nil
}
Exemple #12
0
func (a *Account) SavePlayer(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	var data share.UpdateUser
	if server.Check(r.ReadObject(&data)) {
		return 0, nil
	}
	if data.SaveData.Data == nil {
		log.LogError("save data is nil")
		return 0, nil
	}

	sqlconn := db.sql
	base := rpc.NewMailBox(0, 0, mailbox.App)
	infos := make([]share.ObjectInfo, 0, 128)
	if err := UpdateItem(sqlconn, data.SaveData.Data.DBId, data.SaveData.Data); err != nil {
		log.LogError(err)
		server.Check(server.MailTo(&mailbox, &base, "DbBridge.SavePlayerBak", err.Error()))
		return 0, nil
	}

	if data.Type == share.SAVETYPE_OFFLINE {
		_, err := db.sql.Exec("UPDATE `role_info` SET `status`=?, `serverid`=?, `scene`=?, `scene_x`=?, `scene_y`=?, `scene_z`=?, `scene_dir`=?, `roleinfo`=? WHERE `account`=? and `rolename`=?",
			0,
			"",
			data.Scene, data.X, data.Y, data.Z, data.Dir,
			data.SaveData.RoleInfo,
			data.Account,
			data.Name,
		)
		if err != nil {
			log.LogError(err)
			server.Check(server.MailTo(&mailbox, &base, "DbBridge.SavePlayerBak", err.Error()))
			return 0, nil
		}

		server.Check(server.MailTo(&mailbox, &base, "DbBridge.SavePlayerBak", "ok"))
		return 0, nil
	}

	bakinfo := share.UpdateUserBak{}
	bakinfo.Infos = infos
	server.Check(server.MailTo(&mailbox, &base, "DbBridge.UpdateUserInfo", bakinfo))
	return 0, nil
}
Exemple #13
0
func (a *Account) GetUserInfo(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	reader := server.NewMessageReader(msg)
	account, err := reader.ReadString()
	if server.Check(err) {
		return 0, nil
	}

	s := db.sql
	r, err := s.Query(`SELECT rolename, roleindex, roleinfo, locktime, locked 
FROM role_info
WHERE account=? and deleted =0`, account)
	if err != nil {
		log.LogError(err)
		return 0, nil
	}
	defer r.Close()

	var rolename string
	var roleindex int32
	var roleinfo string
	var locktime mysql.NullTime
	var locked int

	info := &s2c.RoleInfo{}
	info.UserInfo = make([]*s2c.Role, 0, 4)
	for r.Next() {
		err = r.Scan(&rolename, &roleindex, &roleinfo, &locktime, &locked)
		if err != nil {
			log.LogError(err)
			return 0, nil
		}
		if locked == 0 {
			role := &s2c.Role{}
			role.Name = proto.String(rolename)
			role.Index = proto.Int32(roleindex)
			role.Roleinfo = proto.String(roleinfo)

			info.UserInfo = append(info.UserInfo, role)
		}
	}
	server.Check(server.MailTo(nil, &mailbox, "Role.RoleInfo", info))
	return 0, nil
}
Exemple #14
0
func (d *DbBridge) SelectUserBak(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	save := share.LoadUserBak{}
	if server.Check(r.ReadObject(&save)) {
		return 0, nil
	}
	db := server.GetAppByType("database")
	info := share.ClearUser{save.Account, save.Name}
	player := App.Players.GetPlayer(mailbox.Id)
	if player == nil {
		log.LogError("player not found, id:", mailbox.Id)
		db.Call(&mailbox, "Account.ClearPlayerStatus", info)
		//角色没有找到
		return 0, nil
	}

	if player.State != STATE_LOGGED {
		log.LogError("player state not logged")
		db.Call(&mailbox, "Account.ClearPlayerStatus", info)
		player.Leave()
		return 0, nil
	}

	if save.Data == nil {
		db.Call(&mailbox, "Account.ClearPlayerStatus", info)
		server.Check(server.Error(nil, &mailbox, "Login.Error", share.ERROR_SELECT_ROLE_ERROR))
		return 0, nil
	}

	err := player.LoadPlayer(save)
	if server.Check(err) {
		db.Call(&mailbox, "Account.ClearPlayerStatus", info)
		return 0, nil
	}

	status := server.GetAppByType("status")
	if status != nil {
		status.Call(&mailbox, "PlayerList.UpdatePlayer", player.Account, player.ChooseRole, App.Name)
	}
	//App.AreaBridge.getArea(mailbox, save.Scene) //这里会自动加入场景
	return 0, nil
}
Exemple #15
0
func (d *DbBridge) LookLetterBack(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	params := share.DBParams{}
	r := server.NewMessageReader(msg)
	if server.Check(r.ReadObject(&params)) {
		return 0, nil
	}

	if !params["result"].(bool) {
		return 0, nil
	}

	mailbox = params["mailbox"].(rpc.Mailbox)
	p := App.Players.GetPlayer(mailbox.Id)
	if p == nil {
		log.LogError("player not found, id:", mailbox.Id)
		//角色没有找到
		return 0, nil
	}
	player := p.Entity.(*entity.Player)

	db := server.GetAppByType("database")
	if db == nil {
		log.LogError(server.ErrAppNotFound)
		return 0, nil
	}

	warp := server.NewDBWarp(db)
	letters := params["letters"].([]*share.LetterInfo)
	for _, letter := range letters {
		idx := player.MailBox_r.AddRowValue(-1, letter.Source, letter.Source_name, util.UTC2Loc(letter.Send_time.Time.UTC()).Unix(), letter.Title, letter.Content, letter.Appendix, 0, letter.Serial_no, letter.Msg_type)
		if idx == -1 {
			//邮箱满了
			server.Error(nil, &mailbox, "Letter.Error", ERR_MAILBOX_FULL)
			break
		}
		//删信
		warp.RecvLetter(nil, player.GetDbId(), letter.Serial_no, "_", share.DBParams{})
	}

	return 0, nil
}
Exemple #16
0
func (d *DbBridge) SavePlayerBak(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	err, e := r.ReadString()
	if server.Check(e) {
		return 0, nil
	}
	player := App.Players.GetPlayer(mailbox.Id)
	if player == nil || player.State != STATE_SAVING {
		log.LogError(errors.New("player not found"))
		return 0, nil
	}

	if err == "ok" {
		App.Players.RemovePlayer(mailbox.Id)
	} else {
		player.Entity.SetSaveFlag()
		player.SaveFailed()
	}

	return 0, nil
}
Exemple #17
0
func (a *AreaBridge) GetAreaBak(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	areaid, err := r.ReadString()
	if server.Check(err) {
		return 0, nil
	}
	if areaid == "" {
		log.LogError("enter area failed")
		return 0, nil
	}
	player := App.Players.GetPlayer(mailbox.Id)
	if player != nil {
		player.AreaId = areaid
		player.State = STATE_ENTERAREA
		//player.EnterScene(areaid)
		a.enterArea(player, areaid)
		log.LogMessage("enter area:", areaid)
	}

	return 0, nil
}
Exemple #18
0
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")
}
Exemple #19
0
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, &ltime, &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
}