コード例 #1
0
 private void Awake()
 {
     MakeInstance();
     Database    = Resources.Load("TriviaDatabase") as TriviaDatabase;
     CatDatabase = Resources.Load("CategoriesDatabase") as CategoriesDatabase;
     LoadCategories();
 }
コード例 #2
0
ファイル: CommonFunctions.cs プロジェクト: crashdsb/quiz-game
        /// <summary>
        /// Loads the TriviaDatabase, if it doesn't exist creates the database and returns it.
        /// </summary>
        /// <returns></returns>
        public static TriviaDatabase LoadTriviaDatabase()
        {
            TriviaDatabase triviaDatabase = Resources.Load("TriviaDatabase") as TriviaDatabase;

            if (triviaDatabase == null)
            {
                triviaDatabase = CreateTriviaDatabase();
            }
            return(triviaDatabase);
        }
コード例 #3
0
 private void Awake()
 {
     if (instance == null)
     {
         instance = this;
     }
     else if (instance != this)
     {
         Debug.Log("Instance already exists, destroying object");
         Destroy(this);
     }
 }
コード例 #4
0
        private bool IsCatEmpty(int CatID)
        {
            TriviaDatabase triviaData = CommonFunctions.LoadTriviaDatabase();

            for (int i = 0; i < triviaData.COUNT; i++)
            {
                if (triviaData.Trivia(i).category == CatID)
                {
                    return(false);
                }
            }
            return(true);
        }
コード例 #5
0
ファイル: CommonFunctions.cs プロジェクト: crashdsb/quiz-game
        /// <summary>
        /// Creates the TriviaDatabase and returns it
        /// </summary>
        /// <returns></returns>
        public static TriviaDatabase CreateTriviaDatabase()
        {
            TriviaDatabase triviaDatabase = (TriviaDatabase)ScriptableObject.CreateInstance(typeof(TriviaDatabase));

            if (triviaDatabase != null)
            {
                CreateFolders();
                AssetDatabase.CreateAsset(triviaDatabase, trivia_loc);
                AssetDatabase.Refresh();
                AssetDatabase.SaveAssets();
            }
            return(triviaDatabase);
        }
コード例 #6
0
        private void OnEnable()
        {
            if (triviaDatabase == null)
            {
                triviaDatabase = CommonFunctions.LoadTriviaDatabase();
            }

            if (categoryDatabase == null)
            {
                categoryDatabase = CommonFunctions.LoadCategoriesDatabase();
            }
            state = State.BLANK;
        }
コード例 #7
0
        public async Task AddMessageAsync(Message message)
        {
            DateTime time = DateTime.Now.ToLocalTime();

            IReliableDictionary <DateTime, Message> messagesDictionary =
                await this.StateManager.GetOrAddAsync <IReliableDictionary <DateTime, Message> >("messages");

            //dictionary for the scores
            IReliableDictionary <string, int> scoresDictionary =
                await StateManager.GetOrAddAsync <IReliableDictionary <string, int> >("scores");

            var currentQuestion =
                await StateManager.GetOrAddAsync <IReliableQueue <KeyValuePair <string, string> > >("currentQuestion");

            using (ITransaction tx = StateManager.CreateTransaction())
            {
                var currentScoreConditional = await scoresDictionary.TryGetValueAsync(tx, message.Name);

                if (!currentScoreConditional.HasValue)
                {
                    await scoresDictionary.GetOrAddAsync(tx, message.Name, 0);
                }
                await tx.CommitAsync();
            }

            //checking for the right answer
            using (ITransaction tx = this.StateManager.CreateTransaction())
            {
                if (message.MessageText.ToLowerInvariant().Trim() == "try me")
                {
                    var q = TriviaDatabase.GetRandomQuestion();
                    await currentQuestion.TryDequeueAsync(tx);

                    await currentQuestion.EnqueueAsync(tx, q);

                    await tx.CommitAsync();
                }
                else
                {
                    var question = await currentQuestion.TryPeekAsync(tx);

                    if (question.Value.Value.ToLowerInvariant().Trim()
                        == message.MessageText.ToLowerInvariant().Trim())
                    {
                        await messagesDictionary.AddAsync(tx, time,
                                                          new Message { Name = message.Name, MessageText = message.MessageText });

                        await messagesDictionary.AddAsync(tx, time.AddTicks(1),
                                                          new Message { Name = "Admin", MessageText = "You are correct " + message.Name + "! You get one point for this" });

                        //get and update the score
                        var currentScoreConditional = await scoresDictionary.TryGetValueAsync(tx, message.Name);

                        var currentScore = currentScoreConditional.HasValue ? currentScoreConditional.Value + 1 : 0;
                        await scoresDictionary.AddOrUpdateAsync(tx, message.Name, currentScore, (x, y) => ++ y);

                        await currentQuestion.TryDequeueAsync(tx);

                        await currentQuestion.EnqueueAsync(tx, TriviaDatabase.GetRandomQuestion());

                        await tx.CommitAsync();
                    }
                    else
                    {
                        await messagesDictionary.AddAsync(tx, time, message);

                        await tx.CommitAsync();
                    }
                }
            }
        }