コード例 #1
0
        public static void Main()
        {
//            startNewGame();
//            return;
            LocalThreadManager.Start().Process();
            MongoServerLog.AddServerLog("Master.Start", "1", "1");
            var logic = MasterVoteServerLogic.GetServerLogic();

            while (true)
            {
                Console.WriteLine("Press enter to die");
                Console.ReadLine();
                logic.gameTick(null);
            }
        }
コード例 #2
0
        public void Tick()
        {
            lock (locker)
            {
                var sw = new Stopwatch();
                sw.Start();

                List <TrackedVote> votes = new List <TrackedVote>();

                List <TrackedVote> moveVotes   = new List <TrackedVote>();
                List <TrackedVote> attackVotes = new List <TrackedVote>();
                List <TrackedVote> spawnVotes  = new List <TrackedVote>();
                Console.WriteLine("Ticking");


                foreach (var unitVotes in TrackedVotes.GroupBy(a => a.Action.EntityId))
                {
                    var vote = unitVotes.OrderByDescending(a => a.Votes).First();
                    switch (vote.Action.ActionType)
                    {
                    case MongoGameVote.VoteActionType.Move:
                        moveVotes.Add(vote);
                        break;

                    case MongoGameVote.VoteActionType.Attack:
                        attackVotes.Add(vote);
                        break;

                    case MongoGameVote.VoteActionType.Spawn:
                        spawnVotes.Add(vote);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                foreach (var vote in attackVotes)
                {
                    if (vote.Action.Valid(this))
                    {
                        vote.Action.Complete(this);
                        votes.Add(vote);
                    }
                }


                var nextMoveVotes = new List <TrackedVote>();

                foreach (var vote in moveVotes)
                {
                    if (vote.Action.Valid(this))
                    {
                        vote.Action.Start(this);
                        votes.Add(vote);
                        nextMoveVotes.Add(vote);
                    }
                }
                var originalMoveVotes = nextMoveVotes;


                while (nextMoveVotes.Count > 0)
                {
                    var nextVotes = new List <TrackedVote>();

                    foreach (var moveVote in nextMoveVotes)
                    {
                        if (moveVote.Action.NextTick(this))
                        {
                            nextVotes.Add(moveVote);
                        }
                    }
                    nextMoveVotes = nextVotes;
                }


                foreach (var moveVote in originalMoveVotes)
                {
                    moveVote.Action.Complete(this);
                }


                foreach (var spawnVote in spawnVotes)
                {
                    if (spawnVote.Action.Valid(this))
                    {
                        spawnVote.Action.Complete(this);
                    }
                }



                foreach (var entity in this.GameState.Entities)
                {
                    if (entity.Health > 0)
                    {
                        var detail = EntityDetails.Detail[entity.EntityType];
                        entity.Health += detail.HealthRegenRate;
                        if (entity.Health > detail.Health)
                        {
                            entity.Health = detail.Health;
                        }
                    }
                }

                var gameStateFactionData = this.GameBoard.ToFactionData(this.GameState.Terrain);
                sw.Stop();
                var tickTime = sw.ElapsedMilliseconds;
                sw.Reset();
                sw.Start();
                MongoTickResult.TickResult result = new MongoTickResult.TickResult();
                result.Generation = GameState.Generation;
                result.Generated  = DateTime.UtcNow;
                result.Votes      = votes;
                result.UsersVoted = UserVotes.Count;
                result.InsertSync();

                GameState.Generation    += 1;
                GameState.LastGeneration = DateTime.UtcNow;
                GameState.FactionData    = gameStateFactionData;
                GameState.UpdateSync();

                sw.Stop();

                MongoGameVote.Collection.DeleteMany(Builders <MongoGameVote.GameVote> .Filter.Lte(a => a.Generation, GameState.Generation - 1));

                var formattableString = $"{sw.ElapsedMilliseconds}ms Update, {tickTime}ms Tick, Votes: {TrackedVotes.Sum(a => a.Votes)} Actions: {TrackedVotes.Count}  Users Participated: {UserVotes.Count} Generation: {GameState.Generation - 1}";
                MongoServerLog.AddServerLog("Master.vote", formattableString, null);
                Reset();
            }
        }
コード例 #3
0
        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext requestContext)
        {
            pipelines.BeforeRequest.AddItemToStartOfPipeline((a) =>
            {
                start++;
                return(null);
            });

            pipelines.AfterRequest.AddItemToEndOfPipeline((ctx) =>
            {
                finish++;
                //                Console.WriteLine("Request made: " + ctx.Request._Act_Path + " " + ctx.Request.Method);
                ctx.Response.WithHeader("Access-Control-Allow-Origin", "*")
                .WithHeader("Access-Control-Allow-Methods", "POST,GET")
                .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type");
            });


            pipelines.OnError.AddItemToEndOfPipeline((context, exception) =>
            {
                Response response;
                MongoServerLog.AddServerLog("Error", exception, context.Request.Path);
                if (exception is RequestException)
                {
                    var hyException = (RequestException)exception;
                    Negotiator negotiator;
                    switch (hyException.Type)
                    {
                    case RequestExceptionType.Validation:
                        var hyRequestValidationException = (RequestValidationException)hyException;
                        negotiator = new Negotiator(context);
                        negotiator.ValidationError(hyRequestValidationException.Errors);
                        response = container.Resolve <IResponseNegotiator>().NegotiateResponse(negotiator, context);
                        break;

                    case RequestExceptionType.ServerError:
                        var hyServerErrorException = (ServerErrorException)hyException;
                        negotiator = new Negotiator(context);
                        negotiator.ServerError(hyServerErrorException.Errors);
                        response = container.Resolve <IResponseNegotiator>().NegotiateResponse(negotiator, context);
                        break;

                    case RequestExceptionType.Unauthorized:
                        negotiator = new Negotiator(context);
                        negotiator.Unauthorized();
                        response = container.Resolve <IResponseNegotiator>().NegotiateResponse(negotiator, context);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                else
                {
                    var negotiator = new Negotiator(context);
                    negotiator.ServerError(exception.Message);
                    response = container.Resolve <IResponseNegotiator>().NegotiateResponse(negotiator, context);
                }

                return(response.WithHeader("Access-Control-Allow-Origin", "*")
                       .WithHeader("Access-Control-Allow-Methods", "POST,GET")
                       .WithHeader("Access-Control-Allow-Headers", "Accept, Origin, Content-type"));
            });
        }