Beispiel #1
0
        public bool AddVote(MongoGameVote.GameVote vote)
        {
            if (Locked)
            {
                return(false);
            }
            lock (locker)
            {
                var details = vote.Action;
                if (GameState.Generation != vote.Generation)
                {
                    //                    Console.WriteLine("Bad generation " + GameState.Generation + " " + vote.Generation);
                    return(false);
                }

                if (UserVotes.ContainsKey(vote.UserId))
                {
                    UserVotes[vote.UserId]++;
                }
                else
                {
                    UserVotes[vote.UserId] = 1;
                }

                var trackedVote = TrackedVotes.FirstOrDefault(a => details.EntityId == a.Action.EntityId && details.ActionType == a.Action.ActionType && details.Equates(a.Action));

                if (trackedVote == null)
                {
                    TrackedVotes.Add(new TrackedVote()
                    {
                        Action = vote.Action,
                        Votes  = 1,
                    });
                }
                else
                {
                    trackedVote.Votes++;
                }
                return(true);
            }
        }
Beispiel #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()
            });
        }
    }
}