Skip to content

Commit 014746f

Browse files
committed
better json generation
1 parent dfe069b commit 014746f

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

CommandHandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def printHelp():
1111
exit(0)
1212

1313
@staticmethod
14-
def argsHandler(arg1=None, arg2=None):
14+
def argsHandler(self, arg1=None, arg2=None, w=None):
1515
if arg1 is None and arg2 is None:
1616
self.printHelp()
1717

Weapon.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@ def generate(self, weapon_type):
4242
self.clip_size = random.randint(1, 10)
4343
self.rarity = random.randint(0, 10)
4444

45-
46-
4745
def to_string(self):
4846
print(self.name + " " + str(self.rarity) + " " + str(self.dmg))
4947

50-
def generate_json(self, w: Weapon):
48+
def generate_json(self, w):
5149
return json.dumps(w.__dict__)
5250

5351
def create_json_file(self, arg):
@@ -61,6 +59,4 @@ def weapon_generation(self, l, weapon_type):
6159
w = Weapon()
6260
list.append(w)
6361
print(json.dumps(w.__dict__))
64-
self.create_json_file(json.dumps([w.__dict__ for list in list]))
65-
66-
62+
self.create_json_file(json.dumps([w.__dict__ for list in list]))
1 Byte
Binary file not shown.

__pycache__/Weapon.cpython-38.pyc

-55 Bytes
Binary file not shown.

app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def assault_rifle_multiple(number):
4646
return json_type_response(json.dumps([w.__dict__ for weapon_list in weapon_list]))
4747

4848
@app.route('/shotgun/<number>')
49-
def assault_rifle_multiple(number):
49+
def shotgun_multiple(number):
5050
number = int(number)
5151
weapon_list = []
5252
for i in range(number):
@@ -56,7 +56,7 @@ def assault_rifle_multiple(number):
5656
return json_type_response(json.dumps([w.__dict__ for weapon_list in weapon_list]))
5757

5858
@app.route('/sniper_rifle/<number>')
59-
def assault_rifle_multiple(number):
59+
def sniper_rifle_multiple(number):
6060
number = int(number)
6161
weapon_list = []
6262
for i in range(number):

main.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,26 @@ def create_json_file(arg):
1010
f.write(arg)
1111

1212
def weapon_generation(l, weapon_type):
13-
str_array_json = "["
14-
for i in range(l):
13+
if l is not int:
14+
l = int(l)
15+
16+
if l == 1:
1517
w = Weapon()
16-
w.generate(weapon_type)
17-
print(w.generate_json())
18-
str_array_json += w.generate_json()
19-
if i < l - 1:
20-
str_array_json += ","
21-
str_array_json += " ]"
22-
create_json_file(str_array_json)
23-
18+
if weapon_type == "ASSAULT_RIFLE" or weapon_type == "SHOTGUN" or weapon_type == "SNIPER_RIFLE":
19+
w.generate(weapon_type)
20+
else:
21+
print("FATAL ERROR")
22+
raise ValueError("INVALID WEAPON TYPE")
23+
exit(1)
24+
w.generate_json(w)
25+
else:
26+
list = []
27+
if weapon_type == "ASSAULT_RIFLE" or weapon_type == "SHOTGUN" or weapon_type == "SNIPER_RIFLE":
28+
w.generate(weapon_type)
29+
else:
30+
print("FATAL ERROR")
31+
raise ValueError("INVALID WEAPON TYPE")
32+
exit(1)
2433
def main():
2534
start = time.time()
2635

0 commit comments

Comments
 (0)