private void 成绩录入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //通过窗体名称查询该窗体是否已经存在,如存在则显示,否则就新创建一个
            if (this.checkChildFrmExist("ScoreInput") == true)
            {
                return;
            }
            ScoreInput si = new ScoreInput(userName);

            si.MdiParent = this;
            si.Show();
        }
Esempio n. 2
0
    IEnumerator SendScore(string name, float elapsedTime)
    {
        ScoreInput scoreInput = new ScoreInput {
            name        = name,
            elapsedTime = elapsedTime
        };

        string rawInput = JsonUtility.ToJson(scoreInput);

        byte[] jsonBytes = Encoding.UTF8.GetBytes(rawInput);

        DownloadHandlerBuffer downloadHandlerBuffer = new DownloadHandlerBuffer();

        UploadHandlerRaw uploadHandlerRaw = new UploadHandlerRaw(jsonBytes);

        uploadHandlerRaw.contentType = "application/json";

        using (UnityWebRequest www = new UnityWebRequest(API_URL + "/scores", "POST", downloadHandlerBuffer, uploadHandlerRaw)) {
            yield return(www.SendWebRequest());

            submitLoading = false;

            if (www.result != UnityWebRequest.Result.Success)
            {
                OnSubmitError(www.error);
                Debug.Log(www.error);
            }
            else
            {
                OnSubmitError("");
                ScoreOutput myScoreOutput = new ScoreOutput {
                    name        = name,
                    date        = "",
                    elapsedTime = elapsedTime
                };
                currentScores.Add(myScoreOutput);
                currentScores.Sort((a, b) => - a.elapsedTime.CompareTo(b.elapsedTime));
                RefreshScores(currentScores);

                submitForbidden = true;
                RefreshSubmitButtonInteractivity();
                Debug.Log("Done uploading score");
            }
        }
    }
        private bool IsTimeDataValid(
            DateTimeOffset serverStartTime,
            DateTimeOffset serverEndTime,
            ScoreInput scoreInput,
            TimeSpan maxTimeSpan
            )
        {
            if (scoreInput.EndTime < scoreInput.StartTime)
            {
                return(false);
            }

            var startTimeSkew = serverStartTime - scoreInput.StartTime;

            if (startTimeSkew.Duration() > maxTimeSpan)
            {
                return(false);
            }

            var endTimeSkew = serverEndTime - scoreInput.EndTime;

            if (endTimeSkew.Duration() > maxTimeSpan)
            {
                return(false);
            }

            var durationDifference = (serverEndTime - serverStartTime) - (scoreInput.EndTime - scoreInput.StartTime);

            if (durationDifference.Duration() > maxTimeSpan)
            {
                return(false);
            }

            var anyAnwsersOutsideOfSubmissionRange = scoreInput.UserSequence
                                                     .Select(s => s.Time)
                                                     .Any(time => (time < scoreInput.StartTime) || (time > scoreInput.EndTime));

            if (anyAnwsersOutsideOfSubmissionRange)
            {
                return(false);
            }

            return(true);
        }
        public async Task <IActionResult> Post([FromBody] ScoreInput scoreInput)
        {
            var now = DateTimeOffset.UtcNow;

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Save user id from Object Id ("oid") claim onto model
            var userId = User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

            scoreInput.UserId = userId;


            var signedStartTime    = Convert.FromBase64String(scoreInput.SignedStartTime);
            var unsigngedStartTime = dataProtector.Unprotect(signedStartTime);
            var startTime          = DateTimeOffset.Parse(Encoding.UTF8.GetString(unsigngedStartTime));

            // Time data is not value log warning with user token for review later.
            var isTimeDataValid = IsTimeDataValid(
                startTime,
                now,
                scoreInput,
                TimeSpan.FromSeconds(10)
                );

            if (!isTimeDataValid)
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, $"You have been logged for attempted cheating.  Your account will be reviewed and may be deleted."));
            }

            var tableLayout = new TableLayout()
            {
                Width              = scoreInput.TableWidth,
                Height             = scoreInput.TableHeight,
                ExpectedSequence   = scoreInput.ExpectedSequence,
                RandomizedSequence = scoreInput.RandomizedSequence
            };

            var tableLayoutString = JsonConvert.SerializeObject(tableLayout);

            var tableType = new TableType()
            {
                Width      = scoreInput.TableWidth,
                Height     = scoreInput.TableHeight,
                Properties = scoreInput.TableProperties.OrderBy(p => p.Key).ToList()
            };

            var tableTypeString = JsonConvert.SerializeObject(tableType);

            using (var hashAlgorithm = SHA256.Create())
            {
                var tableLayoutHash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(tableLayoutString));
                tableLayout.Id = Convert.ToBase64String(tableLayoutHash).Replace('/', '_');

                var tableTypeHash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(tableTypeString));
                tableType.Id = Convert.ToBase64String(tableTypeHash).Replace('/', '_');
            }

            var scoreDocument = new Documents.Score()
            {
                Sequence             = scoreInput.UserSequence,
                TableLayoutId        = tableLayout.Id,
                TableTypeId          = tableType.Id,
                StartTime            = scoreInput.StartTime,
                EndTime              = scoreInput.EndTime,
                Duration             = scoreInput.EndTime - scoreInput.StartTime,
                DurationMilliseconds = (scoreInput.EndTime - scoreInput.StartTime).TotalMilliseconds,
                UserId = scoreInput.UserId
            };

            var newTableLayout = await documentClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(documentDbOptions.DatabaseName, documentDbOptions.TableLayoutsCollectionName), tableLayout);

            var newTableType = await documentClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(documentDbOptions.DatabaseName, documentDbOptions.TableTypesCollectionName), tableType);

            var newScoreDocument = await documentClient.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(documentDbOptions.DatabaseName, documentDbOptions.ScoresCollectionName), scoreDocument);

            var createdUrl = Url.RouteUrl("GetScore", new { id = newScoreDocument.Resource.Id });

            var newScore = new DomainModels.Score()
            {
                Id = newScoreDocument.Resource.Id,
                DurationMilliseconds = scoreDocument.DurationMilliseconds,
                EndTime   = scoreDocument.EndTime,
                StartTime = scoreDocument.StartTime,
                UserId    = scoreDocument.UserId
            };

            return(Created(createdUrl, newScore));
        }