示例#1
0
        private static async Task <bool> Vote(PostVoteRequest vote)
        {
            //            Console.WriteLine("voting");
            var client = new RestClient(url);

            var request = new RestRequest("api/game/vote", Method.POST);

            request.AddHeader("Accept", "application/json");
            request.AddHeader("Content-Type", "application/json");
            request.AddJsonBody(vote);
            var response = await client.ExecuteTaskAsync(request);

            var settings = new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All,
                Converters       = new List <JsonConverter>()
                {
                    new ObjectIdJsonConverter()
                }
            };
            var state = JsonConvert.DeserializeObject <STResponse <PostVoteResponse> >(response.Content, settings);

            //            Console.WriteLine("voted");

            return(state.Data.GenerationMismatch);
        }
示例#2
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()
            });
        }
    }
}