Exemple #1
0
        public AttackInitiatorFromSrv RequestInitiator(AttackInitiatorToSrv fromClient)
        {
            lock (SyncObj)
            {
                //первые 5 минут не проверяем на отключения, т.к. загрузка может быть долгой (а дисконектит уже после 10 сек)
                if ((fromClient.State == 10 || (DateTime.UtcNow - CreateTime).TotalSeconds > 8 * 60) &&
                    CheckConnect(true))
                {
                    return(new AttackInitiatorFromSrv()
                    {
                        State = State
                    });
                }
                if (fromClient.State == 0 && fromClient.StartHostPlayer != null)
                {
                    State    = 1;
                    TestMode = fromClient.TestMode;
                    return(new AttackInitiatorFromSrv()
                    {
                        State = State
                    });
                }
                if (fromClient.State == 1)
                {
                    return(new AttackInitiatorFromSrv()
                    {
                        State = State
                    });
                }
                if (fromClient.State == 2 && State >= 2 && fromClient.Pawns != null && fromClient.Pawns.Count > 0)
                {
                    Pawns = fromClient.Pawns;
                    //После передачи своих пешек, в ответ передаются данные карты и начинается её длительное создание
                    //в это время включаем на хосте обязательную паузу
                    //После загрузки пауза обновлется на 1 минуту, чтобы атакующий огляделся (ищи SetPauseOnTimeToHost в GameAttacker)
                    SetPauseOnTimeToHost = DateTime.UtcNow.AddMinutes(8);
                    Loger.Log("Server Set 1 SetPauseOnTimeToHost=" + SetPauseOnTimeToHost.Value.ToGoodUtcString());

                    return(new AttackInitiatorFromSrv()
                    {
                        State = State,
                        TestMode = TestMode,
                    });
                }
                if (fromClient.State == 3 && State >= 3)
                {
                    return(new AttackInitiatorFromSrv()
                    {
                        State = State,
                        MapSize = MapSize,
                        TerrainDefNameCell = TerrainDefNameCell,
                        TerrainDefName = TerrainDefName,
                        Thing = Thing,
                        ThingCell = ThingCell,
                    });
                }

                if (fromClient.State == 10 && State < 10)
                {
                    return(new AttackInitiatorFromSrv()
                    {
                        State = State,
                    });
                }
                if (fromClient.State == 10)
                {
                    AttackUpdateTick++;

                    if (StartTime == DateTime.MinValue)
                    {
                        Loger.Log($"Server AttackServer {Attacker.Public.Login} -> {Host.Public.Login} Start");
                        StartTime = DateTime.UtcNow;
                    }

                    if (fromClient.VictoryHostToHost)
                    {
                        VictoryHostToHost = fromClient.VictoryHostToHost;
                    }

                    if (fromClient.TerribleFatalError)
                    {
                        Loger.Log($"Server AttackServer TerribleFatalError {AttackUpdateTick}");
                        if (AttackUpdateTick != 2)
                        {
                            //такая ошибка с отменой атаки возможна только на 2 обновлении (на 1 создаются все пешки, на 2 проверка, что все переданы)
                            //если это не 2, то считаем, что атакующий жульничает
                            VictoryHostToHost = true;
                        }
                        else
                        {
                            TerribleFatalError = fromClient.TerribleFatalError;

                            SendAttackCancel();
                            Finish();
                        }
                    }

                    if (fromClient.SetPauseOnTimeToHost != TimeSpan.MinValue)
                    {
                        SetPauseOnTimeToHost = DateTime.UtcNow + fromClient.SetPauseOnTimeToHost;
                        Loger.Log("Server Set 2 SetPauseOnTimeToHost=" + SetPauseOnTimeToHost.Value.ToGoodUtcString());
                    }

                    if (fromClient.UpdateCommand != null && fromClient.UpdateCommand.Count > 0)
                    {
                        //объединяем
                        for (int i = 0; i < fromClient.UpdateCommand.Count; i++)
                        {
                            var id = fromClient.UpdateCommand[i].HostPawnID;
                            UpdateCommand[id] = fromClient.UpdateCommand[i];
                        }
                    }

                    if (fromClient.NeedNewThingIDs != null && fromClient.NeedNewThingIDs.Count > 0)
                    {
                        //объединяем
                        for (int i = 0; i < fromClient.NeedNewThingIDs.Count; i++)
                        {
                            NeedNewThingIDs.Add(fromClient.NeedNewThingIDs[i]);
                        }
                    }

                    var res = new AttackInitiatorFromSrv()
                    {
                        State           = State,
                        NewPawns        = NewPawns,
                        NewPawnsId      = NewPawnsId,
                        NewThings       = NewThings,
                        NewThingsId     = NewThingsId,
                        NewCorpses      = NewCorpses,
                        Delete          = Delete,
                        UpdateState     = UpdateState.Values.ToList(),
                        Finishing       = VictoryAttacker != null,
                        VictoryAttacker = VictoryAttacker != null ? VictoryAttacker.Value : false,
                    };
                    NewPawns    = new List <ThingEntry>();
                    NewPawnsId  = new List <int>();
                    NewThings   = new List <ThingTrade>();
                    NewThingsId = new List <int>();
                    NewCorpses  = new List <AttackCorpse>();
                    Delete      = new List <int>();
                    UpdateState = new Dictionary <int, AttackThingState>();

                    if (VictoryAttacker != null)
                    {
                        Finish();
                    }

                    return(res);
                }

                return(new AttackInitiatorFromSrv()
                {
                    ErrorText = "Unexpected request " + fromClient.State.ToString() + "! Was expected" + State.ToString()
                });
            }
        }
Exemple #2
0
        public AttackHostFromSrv RequestHost(AttackHostToSrv fromClient)
        {
            //Loger.Log($"Server AttackOnlineHost RequestHost State: {State} -> {fromClient.State}");
            lock (SyncObj)
            {
                //первые 5 минут не проверяем на отключения, т.к. загрузка может быть долгой (а дисконектит уже после 10 сек)
                if ((fromClient.State == 10 || (DateTime.UtcNow - CreateTime).TotalSeconds > 8 * 60) &&
                    CheckConnect(false))
                {
                    return(new AttackHostFromSrv()
                    {
                        State = State
                    });
                }
                if (fromClient.State < State)
                {
                    return(new AttackHostFromSrv()
                    {
                        ErrorText = "Unexpected request " + fromClient.State.ToString() + ". Was expected" + State.ToString()
                    });
                }

                if (fromClient.State == 2)
                {
                    State = 2;
                    return(new AttackHostFromSrv()
                    {
                        State = State,
                        HostPlaceServerId = HostPlaceServerId,
                        InitiatorPlaceServerId = InitiatorPlaceServerId,
                        StartInitiatorPlayer = Attacker.Public.Login,
                        TestMode = TestMode,
                    });
                }

                if (fromClient.State == 4)
                {
                    MapSize            = fromClient.MapSize;
                    TerrainDefNameCell = fromClient.TerrainDefNameCell;
                    TerrainDefName     = fromClient.TerrainDefName;
                    Thing     = fromClient.Thing;
                    ThingCell = fromClient.ThingCell;
                    State     = 4;
                    return(new AttackHostFromSrv()
                    {
                        State = State,
                    });
                }

                if (fromClient.State == 5)
                {
                    State = 5;
                    return(new AttackHostFromSrv()
                    {
                        State = State,
                        Pawns = Pawns
                    });
                }

                if (fromClient.State == 10)
                {
                    State = 10;

                    if (VictoryAttacker == null)
                    {
                        VictoryAttacker = fromClient.VictoryAttacker;
                    }

                    if (fromClient.NewPawnsId.Count > 0 ||
                        fromClient.NewThingsId.Count > 0 ||
                        fromClient.NewCorpses.Count > 0 ||
                        fromClient.Delete.Count > 0)
                    {
                        //удаляем из Delete если сейчас команда добавить с таким id
                        foreach (var n in fromClient.NewPawnsId)
                        {
                            var index = Delete.IndexOf(n);
                            if (index >= 0)
                            {
                                Delete.RemoveAt(index);
                            }
                        }
                        foreach (var n in fromClient.NewThingsId)
                        {
                            var index = Delete.IndexOf(n);
                            if (index >= 0)
                            {
                                Delete.RemoveAt(index);
                            }
                        }
                        foreach (var corps in fromClient.NewCorpses)
                        {
                            var index = Delete.IndexOf(corps.CorpseId);
                            if (index >= 0)
                            {
                                Delete.RemoveAt(index);
                            }
                        }

                        //объединяем
                        for (int i = 0; i < fromClient.NewPawnsId.Count; i++)
                        {
                            if (NewPawnsId.Contains(fromClient.NewPawnsId[i]))
                            {
                                continue;
                            }
                            NewPawnsId.Add(fromClient.NewPawnsId[i]);
                            NewPawns.Add(fromClient.NewPawns[i]);
                        }
                        for (int i = 0; i < fromClient.NewThingsId.Count; i++)
                        {
                            if (NewThingsId.Contains(fromClient.NewThingsId[i]))
                            {
                                continue;
                            }
                            NewThingsId.Add(fromClient.NewThingsId[i]);
                            NewThings.Add(fromClient.NewThings[i]);
                        }
                        for (int i = 0; i < NewCorpses.Count; i++)
                        {
                            if (fromClient.NewCorpses.Any(c => c.PawnId == NewCorpses[i].PawnId || c.CorpseId == NewCorpses[i].CorpseId))
                            {
                                NewCorpses.RemoveAt(i--);
                            }
                        }
                        for (int i = 0; i < fromClient.NewCorpses.Count; i++)
                        {
                            NewCorpses.Add(fromClient.NewCorpses[i]);
                        }
                        for (int i = 0; i < fromClient.Delete.Count; i++)
                        {
                            if (Delete.Contains(fromClient.Delete[i]))
                            {
                                continue;
                            }
                            Delete.Add(fromClient.Delete[i]);
                        }
                        for (int i = 0; i < fromClient.NewCorpses.Count; i++)
                        {
                            if (Delete.Contains(fromClient.NewCorpses[i].PawnId))
                            {
                                continue;
                            }
                            Delete.Add(fromClient.NewCorpses[i].PawnId);
                        }

                        //на всякий случай корректируем: удаляем из добавляемых те, что на удаление
                        foreach (var n in Delete)
                        {
                            var index = NewPawnsId.IndexOf(n);
                            if (index >= 0)
                            {
                                NewPawnsId.RemoveAt(index);
                                NewPawns.RemoveAt(index);
                            }
                            index = NewThingsId.IndexOf(n);
                            if (index >= 0)
                            {
                                NewThingsId.RemoveAt(index);
                                NewThings.RemoveAt(index);
                            }

                            for (int i = 0; i < NewCorpses.Count; i++)
                            {
                                if (n == NewCorpses[i].CorpseId)
                                {
                                    NewCorpses.RemoveAt(i--);
                                }
                            }
                        }
                    }


                    if (fromClient.UpdateState.Count > 0)
                    {
                        //объединяем
                        for (int i = 0; i < fromClient.UpdateState.Count; i++)
                        {
                            var id = fromClient.UpdateState[i].HostThingID;
                            UpdateState[id] = fromClient.UpdateState[i];
                        }
                    }

                    var res = new AttackHostFromSrv()
                    {
                        State              = State,
                        UpdateCommand      = UpdateCommand.Values.ToList(),
                        NeedNewThingIDs    = NeedNewThingIDs.ToList(),
                        SetPauseOnTime     = SetPauseOnTimeToHost == null ? DateTime.MinValue : SetPauseOnTimeToHost.Value,
                        VictoryHost        = VictoryHostToHost,
                        TerribleFatalError = TerribleFatalError,
                    };

                    UpdateCommand   = new Dictionary <int, AttackPawnCommand>();
                    NeedNewThingIDs = new HashSet <int>();

                    if (SetPauseOnTimeToHost != null)
                    {
                        Loger.Log("Server Send SetPauseOnTimeToHost=" + SetPauseOnTimeToHost.Value.ToGoodUtcString());
                    }

                    Host.PVPHostLastTime = DateTime.UtcNow;
                    SetPauseOnTimeToHost = null;
                    VictoryHostToHost    = false;
                    return(res);
                }

                return(new AttackHostFromSrv()
                {
                    ErrorText = "Unexpected request " + fromClient.State.ToString() + "! Was expected" + State.ToString()
                });
            }
        }