Exemplo n.º 1
0
 void SendMatchUpdate(StrokeWarMatch match, InputMessage message)
 {
     lock (syncLock)
     {
         Debug.Assert(Monitor.IsEntered(syncLock));
         Clients.Clients(match.ConnectionIdsByUser.Values.Where(o => o != null).ToArray()).matchUpdate(message);
     }
 }
Exemplo n.º 2
0
        void SendPresence(StrokeWarMatch match, string status)
        {
            int userId  = Context.User.Identity.GetId().Value;
            var userIds =
                match.ConnectionIdsByUser
                .Where(kv => kv.Value != null)
                .Select(kv => kv.Key)
                .ToArray();

            Debug.Assert(Monitor.IsEntered(syncLock));
            lock (syncLock)
                Clients.Clients(match.ConnectionIdsByUser.Values.Where(o => o != null).ToArray()).userChange(userIds, status, userId);
        }
Exemplo n.º 3
0
        public void CalcWord(InputMessage message, StrokeWarMatch match)
        {
            //** CALCULATE SCORE BASED ON THE WORD **\\

            double totalPoints;
            int    diffLevel = message.DiffLevel;

            if (diffLevel == 1)
            {
                totalPoints = 5.0;
            }
            else if (diffLevel == 2)
            {
                totalPoints = 6.0;
            }
            else if (diffLevel == 3)
            {
                totalPoints = 7.0;
            }
            else if (diffLevel == 4)
            {
                totalPoints = 8.0;
            }
            else if (diffLevel == 5)
            {
                totalPoints = 9;
            }
            else if (diffLevel == 10)
            {
                totalPoints = 10.0;
            }
            else
            {
                totalPoints = 5.0;
            }
            totalPoints = (double)message.CompletedStroke / message.TotalStrokes * totalPoints;

            double timePoints    = 0.4 * totalPoints;
            double mistakePoints = 0.2 * totalPoints;
            double peekPoints    = 0.4 * totalPoints;
            // Time calculation
            bool outOfTime = message.OutOfTime;

            if (outOfTime)
            {
                timePoints = 0.0;
            }
            // Peek calculation
            double singlePeek       = 1.0 / 3.0 * peekPoints;
            double fullPeek         = 0.5 * peekPoints;
            double totalMistakes    = (message.Mistakes);
            double totalSinglePeeks = (message.StrokePeeks);
            double totalFullPeeks   = (message.CharPeeks);

            // single peeks
            singlePeek = totalSinglePeeks * singlePeek;
            peekPoints = peekPoints - singlePeek;
            // full peeks
            fullPeek   = totalFullPeeks * fullPeek;
            peekPoints = peekPoints - fullPeek;

            if (peekPoints < 0)
            {
                peekPoints = 0;
            }
            //Mistake calculation
            double mistake = (1.0 / 3.0) * mistakePoints;

            mistake       = totalMistakes * mistake;
            mistakePoints = mistakePoints - mistake;

            if (mistakePoints < 0)
            {
                mistakePoints = 0;
            }
            totalPoints = peekPoints + mistakePoints + timePoints;
            Scores scores;

            if (!match.ScoreByUser.TryGetValue(message.Id, out scores))
            {
                scores              = new Scores();
                scores.TotalScore   = totalPoints;
                scores.TimeScore    = timePoints;
                scores.PeekScore    = peekPoints;
                scores.MistakeScore = mistakePoints;
                if (!message.OutOfTime)
                {
                    scores.TotalTime = message.WordTime;
                }
                else
                {
                    scores.TotalTime = message.TotalTime;
                }

                match.ScoreByUser.Add(message.Id, scores);
            }
            else
            {
                scores.TotalScore   += totalPoints;
                scores.TimeScore    += timePoints;
                scores.PeekScore    += peekPoints;
                scores.MistakeScore += mistakePoints;
                if (message.WordTime != null)
                {
                    if (!message.OutOfTime)
                    {
                        scores.TotalTime += message.WordTime;
                    }
                    else
                    {
                        scores.TotalTime += message.TotalTime;
                    }
                }
            }
        }
Exemplo n.º 4
0
        public void JoinMatch(Guid matchGuidId)
        {
            string          connectionId = Context.ConnectionId;
            int             userId       = Context.User.Identity.GetId().Value;
            MatchInvitation matchInvite  = matchService.GetByGuid(matchGuidId);

            ////////// Only if opponent is USER ID#1, Game is OPEN MATCH. \\\\\\\\\\\\\\\
            lock (syncLock)
            {
                if (matchInvite.Opponents[0] == 1)
                {
                    if (!matchesByMatchId.ContainsKey(matchGuidId))
                    {
                        StrokeWarMatch match = new StrokeWarMatch();
                        match.MatchHubDataCollector = matchHubDataCollector();
                        matchesByMatchId.Add(matchGuidId, match);
                        matchesByConnection.Add(connectionId, match);
                        match.MatchId = matchGuidId;
                        match.MatchHubDataCollector.SetMatchId(matchGuidId);
                        match.ConnectionIdsByUser.Add(userId, connectionId);
                        SendPresence(match, "Open Match");
                    }
                    else
                    {
                        StrokeWarMatch value;
                        if (matchesByMatchId.TryGetValue(matchGuidId, out value))
                        {
                            if (value.IsStarted)
                            {
                                Clients.Client(connectionId).inProgress();
                            }

                            string existingConnection;
                            if (value.ConnectionIdsByUser.TryGetValue(userId, out existingConnection))
                            {
                                if (existingConnection != null)
                                {
                                    matchesByConnection.Remove(existingConnection);
                                    Clients.Client(existingConnection).timeOut();
                                }
                            }
                            matchesByConnection.Add(connectionId, value);
                            value.ConnectionIdsByUser[userId] = connectionId;
                            SendPresence(value, "Open Match");
                        }
                    }
                }
                /////////////////// NORMAL CODE FOR INVITE ONLY GAME \\\\\\\\\\\\\\\\\\\\\\\
                else if (!matchesByMatchId.ContainsKey(matchGuidId))
                {
                    StrokeWarMatch match = new StrokeWarMatch();
                    match.MatchHubDataCollector = matchHubDataCollector();
                    matchesByMatchId.Add(matchGuidId, match);
                    matchesByConnection.Add(connectionId, match);
                    match.MatchId = matchGuidId;
                    match.ConnectionIdsByUser.Add(userId, connectionId);
                    if (userId == matchInvite.ChallengerId)
                    {
                        for (int i = 0; i < matchInvite.Opponents.Length; i++)
                        {
                            if (!match.ConnectionIdsByUser.ContainsKey(matchInvite.Opponents[i]))
                            {
                                match.ConnectionIdsByUser.Add(matchInvite.Opponents[i], null);
                            }
                        }
                        SendPresence(match, "Challenger");
                    }
                    else
                    {
                        match.ConnectionIdsByUser.Add(matchInvite.ChallengerId, null);
                        for (int i = 0; i < matchInvite.Opponents.Length; i++)
                        {
                            if (userId != matchInvite.Opponents[i])
                            {
                                match.ConnectionIdsByUser.Add(matchInvite.Opponents[i], null);
                            }
                        }
                        SendPresence(match, "Opponent");
                    }
                    match.MatchHubDataCollector.SetMatchId(matchGuidId);
                }
                else
                {
                    StrokeWarMatch value;
                    if (matchesByMatchId.TryGetValue(matchGuidId, out value))
                    {
                        if (value.IsStarted)
                        {
                            Clients.Client(connectionId).inProgress();
                        }

                        if (!value.ConnectionIdsByUser.ContainsKey(userId))
                        {
                            throw new HubException("You were not invited to this match.");
                        }
                        string existingConnection;
                        if (value.ConnectionIdsByUser.TryGetValue(userId, out existingConnection))
                        {
                            if (existingConnection != null)
                            {
                                matchesByConnection.Remove(existingConnection);
                                Clients.Client(existingConnection).timeOut();
                            }
                        }
                        matchesByConnection.Add(connectionId, value);
                        value.ConnectionIdsByUser[userId] = connectionId;

                        if (matchInvite.ChallengerId != userId)
                        {
                            SendPresence(value, "Opponent");
                        }
                        else
                        {
                            SendPresence(value, "Challenger");
                        }
                    }
                }
            }
        }