私は、ajaxリクエストに基づいて簡単なブラウザゲームを作成する予定です。私は、クライアント上に10x10のdivで構成されるマップを持っています。サーバー上に私のdivの状態を表す配列[10,10]があります。たとえば、クライアントが特定のdivをクリックすると、バックグラウンドでAjaxリクエストが送信され、配列が更新され、番号1が適切な配列位置。このアイデアに基づいて私はすべてのクライアントに同じマップを見せたいので、マップステートをすべてのクライアントで共有していることが分かっているので、マップステートをアプリケーションステートに格納することを考えていました。だから私の質問はどのように私はこれを行うか、または実際にAsp.Net MVCのアプリケーション状態になる方法です。私は多くを検索し、Asp.Net MVC 3のアプリケーション状態でデータを挿入するための適切な方法を見つけることができませんでした。特に、2次元の配列では少し難しくなりました。これまでの私のコードは、アプリケーションの状態を使用せずに多かれ少なかれ動作します。私は、マップ配列の新しいインスタンスを作成し、そのインスタンスをサーバからクライアントに返すたびに間違っているということです。なぜなら、1つのマップをインスタンス化してすべての人に同じマップを返すだけだからです。私はスタックしている部分。Asp.Net MVC 3のアプリケーション状態で2次元配列を格納して更新する
//ユーザーが自分のサイト
public class HomeController : Controller
{
public MapModel mapModel = new MapModel();
public ActionResult Index()
{
return View(mapModel);
}
}
// Ajaxリクエストのdiv(マップタイル)をユーザーがクリックする
$(".mapTile").click(function() {
var xx = $(this).attr("y");
var yy = $(this).attr("x");
$("#info").text($(this).attr("x"));
var tile = {
X: xx,
Y: yy,
Value: 1
};
$.ajax({
beforeSend: function() { ShowAjaxLoader(); },
url: "/Game/ShowTiles",
type: "POST",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify(tile),
success: function (data) {
HideAjaxLoader(), $.each(data, function (index, item) {
if (item.Value === 1) {
var xCordinate = item.X;
var yCordinate = item.Y;
$("#" + yCordinate + xCordinate).css("background-color", "red");
}
});
},
error: function() { showErrorMsg(); }
});
});
//私のモデル
//地図を訪れたときモデル
public class MapModel
{
private TileModel[,] mapTilesArray;
public TileModel[,] MapTilesArray
{
get
{
return mapTilesArray;
}
set
{
mapTilesArray = value;
}
}
public MapModel()
{
MapTilesArray = new TileModel[10,10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
MapTilesArray[i, j] = new TileModel();
}
}
}
}
//タイルモデル
public class TileModel
{
public int X{ get; set; }
public int Y{ get; set; }
public int Value { get; set; }
}
//私のゲームコントローラ
[HttpPost]
public JsonResult ShowTiles(TileModel tile)
{
MapModel map = new MapModel();
map.MapTilesArray[tile.X, tile.Y].Value = 1;
map.MapTilesArray[tile.X, tile.Y].X = tile.X;
map.MapTilesArray[tile.X, tile.Y].Y = tile.Y;
return Json(map.MapTilesArray);
}