Esempio n. 1
0
        public CRoom(string[] hashPlayers, bool participatingAI)
        {
            if (participatingAI)
            {
                numberOfPlayers = hashPlayers.Length + 1;
            }
            else
            {
                numberOfPlayers = hashPlayers.Length;
            }

            using (MD5 md5Hash = MD5.Create())
            {
                roomHash = GetMd5Hash(md5Hash, numberOfPlayers + creationTime.ToFileTime() + "");
            }

            configuration = ConfigurationType.getInstance().createConfiguration(numberOfPlayers);


            string[] scrumbledPlayers = ScrumblePlayers(hashPlayers);
            if (configuration.weights.Length == hashPlayers.Length + 1)
            {
                scrumbledPlayers = AddAIToPlayers(scrumbledPlayers);
                AIparticipating  = true;
            }

            for (int i = 0; i < configuration.numOfRounds; i++)
            {
                int       proposerID = randGenerator.Next(numberOfPlayers);
                RoundInfo ri         = new RoundInfo(configuration.weights, scrumbledPlayers, proposerID);
                roundsInfo.Add(i, ri);
            }

            AddRoom(this);
        }
Esempio n. 2
0
        public static void UpdateRooms()
        {
            try
            {
                changeLock.WaitOne();
            }
            catch (AbandonedMutexException)
            {
                changeLock.ReleaseMutex();
                changeLock.WaitOne();
            }

            var rooms = HttpContext.Current.Application["OnlineRooms"];

            if (rooms != null)
            {
                var           onlineRooms      = (Dictionary <string, CRoom>)rooms;
                List <string> roomsToBeRemoved = new List <string>();
                int           timeInTheCurrentState;
                bool          coalitionHasBeenFormed = false;

                RoundInfo ri;
                foreach (var room in onlineRooms)
                {
                    if (room.Value.NextRoundTimeOut != null)
                    {
                        if (room.Value.NextRoundTimeOut < DateTime.Now)
                        {
                            if (!room.Value.NextRound())
                            {
                                roomsToBeRemoved.Add(room.Key);
                            }
                            room.Value.NextRoundTimeOut = null;
                        }
                    }
                    else
                    {
                        switch (room.Value.roomState)
                        {
                        case RoomState.Init:
                            ri = room.Value.roundsInfo[room.Value.currentRound];
                            int  realNumberOfPlayers = room.Value.AIparticipating ? room.Value.numberOfPlayers - 1 : room.Value.numberOfPlayers;
                            bool everyoneIsInGame    = Player.isEveryonePlayingInRoom(room.Key, realNumberOfPlayers);
                            if (everyoneIsInGame)
                            {
                                room.Value.stateChangeTime = DateTime.Now;
                                room.Value.roomState       = RoomState.ProposalMaking;
                            }
                            break;

                        case RoomState.ProposalMaking:
                            TimeSpan ts = DateTime.Now - room.Value.stateChangeTime;
                            timeInTheCurrentState = (int)ts.TotalSeconds;
                            if ((room.Value.configuration.ProposerTimeout - timeInTheCurrentState) <= 0)
                            {
                                room.Value.UpdateAllPlayersToSummary(false);
                                room.Value.NextRoundTimeOut = DateTime.Now.AddSeconds(5);
                            }

                            // play the AI
                            int aiIndex;
                            PlayAi(ConfigurationType.getInstance().configType, timeInTheCurrentState, out ri, room, out aiIndex);
                            if (room.Value.OffersWereGiven())
                            {
                                room.Value.stateChangeTime = DateTime.Now;
                                room.Value.roomState       = RoomState.OffersInspection;

                                ri = room.Value.roundsInfo[room.Value.currentRound];
                                foreach (var item in ri.playersRoundInfo)
                                {
                                    if (item.Value.isProposer)
                                    {
                                        Player.SetFutureStatus(item.Key, Player.Status.ProposerWaitingForAnswer);
                                    }
                                    else if (item.Value.playerOffer > 0)
                                    {
                                        Player.SetFutureStatus(item.Key, Player.Status.ProposalInspection);
                                    }
                                    else
                                    {
                                        Player.SetFutureStatus(item.Key, Player.Status.WaitingNotGivenOffer);
                                    }
                                }
                            }
                            break;

                        case RoomState.OffersInspection:
                            timeInTheCurrentState = (int)(DateTime.Now - room.Value.stateChangeTime).TotalSeconds;
                            if ((room.Value.configuration.PlayerTimeout - timeInTheCurrentState) <= 0)
                            {
                                room.Value.UpdateAllPlayersToSummary(false);
                                room.Value.NextRoundTimeOut = DateTime.Now.AddSeconds(5);
                            }

                            // response as the AI
                            ResponseAsAi(ConfigurationType.getInstance().configType, timeInTheCurrentState, out ri, room, out aiIndex);

                            bool everyOneResponded = true;
                            ri = room.Value.roundsInfo[room.Value.currentRound];
                            foreach (var item in ri.playersRoundInfo)
                            {
                                if (item.Value.playerResponse == PlayerResponse.None)
                                {
                                    if (item.Value.playerOffer > 0)
                                    {
                                        everyOneResponded = false;
                                    }
                                    else
                                    {
                                        Player.SetFutureStatus(item.Key, Player.Status.WaitingNotGivenOffer);
                                    }
                                }
                                else
                                {
                                    if (!item.Value.isProposer)
                                    {
                                        //item.Value.responseTime = timeInTheCurrentState;
                                        Player.SetFutureStatus(item.Key, Player.Status.FinishedRespondingWaiting);
                                    }
                                }
                            }
                            if (everyOneResponded)
                            {
                                room.Value.roomState   = RoomState.Summary;
                                coalitionHasBeenFormed = room.Value.isCoalitionFormed();
                                room.Value.UpdateAllPlayersToSummary(coalitionHasBeenFormed);
                                room.Value.NextRoundTimeOut = DateTime.Now.AddSeconds(5);
                            }
                            break;

                        case RoomState.Summary:

                            break;

                        default:
                            break;
                        }
                    }
                }
                foreach (var item in roomsToBeRemoved)
                {
                    onlineRooms.Remove(item);
                }
                HttpContext.Current.Application["OnlineRooms"] = onlineRooms;
            }
            changeLock.ReleaseMutex();
        }