Beispiel #1
0
        /// <summary>
        /// 接受战斗指令并发送到指定client,交换数据
        /// </summary>
        /// <param name="targetclient">目标客户端</param>
        /// <param name="result">接受消息</param>
        private void sendToTargetClient(TcpClient targetclient, byte[] result)
        {
            NetworkStream targetstream = targetclient.GetStream();//获取目标客户端的networkstream
            BattleInfo    battleInfo   = new BattleInfo();

            battleInfo.ReadByBytes(result);
            Console.WriteLine("battleInfo:dic:{0},attack:{1},dirc:{2}", battleInfo.dir, battleInfo.attack, battleInfo.dirc);
            byte[] temp = battleInfo.WriteAsBytes(CommandParam.BATTLE_REPLY);//返回战斗回应和数据
            targetstream.Write(temp, 0, temp.Length);
        }
Beispiel #2
0
        /// <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;
            }
        }