1+ const propertiesService = require ( "../services/properties.service" ) ;
2+ const configUtils = require ( "../utils/config.util" ) ;
3+ const jsonFilesUtils = require ( "../utils/files.util" ) ;
4+
5+ const { Sema } = require ( 'async-sema' ) ;
6+
7+ let togglePropertySema = new Sema ( 1 ) ;
8+
9+ async function toggleProperty ( req , res ) {
10+ togglePropertySema . acquire ( ) ;
11+
12+ try {
13+ console . log ( req . params . property ) ;
14+ await propertiesService . updateProperty ( req . params . property , true ) ;
15+ res . status ( 200 ) . send ( "done" ) ;
16+ } catch ( error ) {
17+ console . error ( error ) ;
18+ res . status ( 500 ) . send ( "error.. " + error . message ) ;
19+ } finally {
20+ togglePropertySema . release ( ) ;
21+ }
22+ }
23+
24+ let ramAllocationSema = new Sema ( 1 ) ;
25+
26+ async function allocateRam ( req , res ) {
27+ ramAllocationSema . acquire ( ) ;
28+
29+ try {
30+ configUtils . updateMemoryAllocated ( req . params . mb , true ) ;
31+ res . status ( 200 ) . send ( `Ram allocation updated to ${ req . params . mb } M` ) ;
32+ } catch ( error ) {
33+ console . error ( error ) ;
34+ res . status ( 500 ) . send ( "error.. " + error . message ) ;
35+ } finally {
36+ ramAllocationSema . release ( ) ;
37+ }
38+ }
39+
40+ async function serverConfig ( req , res ) {
41+ try {
42+ const configJSON = configUtils . getConfigJSON ( ) ;
43+ res . status ( 200 ) . send ( configJSON ) ;
44+ } catch ( error ) {
45+ console . error ( error ) ;
46+ res . status ( 500 ) . send ( "error.. " + error . message ) ;
47+ }
48+ }
49+
50+ async function getWhitelist ( req , res ) {
51+ try {
52+ const whitelistJSON = jsonFilesUtils . getWhitelistJSON ( ) ;
53+ res . status ( 200 ) . send ( whitelistJSON ) ;
54+ } catch ( error ) {
55+ console . error ( error ) ;
56+ res . status ( 500 ) . send ( "error.. " + error . message ) ;
57+ }
58+ }
59+
60+ async function getOps ( req , res ) {
61+ try {
62+ const opsJSON = jsonFilesUtils . getOpsJSON ( ) ;
63+ res . status ( 200 ) . send ( opsJSON ) ;
64+ } catch ( error ) {
65+ console . error ( error ) ;
66+ res . status ( 500 ) . send ( "error.. " + error . message ) ;
67+ }
68+ }
69+
70+ async function getBannedPlayers ( req , res ) {
71+ try {
72+ const bannedplayersJSON = jsonFilesUtils . getBannedPlayersJSON ( ) ;
73+ res . status ( 200 ) . send ( bannedplayersJSON ) ;
74+ } catch ( error ) {
75+ console . error ( error ) ;
76+ res . status ( 500 ) . send ( "error.. " + error . message ) ;
77+ }
78+ }
79+
80+ async function modifyOperator ( req , res ) {
81+ try {
82+ if ( req . params . operation != "add" && req . params . operation != "remove" ) {
83+ res . status ( 404 ) . send ( `Invalid operation ${ req . params . operation } ` ) ;
84+ return ;
85+ }
86+
87+ await jsonFilesUtils . modifyOpsJSON ( req . params . playername , req . params . operation === "add" ) ;
88+ res . status ( 200 ) . send ( `${ req . params . operation === "add" ? "Added" : "Remove" } ${ req . params . playername } as an Operator` ) ;
89+ } catch ( error ) {
90+ console . error ( error ) ;
91+ res . status ( 500 ) . send ( "error.. " + error . message ) ;
92+ }
93+ }
94+
95+ async function modifyWhitelist ( req , res ) {
96+ try {
97+ if ( req . params . operation != "add" && req . params . operation != "remove" ) {
98+ res . status ( 404 ) . send ( `Invalid operation ${ req . params . operation } ` ) ;
99+ return ;
100+ }
101+
102+ await jsonFilesUtils . modifyWhitelistJSON ( req . params . playername , req . params . operation === "add" ) ;
103+ if ( req . params . operation === "add" )
104+ res . status ( 200 ) . send ( `Added ${ req . params . playername } to the Whitelist` ) ;
105+ else
106+ res . status ( 200 ) . send ( `Remove ${ req . params . playername } from the Whitelist` ) ;
107+
108+ } catch ( error ) {
109+ console . error ( error ) ;
110+ res . status ( 500 ) . send ( "error.. " + error . message ) ;
111+ }
112+ }
113+
114+ async function modifyBanned ( req , res ) {
115+ try {
116+ if ( req . params . operation != "add" && req . params . operation != "remove" ) {
117+ res . status ( 404 ) . send ( `Invalid operation ${ req . params . operation } ` ) ;
118+ return ;
119+ }
120+
121+ await jsonFilesUtils . modifyBannedPlayersJSON ( req . params . playername , req . params . operation === "add" ) ;
122+ if ( req . params . operation === "add" )
123+ res . status ( 200 ) . send ( `Banned ${ req . params . playername } ` ) ;
124+ else
125+ res . status ( 200 ) . send ( `Pardoned ${ req . params . playername } ` ) ;
126+
127+ } catch ( error ) {
128+ console . error ( error ) ;
129+ res . status ( 500 ) . send ( "error.. " + error . message ) ;
130+ }
131+ }
132+
133+ async function modifyBannedIPs ( req , res ) {
134+ try {
135+ if ( req . params . operation != "add" && req . params . operation != "remove" ) {
136+ res . status ( 404 ) . send ( `Invalid operation ${ req . params . operation } ` ) ;
137+ return ;
138+ }
139+
140+ await jsonFilesUtils . modifyBannedIPsJSON ( req . params . playername , req . params . operation === "add" ) ;
141+ if ( req . params . operation === "add" )
142+ res . status ( 200 ) . send ( `Banned ${ req . params . playername } ` ) ;
143+ else
144+ res . status ( 200 ) . send ( `Pardoned ${ req . params . playername } ` ) ;
145+
146+ } catch ( error ) {
147+ console . error ( error ) ;
148+ res . status ( 500 ) . send ( "error.. " + error . message ) ;
149+ }
150+ }
151+
152+
153+ module . exports = {
154+ toggleProperty,
155+ allocateRam,
156+ serverConfig,
157+ getWhitelist,
158+ getOps,
159+ getBannedPlayers,
160+ modifyOperator,
161+ modifyWhitelist,
162+ modifyBanned,
163+ modifyBannedIPs
164+ }
0 commit comments