예제 #1
0
파일: ChatHub.cs 프로젝트: TheBobo/Dots
        public bool DrawLine(string tblId, string cellId, string oponentId, string myId)
        {
            var gt = context.GameTables.FirstOrDefault(x => x.GameId == tblId);

            var lastBoardInfoList = context.MoveTables.Where(x => x.TableId == gt.GameId).ToList();
            var lastBoardInfo = lastBoardInfoList[lastBoardInfoList.Count - 1];

            var tableCellStrings = lastBoardInfo.Board.Split(',');
            //Clients.All.allertAMessage(gt.UserInTurn, oponentId, tableCellStrings.Length);

            List<CellObj> tableCellObj = new List<CellObj>();

            foreach (var item in tableCellStrings)
            {
                CellObj obj = new CellObj(item);
                tableCellObj.Add(obj);
            }

            CellObj currentObj = new CellObj(cellId);
            currentObj.Taken = true;

            StringBuilder newBoardState = new StringBuilder();
            foreach (var item in tableCellObj)
            {
                if (item.Row == currentObj.Row && item.Col == currentObj.Col)
                {

                    newBoardState.Append(currentObj.ToString());
                }
                else
                {
                    newBoardState.Append(item.ToString());

                }
                newBoardState.Append(',');
            }
            newBoardState.Length--;

            NewState(cellId, gt.GameId, gt.UserInTurn, newBoardState);

            var hasCloseBox = CloseBox(currentObj, tableCellObj, newBoardState, gt.GameId, gt.UserInTurn);

            if (hasCloseBox.IsClose == false)
            {

                gt.UserInTurn = oponentId;

            }
            else
            {
                var meUserPf = context.Users.FirstOrDefault(x => x.DeviceId == myId);
                var oponentUserPf = context.Users.FirstOrDefault(x => x.DeviceId == oponentId);
                if (hasCloseBox.RowUp != 0 && hasCloseBox.ColUp != 0)
                {
                    string boxId = hasCloseBox.RowUp + "-" + hasCloseBox.ColUp;
                    Clients.Client(meUserPf.UserWebClientId).colorBox(myId, oponentId, tblId, boxId);
                    Clients.Client(oponentUserPf.UserWebClientId).colorBox(myId, oponentId, tblId, boxId);
                }
                if (hasCloseBox.RowDown != 0 && hasCloseBox.ColDown != 0)
                {
                    string boxId = hasCloseBox.RowDown + "-" + hasCloseBox.ColDown;
                    Clients.Client(meUserPf.UserWebClientId).colorBox(myId, oponentId, tblId, boxId);
                    Clients.Client(oponentUserPf.UserWebClientId).colorBox(myId, oponentId, tblId, boxId);
                }
            }
            context.SaveChanges();
            return true;
        }