source: binary-improvements/webserver_legacy/js/players.js@ 461

Last change on this file since 461 was 369, checked in by alloc, 3 years ago

Preparations for A20 release
Changes usage of "SteamID" to "UserID" in console commands
Also changes a bunch of the WebAPI stuff to show / use UserIDs

File size: 8.1 KB
Line 
1function StartPlayersModule () {
2 var sortParamName = "sort";
3 var filterParamName = "filter";
4
5
6function prettyDate(date){
7 var diff = (((new Date()).getTime() - date.getTime()) / 1000),
8 day_diff = Math.floor(diff / 86400);
9 if ( isNaN(day_diff) || day_diff < 0 ) { return ''; }
10 return day_diff == 0 && (
11 diff < 60 && 'just now' ||
12 diff < 120 && '1 minute ago' ||
13 diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' ||
14 diff < 7200 && '1 hour ago' ||
15 diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') ||
16 day_diff == 1 && 'Yesterday' ||
17 day_diff < 7 && day_diff + ' days ago' ||
18 day_diff < 61 && Math.ceil( day_diff / 7 ) + ' weeks ago' ||
19 day_diff < 730 && Math.floor( day_diff / 30 ) + ' months ago' ||
20 Math.floor( day_diff / 365 ) + ' years ago';
21}
22
23
24 // Define columns to be shown
25 var columns = [
26 [ "steamid", "UserID" ],
27 [ "entityid", "EntityID" ],
28 [ "ip", "IP" ],
29 [ "name", "Name",
30 function(text, data) {
31 return $("<div>").text(text).html();
32 }
33 ],
34 [ "online", "Online", null,
35 function(text, data) {
36 // add text to data-attribute; this overrides the parser
37 data.$cell.attr(data.config.textAttribute, text);
38 if (text == 'false') {
39 return "<img src=\"img/oxygen-icons/32x32/status/task-reject.png\" width=\"16\">";
40 } else {
41 return "<img src=\"img/oxygen-icons/32x32/status/task-complete.png\" width=\"16\">";
42 }
43 }
44 ],
45 [ "position", "Position", function(inp) { return inp.x + "/" + inp.y + "/" + inp.z; } ],
46 [ "totalplaytime", "Total Playtime", null,
47 function(text, data) {
48 // add text to data-attribute; this overrides the parser
49 data.$cell.attr(data.config.textAttribute, text);
50 var minutes = Math.floor(text / 60);
51 var hours = Math.floor(minutes / 60);
52 minutes = minutes % 60;
53 if (hours > 0) {
54 return hours + " hours " + minutes + " minutes";
55 } else {
56 return minutes + " minutes";
57 }
58 }
59 ],
60 [ "lastonline", "Last Online", null,
61 function(text, data) {
62 var date = new Date(text);
63 if (date instanceof Date && isFinite(date) ) {
64 data.$cell.attr(data.config.textAttribute, text);
65 return '<span class="players_dateonline" title="' + date.toLocaleString() + '">' + prettyDate(date) + '</span>';
66 }
67 return text;
68 }
69 ],
70 [ "ping", "Ping" ],
71 ];
72
73 // Add column headers to <table>
74 for (var i = 0; i < columns.length; i++) {
75 $(".players_columns").append ("<td>" + columns[i][1] + "</td>");
76 }
77
78 // Set pager colspan accordingly
79 $(".players_pager").attr ("colspan", columns.length);
80
81 // Define header names array for tablesorter
82 var headers = [];
83 for (var c = 0; c < columns.length; c++) {
84 headers.push (columns[c][1]);
85 }
86
87 // Build table formatter object
88 var formatterSettings = {};
89 for (var i = 0; i < columns.length; i++) {
90 if (columns[i].length > 3 && columns[i][3] != null) {
91 formatterSettings [i] = columns[i][3];
92 //var formatter = columns[i][3];
93 //var column
94 }
95 }
96
97
98 function FindColumnIndexByField (fieldname) {
99 for(var i = 0; i < columns.length; i++) {
100 if(columns[i][0] === fieldname) {
101 return i;
102 }
103 }
104 return -1;
105 }
106
107
108
109 $(".players_tablesorter")
110 .tablesorter({
111 theme: 'default',
112 widthFixed: true,
113 sortLocaleCompare: true, // needed for accented characters in the data
114 sortList: [ [FindColumnIndexByField("name"),0] ],
115 widgets: ['zebra', 'formatter', 'filter'],
116 widgetOptions: {
117 formatter_column: formatterSettings
118 }
119 })
120 .tablesorterPager({
121 container: $(".players_pager"),
122
123 // If you want to use ajaxUrl placeholders, here is an example:
124 // ajaxUrl: "http:/mydatabase.com?page={page}&size={size}&{sortList:col}"
125 // where {page} is replaced by the page number (or use {page+1} to get a one-based index),
126 // {size} is replaced by the number of records to show,
127 // {sortList:col} adds the sortList to the url into a "col" array, and {filterList:fcol} adds
128 // the filterList to the url into an "fcol" array.
129 // So a sortList = [[2,0],[3,0]] becomes "&col[2]=0&col[3]=0" in the url
130 // and a filterList = [[2,Blue],[3,13]] becomes "&fcol[2]=Blue&fcol[3]=13" in the url
131 ajaxUrl : '../api/getplayerlist?page={page}&rowsperpage={size}&{filterList:' + filterParamName + '}&{sortList:' + sortParamName + '}',
132
133 customAjaxUrl: function(table, url) {
134 var address = url.substring (0, url.indexOf ("?"));
135 var queryString = url.substring (url.indexOf ("?") + 1);
136 var params = queryString.split("&");
137 for (var i = 0; i < params.length; i++) {
138 var pair = params[i].split("=");
139
140 if ((pair.length == 2) && (pair[0].indexOf ("[") >= 0)) {
141 var paramName = pair[0].substring (0, pair[0].indexOf ("["));
142 var paramIndex = pair[0].substring (pair[0].indexOf ("[") + 1, pair[0].indexOf ("]"));
143 if ((paramName == sortParamName) || (paramName == filterParamName)) {
144 paramIndex = columns[paramIndex][0];
145 pair[0] = paramName + "[" + paramIndex + "]";
146 params[i] = pair.join("=");
147 }
148 }
149 }
150
151 queryString = params.join("&");
152
153 url = address + "?" + queryString;
154
155
156 $(table).trigger('changingUrl', url);
157 return url;
158 },
159
160 ajaxError: null,
161
162 // add more ajax settings here
163 // see http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings
164 ajaxObject: {
165 dataType: 'json'
166 },
167
168 ajaxProcessing: function(data){
169 var rows = [];
170 for (var i=0; i < data.players.length; i++) {
171 var row = [];
172 for (var c = 0; c < columns.length; c++) {
173 var col = columns[c];
174 var val = data.players[i][col[0]];
175 if (col.length > 2 && col[2] != null) {
176 val = col[2] (val);
177 }
178 row.push (val);
179 }
180 rows.push (row);
181 }
182
183 return {
184 "total": data.total,
185 "headers": headers,
186 "rows": rows
187 };
188 },
189
190 // Set this option to false if your table data is preloaded into the table, but you are still using ajax
191 processAjaxOnInit: true,
192
193 // output string - default is '{page}/{totalPages}';
194 // possible variables: {size}, {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
195 // also {page:input} & {startRow:input} will add a modifiable input in place of the value
196 output: '{startRow} to {endRow} ({totalRows})',
197
198 // apply disabled classname (cssDisabled option) to the pager arrows when the rows
199 // are at either extreme is visible; default is true
200 updateArrows: true,
201
202 // starting page of the pager (zero based index)
203 page: 0,
204
205 // Number of visible rows - default is 10
206 size: 25,
207
208 // Saves the current pager page size and number (requires storage widget)
209 savePages: false,
210
211 // Reset pager to this page after filtering; set to desired page number (zero-based index),
212 // or false to not change page at filter start
213 pageReset: 0,
214
215 // if true, the table will remain the same height no matter how many records are displayed.
216 // The space is made up by an empty table row set to a height to compensate; default is false
217 fixedHeight: false,
218
219 // remove rows from the table to speed up the sort of large tables.
220 // setting this to false, only hides the non-visible rows; needed if you plan to
221 // add/remove rows with the pager enabled.
222 removeRows: false,
223
224 // If true, child rows will be counted towards the pager set size
225 countChildRows: false,
226
227 // css class names of pager arrows
228 cssNext : '.players_next', // next page arrow
229 cssPrev : '.players_prev', // previous page arrow
230 cssFirst : '.players_first', // go to first page arrow
231 cssLast : '.players_last', // go to last page arrow
232 cssGoto : '.players_gotoPage', // page select dropdown - select dropdown that set the "page" option
233
234 cssPageDisplay : '.players_pagedisplay', // location of where the "output" is displayed
235 cssPageSize : '.players_pagesize', // page size selector - select dropdown that sets the "size" option
236
237 // class added to arrows when at the extremes; see the "updateArrows" option
238 // (i.e. prev/first arrows are "disabled" when on the first page)
239 cssDisabled : 'disabled', // Note there is no period "." in front of this class name
240 cssErrorRow : 'tablesorter-errorRow' // error information row
241
242 });
243/*
244 var $url = $('#players_url');
245 $('.players_tablesorter').on('changingUrl', function(e, url){
246 $url.html(url);
247 });
248*/
249}
250
251
Note: See TracBrowser for help on using the repository browser.