Ejemplo n.º 1
0
        public async Task AddPlayer(HubCallerContext context, IGroupManager groups, HttpContext httpContext)
        {
            if (IsGameRunning)
            {
                await Clients.Client(context.ConnectionId).InvokeAsync("Reject", players);

                await groups.RemoveAsync(context.ConnectionId, PlayingGroup);

                await groups.AddAsync(context.ConnectionId, WaitlistGroup);
            }
            else if (players.TrueForAll(p => p.ConnectionId != context.ConnectionId))
            {
                await groups.RemoveAsync(context.ConnectionId, WaitlistGroup);

                await groups.AddAsync(context.ConnectionId, PlayingGroup);

                lock (playersLock)
                {
                    players.Add(new Player
                    {
                        ConnectionId  = context.ConnectionId,
                        IpAddress     = httpContext.Connection.RemoteIpAddress.ToString(),
                        Username      = DbContext.GetUsername(GopUser.GetCurrentUser(httpContext)),
                        Run           = true,
                        IsWatching    = true,
                        StartLocation = DefaultStartingLocations[Math.Min(DefaultStartingLocations.Length - 1, players.Count)]
                    });
                }
                await Clients.Client(context.ConnectionId).InvokeAsync("SetGameParams", Altar, Seed);

                await PlayingClients.UpdatePlayers(players, true);
                await UpdatePlayerIndices();
            }
        }
Ejemplo n.º 2
0
        public async Task RemovePlayer(HubCallerContext context)
        {
            int playerIndex = -1;

            lock (playersLock)
            {
                playerIndex = players.FindIndex(p => p.ConnectionId == context.ConnectionId);
                if (playerIndex != -1)
                {
                    players.RemoveAt(playerIndex);
                }
            }

            if (playerIndex != -1)
            {
                await PlayingClients.RemovePlayer(playerIndex);
                await UpdatePlayerIndices();

                if (players.Count(p => !p.IsWatching) == 0)
                {
                    // No more players, restart the whole thing.
                    await SendNewGameSignal();
                }
            }
        }
Ejemplo n.º 3
0
 private async Task StopGame(bool callClientStop = true)
 {
     timer.Change(Timeout.Infinite, TickLength);
     CurrentTick   = -CountdownLength;
     IsGameRunning = false;
     if (callClientStop)
     {
         await PlayingClients.Stop();
     }
 }
Ejemplo n.º 4
0
        public async Task SetPlayerLocation(HubCallerContext context, int x, int y)
        {
            var player = players.Find(p => p.ConnectionId == context.ConnectionId);

            if (player != null)
            {
                player.StartLocation = new Point(x, y);
                await PlayingClients.UpdatePlayers(players, false);
            }
        }
Ejemplo n.º 5
0
        public Task SendAction(HubCallerContext context, string action)
        {
            var player = GetPlayer(context);

            if (player != null && !(player.IsWatching && IsAttractOrbAction(action)))
            {
                player.Action = action;
            }
            return(PlayingClients.UpdatePlayers(players, false));
        }
Ejemplo n.º 6
0
        public async Task SetReady(HubCallerContext context, bool ready)
        {
            var player = GetPlayer(context);

            if (player != null)
            {
                player.IsReady = ready;
            }
            await PlayingClients.UpdatePlayers(players, false);

            StartGameIfReady();
        }
Ejemplo n.º 7
0
        public async Task SetWatching(HubCallerContext context, bool watching)
        {
            var player = GetPlayer(context);

            if (player != null)
            {
                player.IsWatching = watching;
                if (watching)
                {
                    // Clear player's action if watching.
                    player.Action = null;
                }
            }
            await PlayingClients.UpdatePlayers(players, false);

            StartGameIfReady();
        }
Ejemplo n.º 8
0
        public async Task SendNewGameSignal()
        {
            if (IsGameRunning)
            {
                await StopGame();
            }

            HasSaved = false;
            await Clients.Group(WaitlistGroup).InvokeAsync("NotifyRejoin");

            foreach (var p in players)
            {
                p.IsReady = false;
            }
            await PlayingClients.UpdatePlayers(players, false);

            await PlayingClients.NewGame();
        }
Ejemplo n.º 9
0
        private async void Tick(object state)
        {
            ++CurrentTick;
            if (CurrentTick <= 0)
            {
                await PlayingClients.SetCountdown(-CurrentTick + 1);
            }
            else if (CurrentTick <= TicksPerAltar)
            {
                await PlayingClients.Tick(players, CurrentTick);

                foreach (var p in players)
                {
                    p.Action = null;
                }
            }

            if (CurrentTick >= TicksPerAltar)
            {
                // Game is over
                await StopGame(false);
            }
        }
Ejemplo n.º 10
0
 public Task SetGameParams(int altar, int seed)
 {
     Altar = altar;
     Seed  = seed;
     return(PlayingClients.SetGameParams(altar, seed));
 }