source: binary-improvements/webserver/js/index.js@ 165

Last change on this file since 165 was 163, checked in by alloc, 10 years ago

fixes

  • Property svn:executable set to *
File size: 6.9 KB
RevLine 
[150]1var REGIONSIZE = 512;
2var CHUNKSIZE = 16;
3var TILESIZE = 128;
4var MAXZOOM = 4;
5
[163]6var BAG_COLS = 8;
7var BAG_ROWS = 4;
8var BELT_COLS = 8;
9var INV_ITEM_WIDTH = 58;
10var INV_ITEM_HEIGHT = 40;
11
[133]12SDTD_Projection = {
13 project: function (latlng) {
14 return new L.Point(latlng.lng / 16, latlng.lat / 16);
15 },
16
17 unproject: function (point) {
18 return new L.LatLng(point.y * 16, point.x * 16);
19 }
20};
21
22SDTD_CRS = L.extend({}, L.CRS.Simple, {
23 projection: SDTD_Projection,
24 transformation: new L.Transformation(1, 0, -1, 0),
25
26 scale: function (zoom) {
27 return Math.pow(2, zoom);
28 }
29});
30
[149]31var CoordToChunk = function(latlng) {
[150]32 var x = Math.floor(((latlng.lng + 16777216) / CHUNKSIZE) - (16777216 / CHUNKSIZE));
33 var y = Math.floor(((latlng.lat + 16777216) / CHUNKSIZE) - (16777216 / CHUNKSIZE));
[149]34 return L.latLng(y, x);
35}
36
37var CoordToRegion = function(latlng) {
[150]38 var x = Math.floor(((latlng.lng + 16777216) / REGIONSIZE) - (16777216 / REGIONSIZE));
39 var y = Math.floor(((latlng.lat + 16777216) / REGIONSIZE) - (16777216 / REGIONSIZE));
[149]40 return L.latLng(y, x);
41}
42
43var FormatCoord = function(latlng) {
44 return Math.abs(latlng.lat)+ (latlng.lat>=0 ? " N" : " S") + " / " + Math.abs(latlng.lng) + (latlng.lng>=0 ? " E" : " W");
45}
46
[150]47var FormatRegionFileName = function(latlng) {
48 return "r." + latlng.lng + "." + latlng.lat + ".7rg";
49}
50
[133]51var map = L.map('map', {
52 zoomControl: true,
53 attributionControl: false,
54 crs: SDTD_CRS
55}).setView([0, 0], 0);
56
[150]57var tileLayer = L.tileLayer('../../map/{z}/{x}/{y}.png?t={time}', {
58 maxZoom: MAXZOOM+1,
[133]59 minZoom: 0,
[150]60 maxNativeZoom: MAXZOOM,
61 tileSize: TILESIZE,
[133]62 continuousWorld: true,
63 tms: true,
64 unloadInvisibleTiles: true,
65 time: function() { return new Date().getTime(); }
[150]66});
67
68var regionLayer = L.tileLayer.canvas({
69 maxZoom: MAXZOOM+1,
70 minZoom: 0,
71 maxNativeZoom: MAXZOOM+1,
72 tileSize: TILESIZE,
73 continuousWorld: true
74});
75
76regionLayer.drawTile = function(canvas, tilePoint, zoom) {
77 var blockWorldSize = TILESIZE * Math.pow(2, MAXZOOM-zoom);
78 var tileLeft = tilePoint.x * blockWorldSize;
79 var tileBottom = (-1-tilePoint.y) * blockWorldSize;
80 var blockPos = L.latLng(tileBottom, tileLeft);
81
82 var ctx = canvas.getContext('2d');
83
84 ctx.strokeStyle = "lightblue";
85 ctx.fillStyle = "lightblue";
86 ctx.lineWidth = 1;
87 ctx.font="14px Arial";
88
89 var lineCount = blockWorldSize / REGIONSIZE;
90 if (lineCount >= 1) {
91 var pos = 0;
92 while (pos < TILESIZE) {
93 // Vertical
94 ctx.beginPath();
95 ctx.moveTo(pos, 0);
96 ctx.lineTo(pos, TILESIZE);
97 ctx.stroke();
98
99 // Horizontal
100 ctx.beginPath();
101 ctx.moveTo(0, pos);
102 ctx.lineTo(TILESIZE, pos);
103 ctx.stroke();
104
105 pos += TILESIZE / lineCount;
106 }
107 ctx.fillText(FormatRegionFileName(CoordToRegion(blockPos)), 4, TILESIZE-5);
108 } else {
109 if ((tileLeft % REGIONSIZE) == 0) {
110 // Vertical
111 ctx.beginPath();
112 ctx.moveTo(0, 0);
113 ctx.lineTo(0, TILESIZE);
114 ctx.stroke();
115 }
116 if ((tileBottom % REGIONSIZE) == 0) {
117 // Horizontal
118 ctx.beginPath();
119 ctx.moveTo(0, TILESIZE);
120 ctx.lineTo(TILESIZE, TILESIZE);
121 ctx.stroke();
122 }
123 if ((tileLeft % REGIONSIZE) == 0 && (tileBottom % REGIONSIZE) == 0) {
124 ctx.fillText(FormatRegionFileName(CoordToRegion(blockPos)), 4, TILESIZE-5);
125 }
126 }
127
128 var pos = tileLeft;
129
130}
131
[153]132var playersOnlineMarkerGroup = L.layerGroup();
133var playersOfflineMarkerGroup = L.layerGroup();
134
[150]135var baseLayers = {
136 //"Map": tileLayer
137};
138
139var overlays = {
[153]140 "Region files": regionLayer,
[157]141 "Players (online) (<span id='mapControlOnlineCount'>0</span>)" : playersOnlineMarkerGroup,
142 "Players (offline) (<span id='mapControlOfflineCount'>0</span>)" : playersOfflineMarkerGroup
[150]143};
144
145tileLayer.addTo(map);
146L.control.layers(baseLayers, overlays, {
147 collapsed: false
[133]148}).addTo(map);
149
150map.on('mousemove', function(e) {
[149]151 L.DomUtil.get('pos').textContent = FormatCoord(e.latlng);
[133]152});
[153]153
154var playersMappingList = {};
155
[163]156var showInv = function(steamid) {
157 $.getJSON( "../api/getplayerinventory", { steamid: steamid })
158 .done(function(data) {
159 $("#invPlayerName").text(playersMappingList[steamid].name);
160 for (var y = 0; y < BAG_ROWS; y++) {
161 for (var x = 0; x < BAG_COLS; x++) {
162 if (data.bag[y*BAG_COLS+x].count > 0) {
163 $("#bagField"+x+"_"+y).attr("style", "background-image: url(itemimages/" + data.bag[y*BAG_COLS+x].name + ".png);");
164 $("#bagFieldText"+x+"_"+y).text(data.bag[y*BAG_COLS+x].count);
165 } else {
166 $("#bagField"+x+"_"+y).attr("style", "background-image: none;");
167 $("#bagFieldText"+x+"_"+y).text("");
168 }
169 }
170 }
171
172 for (var x = 0; x < BELT_COLS; x++) {
173 if (data.belt[x].count > 0) {
174 $("#beltField"+x).attr("style", "background-image: url(itemimages/" + data.belt[x].name + ".png);");
175 $("#beltFieldText"+x).text(data.belt[x].count);
176 } else {
177 $("#beltField"+x).attr("style", "background-image: none;");
178 $("#beltFieldText"+x).text("");
179 }
180 }
181
182 $( "#dialog-message" ).dialog({
183 modal: true,
184 width: BAG_COLS*INV_ITEM_WIDTH + 60,
185 buttons: {
186 Ok: function() {
187 $( this ).dialog( "close" );
188 }
189 }
190 });
191 })
192 .fail(function() {
193 console.log("Error fetching player inventory");
194 })
195 .always(function() {
196 });
197};
198
199for (var y = 0; y < BAG_ROWS; y++) {
200 $("#bagTable").append("<tr id=\"bagRow"+y+"\"></tr>");
201 for (var x = 0; x < BAG_COLS; x++) {
202 $("#bagRow"+y).append(
203 "<td class=\"invField\" id=\"bagField"+x+"_"+y+"\">" +
204 "<span class=\"invFieldText\" id=\"bagFieldText"+x+"_"+y+"\"></span>" +
205 "</td>");
206 }
207}
208
209$("#beltTable").append("<tr id=\"beltRow0\"></tr>");
210for (var x = 0; x < BELT_COLS; x++) {
211 $("#beltRow0").append(
212 "<td class=\"invField\" id=\"beltField"+x+"\">" +
213 "<span class=\"invFieldText\" id=\"beltFieldText"+x+"\"></span>" +
214 "</td>");
215}
216
[153]217var setPlayerMarkers = function(data) {
[157]218 var online = 0;
219 var offline = 0;
[153]220 $.each( data, function( key, val ) {
221 var marker;
222 if (playersMappingList.hasOwnProperty(val.steamid)) {
223 marker = playersMappingList[val.steamid].currentPosMarker;
224 marker.setLatLng([val.position.z, val.position.x]);
225 } else {
[163]226 marker = L.marker([val.position.z, val.position.x]).bindPopup(
227 "Player: " + val.name + "<br/>" +
228 "<a onClick='showInv(\""+val.steamid+"\")'>Show inventory</a>"
229 );
[153]230 playersMappingList[val.steamid] = { online: !val.online };
231 }
232 if (playersMappingList[val.steamid].online != val.online) {
233 if (val.online) {
234 marker.setOpacity(1.0);
235 playersOfflineMarkerGroup.removeLayer(marker);
236 playersOnlineMarkerGroup.addLayer(marker);
237 } else {
238 marker.setOpacity(0.5);
239 playersOnlineMarkerGroup.removeLayer(marker);
240 playersOfflineMarkerGroup.addLayer(marker);
241 }
242 }
243 val.currentPosMarker = marker;
244 playersMappingList[val.steamid] = val;
[157]245
246 if (val.online)
247 online++;
248 else
249 offline++;
[153]250 });
[157]251 $( "#mapControlOnlineCount" ).text( online );
252 $( "#mapControlOfflineCount" ).text( offline );
[153]253}
254
255var updatePlayerEvent = function() {
256 $.getJSON( "../api/getplayerslocation")
257 .done(setPlayerMarkers)
258 .fail(function() {
259 console.log("Error fetching players list");
260 })
261 .always(function() {
262 window.setTimeout(updatePlayerEvent, 2000);
263 });
264}
265
266window.setTimeout(updatePlayerEvent, 500);
267
Note: See TracBrowser for help on using the repository browser.