int GetAccountId(string accountName, string password) { mCommondStringBuilder.Clear(); mCommondStringBuilder.Append("SELECT AccountId FROM dbo.Account Where AccountName = '"); mCommondStringBuilder.Append(accountName); mCommondStringBuilder.Append("' AND Password = '******'"); string sqlStr = mCommondStringBuilder.ToString(); int accountId = -1; try { SqlCommand checkCommond = new SqlCommand(sqlStr, mConnection); SqlDataReader sqlReader = checkCommond.ExecuteReader(); if (sqlReader.Read()) { accountId = sqlReader.GetInt32(0); } sqlReader.Close(); LBLogger.Info(LogTag, "账号登录验证成功 " + accountId + " " + sqlStr); } catch (Exception ex) { LBLogger.Error(LogTag, "账号登录验证异常:" + accountName + " " + password + " " + sqlStr + " Ex:" + ex.Message); } finally { } return(accountId); }
public bool PlayersEnterScene(int roomId, LBPlayerEnterSceneInfo[] playerInfos) { for (int i = 0; i < playerInfos.Length; ++i) { if (null != GetSceneByPlayerId(playerInfos[i].playerId)) { return(false); } } LBScene curScene = GetSceneByRoomId(roomId); if (null == curScene) { curScene = mCachedSceneMgr.GetObject(); curScene.SetRoomId(roomId); mSceneDic[curScene.SceneId] = curScene; mRoomIdSceneIdDic[roomId] = curScene.SceneId; } if (!curScene.PlayerEnterScene(playerInfos)) { LBLogger.Error(LogTag, "玩家进入场景"); FreeScene(curScene.SceneId); return(false); } for (int i = 0; i < playerInfos.Length; ++i) { mPlayerIdSceneIdDic[playerInfos[i].playerId] = curScene.SceneId; } return(true); }
bool CratePlayer(string playerName, int accountId, out int playerId) { playerId = CommonDefine.InvalidPlayerId; if (!OpenSql()) { return(false); } mCommondStringBuilder.Clear(); mCommondStringBuilder.Append("INSERT INTO[dbo].[Player] ([PlayerName],[accountId]) VALUES('"); mCommondStringBuilder.Append(playerName); mCommondStringBuilder.Append("',"); mCommondStringBuilder.Append(accountId); mCommondStringBuilder.Append(")"); string sqlStr = mCommondStringBuilder.ToString(); try { SqlCommand checkCommond = new SqlCommand(sqlStr, mConnection); SqlDataReader sqlReader = checkCommond.ExecuteReader(); if (sqlReader.Read()) { playerId = sqlReader.GetInt32(0); } sqlReader.Close(); LBLogger.Info(LogTag, "创建玩家执行 " + playerId + " " + sqlStr); } catch (Exception ex) { LBLogger.Error(LogTag, "创建玩家异常:" + accountId + " " + sqlStr + " Ex:" + ex.Message); } return(playerId != CommonDefine.InvalidPlayerId); }
public bool PlayerEnterScene(LBPlayerEnterSceneInfo[] playerInfos) { for (int i = 0; i < playerInfos.Length; ++i) { if (IsPlayerInScene(playerInfos[i].playerId)) { LBLogger.Error(LogTag, "玩家已经在场景中了 " + playerInfos[i].playerId + " " + playerInfos[i].playerName); return(false); } } int emptyDataCount = GetEmptyDataCount(); if (emptyDataCount < playerInfos.Length) { LBLogger.Error(LogTag, "当前场景空余人数为:" + emptyDataCount + " 加入场景人数:" + playerInfos.Length); return(false); } for (int i = 0; i < playerInfos.Length; ++i) { LBPlayerEnterSceneInfo enterSceneInfo = playerInfos[i]; LBScenePlayerInfo playerInfo = GetEmptyData(); playerInfo.playerId = enterSceneInfo.playerId; playerInfo.playerName = enterSceneInfo.playerName; playerInfo.connectionId = enterSceneInfo.connectionId; } BroadcastEvent(RpId.AllMemberReady, null); return(true); }
/// <summary> /// 账号在房间内发起准备 /// </summary> /// <param name="accountId"></param> /// <returns></returns> public bool ReadyInRoom(int accountId) { bool isSuccess = false; for (int i = 0; i < MemberInfoArray.Length; ++i) { if (MemberInfoArray[i].playerId == accountId) { MemberInfoArray[i].isReady = true; isSuccess = true; } } if (!isSuccess) { return(false); } if (IsAllMemberReady()) { bool isNotFind = false; LBPlayerEnterSceneInfo[] playerInfos = new LBPlayerEnterSceneInfo[MemberInfoArray.Length]; for (int i = 0; i < MemberInfoArray.Length; ++i) { LBPlayer curPlayer = LBPlayerManager.Instance.GetPlayerByPlayerId(MemberInfoArray[i].playerId); if (null == curPlayer) { //异常情况,应该是bug LBLogger.Error(LogTag, "准备进入场景时找不到对应的玩家 " + MemberInfoArray[i].playerId); isNotFind = true; break; } playerInfos[i] = new LBPlayerEnterSceneInfo(); playerInfos[i].playerId = curPlayer.PlayerId; playerInfos[i].playerName = curPlayer.PlayerName; playerInfos[i].connectionId = curPlayer.ConnectionId; } if (isNotFind) { //异常处理情况 } else { if (LBSceneManager.Instance.PlayersEnterScene(RoomId, playerInfos)) { IsPlaying = true; } else { LBLogger.Error(LogTag, "进入场景失败,房间ID:" + RoomId); } } } else { BroadcastEvent(RpId.RoomAccountInfo, RpRoomMemberInfo.Serialization(RqCommonFunc.CreateRoomPlayerList(RoomId))); } return(true); }
/// <summary> /// 添加连接 /// </summary> /// <param name="peerConnectionId"></param> /// <param name="peer"></param> public void AddPeer(int peerConnectionId, MyPeer peer) { if (mPeerDic.ContainsKey(peerConnectionId)) { LBLogger.Error(LogTag, "连接管理器,插入已经存在的连接 " + peerConnectionId.ToString()); return; } mPeerDic[peerConnectionId] = peer; }
public bool PlayerBattleInstruction(int playerId, BattleInstructionBase instruction) { LBScene scene = GetSceneByPlayerId(playerId); if (scene == null) { LBLogger.Error(LogTag, "收到玩家的战斗指令,但是没有找到对应的场景 " + playerId); return(false); } return(scene.PlayerBattleInstruction(playerId, instruction)); }
public bool PlayerLoadFinish(int playerId) { LBScene scene = GetSceneByPlayerId(playerId); if (scene == null) { LBLogger.Error(LogTag, "玩家通知加載成功,但是沒有找到對應的場景 " + playerId); return(false); } return(scene.LoadFinish(playerId)); }
bool OpenSql() { if (mConnection.State == System.Data.ConnectionState.Open) { return(true); } mConnection.Open(); if (mConnection.State == System.Data.ConnectionState.Open) { return(true); } LBLogger.Error(LogTag, "数据库连接不上"); return(false); }
public void PlayerLogin(string accountName, int accountId, int peerConnectionId) { int playerId; string playerName; if (!GetPlayerInfo(accountId, out playerId, out playerName)) { playerName = accountName; if (!CratePlayer(playerName, accountId, out playerId)) { LBLogger.Error(LogTag, "创建玩家失败, " + playerName + " " + accountId); return; } } LBPlayerManager.Instance.PlayerLogin(playerId, playerName, peerConnectionId); }
public void PlayerLogin(int playerId, string playerName, int connectionId) { LBPlayer curPlayer = GetPlayerByPlayerId(playerId); if (null != curPlayer) { LBLogger.Error(LogTag, "当前玩家已经登录,不能再次登录 " + playerId + " " + playerName); return; } curPlayer = mCacheManager.GetObject(); curPlayer.Login(playerId, playerName, connectionId); mPlayerDic[playerId] = curPlayer; mPlayerConnectionDic[connectionId] = curPlayer; LBPeerManager.Instance.SendMessage(connectionId, RpId.PlayerLogin, RpPlayerLogin.Serialization(playerId, playerName)); }
public bool PlayerEnterRoom(int roomId, int playerId) { LBRoom room = GetRoomById(roomId); if (room == null) { LBLogger.Error(LogTag, "房间不存在 " + roomId); return(false); } bool enterRoomResult = room.EnterRoom(playerId); if (enterRoomResult) { mPlayerIdRoomIdDic[playerId] = roomId; } return(enterRoomResult); }
public void CreateAccount(string accountName, string password, int peerConnectionId) { if (!OpenSql()) { return; } int accountId = GetAccountId(accountName, password); if (-1 != accountId) { LBAccountManager.Instance.CreateAccountResult(false, accountId, accountName, password, peerConnectionId); return; } mCommondStringBuilder.Clear(); mCommondStringBuilder.Append("INSERT INTO[dbo].[Account] ([AccountName],[Password]) VALUES('"); mCommondStringBuilder.Append(accountName); mCommondStringBuilder.Append("','"); mCommondStringBuilder.Append(password); mCommondStringBuilder.Append("')"); string sqlStr = mCommondStringBuilder.ToString(); bool insertRet = false; try { SqlCommand sqlCommand = new SqlCommand(sqlStr, mConnection); int ret = sqlCommand.ExecuteNonQuery(); insertRet = ret != 0; LBLogger.Info(LogTag, "账号创建成功 " + ret + " " + sqlStr); } catch (Exception ex) { LBLogger.Error(LogTag, "账号创建异常:" + accountName + " " + password + " " + sqlStr + " " + ex.Message); } if (insertRet) { accountId = GetAccountId(accountName, password); insertRet = accountId != -1; } LBAccountManager.Instance.CreateAccountResult(insertRet, accountId, accountName, password, peerConnectionId); }
public static void OnOperateRequest(MyPeer peer, OperationRequest operationRequest) { LBLogger.Info(LogTag, "请求创建账号"); RqCreateAccount rqCreateAccount = Deserialization(operationRequest.Parameters); if (null != rqCreateAccount) { LBAccountManager.Instance.AccountCreate(rqCreateAccount.AccountName, rqCreateAccount.Password, peer.ConnectionId); } else { if (ParseErrorCode == RqLoginErrorCode.Null) { LBLogger.Error(LogTag, "登录游戏解析失败,但是没有错误码"); return; } peer.SendCustomEvent(RpId.CreateAccountResult, RpCreateAccountResult.Serialization(false, RpCreateAccountResult.CreateAccountErrorCode.ParseError)); } }
public static void OnOperateRequest(MyPeer peer, OperationRequest operationRequest) { LBPlayer curPlayer = LBPlayerManager.Instance.GetPlayerByConnectionId(peer.ConnectionId); if (curPlayer == null) { //LBLogger.Error("RqBattleInstructionHandler", "玩家没有登录"); return; } byte[] byteArray = RqBattleInstruction.Deserialization(operationRequest.Parameters); int index = 0; BattleInstructionBase battleInstruction = BattleInstructionBase.Deserializetion(byteArray, ref index); if (null == battleInstruction) { LBLogger.Error("RqBattleInstructionHandler", "消息解析失败"); return; } LBSceneManager.Instance.PlayerBattleInstruction(curPlayer.PlayerId, battleInstruction); }
/// <summary> /// 账号进入房间 /// </summary> /// <param name="playerId"></param> /// <returns></returns> public bool EnterRoom(int playerId) { LBLogger.Error(LogTag, "请求进入房间 " + playerId); int emptyIndex = FindEmptyMemberIndex(); if (emptyIndex < 0) { return(false); } MemberInfoArray[emptyIndex].playerId = playerId; LBPlayer curPlayer = LBPlayerManager.Instance.GetPlayerByPlayerId(playerId); if (null != curPlayer) { LBLogger.Error(LogTag, "发起进入房间消息 " + playerId + " " + curPlayer.ConnectionId); LBPeerManager.Instance.SendMessage(curPlayer.ConnectionId, RpId.EnterRoom, RpEnterRoom.Serialization(true, RoomId, RoomName)); } BroadcastEvent(RpId.RoomAccountInfo, RpRoomMemberInfo.Serialization(RqCommonFunc.CreateRoomPlayerList(RoomId))); return(true); }
public void AccountLogin(string accountName, string password, int peerConnectionId) { LBAccount curAccount = GetAccountByName(accountName); if (null != curAccount) { if (curAccount.IsCorrectPassword(password)) { if (peerConnectionId == curAccount.PeerConnectionId) { LBLogger.Error(LogTag, "当前连接重复发送登录 " + peerConnectionId); return; } //TODO 这里的逻辑感觉有点不合理,应该抽出方法 mPeerIdDic.Remove(curAccount.PeerConnectionId); curAccount.KickAccount(); curAccount.Login(peerConnectionId); mPeerIdDic[curAccount.PeerConnectionId] = curAccount; MyPeer peer = LBPeerManager.Instance.GetPeer(peerConnectionId); if (null != peer) { peer.SendCustomEvent(RpId.LoginResult, RpLoginResult.Serialization(true, RpLoginResult.LoginErrorCode.ParseError)); LBSqlManager.Instance.PlayerLogin(curAccount.AccountName, curAccount.AccountId, peerConnectionId); } } else { //提示客户端密码错误 MyPeer peer = LBPeerManager.Instance.GetPeer(peerConnectionId); if (null != peer) { peer.SendCustomEvent(RpId.LoginResult, RpLoginResult.Serialization(false, RpLoginResult.LoginErrorCode.PasswordError)); } } } else { LBSqlManager.Instance.CheckAccount(accountName, password, peerConnectionId); } }
public void AccountLoginResult(bool success, int accountId, string accountName, string password, int peerConnectionId) { LBLogger.Info(LogTag, "数据服务器返回结果 " + success + " " + accountId + " " + peerConnectionId); LBAccount curAccount = GetAccountByName(accountName); if (null != curAccount) { //客户端发送了多条登录消息? LBLogger.Error(LogTag, "收到账号登录结果时,当前账号已经存,账号名 " + accountName); return; } curAccount = GetAccountById(accountId); if (null != curAccount) { //客户端发送了多条登录消息? LBLogger.Error(LogTag, "收到账号登录结果时,当前账号已经存。账号ID " + accountName + " 为什么mAccountNameDic中没有账号?"); return; } MyPeer peer = LBPeerManager.Instance.GetPeer(peerConnectionId); if (null == peer) { //客户端主动断开连接了? LBLogger.Info(LogTag, "没有找到网络连接 " + peerConnectionId); return; } if (success) { LBAccount account = mAccountCacheMgr.GetObject(); account.SetAccountInfo(accountName, password, accountId, peerConnectionId); mAccountIdDic[accountId] = account; mAccountNameDic[accountName] = account; mPeerIdDic[peerConnectionId] = account; } peer.SendCustomEvent(RpId.LoginResult, RpLoginResult.Serialization(success, RpLoginResult.LoginErrorCode.PasswordError)); }
bool GetPlayerInfo(int accountId, out int playerId, out string playerName) { playerId = CommonDefine.InvalidPlayerId; playerName = string.Empty; if (!OpenSql()) { return(false); } mCommondStringBuilder.Clear(); mCommondStringBuilder.Append("SELECT PlayerId, PlayerName FROM dbo.Player Where AccountId = "); mCommondStringBuilder.Append(accountId.ToString()); string sqlStr = mCommondStringBuilder.ToString(); try { SqlCommand checkCommond = new SqlCommand(sqlStr, mConnection); SqlDataReader sqlReader = checkCommond.ExecuteReader(); if (sqlReader.Read()) { playerId = sqlReader.GetInt32(0); LBLogger.Info(LogTag, "玩家登录执行 Read success " + playerId); playerName = sqlReader.GetString(1); } sqlReader.Close(); LBLogger.Info(LogTag, "玩家登录执行 " + playerId + " " + sqlStr); } catch (Exception ex) { LBLogger.Error(LogTag, "玩家登录验证异常:" + accountId + " " + sqlStr + " Ex:" + ex.Message); } return(playerId != CommonDefine.InvalidPlayerId); }