Example #1
0
        private void BroadCastFeedBack(long frame)
        {
            byte[] dataS2C = null;

            UpdateS2C updateS2C = new UpdateS2C();

            updateS2C.battleId  = 0;
            updateS2C.timestamp = frame;

            if (status == LocalBattleStatus.Start)
            {
                NoticeS2C noticeS2C = new NoticeS2C();
                noticeS2C.type = NoticeType.BattleBegin;
                dataS2C        = ProtobufUtils.Serialize(noticeS2C);

                localMessageHandlers[MsgCode.NoticeMessage](dataS2C);
                status = LocalBattleStatus.Normaly;
            }

            for (int i = messageList.Count - 1; i >= 0; i--)
            {
                if (messageList[i].msgCode == MsgCode.UpdateMessage)
                {
                    UpdateC2S updateC2S = ProtobufUtils.Deserialize <UpdateC2S>(messageList[i].data);
                    updateS2C.ops.Add(updateC2S.operation);
                }
                else if (messageList[i].msgCode == MsgCode.NoticeMessage)
                {
                    NoticeC2S noticeC2S = ProtobufUtils.Deserialize <NoticeC2S>(messageList[i].data);

                    NoticeS2C noticeS2C = new NoticeS2C();
                    noticeS2C.type = noticeC2S.type;
                    dataS2C        = ProtobufUtils.Serialize(noticeS2C);

                    DebugUtils.Log(DebugUtils.Type.LocalBattleMessage, string.Format("Send {0} feed back succeed ", messageList[i].msgCode));
                    localMessageHandlers[MsgCode.NoticeMessage](dataS2C);

                    // battle end need to stop send updateS2CS
                    if (noticeC2S.type == NoticeType.BattleResultBlueWin ||
                        noticeC2S.type == NoticeType.BattleResultRedWin ||
                        noticeC2S.type == NoticeType.BattleResultDraw)
                    {
                        status = LocalBattleStatus.End;
                    }
                }
                else if (messageList[i].msgCode == MsgCode.SyncMessage)
                {
                    SyncC2S syncC2S = ProtobufUtils.Deserialize <SyncC2S>(messageList[i].data);

                    Sync s = new Sync();
                    s.syncState        = syncC2S.syncState;
                    s.unitId           = syncC2S.uintId;
                    s.continuedWalkNum = syncC2S.continuedWalkNum;
                    s.timestamp        = frame;
                    foreach (Position item in syncC2S.positions)
                    {
                        s.positions.Add(item);
                    }

                    Operation operation = new Operation();
                    operation.sync   = s;
                    operation.opType = OperationType.SyncPath;

                    updateS2C.ops.Add(operation);
                }
                else if (messageList[i].msgCode == MsgCode.QuitBattleMessage)
                {
                    QuitBattleC2S quitBattleC2S = ProtobufUtils.Deserialize <QuitBattleC2S>(messageList[i].data);
                    QuitBattleS2C quitBattleS2C = new QuitBattleS2C();
                    quitBattleS2C.serverIp   = "127.0.0.1";
                    quitBattleS2C.serverPort = 0;
                    quitBattleS2C.serverType = ServerType.GameServer;
                    dataS2C = ProtobufUtils.Serialize(quitBattleS2C);

                    DebugUtils.Log(DebugUtils.Type.LocalBattleMessage, string.Format("Send {0} feed back succeed ", messageList[i].msgCode));
                    localMessageHandlers[MsgCode.QuitBattleMessage](dataS2C);
                    status = LocalBattleStatus.End;

                    return;
                }
                else if (messageList[i].msgCode == MsgCode.UploadSituationMessage)
                {
                    UploadSituationC2S uploadSituationC2S = ProtobufUtils.Deserialize <UploadSituationC2S>(messageList[i].data);

                    UploadSituationS2C uploadSituationS2C = new UploadSituationS2C();
                    uploadSituationS2C.type = uploadSituationC2S.type;
                    dataS2C = ProtobufUtils.Serialize(uploadSituationS2C);

                    DebugUtils.Log(DebugUtils.Type.LocalBattleMessage, string.Format("Send {0} feed back succeed ", messageList[i].msgCode));

                    Action <byte[]> callback = null;
                    if (localMessageHandlers.TryGetValue(MsgCode.UploadSituationMessage, out callback))
                    {
                        callback.Invoke(dataS2C);
                    }
                }
                else
                {
                    DebugUtils.LogError(DebugUtils.Type.LocalBattleMessage, string.Format("Can't handle this local message now message code = {0} ", messageList[i].msgCode));
                }

                messageList.RemoveAt(i);
            }

            dataS2C = ProtobufUtils.Serialize(updateS2C);

            for (int i = 0; i < updateS2C.ops.Count; i++)
            {
                DebugUtils.Log(DebugUtils.Type.LocalBattleMessage, string.Format("Frame: Send feed back operation {0}", updateS2C.ops[i].opType));
            }

            localMessageHandlers[MsgCode.UpdateMessage](dataS2C);
        }
        private void BroadCastFeedBack(Frame frames)
        {
            byte[] dataS2C = null;

            // Send UpdateS2C
            UpdateS2C updateS2C = new UpdateS2C();

            updateS2C.battleId  = battleId;
            updateS2C.timestamp = frames.frame;

            for (int j = 0; j < frames.operations.Count; j++)
            {
                updateS2C.ops.Add(frames.operations[j]);
            }

            dataS2C = ProtobufUtils.Serialize(updateS2C);

            for (int i = 0; i < updateS2C.ops.Count; i++)
            {
                DebugUtils.Log(DebugUtils.Type.SimulateBattleMessage, string.Format("Frame: Send feed back operation {0}", updateS2C.ops[i].opType));
            }

            simMessageHandlers[MsgCode.UpdateMessage](dataS2C);

            for (int i = messageList.Count - 1; i >= 0; i--)
            {
                if (messageList[i].msgCode == MsgCode.NoticeMessage)
                {
                    // Send NoticeMessage
                    NoticeC2S noticeC2S = ProtobufUtils.Deserialize <NoticeC2S>(messageList[i].data);

                    SetCurrentBattleResult(noticeC2S);
                }
                else if (messageList[i].msgCode == MsgCode.QuitBattleMessage)
                {
                    // Send QuitBattleMessage
                    DebugUtils.Log(DebugUtils.Type.SimulateBattleMessage, string.Format("Send {0} feed back succeed ", messageList[i].msgCode));

                    SetCurrentBattleResult();
                    return;
                }
                else if (messageList[i].msgCode == MsgCode.UploadSituationMessage)
                {
                    // Send UploadSituationMessage
                    UploadSituationC2S uploadSituationC2S = ProtobufUtils.Deserialize <UploadSituationC2S>(messageList[i].data);

                    int type = uploadSituationC2S.type;
                    if (type == 1)
                    {
                        // send data
                        long           id       = uploadSituationC2S.battleSituation.playerId;
                        List <Battler> battlers = DataManager.GetInstance().GetBattlers();
                        Battler        battler  = battlers.Find(p => p.playerId == id);

                        if (battler != null)
                        {
                            BattleSituation situantion = sideBattleSituations[battler.side].Find(p => p.playerId == battler.playerId);
                            ConvertSituantionData(situantion, uploadSituationC2S.battleSituation);
                        }
                        else
                        {
                            DebugUtils.LogError(DebugUtils.Type.SimulateBattleMessage, string.Format("Can't find battler {0} in playback {1}", id, DataManager.GetInstance().GetBattleId()));
                        }
                    }
                    else if (type == 2)
                    {
                        // request data
                        UploadSituationS2C uploadSituationS2C = new UploadSituationS2C();
                        uploadSituationS2C.type = uploadSituationC2S.type;

                        FillSituantionData(MatchSide.Red, uploadSituationS2C.redBattleSituations);
                        FillSituantionData(MatchSide.Blue, uploadSituationS2C.blueBattleSituations);

                        dataS2C = ProtobufUtils.Serialize(uploadSituationS2C);

                        DebugUtils.Log(DebugUtils.Type.SimulateBattleMessage, string.Format("Send {0} feed back succeed ", messageList[i].msgCode));

                        Action <byte[]> callback = null;
                        if (simMessageHandlers.TryGetValue(MsgCode.UploadSituationMessage, out callback))
                        {
                            callback.Invoke(dataS2C);
                        }
                    }
                }
                else
                {
                    //DebugUtils.LogError( DebugUtils.Type.SimulateBattleMessage, string.Format( "Can't handle this local message now message code = {0} ", messageList[i].msgCode ) );
                }

                messageList.RemoveAt(i);
            }
        }