Beispiel #1
0
        public async Task UpdateMove(UserRequest user, ActorInfo actorInfo, string move)
        {
            string           actorServiceUri = $"{this.Context.CodePackageActivationContext.ApplicationName}/SimulationActorService{GetSuffix(actorInfo.ActorIndex)}";
            ISimulationActor simulationActor = ActorProxy.Create <ISimulationActor>(actorInfo.ActorId, new Uri(actorServiceUri));

            await simulationActor.UpdateMove(user, actorInfo, move);
        }
Beispiel #2
0
        public async Task SendInput(UserRequest user, ActorInfo actorInfo, UserInput input)
        {
            string           actorServiceUri = $"{this.Context.CodePackageActivationContext.ApplicationName}/SimulationActorService{GetSuffix(actorInfo.ActorIndex)}";
            ISimulationActor simulationActor = ActorProxy.Create <ISimulationActor>(actorInfo.ActorId, new Uri(actorServiceUri));

            await simulationActor.ApplyInput(user, input);
        }
Beispiel #3
0
        public async Task <GameState> GetGameState(UserRequest user, ActorInfo actorInfo)
        {
            string           actorServiceUri = $"{this.Context.CodePackageActivationContext.ApplicationName}/SimulationActorService{GetSuffix(actorInfo.ActorIndex)}";
            ISimulationActor simulationActor = ActorProxy.Create <ISimulationActor>(actorInfo.ActorId, new Uri(actorServiceUri));

            return(await simulationActor.GetGameState(user));
        }
Beispiel #4
0
        private async Task <bool> MatchmakeOneGame(CancellationToken cancellationToken, IReliableConcurrentQueue <UserRequest> queue, IReliableDictionary <ActorInfo, PlayersInMatch> usedActors, IReliableDictionary <UserRequest, ActorInfo> matchmakedUsers)
        {
            try
            {
                ConditionalValue <UserRequest> ret;
                PlayersInMatch players = new PlayersInMatch();
                ActorInfo      actorId;

                using (var tx = this.StateManager.CreateTransaction())
                {
                    do
                    {
                        ret = await queue.TryDequeueAsync(tx, cancellationToken);

                        if (ret.HasValue)
                        {
                            players = players.AddPlayer(new UserRequest(ret.Value));
                        }
                    }while (!cancellationToken.IsCancellationRequested && ret.HasValue && players.Count < MatchSize);

                    if (cancellationToken.IsCancellationRequested || players.Count != MatchSize)
                    {
                        ServiceEventSource.Current.ServiceMessage(this.Context, cancellationToken.IsCancellationRequested ? $"Cancellation requested!" : $"Not enough players in the queue to matchmake!");
                        tx.Abort();
                        return(false);
                    }

                    // found enough players - assign them actor
                    //bool usedActor = false;
                    //do
                    //{
                    //    actorId = ActorId.CreateRandom();
                    //    usedActor = await usedActors.ContainsKeyAsync(tx, actorId);
                    //}
                    //while (!cancellationToken.IsCancellationRequested && usedActor);

                    actorId = await GetSimulationActorId(tx, cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        ServiceEventSource.Current.ServiceMessage(this.Context, $"Cancellation requested!");
                        tx.Abort();
                        return(false);
                    }

                    bool added = await usedActors.TryAddAsync(tx, actorId, players);

                    if (!added)
                    {
                        ServiceEventSource.Current.ServiceMessage(this.Context, $"Tried to add already used actor {actorId}");
                        tx.Abort();
                        return(false);
                    }

                    var playersToAdd = players.GetList();
                    List <UserRequest> addedPlayers = new List <UserRequest>();

                    foreach (var player in playersToAdd)
                    {
                        added = await matchmakedUsers.TryAddAsync(tx, player, actorId);

                        if (added)
                        {
                            addedPlayers.Add(player);
                        }
                    }

                    if (addedPlayers.Count != playersToAdd.Count)
                    {
                        foreach (var player in addedPlayers)
                        {
                            await matchmakedUsers.TryRemoveAsync(tx, player);

                            await queue.EnqueueAsync(tx, player);
                        }
                        ServiceEventSource.Current.ServiceMessage(this.Context, $"Some duplicated requests encountered");
                    }

                    await tx.CommitAsync();
                }

                List <UserRequest> playersList = players.GetList();

                // Create actor simulation
                int index = actorId.ActorIndex;

                string           suffix          = GetSimulationActorNameSuffix(index);
                string           actorServiceUri = $"{this.context.CodePackageActivationContext.ApplicationName}/SimulationActorService{suffix}";
                ISimulationActor simulationActor = ActorProxy.Create <ISimulationActor>(actorId.ActorId, new Uri(actorServiceUri));

                bool simulated = await simulationActor.SimulateMatch(playersList, actorId);

                if (!simulated)
                {
                    ServiceEventSource.Current.Message($"Something went wrong with simulation");
                    return(false);
                }

                await simulationActor.SubscribeAsync <ISimulationEvents>(this, ResubsriptionInterval);

                // Notify clients
                IWebService webService = ServiceProxy.Create <IWebService>(new Uri($"{this.context.CodePackageActivationContext.ApplicationName}/WebService"));
                await webService.StartGame(actorId, playersList);

                StringBuilder builder = new StringBuilder();
                builder.Append($"Created new match\n ActorID: {actorId}\n");

                for (int i = 0; i < players.Count; i++)
                {
                    builder.Append($"UserID_{i}: {playersList[i]}\n");
                }

                ServiceEventSource.Current.ServiceMessage(this.Context, builder.ToString());

                return(true);
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Message(string.Format("Exception {0}", ex));
                return(false);
            }
        }