/// <summary> /// 更新战斗数据 /// </summary> /// <param name="new_reviewInfo">UserAndReviewInfo类型</param> /// <returns>返回值,是否更新成功</returns> public static bool updateReviewInfo(ReviewInfo new_reviewInfo) { string updateStr = "update battlereview set battleNum = @new_battleNum,victoryNum = @new_victoryNum,escapeNum = @new_escapeNum,singlehighestScore = @new_singlehighestScore,totalScore = @new_totalScore where username = @username"; MySqlParameter[] param = { new MySqlParameter("@new_battleNum", new_reviewInfo.battleNum), new MySqlParameter("@new_victoryNum", new_reviewInfo.victoryNum), new MySqlParameter ("@new_escapeNum", new_reviewInfo.escapeNum), new MySqlParameter("@new_singlehighestScore", new_reviewInfo.singlehighestScore), new MySqlParameter("@new_totalScore", new_reviewInfo.totalScore), new MySqlParameter("@username", new_reviewInfo.username) }; MySqlHelper.ExecuteNonQuery(conn, CommandType.Text, updateStr, param); return(true); }
/// <summary> /// 通过username查询战斗记录 /// </summary> /// <param name="username">string类型</param> /// <param name="uarInfo">UserAndReviewInfo类型</param> /// 参数类型 MySqlParameter("@username","xxx") public static bool queryReviewInfoByUserName(string username, ReviewInfo uarInfo) { bool isExist = true; string queryStr = "select * from battlereview where username = @username"; MySqlParameter param = new MySqlParameter("@username", username); MySqlDataReader msReader = MySqlHelper.ExecuteReader(conn, CommandType.Text, queryStr, param); if (msReader.Read() == true) { //储存数据 uarInfo.username = msReader["username"].ToString(); uarInfo.battleNum = Convert.ToInt32(msReader["battleNum"].ToString()); uarInfo.victoryNum = Convert.ToInt32(msReader["victoryNum"].ToString()); uarInfo.escapeNum = Convert.ToInt32(msReader["escapeNum"].ToString()); uarInfo.singlehighestScore = Convert.ToInt32(msReader["singlehighestScore"].ToString()); uarInfo.totalScore = Convert.ToInt32(msReader["totalScore"].ToString()); isExist = true; } else { isExist = false; } return(isExist); }
/// <summary> /// 服务器接受客户端消息时执行 /// </summary> /// <param name="sender"></param> /// <param name="e">接受数据参数-Tcpclient,byte[]</param> static void server_DatagramReceived(object sender, TcpDatagramReceivedEventArgs <byte[]> e) { //TODO int headData = DataHandleHelper.getHeadData(e.Datagram); byte[] receviceData = e.Datagram; string username; bool isExist; int battleCode; switch (headData) { case CommandParam.LOGIN_REQUEST: //登陆请求 username = DataHandleHelper.getStringParam(receviceData); UserInfo userInfo = new UserInfo(); isExist = ConectMySqldb.queryUserInfoByUserName(username, userInfo); if (isExist) { if (onLineList.ContainsKey(e.TcpClient) || onLineList.ContainsValue(username)) { userInfo = new UserInfo(); //已存在用户或客户端,返回空值 } else { onLineList.Add(e.TcpClient, username); } } else { userInfo = new UserInfo(); } //返回数据 server.Send(e.TcpClient, userInfo, CommandParam.LOGIN_RETURN); break; case CommandParam.LOADING_REQUEST: //载入请求 username = DataHandleHelper.getStringParam(receviceData); ReviewInfo reviewInfo = new ReviewInfo(); isExist = ConectMySqldb.queryReviewInfoByUserName(username, reviewInfo); if (!isExist) { reviewInfo = new ReviewInfo(); //不存在,返回空值 } //返回数据 server.Send(e.TcpClient, reviewInfo, CommandParam.LOAD_RETURN); break; case CommandParam.MATCH_REQUEST: //匹配请求 MatchInfo matchInfo = new MatchInfo(); if (linelist.Count == 0) { linelist.Add(e.TcpClient); matchInfo.isMatch = 0; matchInfo.battleCode = -1; } else if (linelist.Count == 1) { if (linelist.Contains(e.TcpClient)) { matchInfo.isMatch = 0; matchInfo.battleCode = -1; } else { TcpClient emeny = linelist.ElementAt(0); linelist.Remove(emeny); //移除第一个匹配的元素 TcpClient[] battle = new TcpClient[] { emeny, e.TcpClient }; battleList.Add(battle); matchInfo.isMatch = 1; matchInfo.battleCode = battleList.IndexOf(battle); //在对战列表的位置 } } else { matchInfo.isMatch = 0; matchInfo.battleCode = -1; } //返回数据 server.Send(e.TcpClient, matchInfo, CommandParam.MATCH_RETURN); break; case CommandParam.BATTLE_REQUEST: //战斗数据交换 battleCode = DataHandleHelper.getIntParam(receviceData); BattleInfo battleInfo = new BattleInfo(); battleInfo.ReadByBytes(receviceData); if (battleCode < 0) { return; //找不到战斗列表 } else { foreach (var p in battleList.ElementAt(battleCode)) { if (p != e.TcpClient) { TcpClient target = p; battleInfo.setBattelCode(battleCode); server.Send(target, battleInfo, CommandParam.BATTLE_RETURN); } } } break; case CommandParam.UPDATE_REQUEST: //更新数据 ReviewInfo updatereviewInfo = new ReviewInfo(); updatereviewInfo.ReadByBytes(receviceData); UpdateInfo updateInfo = new UpdateInfo(); bool isUpdate = ConectMySqldb.updateReviewInfo(updatereviewInfo); if (isUpdate) { updateInfo.isUpdate = 1; } else { updateInfo.isUpdate = 0; } //返回数据 server.Send(e.TcpClient, updateInfo, CommandParam.UPDATE_RETURN); break; case CommandParam.FINSH_REQUEST: battleCode = DataHandleHelper.getIntParam(receviceData); if (battleList.ElementAt(battleCode) != null) { battleList.RemoveAt(battleCode); } server.Send(e.TcpClient, "Gamerover"); break; default: server.Send(e.TcpClient, "参数错误"); break; } }
public void doDealMsg(byte[] buffer, NetworkStream streamToClient, TcpClient client, List <TcpClient> clients) { byte[] temp;//临时变量 Console.WriteLine(getRequest(buffer)); switch (getRequest(buffer)) { case CommandParam.LOGIN_REQUEST: //登录验证 UserInfo userInfo = new UserInfo(); bool isExitUser = ConectMySqldb.queryUserInfoByUserName(getUsername(buffer), userInfo); if (isExitUser) { Console.Write("\nusername: {0} ,password: {1} ", userInfo.username, userInfo.password); temp = userInfo.WriteAsBytes(CommandParam.LOGIN_SUCCESS_REPLY); //返回登陆成功回应 try { Name2Client.Add(client, userInfo.username); //绑定用户名和客户端 streamToClient.Write(temp, 0, temp.Length); Console.WriteLine("ClientsCount:{0},Client:{1},username:{2}", Name2Client.Count, Name2Client.ElementAt(0).Key.Client.RemoteEndPoint, Name2Client.ElementAt(0).Value); } catch (ArgumentException ex) { sendOnlyReply(CommandParam.LOGIN_FAIL_REPLY, streamToClient); Console.WriteLine("此用户已经存在:" + ex.Message); } } else { sendOnlyReply(CommandParam.LOGIN_FAIL_REPLY, streamToClient); //返回失败成功回应 } break; case CommandParam.LOADING_REQUEST: //载入请求 ReviewInfo reviewInfo = new ReviewInfo(); ConectMySqldb.queryReviewInfoByUserName(getUsername(buffer), reviewInfo); temp = reviewInfo.WriteAsBytes(CommandParam.LOADING_SUCCESS_REPLY); //返回载入成功回应 streamToClient.Write(temp, 0, temp.Length); Console.WriteLine("\nbattleNum:{0},victoryNum:{1},escapeNum:{2},singlehighestScore:{3},totalScore:{4}", reviewInfo.battleNum, reviewInfo.victoryNum, reviewInfo.escapeNum, reviewInfo.singlehighestScore, reviewInfo.totalScore); break; case CommandParam.UPDATE_REQUEST: //更新请求 ReviewInfo new_reviewInfo = new ReviewInfo(); new_reviewInfo.ReadByBytes(buffer); bool isUpdate = ConectMySqldb.updateReviewInfo(new_reviewInfo); if (isUpdate) { Console.WriteLine("更新成功"); } else { Console.WriteLine("更新失败"); sendOnlyReply(CommandParam.UPDATE_FAIL_REPLY, streamToClient); } break; case CommandParam.MATCH_REQUEST: //匹配请求 Console.WriteLine("yes"); bool isready = getIsReady(buffer, GameState); if (clients.Count < 2 || !isready) { sendOnlyReply(CommandParam.MATCH_FAIL_REPLY, streamToClient); //返回匹配失败回应 } else if (clients.Count == 2 && isready) { sendOnlyReply(CommandParam.MATCH_SUCCESS_REPLY, streamToClient); //返回匹配成功回应 } else { sendOnlyReply(CommandParam.MATCH_FAIL_REPLY, streamToClient); //返回匹配失败回应 //return; } break; case CommandParam.BATTLE_REQUEST: //发送指令 if (clients.ElementAt(0) == client) { sendToTargetClient(clients.ElementAt(1), buffer); Console.WriteLine("send to {0}", clients.ElementAt(1).Client.RemoteEndPoint); } else if (clients.ElementAt(1) == client) { sendToTargetClient(clients.ElementAt(0), buffer); Console.WriteLine("send to {0}", clients.ElementAt(0).Client.RemoteEndPoint); } break; default: sendOnlyReply(CommandParam.IS_ONLINE, streamToClient); //心跳机制 break; } }
/// <summary> /// 消息接受,回调函数,执行以下功能 /// 登陆验证 /// 载入请求 /// 匹配请求 /// 发送指令 /// </summary> /// <param name="ar">异步回调的结果</param> private void ReadComplete(IAsyncResult ar) { int byteRead = 0; byte[] temp;//临时变量 try { byteRead = streamToClient.EndRead(ar);//接受消息长度 if (byteRead == 0) { Console.WriteLine("Client offine"); return; } Console.WriteLine(getRequest(buffer)); switch (getRequest(buffer)) { case CommandParam.LOGIN_REQUEST: //登录验证 UserInfo userInfo = new UserInfo(); bool isExitUser = ConectMySqldb.queryUserInfoByUserName(getUsername(buffer), userInfo); if (isExitUser) { Console.Write("\nusername: {0} ,password: {1} ", userInfo.username, userInfo.password); temp = userInfo.WriteAsBytes(CommandParam.LOGIN_SUCCESS_REPLY); //返回登陆成功回应 try { Name2Client.Add(client, userInfo.username); //绑定用户名和客户端 streamToClient.Write(temp, 0, temp.Length); Console.WriteLine("ClientsCount:{0},Client:{1},username:{2}", Name2Client.Count, Name2Client.ElementAt(0).Key.Client.RemoteEndPoint, Name2Client.ElementAt(0).Value); } catch (ArgumentException ex) { sendOnlyReply(CommandParam.LOGIN_FAIL_REPLY, streamToClient); Console.WriteLine("此用户已经存在:" + ex.Message); } } else { sendOnlyReply(CommandParam.LOGIN_FAIL_REPLY, streamToClient); //返回失败成功回应 } break; case CommandParam.LOADING_REQUEST: //载入请求 ReviewInfo reviewInfo = new ReviewInfo(); ConectMySqldb.queryReviewInfoByUserName(getUsername(buffer), reviewInfo); temp = reviewInfo.WriteAsBytes(CommandParam.LOADING_SUCCESS_REPLY); //返回载入成功回应 streamToClient.Write(temp, 0, temp.Length); Console.WriteLine("\nbattleNum:{0},victoryNum:{1},escapeNum:{2},singlehighestScore:{3},totalScore:{4}", reviewInfo.battleNum, reviewInfo.victoryNum, reviewInfo.escapeNum, reviewInfo.singlehighestScore, reviewInfo.totalScore); break; case CommandParam.UPDATE_REQUEST: //更新请求 ReviewInfo new_reviewInfo = new ReviewInfo(); new_reviewInfo.ReadByBytes(buffer); bool isUpdate = ConectMySqldb.updateReviewInfo(new_reviewInfo); if (isUpdate) { Console.WriteLine("更新成功"); } else { Console.WriteLine("更新失败"); sendOnlyReply(CommandParam.UPDATE_FAIL_REPLY, streamToClient); } break; case CommandParam.MATCH_REQUEST: //匹配请求 Console.WriteLine("yes"); bool isready = getIsReady(buffer, GameState); if (clients.Count < 2 || !isready) { sendOnlyReply(CommandParam.MATCH_FAIL_REPLY, streamToClient); //返回匹配失败回应 } else if (clients.Count == 2 && isready) { sendOnlyReply(CommandParam.MATCH_SUCCESS_REPLY, streamToClient); //返回匹配成功回应 } else { sendOnlyReply(CommandParam.MATCH_FAIL_REPLY, streamToClient); //返回匹配失败回应 //return; } break; case CommandParam.BATTLE_REQUEST: //发送指令 if (clients.ElementAt(0) == this.client) { sendToTargetClient(clients.ElementAt(1), buffer); Console.WriteLine("send to {0}", clients.ElementAt(1).Client.RemoteEndPoint); } else if (clients.ElementAt(1) == this.client) { sendToTargetClient(clients.ElementAt(0), buffer); Console.WriteLine("send to {0}", clients.ElementAt(0).Client.RemoteEndPoint); } break; default: sendOnlyReply(CommandParam.IS_ONLINE, streamToClient); //心跳机制 break; } AsyncCallback callback = new AsyncCallback(ReadComplete); streamToClient.BeginRead(buffer, 0, BufferSize, callback, null); } catch (Exception ex) { if (streamToClient != null) { streamToClient.Dispose(); clients.Remove(client); } client.Close(); Console.Write(ex.Message); } }