Commit 23c9f1b0 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

erp5_officejs_drone_capture_flag: limit variables conflicts

Move `spawnDrone` function (where user code is evaluated) and rename
used variables to avoid conflicts with the variables used by the user.
Previously if an user would have assign a value to `i` without declaring
the variable first, it would not have thrown any error as `i` was
already existing in the execution scope.
parent d19fa907
...@@ -6,6 +6,56 @@ var GAMEPARAMETERS = {}, TEAM_USER = "user", TEAM_ENEMY = "enemy"; ...@@ -6,6 +6,56 @@ var GAMEPARAMETERS = {}, TEAM_USER = "user", TEAM_ENEMY = "enemy";
//for DEBUG/TEST mode //for DEBUG/TEST mode
var baseLogFunction = console.log, console_log = ""; var baseLogFunction = console.log, console_log = "";
function spawnDrone(spawnDrone_x, spawnDrone_y, spawnDrone_z, spawnDrone_index,
spawnDrone_drone_info, spawnDrone_api, spawnDrone_team,
spawnDrone_scene, spawnDrone_droneList_user,
spawnDrone_droneList_enemy) {
"use strict";
var default_drone_AI = spawnDrone_api.getDroneAI(), spawnDrone_code,
code_eval;
if (default_drone_AI) {
spawnDrone_code = default_drone_AI;
} else {
spawnDrone_code = spawnDrone_drone_info.script_content;
}
if (!spawnDrone_code.includes("me.onStart")) {
spawnDrone_code = "me.onStart = function () { me.exit(); };";
}
code_eval = "let spawnDrone_drone = new DroneManager(spawnDrone_scene, " +
spawnDrone_index + ', spawnDrone_api, spawnDrone_team);' +
"let droneMe = function(NativeDate, me, Math, window, DroneManager," +
" GameManager, FixedWingDroneAPI, EnemyDroneAPI, BABYLON, " +
"GAMEPARAMETERS) {" +
"Date.now = function () {" +
"return me._API._gameManager.getCurrentTime();}; " +
"function Date() {if (!(this instanceof Date)) " +
"{throw new Error('Missing new operator');} " +
"if (arguments.length === 0) {return new NativeDate(Date.now());} " +
"else {return new NativeDate(...arguments);}}";
// Simple desactivation of direct access of all globals
// It is still accessible in reality, but it will me more visible
// if people really access them
if (spawnDrone_x !== null && spawnDrone_y !== null && spawnDrone_z !== null) {
code_eval += "me.setStartingPosition(" + spawnDrone_x + ", "
+ spawnDrone_y + ", " + spawnDrone_z + ");";
}
//base = code_eval;
code_eval +=
spawnDrone_code + "}; droneMe(Date, spawnDrone_drone, Math, {});";
//base +=
// "};spawnDrone_droneList_" + spawnDrone_team + ".push(spawnDrone_drone)";
code_eval +=
"spawnDrone_droneList_" + spawnDrone_team + ".push(spawnDrone_drone)";
/*jslint evil: true*/
eval(code_eval);
/*jslint evil: false*/
/*try {
eval(code_eval);
} catch (error) {
eval(base);
}*/
}
/******************************* DRONE MANAGER ********************************/ /******************************* DRONE MANAGER ********************************/
var DroneManager = /** @class */ (function () { var DroneManager = /** @class */ (function () {
"use strict"; "use strict";
...@@ -1331,46 +1381,6 @@ var GameManager = /** @class */ (function () { ...@@ -1331,46 +1381,6 @@ var GameManager = /** @class */ (function () {
} }
return false; return false;
} }
function spawnDrone(x, y, z, index, drone_info, api, team) {
var default_drone_AI = api.getDroneAI(), code, code_eval;
if (default_drone_AI) {
code = default_drone_AI;
} else {
code = drone_info.script_content;
}
if (!code.includes("me.onStart")) {
code = "me.onStart = function () { me.exit(); };";
}
code_eval = "let drone = new DroneManager(ctx._scene, " +
index + ', api, team);' +
"let droneMe = function(NativeDate, me, Math, window, DroneManager," +
" GameManager, FixedWingDroneAPI, EnemyDroneAPI, BABYLON, " +
"GAMEPARAMETERS) {" +
"Date.now = function () {" +
"return me._API._gameManager.getCurrentTime();}; " +
"function Date() {if (!(this instanceof Date)) " +
"{throw new Error('Missing new operator');} " +
"if (arguments.length === 0) {return new NativeDate(Date.now());} " +
"else {return new NativeDate(...arguments);}}";
// Simple desactivation of direct access of all globals
// It is still accessible in reality, but it will me more visible
// if people really access them
if (x !== null && y !== null && z !== null) {
code_eval += "me.setStartingPosition(" + x + ", " + y + ", " + z + ");";
}
//base = code_eval;
code_eval += code + "}; droneMe(Date, drone, Math, {});";
//base += "};ctx._droneList_" + team + ".push(drone)";
code_eval += "ctx._droneList_" + team + ".push(drone)";
/*jslint evil: true*/
eval(code_eval);
/*jslint evil: false*/
/*try {
eval(code_eval);
} catch (error) {
eval(base);
}*/
}
function randomSpherePoint(x0, y0, z0, rx0, ry0, rz0) { function randomSpherePoint(x0, y0, z0, rx0, ry0, rz0) {
var u = Math.random(), v = Math.random(), var u = Math.random(), v = Math.random(),
rx = Math.random() * rx0, ry = Math.random() * ry0, rx = Math.random() * rx0, ry = Math.random() * ry0,
...@@ -1404,7 +1414,8 @@ var GameManager = /** @class */ (function () { ...@@ -1404,7 +1414,8 @@ var GameManager = /** @class */ (function () {
i + id_offset i + id_offset
); );
spawnDrone(position.x, position.y, position.z, i + id_offset, spawnDrone(position.x, position.y, position.z, i + id_offset,
drone_list[i], api, team); drone_list[i], api, team, ctx._scene, ctx._droneList_user,
ctx._droneList_enemy);
} }
} }
}; };
......
...@@ -246,7 +246,7 @@ ...@@ -246,7 +246,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1016.21924.8943.57122</string> </value> <value> <string>1016.21978.22579.46609</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -266,7 +266,7 @@ ...@@ -266,7 +266,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1714738813.84</float> <float>1714742387.62</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -6,6 +6,50 @@ var GAMEPARAMETERS = {}; ...@@ -6,6 +6,50 @@ var GAMEPARAMETERS = {};
//for DEBUG/TEST mode //for DEBUG/TEST mode
var baseLogFunction = console.log, console_log = ""; var baseLogFunction = console.log, console_log = "";
function spawnDrone(spawnDrone_x, spawnDrone_y, spawnDrone_z, spawnDrone_index,
spawnDrone_drone_info, spawnDrone_api, spawnDrone_scene,
spawnDrone_droneList) {
"use strict";
var default_drone_AI = spawnDrone_api.getDroneAI(), spawnDrone_code,
spawnDrone_base, code_eval;
if (default_drone_AI) {
spawnDrone_code = default_drone_AI;
} else {
spawnDrone_code = spawnDrone_drone_info.script_content;
}
code_eval = "let spawnDrone_drone = new DroneManager(spawnDrone_scene, " +
spawnDrone_index + ', spawnDrone_api);' +
"let droneMe = function(NativeDate, me, Math, window, DroneManager," +
" GameManager, DroneLogAPI, FixedWingDroneAPI, BABYLON, " +
"GAMEPARAMETERS) {" +
"Date.now = function () {" +
"return me._API._gameManager.getCurrentTime();}; " +
"function Date() {if (!(this instanceof Date)) " +
"{throw new Error('Missing new operator');} " +
"if (arguments.length === 0) {return new NativeDate(Date.now());} " +
"else {return new NativeDate(...arguments);}}";
// Simple desactivation of direct access of all globals
// It is still accessible in reality, but it will me more visible
// if people really access them
if (spawnDrone_x !== null && spawnDrone_y !== null && spawnDrone_z !== null) {
code_eval += "me.setStartingPosition(" + spawnDrone_x + ", "
+ spawnDrone_y + ", " + spawnDrone_z + ");";
}
spawnDrone_base = code_eval;
code_eval +=
spawnDrone_code + "}; droneMe(Date, spawnDrone_drone, Math, {});";
spawnDrone_base += "};spawnDrone_droneList.push(spawnDrone_drone)";
code_eval += "spawnDrone_droneList.push(spawnDrone_drone)";
/*jslint evil: true*/
try {
eval(code_eval);
} catch (error) {
console.error(error);
eval(spawnDrone_base);
}
/*jslint evil: false*/
}
/******************************* DRONE MANAGER ********************************/ /******************************* DRONE MANAGER ********************************/
var DroneManager = /** @class */ (function () { var DroneManager = /** @class */ (function () {
"use strict"; "use strict";
...@@ -1002,43 +1046,6 @@ var GameManager = /** @class */ (function () { ...@@ -1002,43 +1046,6 @@ var GameManager = /** @class */ (function () {
} }
return false; return false;
} }
function spawnDrone(x, y, z, index, drone_info, api) {
var default_drone_AI = api.getDroneAI(), code, base, code_eval;
if (default_drone_AI) {
code = default_drone_AI;
} else {
code = drone_info.script_content;
}
code_eval = "let drone = new DroneManager(ctx._scene, " +
index + ', api);' +
"let droneMe = function(NativeDate, me, Math, window, DroneManager," +
" GameManager, DroneLogAPI, FixedWingDroneAPI, BABYLON, " +
"GAMEPARAMETERS) {" +
"Date.now = function () {" +
"return me._API._gameManager.getCurrentTime();}; " +
"function Date() {if (!(this instanceof Date)) " +
"{throw new Error('Missing new operator');} " +
"if (arguments.length === 0) {return new NativeDate(Date.now());} " +
"else {return new NativeDate(...arguments);}}";
// Simple desactivation of direct access of all globals
// It is still accessible in reality, but it will me more visible
// if people really access them
if (x !== null && y !== null && z !== null) {
code_eval += "me.setStartingPosition(" + x + ", " + y + ", " + z + ");";
}
base = code_eval;
code_eval += code + "}; droneMe(Date, drone, Math, {});";
base += "};ctx._droneList.push(drone)";
code_eval += "ctx._droneList.push(drone)";
/*jslint evil: true*/
try {
eval(code_eval);
} catch (error) {
console.error(error);
eval(base);
}
/*jslint evil: false*/
}
function randomSpherePoint(x0, y0, z0, rx0, ry0, rz0) { function randomSpherePoint(x0, y0, z0, rx0, ry0, rz0) {
var u = Math.random(), v = Math.random(), var u = Math.random(), v = Math.random(),
rx = Math.random() * rx0, ry = Math.random() * ry0, rx = Math.random() * rx0, ry = Math.random() * ry0,
...@@ -1066,7 +1073,7 @@ var GameManager = /** @class */ (function () { ...@@ -1066,7 +1073,7 @@ var GameManager = /** @class */ (function () {
i i
); );
spawnDrone(position.x, position.y, position.z, i, spawnDrone(position.x, position.y, position.z, i,
drone_list[i], api); drone_list[i], api, ctx._scene, ctx._droneList);
} }
} }
}; };
......
...@@ -240,7 +240,7 @@ ...@@ -240,7 +240,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1016.21930.60269.31744</string> </value> <value> <string>1016.21987.28184.16844</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -260,7 +260,7 @@ ...@@ -260,7 +260,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1714739216.07</float> <float>1714742619.34</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment