SHOW:
|
|
- or go back to the newest paste.
1 | // ==UserScript== | |
2 | // @name TagPro Group Presets | |
3 | // @namespace http://www.arfie.nl | |
4 | // @version 1.0 | |
5 | // @description Allows you to create presets for group/private game settings | |
6 | // @author Ruud Verbeek | |
7 | // @include http://tagpro-*.koalabeast.com/groups/* | |
8 | // @include http://tangent.jukejuice.com/groups/* | |
9 | // @include http://maptest*.newcompte.fr/groups/* | |
10 | // @grant none | |
11 | // ==/UserScript== | |
12 | ||
13 | function getCookie(e, t) { | |
14 | var n = $.cookie(e); | |
15 | $.removeCookie(e); | |
16 | var r = $.cookie(e) === undefined; | |
17 | (n === undefined || r) && $.cookie(e, n || t, { | |
18 | expires: 36500, | |
19 | path: "/", | |
20 | domain: tagpro.cookieHost | |
21 | }); | |
22 | return n || t; | |
23 | } | |
24 | ||
25 | function setCookie(name, value) { | |
26 | $.removeCookie(name); | |
27 | $.cookie(name, value, {domain: tagpro.cookieHost, path: '/', expires: 36500}); | |
28 | } | |
29 | ||
30 | var cookie = getCookie('groupPresets', '{}'); | |
31 | console.log(JSON.stringify(cookie)); | |
32 | presets = JSON.parse(cookie); | |
33 | ||
34 | var defaults = [ | |
35 | ['redTeamName', 'Red Team Name', 'Red'], | |
36 | ['blueTeamName', 'Blue Team Name', 'Blue'], | |
37 | ['redTeamScore', 'Red Score', '0'], | |
38 | ['blueTeamScore', 'Blue Score', '0'], | |
39 | ['map', 'Map', ''], | |
40 | ['time', 'Time Limit', '12'], | |
41 | ['caps', 'Capture Limit', '3'], | |
42 | ['accel', 'Acceleration', '1'], | |
43 | ['topspeed', 'Top Speed', '1'], | |
44 | ['bounce', 'Bounciness', '1'], | |
45 | ['playerRespawnTime', 'Player Respawn Time', '3000'], | |
46 | ['speedPadRespawnTime', 'Boost Respawn Time', '10000'], | |
47 | ['dynamiteRespawnTime', 'Bomb Respawn Time', '30000'], | |
48 | ['buffRespawnTime', 'Powerup Respawn Time', '60000'], | |
49 | ['potatoTime', 'Potato', '0'], | |
50 | ['selfAssignment', 'Allow self-assignment', true] | |
51 | ]; | |
52 | ||
53 | presets['Default'] = { | |
54 | _defaults: true, // set other settings to default | |
55 | _standard: true // can't be removed | |
56 | }; | |
57 | presets['Pick Up Game'] = { | |
58 | caps: 0, | |
59 | time: 10, | |
60 | ||
61 | _defaults: false, | |
62 | _standard: true | |
63 | }; | |
64 | presets['Racing'] = { | |
65 | caps: 0, | |
66 | time: 8, | |
67 | speedPadRespawnTime: 0, | |
68 | - | dynamiteRespawnTime: 15001, |
68 | + | dynamiteRespawnTime: 15000, |
69 | ||
70 | ||
71 | _defaults: false, | |
72 | _standard: true | |
73 | }; | |
74 | presets['Hockey'] = { | |
75 | map: 'Hockey', | |
76 | caps: 0, | |
77 | time: 10, | |
78 | ||
79 | _defaults: false, | |
80 | _standard: true | |
81 | }; | |
82 | presets['Open Field Masters'] = { | |
83 | map: 'OFM', | |
84 | time: 5, | |
85 | ||
86 | _defaults: false, | |
87 | _standard: true | |
88 | }; | |
89 | ||
90 | function isLeader() {return !$('[name=map]').prop('disabled');} | |
91 | ||
92 | function apply(preset) { | |
93 | function set(setting, value) { | |
94 | tagpro.group.socket.emit('setting', {name: setting, value: value}); | |
95 | } | |
96 | if(isLeader()) { | |
97 | var setDefaults = preset._defaults; | |
98 | for(var i in defaults) { | |
99 | var setting = defaults[i]; | |
100 | if(preset[setting[0]] !== undefined) | |
101 | set(setting[0], preset[setting[0]]); | |
102 | else if(setDefaults) | |
103 | set(setting[0], setting[2]); | |
104 | } | |
105 | } else console.log('you need to be leader'); | |
106 | } | |
107 | ||
108 | function fillInPresets() { | |
109 | function usePreset(preset) { | |
110 | return function() { | |
111 | $('.presetsWindow').fadeOut(); | |
112 | apply(preset); | |
113 | }; | |
114 | } | |
115 | function editPreset(name, preset) { | |
116 | return function() { | |
117 | resetEditor(); | |
118 | for(var setting in preset) { | |
119 | var val = preset[setting]; | |
120 | switch(setting) { | |
121 | case '_defaults': $('#defaults').prop('checked', val); break; | |
122 | case 'selfAssignment': | |
123 | $('#preset-selfAssignment').prop('checked', val).parent().parent().css({color:'white'}).find('td:first-child input').prop('checked', true); | |
124 | break; | |
125 | default: | |
126 | $('#preset-'+setting).val(val).parent().parent().css({color:'white'}).find('td:first-child input').prop('checked', true); | |
127 | break; | |
128 | } | |
129 | } | |
130 | $('#presetName').val(name); | |
131 | $('#newPresetSave').css({opacity:'1.0'}); | |
132 | $('#newPresetWindow').fadeIn(); | |
133 | }; | |
134 | } | |
135 | $('#presetsWindow table tr').remove(); | |
136 | for(var preset in presets) { | |
137 | var tr = $('<tr></tr>'); | |
138 | tr.append('<td style="width: 60%;"><a class="presetUse">'+preset+'</a></td>'); | |
139 | if(!presets[preset]._standard) | |
140 | tr.append('<td style="width: 20%;"><a class="presetEdit">edit</a></td><td style="width: 20%;"><a class="presetRemove">delete</a></td>'); | |
141 | else | |
142 | tr.find('td').attr('colspan', 3); | |
143 | ||
144 | tr.appendTo('#presetsWindow table'); | |
145 | ||
146 | tr.find('td a.presetUse').click(usePreset(presets[preset])); | |
147 | tr.find('td a.presetEdit').click(editPreset(preset, presets[preset])); | |
148 | tr.find('td a.presetRemove').click(function(preset){return function() { | |
149 | delete presets[preset]; | |
150 | setCookie('groupPresets', JSON.stringify(presets)); | |
151 | fillInPresets(); | |
152 | };}(preset)); | |
153 | } | |
154 | $('#presetsWindow table').append('<tr><td style="background-color:transparent"><a id="newPresetButton">New preset</a></td><td style="background-color:transparent"/><td style="background-color:transparent"/></tr>'); | |
155 | $('#newPresetButton').click(editPreset('', {})); | |
156 | } | |
157 | ||
158 | function resetEditor() { | |
159 | $('#presetName').val(''); | |
160 | $('#newPresetSave').css({opacity:'0.25'}); | |
161 | $('#defaults').prop('checked', false); | |
162 | for(var i in defaults) { | |
163 | var setting = defaults[i]; | |
164 | $('#preset-enabled-'+setting[0]).prop('checked', false).parent().parent().css({color:'gray'}); | |
165 | if(setting[0] === 'selfAssignment') | |
166 | $('#preset-selfAssignment').prop('checked', true); | |
167 | else | |
168 | $('#preset-'+setting[0]).val(setting[2]); | |
169 | } | |
170 | } | |
171 | ||
172 | $('#settings h2').append('<a id="presetsButton">Presets</a>'); | |
173 | ||
174 | $('body').append('<div id="presetsWindow" class="window presetsWindow"></div><div class="presetsWindow" id="presetsWindowOverlay"></div>'); | |
175 | $('#presetsWindow') | |
176 | .append('<h2>Presets</h2>') | |
177 | .append('<table style="border-collapse: separate; border-spacing: 2px;"></table>') | |
178 | .append('<a id="presetsClose">Close</a>'); | |
179 | ||
180 | $('body').append('<div class="window" id="newPresetWindow"></div>'); | |
181 | $('#newPresetWindow') | |
182 | .append('<h2>New Preset<a id="resetEditor">Reset</a></h2>') | |
183 | .append('<input type="text" id="presetName" placeholder="Name..." />') | |
184 | .append('<table></table>') | |
185 | .append('<input type="checkbox" id="defaults"/><label for="defaults">Reset other settings to defaults</label>') | |
186 | .append('<a style="opacity:0.25" id="newPresetSave">Save</a><a id="newPresetCancel">Cancel</a>'); | |
187 | ||
188 | for(var i in defaults) { | |
189 | var setting = defaults[i]; | |
190 | var tr = $('<tr></tr>'); | |
191 | tr.append('<td style="width:24px;"><input type="checkbox" id="preset-enabled-'+setting[0]+'"/></td>'); | |
192 | tr.append('<td style="width:160px;"><label for="preset-'+setting[0]+'">'+setting[1]+'</label></td>'); | |
193 | tr.append('<td></td>'); | |
194 | var el = $('[name='+setting[0]+']').clone().appendTo(tr.find('td:last-child')).attr('id', 'preset-'+setting[0]).attr('name', null).unbind(); | |
195 | if(setting[0] === 'map') { | |
196 | el.find('option[value=marsballrace]').text('Anger Management'); | |
197 | el.find('option:nth-child(2)').remove(); | |
198 | } | |
199 | tr.appendTo('#newPresetWindow table'); | |
200 | ||
201 | if(setting[0] === 'selfAssignment') | |
202 | el.prop('checked', true); | |
203 | else | |
204 | el.val(setting[2]); | |
205 | ||
206 | tr.find('td:first-child input').change(function(name, def){return function() { | |
207 | if(!$(this).prop('checked')) | |
208 | $('#preset-'+name).val(def); | |
209 | $(this).parent().parent().css({color: $(this).prop('checked') ? 'white' : 'gray'}); | |
210 | };}(setting[0], setting[2])); | |
211 | ||
212 | el.on(el.attr('type') === undefined && el.prop('tagName') === 'INPUT' ? 'keyup' : 'change', function(def){return function(){ | |
213 | var val = $(this).attr('type') === 'checkbox' ? $(this).prop('checked') : $(this).val(); | |
214 | $(this).parent().prev().prev().find('input').prop('checked', val != def); | |
215 | $(this).parent().parent().css({color: val != def ? 'white' : 'gray'}); | |
216 | };}(setting[2])); | |
217 | } | |
218 | ||
219 | fillInPresets(); | |
220 | ||
221 | $('#presetsButton').click(function() {$('.presetsWindow').fadeIn();}); | |
222 | $('#presetsClose').click(function() {$('.presetsWindow').fadeOut();}); | |
223 | ||
224 | $('#resetEditor').click(resetEditor); | |
225 | ||
226 | $('#presetName').keyup(function() { | |
227 | $('#newPresetSave').css({opacity: $(this).val() === '' ? '0.25' : '1.0'}); | |
228 | }); | |
229 | ||
230 | $('#newPresetSave').click(function() { | |
231 | if($('#presetName').val() !== '') { | |
232 | var preset = {}; | |
233 | for(var i in defaults) { | |
234 | var setting = defaults[i]; | |
235 | var el = $('#preset-'+setting[0]); | |
236 | if($('#preset-enabled-'+setting[0]).prop('checked')) | |
237 | preset[setting[0]] = el.attr('type') === 'checkbox' ? el.prop('checked') : el.val(); | |
238 | } | |
239 | preset._defaults = $('#defaults').prop('checked'); | |
240 | presets[$('#presetName').val()] = preset; | |
241 | setCookie('groupPresets', JSON.stringify(presets)); | |
242 | $('#newPresetWindow').fadeOut(); | |
243 | setTimeout(resetEditor, 500); | |
244 | fillInPresets(); | |
245 | } | |
246 | }); | |
247 | $('#newPresetCancel').click(function() { | |
248 | $('#newPresetWindow').fadeOut(); | |
249 | setTimeout(resetEditor, 500); | |
250 | }); | |
251 | ||
252 | $('.presetsWindow, #newPresetWindow').hide(); | |
253 | ||
254 | $('body').append('<style type="text/css">' + | |
255 | '#presetsButton {float: right}' + | |
256 | '#presetsWindow {z-index: 10; border: 4px solid white; background-color: rgba(0, 0, 0, 0.9); width: 320px; height: 480px; position: fixed; left: calc(50% - 160px); top: calc(50% - 240px); text-align: center; padding: .5em;}' + | |
257 | '#presetsWindowOverlay {background-color: rgba(0, 0, 0, 0.5); position: absolute; left: 0; top: 0; width: 100%; height: 100%;}' + | |
258 | '#presetsClose {color: black; text-decoration: none; display: inline-block; padding: 5px 10px; width: 150px; font-size: 14pt; cursor: pointer; background-color: #ace600; border: 1px solid #608100; text-align: center; position: absolute; bottom: 1.5em; right: 85px;}' + | |
259 | '#presetsButton, .presetUse, .presetEdit, .presetRemove, #newPresetButton, #resetEditor {color: #0f0; cursor: pointer; display: block; height: 100%;}' + | |
260 | 'a:hover {text-decoration: none;}' + | |
261 | '#presetsWindow table {width: 100%;}' + | |
262 | '#presetsWindow table tbody {width: 100%; height: 370px; overflow: auto; float: left;}' + | |
263 | '#presetsWindow table tbody tr {display: table; width: 100%; text-align: left;}' + | |
264 | '#presetsWindow table tr:nth-child(even) td {background-color: #666;}' + | |
265 | '#presetsWindow table tr:nth-child(odd) td {background-color: #444;}' + | |
266 | '#newPresetWindow table tr {color: gray; -webkit-transition: color .25s; transition: color .25s;}' + | |
267 | '#newPresetWindow {z-index: 11; border: 4px solid white; background-color: rgba(0, 0, 0, 0.9); width: 480px; height: 640px; position: fixed; left: calc(50% - 240px); top: calc(50% - 320px); text-align: center; padding: .5em;}' + | |
268 | '#newPresetSave, #newPresetCancel {color: black; text-decoration: none; display: inline-block; padding: 5px 10px; width: 150px; font-size: 14pt; cursor: pointer; background-color: #ace600; border: 1px solid #608100; text-align: center; position: absolute; bottom: 1.5em;}' + | |
269 | '#newPresetSave {left: 1.5em; transition: opacity .25s; -webkit-transition: opacity .25s;}' + | |
270 | '#resetEditor {float:right;}' + | |
271 | '#newPresetCancel {right: 1.5em;}' + | |
272 | '</style>'); |