public async Task <EntryResult> AddNewEntry(NewEntry newEntry) { await _repository.AddNewPlayerIfNecessary(newEntry.Name); bool banned = await _repository.IsPlayerBanned(newEntry.Name); if (banned) { EntryResult result = new EntryResult(); result.Name = newEntry.Name; result.Banned = true; result.BestRanking = 0; result.BestScore = 0; result.Score = 0; result.Ranking = 0; return(result); } else { ScoreEntry entry = new ScoreEntry(); entry.Date = DateTime.UtcNow; entry.Name = newEntry.Name; entry.Score = newEntry.Score; entry._id = Guid.NewGuid(); return(await _repository.AddNewEntry(entry)); } }
//******************************************* // Use this for initialization void Start() { backToNameButton.onClick.AddListener(BackToNameButtonPressed); exitButton.onClick.AddListener(ExitButtonPressed); nameSubmitButton.onClick.AddListener(NameSubmitButtonPressed); backFromScoresButton.onClick.AddListener(BackFromScoresButtonPressed); myBestScoresButton.onClick.AddListener(MyBestButtonPressed); myBestTodayButton.onClick.AddListener(MyBestTodayButtonPressed); myBestWeekButton.onClick.AddListener(MyBestWeekButtonPressed); myBestMonthButton.onClick.AddListener(MyBestMonthButtonPressed); highScoresButton.onClick.AddListener(HighScoresButtonPressed); highScoresTodayButton.onClick.AddListener(HighScoresTodayButtonPressed); highScoresWeekButton.onClick.AddListener(HighScoresWeekButtonPressed); highScoresMonthButton.onClick.AddListener(HighScoresMonthButtonPressed); statsMenuButton.onClick.AddListener(StatsButtonPressed); playersName = "Anonymous"; playersScore = 0; gameIsOn = false; gameIsPlayed = false; gameTimer = 0f; commonScore = 0; averageScore = 0f; commonScoreText.text = ""; averageScoreText.text = ""; _entryResult = null; mainPanel.gameObject.SetActive(false); gamePanel.gameObject.SetActive(false); scorePanel.gameObject.SetActive(false); statsPanel.gameObject.SetActive(false); namePanel.gameObject.SetActive(true); currentMenu = Menu.InNameMenu; }
public RemoteTask <string> ApplyUserTemplate(EntryResult entryResult) { // TODO: Use Stubble.Compilation return(MustacheEngine.RenderAsync(Config.RenderTemplate, entryResult) .AsTask() .ConfigureRemoteTask(ex => { switch (ex) { case StubbleException stubbleException: LogTo.Warning(stubbleException, @"Mustache rendering failed. Template: ------------------------------------- {RenderTemplate} Data: ------------------------------------- {EntryResult}", Config.RenderTemplate, entryResult.Serialize()); break; default: LogTo.Error(ex, "An exception was thrown while applying user template"); break; } })); #endregion }
void FillInScoreListWithEntry(ScoreEntry[] entries, int fromRank, EntryResult entry) { int scoresSet = 0; for (int i = 0; i < entries.Length; i++) { string rank; int r = fromRank + scoresSet; if (r < 10) { rank = "0" + r; } else { rank = "" + r; } rankText[i].text = rank; nameText[i].text = entries[i].name; scoreText[i].text = "" + entries[i].score; if (entries[i].name.Equals(entry.name)) { if (r == entry.ranking) { rankText[i].color = highlightOrange; nameText[i].color = highlightOrange; scoreText[i].color = highlightOrange; } else { rankText[i].color = highLightPurple; nameText[i].color = highLightPurple; scoreText[i].color = highLightPurple; } } else { rankText[i].color = normalYellow; nameText[i].color = normalYellow; scoreText[i].color = normalYellow; } scoresSet++; if (scoresSet == 10) { break; } } while (scoresSet < 10) { string rank; int r = fromRank + scoresSet; if (r < 10) { rank = "0" + r; } else { rank = "" + r; } rankText[scoresSet].text = rank; nameText[scoresSet].text = ""; scoreText[scoresSet].text = ""; rankText[scoresSet].color = normalYellow; nameText[scoresSet].color = normalYellow; scoreText[scoresSet].color = normalYellow; scoresSet++; } }
public IEnumerator ISendResult() { //tämä ei toimi NewEntry n = new NewEntry { name = playersName, score = playersScore }; string entryJson = JsonUtility.ToJson(n); byte[] bytes = Encoding.UTF8.GetBytes(entryJson); WWWForm form = new WWWForm(); form.AddBinaryData("body", bytes); UnityWebRequest req = UnityWebRequest.Post(www, form); req.SetRequestHeader("Content-Type", "application/json"); req.SetRequestHeader("UnityKey", "unity1234"); req.uploadHandler = new UploadHandlerRaw(bytes); req.uploadHandler.contentType = "application/json"; yield return(req.SendWebRequest()); if (req.isNetworkError || req.isHttpError) { Debug.Log(req.error); } else { string a = req.downloadHandler.text; EntryResult result = JsonUtility.FromJson <EntryResult>(a); if (result.ranking > 0) { madeItOrNotText.text = "YOU MADE IT TO THE LIST"; int startFrom; if (result.ranking < 10) { startFrom = 1; } else { startFrom = result.ranking - 4; } string searchUrl = www + "?slice=" + startFrom; UnityWebRequest webRequest = UnityWebRequest.Get(searchUrl); webRequest.SetRequestHeader("unityKey", "unity1234"); yield return(webRequest.SendWebRequest()); if (webRequest.isNetworkError || webRequest.isHttpError) { Debug.Log(webRequest.error); } else { string entryList = webRequest.downloadHandler.text; ScoreEntryArray sArray = JsonUtility.FromJson <ScoreEntryArray>("{\"entries\":" + entryList.ToString() + "}"); FillInScoreListWithEntry(sArray.entries.ToArray(), startFrom, result); } } else { madeItOrNotText.text = "Bummer... Not good enough"; HighScoresButtonPressed(); } } }
public EntryResult AddNewScoreInit(NewEntry n) { bool isBanned = AddNewPlayer(n.name); if (isBanned) { EntryResult e = new EntryResult { banned = true, bestranking = 0, bestscore = 0, name = n.name, ranking = 0, score = 0 }; return(e); } else { int r = UnityEngine.Random.Range(0, 50); DateTime d = DateTime.UtcNow; d = d.AddDays(-r); ScoreEntry entry = new ScoreEntry { _id = Guid.NewGuid(), name = n.name, score = n.score, date = d }; AddNewEntry(entry); List <Player> bannedPlayers = GetBannedPlayers(true); List <ScoreEntry> legalScores = new List <ScoreEntry>(); EntryResult result = new EntryResult { name = n.name, banned = false, score = n.score }; bool foundBest = false; bool foundThis = false; for (int i = 0; i < scores.Count; i++) { bool excluded = false; for (int j = 0; j < bannedPlayers.Count; j++) { if (scores[i].name.Equals(bannedPlayers[j].name)) { excluded = true; break; } } if (!excluded) { legalScores.Add(scores[i]); if (scores[i].name.Equals(entry.name)) { if (!foundBest) { foundBest = true; result.bestscore = scores[i].score; result.bestranking = legalScores.Count; } if (scores[i]._id == entry._id) { foundThis = true; result.score = scores[i].score; result.ranking = legalScores.Count; } } } if (foundThis) { break; } } return(result); } }
public async Task <EntryResult> AddNewEntry(ScoreEntry entry) { EntryResult result = new EntryResult(); result.Name = entry.Name; result.Score = entry.Score; var filter = Builders <ScoreEntry> .Filter.Gte("Score", entry.Score); var betterScores = await _scores.Find(filter).ToListAsync(); bool qualifies; if (betterScores.Count == _maxScoreEntries) { qualifies = false; } else { //Inserts new score await _scores.InsertOneAsync(entry); qualifies = true; } //Gets the ranking of the score filter = Builders <ScoreEntry> .Filter.Eq("_id", entry._id); var sort = Builders <ScoreEntry> .Sort.Descending("Score"); var cursor = _scores.Find(_ => true).Sort(sort); var entries = await cursor.ToListAsync(); if (qualifies) { for (int i = 0; i < entries.Count; i++) { if (entries[i]._id == entry._id) { result.Ranking = i + 1; break; } } } else { result.Ranking = 0; } //Next, the best score & its ranking for (int i = 0; i < entries.Count; i++) { if (entries[i].Name.Equals(entry.Name)) { result.BestScore = entries[i].Score; result.BestRanking = i + 1; break; } } return(result); }