예제 #1
0
func (playerModule *PlayerModule) AlterName(playerObj *player.Player, newName string) rpc.ResponseObject {
	responseObj := rpc.GetInitResponseObj()
	oldName := playerObj.Name

	// 判断名称是否有变化
	if oldName == newName {
		return responseObj.SetResultStatus(rpc.NameNotChange)
	}

	// 判断新名字是否存在
	if _, exists := playerNameBLL.GetIdByName(newName); exists {
		return responseObj.SetResultStatus(rpc.NameExist)
	}

	// 更新数据
	if rs := UpdatePlayerName(playerObj, newName); rs != rpc.Success {
		return responseObj.SetResultStatus(rs)
	}

	// 删除原来的名称并添加新名称
	playerNameBLL.UnRegisterNameAndId(oldName)
	playerNameBLL.RegisterNameAndId(newName, playerObj.Id)

	return responseObj
}
예제 #2
0
func (playerModule *PlayerModule) Relogin(clientObj *rpc.Client, id string) rpc.ResponseObject {
	responseObj := rpc.GetInitResponseObj()

	// 判断玩家是否在缓存中已经存在
	var playerObj *player.Player
	var rs rpc.ResultStatus
	playerObj, rs = GetPlayerById(clientObj, id)
	if rs != rpc.Success {
		return responseObj.SetResultStatus(rs)
	}

	// 判断是否重复登陆
	if oldClientObj, ok := rpc.GetClientByPlayerId(playerObj.Id); ok {
		// 如果不是同一个客户端,则将之前的未发送数据Clone过来
		if clientObj != oldClientObj {
			// 先将未发送的响应的推送数据Clone到新连接,再断开连接
			rpc.CloneUnsendData(oldClientObj, clientObj)
			oldClientObj.LogoutAndQuit()
		}
	}

	// 玩家上线
	clientObj.PlayerLogin(playerObj.Id)

	// 组装返回值
	responseObj.Data = assembleToClient(playerObj)

	return responseObj
}
예제 #3
0
// 更新玩家货币信息
// playerObj:玩家对象
// newVipExp:新的VipExp
// newDiamond:新的Diamon
// newGold:新的Gold
// 返回值:
// 返回值枚举值
func UpdateMoney(playerObj *player.Player, newVipExp, newDiamond, newGold int) rpc.ResultStatus {
	method := func(tx *sql.Tx) rpc.ResultStatus {
		UpdateInfo_VipExp(playerObj, newVipExp)
		UpdateInfo_Diamond(playerObj, newDiamond)
		UpdateInfo_Gold(playerObj, newGold)

		_, err := playerDAL.UpdateInfo(tx, playerObj)
		if err != nil {
			return rpc.DataError
		}

		return rpc.Success
	}

	rs := CallMethod(playerObj, method)
	if rs == rpc.Success {
		// 组装推送数据
		pushObj := rpc.GetInitResponseObj()

		data := make(map[string]interface{})
		data["Money"] = playerObj.VipExp
		data["Gold"] = playerObj.Diamond
		data["Silver"] = playerObj.Gold
		pushObj.Data = assemblePushData(data)

		// 推送数据给客户端
		PushDataToPlayer(playerObj, pushObj)
	}

	return rs
}
예제 #4
0
// 更新玩家经验和等级
// playerObj:玩家对象
// newLv:新的玩家等级
// newExp:新的玩家经验
// 返回值:
// 返回值枚举值
func UpdateLvAndExp(playerObj *player.Player, newLv, newExp int) rpc.ResultStatus {
	method := func(tx *sql.Tx) rpc.ResultStatus {
		rs := UpdateExp(tx, playerObj, newExp)
		if rs != rpc.Success {
			return rs
		}

		rs = UpdateLv(tx, playerObj, newLv)
		if rs != rpc.Success {
			return rs
		}

		return rpc.Success
	}

	rs := CallMethod(playerObj, method)
	if rs == rpc.Success {
		// 组装推送数据
		pushObj := rpc.GetInitResponseObj()

		data := make(map[string]interface{})
		data["Lv"] = playerObj.Lv
		data["Exp"] = playerObj.Exp
		pushObj.Data = assemblePushData(data)

		// 推送数据给客户端
		PushDataToPlayer(playerObj, pushObj)
	}

	return rs
}
예제 #5
0
// 更新玩家名称
// playerObj:玩家对象
// newName:新的玩家名称
// 返回值:
// 返回值枚举值
func UpdatePlayerName(playerObj *player.Player, newName string) rpc.ResultStatus {
	method := func(tx *sql.Tx) rpc.ResultStatus {
		// 更新缓存
		UpdateInfo_Name(playerObj, newName)

		// 更新数据库
		_, err := playerDAL.UpdatePlayerName(tx, playerObj)
		if err != nil {
			return rpc.DataError
		}

		return rpc.Success
	}

	rs := CallMethod(playerObj, method)
	if rs == rpc.Success {
		// 组装推送数据
		pushObj := rpc.GetInitResponseObj()

		data := make(map[string]interface{})
		data["Name"] = newName
		pushObj.Data = assemblePushData(data)

		// 推送数据给客户端
		PushDataToPlayer(playerObj, pushObj)
	}

	return rs
}
예제 #6
0
func (playerModule *PlayerModule) AddMoney(playerObj *player.Player, vipExp, diamond, gold int) rpc.ResponseObject {
	responseObj := rpc.GetInitResponseObj()

	// 更新数据
	if rs := UpdateMoney(playerObj, playerObj.VipExp+vipExp, playerObj.Diamond+diamond, playerObj.Gold+gold); rs != rpc.Success {
		return responseObj.SetResultStatus(rs)
	}

	return responseObj
}
예제 #7
0
func (playerModule *PlayerModule) AddLvAndExp(playerObj *player.Player, lv, exp int) rpc.ResponseObject {
	responseObj := rpc.GetInitResponseObj()

	newLv := playerObj.Lv + lv
	newExp := playerObj.Exp + exp

	// 更新数据
	if rs := UpdateLvAndExp(playerObj, newLv, newExp); rs != rpc.Success {
		return responseObj.SetResultStatus(rs)
	}

	return responseObj
}
예제 #8
0
func (playerModule *PlayerModule) Login(clientObj *rpc.Client, partnerId, serverId int, userId string) rpc.ResponseObject {
	responseObj := rpc.GetInitResponseObj()

	// 判断玩家是否在缓存中已经存在
	var playerObj *player.Player
	var rs rpc.ResultStatus
	playerObj, rs = GetPlayerByUserId(clientObj, partnerId, serverId, userId)

	if rs == rpc.Success {
		// 判断是否重复登陆
		if oldClientObj, ok := rpc.GetClientByPlayerId(playerObj.Id); ok {
			// 如果不是同一个客户端,则先发送重复登陆的信息,然后玩家登出,客户端退出
			if clientObj != oldClientObj {
				// 先发送被踢下去的信息,再断开连接
				PushDataToClient(oldClientObj, responseObj.SetResultStatus(rpc.LoginAgain))
				oldClientObj.LogoutAndQuit()
			}
		}
	} else if rs == rpc.PlayerNotFound {
		// 创建新玩家
		// playerObj = player.New(stringUtil.GetNewGUID(), name, partnerId, serverId, clientObj.Id())

		// 注册玩家名称和Id
		// playerNameBLL.RegisterNameAndId(name, playerObj.Id)
		return responseObj.SetResultStatus(rs)
	} else {
		return responseObj.SetResultStatus(rs)
	}

	// 玩家上线
	clientObj.PlayerLogin(playerObj.Id)

	// 组装返回值
	responseObj.Data = assembleToClient(playerObj)

	return responseObj
}
예제 #9
0
func (test *TestModule) Sub(playerObj *player.Player, x, y int) rpc.ResponseObject {
	responseObj := rpc.GetInitResponseObj()
	responseObj.Data = x - y

	return responseObj
}