Пример #1
0
        public string GetDrawChoice(string username, int playerNum)
        {
            RPSDbContext db = new RPSDbContext();
            Room         room;
            string       choice;

            if (playerNum == 1)
            {
                room = db.Rooms.Where(x => x.Player1 == username).OrderByDescending(x => x.RoomId).First();
                while (room.Player2Choice == null)
                {
                    Thread.Sleep(200);
                    db   = new RPSDbContext();
                    room = db.Rooms.Where(x => x.Player1 == username).OrderByDescending(x => x.RoomId).First();
                }
                choice             = room.Player2Choice;
                room.Player2Choice = null;
            }
            else
            {
                room = db.Rooms.Where(x => x.Player2 == username).OrderByDescending(x => x.RoomId).First();
                while (room.Player1Choice == null)
                {
                    Thread.Sleep(200);
                    db   = new RPSDbContext();
                    room = db.Rooms.Where(x => x.Player2 == username).OrderByDescending(x => x.RoomId).First();
                }
                choice             = room.Player1Choice;
                room.Player1Choice = null;
            }
            db.SaveChanges();
            return(choice);
        }
Пример #2
0
        public string[] GetLastMove(string username, int playerNum)
        {
            RPSDbContext db = new RPSDbContext();
            Room         room;

            string[] lastMove = { "" };
            if (playerNum == 1)
            {
                room = db.Rooms.Where(x => x.Player1 == username).OrderByDescending(x => x.RoomId).First();
                if (room.Player2LastMove == null)
                {
                    return(lastMove);
                }
                lastMove             = room.Player2LastMove.Split(',');
                room.Player2LastMove = null;
            }
            else
            {
                room = db.Rooms.Where(x => x.Player2 == username).OrderByDescending(x => x.RoomId).First();
                if (room.Player1LastMove == null)
                {
                    return(lastMove);
                }
                lastMove             = room.Player1LastMove.Split(',');
                room.Player1LastMove = null;
            }
            db.SaveChanges();
            return(lastMove);
        }
Пример #3
0
        public string[] GetInitPawnsLoc(string username, int playerNum)
        {
            RPSDbContext db = new RPSDbContext();
            Room         room;

            string[] pawnsLoc;
            if (playerNum == 1)
            {
                room = db.Rooms.Where(x => x.Player1 == username).OrderByDescending(x => x.RoomId).First();
                while (room.Player2InitPawns == null)
                {
                    Thread.Sleep(200);
                    db   = new RPSDbContext();
                    room = db.Rooms.Where(x => x.Player1 == username).OrderByDescending(x => x.RoomId).First();
                }
                pawnsLoc = room.Player2InitPawns.Split(',');
                room.Player2InitPawns = null;
            }
            else
            {
                room = db.Rooms.Where(x => x.Player2 == username).OrderByDescending(x => x.RoomId).First();
                while (room.Player1InitPawns == null)
                {
                    Thread.Sleep(200);
                    db   = new RPSDbContext();
                    room = db.Rooms.Where(x => x.Player2 == username).OrderByDescending(x => x.RoomId).First();
                }
                pawnsLoc = room.Player1InitPawns.Split(',');
                room.Player1InitPawns = null;
            }
            db.SaveChanges();
            return(pawnsLoc);
        }
Пример #4
0
        public HttpResponseMessage PostPlayerToQueue()
        {
            RPSDbContext db       = new RPSDbContext();
            Room         room     = db.Rooms.FirstOrDefault(x => x.Player1 != null && x.Player2 == null);
            string       username = HttpContext.Current.Request.Params["username"];

            if (room == null)
            {
                Room newRoom = new Room()
                {
                    Player1 = username,
                    Player2 = null
                };
                db.Rooms.Add(newRoom);
                db.SaveChanges();
                return(Request.CreateResponse(HttpStatusCode.Created, "Player opened a room"));
            }
            if (room.Player1 != username)
            {
                room.Player2 = username;
                db.SaveChanges();
                return(Request.CreateResponse(HttpStatusCode.OK, "Player entered as second"));
            }
            return(Request.CreateResponse(HttpStatusCode.BadRequest, "Same player name"));
        }
Пример #5
0
        public HttpResponseMessage GetReadyForGame(string username)
        {
            RPSDbContext db   = new RPSDbContext();
            Room         room = db.Rooms.OrderByDescending(x => x.RoomId).FirstOrDefault();

            if (room.Player2 == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, "Second player is not here yet"));
            }
            return(Request.CreateResponse(HttpStatusCode.OK, "Second player entered my room"));
        }
Пример #6
0
        public string GetWinner(string username, int playerNum)
        {
            RPSDbContext db = new RPSDbContext();
            Room         room;

            if (playerNum == 1)
            {
                room = db.Rooms.Where(x => x.Player1 == username).OrderByDescending(x => x.RoomId).First();
                return(room.Player2);
            }
            room = db.Rooms.Where(x => x.Player2 == username).OrderByDescending(x => x.RoomId).First();
            return(room.Player1);
        }
Пример #7
0
        public HttpResponseMessage DeletePlayerFromQueue(string username)
        {
            RPSDbContext db            = new RPSDbContext();
            Room         playerInQueue = db.Rooms.OrderByDescending(x => x.RoomId).FirstOrDefault(x => x.Player1 == username && x.Player2 == null);

            if (playerInQueue == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, "Player is not in queue"));
            }
            db.Rooms.Remove(playerInQueue);
            db.SaveChanges();
            return(Request.CreateResponse(HttpStatusCode.OK, "Player found and was deleted"));
        }
Пример #8
0
        public HttpResponseMessage PostDrawChoice()
        {
            string       choice    = HttpContext.Current.Request.Params["choice"];
            string       username  = HttpContext.Current.Request.Params["username"];
            int          playerNum = Convert.ToInt32(HttpContext.Current.Request.Params["playerNum"]);
            RPSDbContext db        = new RPSDbContext();
            Room         room;

            if (playerNum == 1)
            {
                room = db.Rooms.Where(x => x.Player1 == username).OrderByDescending(x => x.RoomId).First();
                room.Player1Choice = choice;
            }
            else
            {
                room = db.Rooms.Where(x => x.Player2 == username).OrderByDescending(x => x.RoomId).First();
                room.Player2Choice = choice;
            }
            return(db.SaveChanges() > 0 ? Request.CreateResponse(HttpStatusCode.OK, true) : Request.CreateResponse(HttpStatusCode.BadRequest, false));
        }