コード例 #1
0
ファイル: UserController.cs プロジェクト: kurbaniec/SWE1-MTCG
        public Response GetDeck(RequestParam param, AuthDetails?user)
        {
            if (user is null)
            {
                return(Response.Status(Status.BadRequest));
            }
            var cards = db.GetUserDeck(user.Username);

            // Send in requested format
            if (!param.Empty && param.Value["format"] == "plain")
            {
                // Send in plaintext
                var plainResponse
                    = cards.Aggregate("Id,Name,Damage\r\n", (current, card)
                                      => current + $"{card.Id},{card.Name},{card.Damage}\r\n");
                // Trim last newline
                // See: https://stackoverflow.com/a/1038062/12347616
                plainResponse = plainResponse.TrimEnd('\r', '\n');
                return(Response.PlainText(plainResponse));
            }
            // Send in JSON
            var jsonResponse = new Dictionary <string, object>();
            var cardsResponse
                = cards.Select(card => new Dictionary <string, object>
            {
                ["Id"] = card.Id, ["Name"] = card.Name, ["Damage"] = card.Damage
            }).ToList();

            jsonResponse["deck"] = cardsResponse;
            return(Response.Json(jsonResponse));
        }
コード例 #2
0
        /// <summary>
        /// Used to process a battle.
        /// </summary>
        /// <param name="playerA"></param>
        /// <param name="playerB"></param>
        private void Process(IPlayer playerA, IPlayer playerB)
        {
            // Get loggers
            IBattleLog battleLog = new BattleLog();

            var(playerLogA, playerLogB) = battleLog.GetPlayerLogs(playerA.Username, playerB.Username);
            // Get cards
            var rawDeckA = db.GetUserDeck(playerA.Username);
            var rawDeckB = db.GetUserDeck(playerB.Username);
            // Convert card data schema to functional card
            var deckA = rawDeckA.Select(
                rawCard => CardFactory.Print(rawCard.Name, rawCard.Damage, playerLogA)).
                        Where(card => card != null).ToList() !;
            var deckB = rawDeckB.Select(
                rawCard => CardFactory.Print(rawCard.Name, rawCard.Damage, playerLogB)).
                        Where(card => card != null).ToList() !;

            playerA.AddDeck(deckA !);
            playerB.AddDeck(deckB !);
            // Let's get ready to rumble
            var battle = new Battle(playerA, playerB, battleLog);
            var result = battle.ProcessBattle();
            var log    = (JsonConvert.SerializeObject(result.Log) is var json && json is {}) ? json : "";