Пример #1
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()
                });
            }
        }
Пример #2
0
        public AttackHostFromSrv RequestHost(AttackHostToSrv fromClient)
        {
            lock (SyncObj)
            {
                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
                    });
                }

                if (fromClient.State == 4)
                {
                    State              = 4;
                    MapSize            = fromClient.MapSize;
                    TerrainDefNameCell = fromClient.TerrainDefNameCell;
                    TerrainDefName     = fromClient.TerrainDefName;
                    Thing              = fromClient.Thing;
                    ThingCell          = fromClient.ThingCell;
                    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 (fromClient.NewPawnsId.Count > 0 ||
                        fromClient.NewThingsId.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);
                            }
                        }

                        //объединяем
                        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 < fromClient.Delete.Count; i++)
                        {
                            if (Delete.Contains(fromClient.Delete[i]))
                            {
                                continue;
                            }
                            Delete.Add(fromClient.Delete[i]);
                        }

                        //на всякий случай корректируем: удаляем из добавляемых те, что на удаление
                        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);
                            }
                        }
                    }


                    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(),
                    };

                    UpdateCommand = new Dictionary <int, AttackPawnCommand>();
                    return(res);
                }

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