asp.net web api - Identify the starting point resource for REST service -
i creating asp.net web api service maze game.
- users should cells details in maze visualize maze in 2d plane. achieving using method
public list<cell> get()
- user should details of cell giving cellid input. achieving using method
public cellrepresentation get(string id)
. note cellrepresentation used return hypermedia links using webapi.hal. these links represent target cells possible movements up, down, left , right.
now, need way communicate starting point cell users start game. [in following maze , cell “10” starting point]. rest based way send message?
controller
public class cellscontroller : apicontroller { public cellscontroller() { } //get cells in maze public list<cell> get() { maze m = new maze(); return m.getcells(); } //get specific cell public cellrepresentation get(string id) { maze m = new maze(); cell c = m.getcell(id); list<cell> possiblelists = m.getpossiblerelatedcells(c); cellrepresentation beerrep = new cellrepresentation(c, possiblelists); return beerrep; } }
hal related classes
public class cellrepresentation : webapi.hal.representation { //cellid cell thecell; list<cell> possiblemovecells; public cellrepresentation(cell c, list<cell> possiblemovecells ) { thecell = c; this.possiblemovecells = possiblemovecells; } public override string rel { { return linktemplates.celllinks.cellentry.rel; } set { } } public override string href { { return linktemplates.celllinks.cellentry.createlink(new { id = thecell.cellid }).href; } set { } } protected override void createhypermedia() { foreach (cell relatedcell in possiblemovecells) { if (relatedcell.relativename == "up") { links.add(linktemplates.celllinks.uplink.createlink(new { id = relatedcell.cellid })); } if (relatedcell.relativename == "right") { links.add(linktemplates.celllinks.rightlink.createlink(new { id = relatedcell.cellid })); } if (relatedcell.relativename == "down") { links.add(linktemplates.celllinks.downlink.createlink(new { id = relatedcell.cellid })); } if (relatedcell.relativename == "left") { links.add(linktemplates.celllinks.leftlink.createlink(new { id = relatedcell.cellid })); } } } } public static class linktemplates { public static class celllinks { public static link cellentry { { return new link("self", "~/api/cells/{id}"); } } public static link uplink { { return new link("up", "~/api/cells/{id}"); } } public static link rightlink { { return new link("right", "~/api/cells/{id}"); } } public static link downlink { { return new link("down", "~/api/cells/{id}"); } } public static link leftlink { { return new link("left", "~/api/cells/{id}"); } } } }
business classes
public class cell { public int xval { get; set; } public int yval { get; set; } public bool topiswall { get; set; } public bool rightiswall { get; set; } public bool bottomiswall { get; set; } public bool leftiswall { get; set; } public bool isstartcell { get; set; } public bool isextcell { get; set; } public string relativename { get; set; } //top, right, etc. public string cellid { { string characterid = xval.tostring() + yval.tostring(); return characterid; //example 10 } } } public class maze { list<cell> cells; public maze() { cells = createcells(); } public cell getfirtcell() { cell firstcell = null; foreach (cell c in cells) { if(c.isstartcell ) { firstcell = c; break; } } return firstcell; } public cell getcell(string cellid) { cell thecell = null; foreach (cell c in cells) { if (c.cellid == cellid) { thecell = c; break; } } return thecell; } public list<cell> getcells() { return cells; } public list<cell> getpossiblerelatedcells(cell inputcell) { list<cell> possiblecells = new list<cell>(); foreach (cell c in cells) { if (c.xval == inputcell.xval-1 && c.rightiswall == false && c.yval== inputcell.yval ) { //go left input cell c.relativename = "left"; possiblecells.add(c); } else if (c.xval == inputcell.xval + 1 && c.leftiswall == false && c.yval == inputcell.yval ) { //go right input cell c.relativename = "right"; possiblecells.add(c); } else if (c.yval == inputcell.yval - 1 && c.topiswall == false && c.xval == inputcell.xval ) { //go down input cell c.relativename = "down"; possiblecells.add(c); } else if (c.yval == inputcell.yval + 1 && c.bottomiswall == false && c.xval == inputcell.xval) { //go input cell c.relativename = "up"; possiblecells.add(c); } } return possiblecells; } public list<cell> createcells() { list<cell> cells = new list<cell>(); //cells = cell cell1 = new cell { xval = 0,yval = 0,topiswall = false,rightiswall = false,bottomiswall = true,leftiswall = true, relativename="self" }; cell cell2 = new cell { xval = 1,yval = 0,topiswall = true,rightiswall = false,bottomiswall = false,leftiswall = false, isstartcell = true, //--start relativename="self" }; cell cell3 = new cell { xval = 2,yval = 0,topiswall = false,rightiswall = false,bottomiswall = true,leftiswall = false, relativename="self" }; cell cell4 = new cell { xval = 3,yval = 0,topiswall = false,rightiswall = true,bottomiswall = true,leftiswall = false, relativename = "self" }; cell cell5 = new cell { xval = 0,yval = 1,topiswall = true,rightiswall = false,bottomiswall = false,leftiswall = true, relativename = "self" }; cell cell6 = new cell { xval = 0,yval = 0,topiswall = true,rightiswall = false,bottomiswall = false,leftiswall = false, relativename = "self" }; cell cell7 = new cell { xval = 1,yval = 1,topiswall = true,rightiswall = false,bottomiswall = true,leftiswall = false, relativename = "self" }; cell cell8 = new cell { xval = 2,yval = 1,topiswall = false,rightiswall = true,bottomiswall = false,leftiswall = false, relativename = "self" }; cell cell9 = new cell { xval = 3,yval = 1,topiswall = false,rightiswall = true,bottomiswall = false,leftiswall = true, relativename = "self" }; cell cell10 = new cell { xval = 2,yval = 2,topiswall = true,rightiswall = true,bottomiswall = false,leftiswall = true, relativename = "self" }; cell cell11 = new cell { xval = 3,yval = 2,topiswall = true,rightiswall = true,bottomiswall = false,leftiswall = true, relativename = "self" }; cell cell12 = new cell { xval = 3,yval = 3,topiswall = true,rightiswall = true,bottomiswall = false,leftiswall = false, isextcell = true, //--exit relativename = "self" }; cells.add(cell1); cells.add(cell2); cells.add(cell3); cells.add(cell4); cells.add(cell5); cells.add(cell6); cells.add(cell7); cells.add(cell8); cells.add(cell9); cells.add(cell10); cells.add(cell11); cells.add(cell12); return cells; } }
maze
Comments
Post a Comment