示例#1
0
 public SpaceBattleGame(Guid matchId, bool loadMatchData)
 {
     //load the match
     if (loadMatchData)
     {
         var m = CloudWarsData.GetMatch(matchId);
         this.MatchId      = m.Id;
         this.Player1      = m.Player1;
         this.Player2      = m.Player2;
         this.Player1Ready = m.Player1Ready;
         this.Player2Ready = m.Player2Ready;
         this.Turn         = m.Turn;
         this.Initialized  = m.Initialized;
         this.MatchEnded   = m.MatchEnded;
         this.Winner       = m.Winner;
         this.PlayingNow   = m.PlayingNow;
         //Get the units
         var dbUnits = CloudWarsData.GetUnits(matchId);
         Units = new List <IGameUnit>();
         foreach (var u in dbUnits)
         {
             Units.Add(new GameUnit(u));
         }
     }
     else
     {
         this.MatchId = matchId;
     }
 }
示例#2
0
        /// <summary>
        /// Check if there are users that have not been active for some period of time
        /// </summary>
        public void CheckUserStatus(object state)
        {
            //get the list of users
            var players = CloudWarsData.GetPlayers();
            var matches = CloudWarsData.GetMatches();

            //process the list
            foreach (var p in players)
            {
                if (!p.IsOnline)
                {
                    continue;
                }
                //check the last time this user was active
                TimeSpan difference = DateTime.UtcNow - p.LastActivity;
                if (difference.TotalSeconds > 30)
                {
                    //check if the user is part of an active match
                    var match = matches.FirstOrDefault(m => m.Player1 == p.Id || m.Player2 == p.Id);
                    //do whatever is needed
                    if (match != null)
                    {
                        //the match is lost
                        var game = _Factory.GetGame(match.Id, false);
                        game.MatchFinished(p.Id == match.Player1 ? match.Player2 : match.Player1, p.Id != match.Player1 ? match.Player2 : match.Player1);
                    }
                    //update the player
                    CloudWarsData.UpdatePlayer(values: new { IsOnline = false, Status = PlayerStatus.OffLine }, where : new { Id = p.Id });
                }
            }
        }
示例#3
0
        private void UpdateMatch()
        {
            var cols = new { Turn = this.Turn, Player1Ready = this.Player1Ready, Player2Ready = this.Player2Ready, Initialized = this.Initialized, MatchEnded = this.MatchEnded, Winner = this.Winner, PlayingNow = this.PlayingNow };

            var where = new { Id = this.MatchId };
            CloudWarsData.UpdateMatch(values: cols, where : where);
        }
示例#4
0
        public void ShotMissed(Guid matchId, Guid player1, Guid player2, Common.Other.Position coordinates)
        {
            var n = new PlayerNotification {
                Id = Guid.NewGuid(), MatchId = matchId, PlayerId = player1, OtherPlayer = player2, Row = coordinates.Row, Col = coordinates.Column, NotificationType = NotificationTypes.ShotMissed
            };

            CloudWarsData.AddNotification(n);
        }
示例#5
0
        public void ShotMade(Guid matchId, Guid player1, Guid player2, int healthAfterAttack, Guid unitId, Common.Other.Position coordinates)
        {
            var n = new PlayerNotification {
                Id = Guid.NewGuid(), MatchId = matchId, PlayerId = player1, OtherPlayer = player2, Row = coordinates.Row, Col = coordinates.Column, NotificationType = NotificationTypes.ShotMade
            };

            CloudWarsData.AddNotification(n);
        }
示例#6
0
        public void StartMatch(Guid matchId, Guid player1, Guid player2)
        {
            var n = new PlayerNotification {
                Id = Guid.NewGuid(), MatchId = matchId, PlayerId = player1, OtherPlayer = player2, NotificationType = NotificationTypes.StartMatch
            };

            CloudWarsData.AddNotification(n);
        }
示例#7
0
        public void ChallengeAccepted(Guid matchId, Guid fromPlayer, Guid toPlayer)
        {
            var n = new PlayerNotification {
                Id = Guid.NewGuid(), MatchId = matchId, PlayerId = toPlayer, OtherPlayer = fromPlayer, NotificationType = NotificationTypes.ChallengeAccepted
            };

            CloudWarsData.AddNotification(n);
        }
示例#8
0
        public void PlayerLost(Guid playerId, Guid matchId)
        {
            var n = new PlayerNotification {
                Id = Guid.NewGuid(), MatchId = matchId, PlayerId = playerId, NotificationType = NotificationTypes.PlayerLost
            };

            CloudWarsData.AddNotification(n);
        }
示例#9
0
        public void ChallengePlayer(Guid fromPlayer, Guid toPlayer)
        {
            var n = new PlayerNotification {
                Id = Guid.NewGuid(), PlayerId = toPlayer, OtherPlayer = fromPlayer, NotificationType = NotificationTypes.ChallengePlayer
            };

            CloudWarsData.AddNotification(n);
        }
示例#10
0
 /// <summary>
 /// Create the match and
 /// </summary>
 public void CreateMatch(Guid player1, Guid player2)
 {
     Player1 = player1;
     Player2 = player2;
     //Create the match and set the matchid
     MatchId = CloudWarsData.CreateMatch(player1, player2);
     Turn    = player1;
 }
示例#11
0
        public Tuple <string, string> RejectChallenge(Guid challengeId)
        {
            var c  = CloudWarsData.GetChallenge(challengeId);
            var p1 = CloudWarsData.GetPlayer(c.Player1);
            var p2 = CloudWarsData.GetPlayer(c.Player2);

            CloudWarsData.RejectChallenge(challengeId);
            return(new Tuple <string, string>(p1.ClientId, p2.DisplayName));
        }
示例#12
0
        public Tuple <Guid, Guid, Guid> AcceptChallenge(Guid challengeId)
        {
            //Get the challenge
            var c = CloudWarsData.GetChallenge(challengeId);

            //accept the challenge and create the match
            CloudWarsData.AcceptChallenge(challengeId);
            this.CreateMatch(c.Player1, c.Player2);
            return(new Tuple <Guid, Guid, Guid>(this.MatchId, c.Player1, c.Player2));
        }
示例#13
0
 public Message MatchFinished(Guid winner, Guid losser)
 {
     //update player stats
     CloudWarsData.PlayerWin(winner);
     CloudWarsData.PlayerLose(losser);
     //delete the match
     CloudWarsData.DeleteMatch(this.MatchId);
     //return the message
     return(new Message {
         Winner = winner, Losser = losser, Command = Command.EndGame, MatchId = this.MatchId
     });
 }
示例#14
0
 public string GetClientId(Guid playerId)
 {
     return(CloudWarsData.GetClientId(playerId));
 }
示例#15
0
 public Guid GetPlayerId(string clientId)
 {
     return(CloudWarsData.GetPlayerId(clientId));
 }
示例#16
0
        /// <summary>
        /// The service will take care of:
        /// - Tracking player's activity to determine if is still online
        /// - Tracking games
        /// </summary>
        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.WriteLine("Cloud Wars engine started", "Information");
            //TODO:instantiate the proper factory

            timer = new Timer(CheckUserStatus, null, 5000, 5000);

            while (true)
            {
                //check every half of second if there are active matches and process the queue
                Thread.Sleep(500);
                //if no active matches are found, and no logged players  are found, avoid access to the queue
                int onlinePlayers = CloudWarsData.OnlinePlayers();
                int activeMatches = CloudWarsData.ActiveMatches();
                //use a thread pool to process the messages
                if (activeMatches > 0 || onlinePlayers > 0)
                {
                    //process messages from matches
                    var msgs = QueueHelper.GetMessagesFromServerQueue();
                    foreach (var m in msgs)
                    {
                        switch (m.Command)
                        {
                        case Common.Other.Command.GameAction:
                            switch (m.Action)
                            {
                            case Common.Other.GameAction.Attack:
                                _Factory.GetGame(m.MatchId, true).PlayerAttack(m.Coordinates, m.PlayerId);
                                break;

                            case Common.Other.GameAction.PlayerReady:
                                _Factory.GetGame(m.MatchId, true).PlayerReady(m.PlayerId);
                                break;

                            case Common.Other.GameAction.UnitMoved:
                                _Factory.GetGame().PlayerMoveTo(m.Coordinates, m.UnitId);
                                break;
                            }
                            break;
                        }
                    }
                }

                //do not process this queue if there are not players logged in
                if (onlinePlayers > 0)
                {
                    //process messages from matches
                    var msgs = QueueHelper.GetMessagesFromClientQueue();
                    foreach (var m in msgs)
                    {
                        switch (m.Command)
                        {
                        case Common.Other.Command.GameAction:
                            switch (m.Action)
                            {
                            case Common.Other.GameAction.ShotMade:
                                _Factory.GetClientFeedback().ShotMade(m.MatchId, m.Player1, m.Player2, m.HealthAfterAttack, m.UnitId, m.Coordinates);
                                break;

                            case Common.Other.GameAction.ShotMissed:
                                _Factory.GetClientFeedback().ShotMissed(m.MatchId, m.Player1, m.Player2, m.Coordinates);
                                break;
                            }
                            break;

                        case Common.Other.Command.ChallengePlayer:
                            _Factory.GetClientFeedback().ChallengePlayer(m.Player1, m.Player2);
                            break;

                        case Common.Other.Command.ChallengeAccepted:
                            //Create the match
                            var match = _Factory.GetGame();
                            match.CreateMatch(m.Player1, m.Player2);
                            _Factory.GetClientFeedback().ChallengeAccepted(match.MatchId, m.Player1, m.Player2);
                            break;

                        case Common.Other.Command.StartGame:
                            _Factory.GetClientFeedback().StartMatch(m.MatchId, m.Player1, m.Player2);
                            break;

                        case Common.Other.Command.EndGame:
                            _Factory.GetClientFeedback().PlayerWon(m.Winner, m.MatchId);
                            _Factory.GetClientFeedback().PlayerLost(m.Losser, m.MatchId);
                            break;
                        }
                    }
                }
            }
        }
示例#17
0
 public static IEnumerable <Player> GetOnlinePlayers()
 {
     return(CloudWarsData.GetOnlinePlayers());
 }
示例#18
0
 public Guid?IsPlayingMatch(Guid playerId)
 {
     return(CloudWarsData.IsPlayingMatch(playerId));
 }
示例#19
0
 public void PlayerIsOnLine(string liveId, string clientId, double latitude, double longitude)
 {
     CloudWarsData.PlayerIsOnline(liveId, clientId, latitude, longitude);
 }
示例#20
0
 public void PlayerDisconnected(string clientId)
 {
     CloudWarsData.UpdatePlayer(new { Status = PlayerStatus.OffLine, ClientId = string.Empty }, new { ClientId = clientId });
 }
示例#21
0
 private void UpdateMatch(object columns, object where)
 {
     CloudWarsData.UpdateMatch(values: columns, where : where);
 }
示例#22
0
 public string GetPlayerNameByClientId(string clientId)
 {
     return(CloudWarsData.GetPlayerName(clientId));
 }
示例#23
0
 private void UpdatePlayer(object columns, object where)
 {
     CloudWarsData.UpdatePlayer(values: columns, where : where);
 }
示例#24
0
 public static IEnumerable <Player> GetNearbyPlayers(string liveId)
 {
     return(CloudWarsData.GetOnlinePlayersNearBy(liveId));
 }
示例#25
0
 public void ChallengePlayer(Guid fromId, Guid toId)
 {
     CloudWarsData.ChallengePlayer(fromId, toId);
 }