Esempio n. 1
0
 public static async Task <GetStateResponse> GetState(VoteServerLogic logic, GetStateRequest model)
 {
     return(new GetStateResponse()
     {
         State = logic.GameManager.GameState
     });
 }
Esempio n. 2
0
 public static async Task <GetMetricsResponse> GetMetrics(VoteServerLogic logic)
 {
     return(new GetMetricsResponse()
     {
         Metrics = new GameMetrics()
         {
             Generation = logic.GameManager.GameState.Generation,
             Votes = logic.GameManager.TrackedVotes.ToArray(),
             UsersVoted = logic.GameManager.UserVotes.Count,
             NextGeneration = logic.GameManager.GameState.LastGeneration.AddSeconds(logic.GameManager.GameState.TickIntervalSeconds)
         }
     });
 }
Esempio n. 3
0
        public static async Task <GetMetricsResponse> GetGenerationResult(VoteServerLogic logic, GetGenerationRequest model)
        {
            if (model.Generation + 1 == logic.GameManager.GameState.Generation)
            {
                var tickResult = await MongoTickResult.Collection.GetOne(a => a.Generation == model.Generation);

                return(new GetMetricsResponse()
                {
                    Metrics = new GameMetrics()
                    {
                        Generation = tickResult.Generation,
                        Votes = tickResult.Votes.ToArray(),
                        UsersVoted = tickResult.UsersVoted
                    }
                });
            }
            return(null);
        }
Esempio n. 4
0
        public static async Task <PostVoteResponse> VoteAction(VoteServerLogic logic, PostVoteRequest model)
        {
            if (logic.GameManager.Locked)
            {
                return new PostVoteResponse()
                       {
                           IssueVoting = true
                       }
            }
            ;

            var gameState = logic.GameManager.GameState;
            var board     = logic.GameManager.GameBoard;

            if (model.Generation != gameState.Generation)
            {
                return(new PostVoteResponse()
                {
                    GenerationMismatch = true
                });
            }

            var entity = gameState.GetEntityById(model.EntityId);

            if (entity == null)
            {
                return new PostVoteResponse()
                       {
                           IssueVoting = true
                       }
            }
            ;
            MongoGameVote.VoteAction action;

            var hex1 = board.GetHexagon(entity.X, entity.Z);
            var hex2 = board.GetHexagon(model.X, model.Z);

            if (hex1 == null || hex2 == null)
            {
                return(new PostVoteResponse()
                {
                    IssueVoting = true
                });
            }
            var distance = HexUtils.Distance(hex1, hex2);


            var detail = EntityDetails.Detail[entity.EntityType];


            switch (model.Action)
            {
            case VoteActionType.Move:

                if (distance <= 0 || distance > detail.MoveRadius)
                {
                    return(new PostVoteResponse()
                    {
                        IssueVoting = true
                    });;
                }
                action = new MongoGameVote.MoveVoteAction()
                {
                    EntityId = model.EntityId, X = model.X, Z = model.Z
                };
                break;

            case VoteActionType.Attack:

                if (distance <= 0 || distance > detail.AttackRadius)
                {
                    return(new PostVoteResponse()
                    {
                        IssueVoting = true
                    });;
                }
                var attackEntity = gameState.GetEntityByLocation(model.X, model.Z);
                if (attackEntity == null || attackEntity.FactionId == entity.FactionId)
                {
                    return(new PostVoteResponse()
                    {
                        IssueVoting = true
                    });;
                }
                action = new MongoGameVote.AttackVoteAction()
                {
                    EntityId = model.EntityId, X = model.X, Z = model.Z
                };
                break;

            case VoteActionType.Spawn:
                if (distance <= 0 || distance > detail.SpawnRadius)
                {
                    return(new PostVoteResponse()
                    {
                        IssueVoting = true
                    });;
                }
                action = new MongoGameVote.SpawnVoteAction()
                {
                    EntityId = model.EntityId, X = model.X, Z = model.Z, EntityType = model.EntityType.Value
                };
                break;

            default:
                throw new RequestValidationException("Action not found");
            }



            MongoGameVote.GameVote gameVote = new MongoGameVote.GameVote()
            {
                Generated  = DateTime.UtcNow,
                Generation = model.Generation,
                UserId     = model.UserId,
                Action     = action
            };

            gameVote.Insert();



            var trackedVotes = logic.GameManager.TrackedVotes.Where(a => a.Action.EntityId == model.EntityId).ToList();
            var trackedVote  = trackedVotes.FirstOrDefault(a => action.ActionType == a.Action.ActionType && action.Equates(a.Action));

            if (trackedVote == null)
            {
                trackedVotes.Add(new TrackedVote()
                {
                    Action = action,
                    Votes  = 1,
                });
            }
            else
            {
                trackedVote.Votes++;
            }

            logic.Client.SendAllPoolMessage("VotePool", "AddVote", new GameVoteMessage()
            {
                Vote = gameVote
            });


            return(new PostVoteResponse()
            {
                Votes = trackedVotes.ToArray()
            });
        }
    }
}