public string MakeSendMessage(string receivedMessage)
        {
            var message = JsonConvert.DeserializeObject <ClientMessage>(receivedMessage);

            var(methodSuccess, sendMessage) = message switch
            {
                CreatePlayerMessage cr => CreatePlayer(cr),
                CloseCreateMessage cl => CloseCreate(cl),
                GetMatchInfoMessage gm => GetMatchInfo(gm),
                GetAllMatchesMessage _ => GetAllMatches(),
                DiceMessage dm => ThrowDice(dm),
                GetStartedMatchMessage gsm => GetStatedMatch(gsm),
                GetRankingMessage gr => GetRanking(gr),
                GetMatchViewImageMessage gmv => GetMatchView(gmv),
                _ => throw new NotImplementedException()
            };

            return(HeaderProtocol.MakeHeader(sendMessage, methodSuccess));
        }
        private (bool, string) ThrowDice(DiceMessage diceMessage)
        {
            var matchCore = _startedMatch[diceMessage.MatchKey];

            if (matchCore.ActionSchedule.Peek() != diceMessage.PlayerId)
            {
                return(false, JsonConvert.SerializeObject(new FailedMessage("まだあなたのターンではありません")));
            }

            var dice     = Dice();
            var firstPos = matchCore.Players[diceMessage.PlayerId].Position + dice;
            var action   = new PlayerAction
            {
                PlayerID = diceMessage.PlayerId,
                Length   = dice
            };
            var result = matchCore.ReflectAction(action);

            _startedMatch[diceMessage.MatchKey] = matchCore;

            var pos = matchCore.Players[diceMessage.PlayerId].Position;

            return(result switch
            {
                ReflectionStatus.NextSuccess => (true,
                                                 JsonConvert.SerializeObject(new DiceResultMessage(dice, matchCore.Field.Squares[firstPos].Message,
                                                                                                   firstPos, pos))),
                ReflectionStatus.AlreadyFinished => (true,
                                                     JsonConvert.SerializeObject(new AlreadyFinishedMessage(matchCore.TopPlayerId, matchCore.Ranking !))),
                ReflectionStatus.NotYourTurn => (false,
                                                 JsonConvert.SerializeObject(new FailedMessage("まだあなたのターンではありません"))),
                ReflectionStatus.PrevDiceSuccess => (true,
                                                     JsonConvert.SerializeObject(new DiceResultMessage(dice, "", pos, pos))),
                ReflectionStatus.Error => throw new ArgumentException(),
                ReflectionStatus.PlayerGoal => (true,
                                                JsonConvert.SerializeObject(new DiceResultMessage(dice, "", 30, 30)
                {
                    Ranking = matchCore.Ranking
                })),
                _ => throw new ArgumentOutOfRangeException()
            });
Exemple #3
0
        /// <summary>
        /// ダイスを投げた結果を取得する
        /// </summary>
        /// <param name="matchKey"></param>
        /// <param name="playerId"></param>
        /// <returns></returns>
        public (ReflectionStatus, int, int, int, IEnumerable <int>) ThrowDice(string matchKey, int playerId)
        {
            var ranking  = new List <int>();
            var sendMsg  = new DiceMessage(matchKey, playerId);
            var sendJson = JsonConvert.SerializeObject(sendMsg);

            var(r, recvJson) = SocketManager.SendRecv(sendJson);
            while (!r)
            {
                sendJson      = JsonConvert.SerializeObject(sendMsg);
                (r, recvJson) = SocketManager.SendRecv(sendJson);
            }
            if (!r && recvJson == string.Empty)
            {
                return(ReflectionStatus.NotYourTurn, -1, -1, -1, ranking);
            }
            var tempMsg = JsonConvert.DeserializeObject <ServerMessage>(recvJson);

            switch (tempMsg.MethodType)
            {
            case "diceResult":
                var diceResult = JsonConvert.DeserializeObject <DiceResultMessage>(recvJson);
                var dice       = diceResult.Dice;
                var startPos   = diceResult.FirstPosition;
                var finishPos  = diceResult.FinalPosition;
                return((startPos < 31)
                                                ? (ReflectionStatus.NextSuccess, dice, startPos, finishPos, ranking)
                                                : (ReflectionStatus.PrevDiceSuccess, dice, startPos, finishPos, ranking));

            case "alreadyFinished":
                var alreadyFinished = JsonConvert.DeserializeObject <AlreadyFinishedMessage>(recvJson);
                var goalPlayerId    = alreadyFinished.GoaledPlayerId;
                ranking = new List <int>(alreadyFinished.Ranking);
                return(ReflectionStatus.AlreadyFinished, goalPlayerId, -1, -1, ranking);

            case "failed":
            default:
                return(ReflectionStatus.NotYourTurn, -1, -1, -1, ranking);
            }
        }
 public virtual Task <Message> SendDiceAsync(DiceMessage message, CancellationToken cancellationToken = default)
 {
     return(Client.SendDiceAsync(message.ChatId,
                                 message.DisableNotification, message.ReplyToMessageId, message.ReplyMarkup, cancellationToken,
                                 message.Emoji));
 }
Exemple #5
0
 public void ReceiveDiceMessage(DiceMessage obj)
 {
     this.DiceRolls.Insert(0, new Roll {
         Value = obj.Message
     });
 }