code-battles 1.5.4 → 1.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +4 -1
- package/dist/code_battles/battles.py +36 -24
- package/dist/esm/index.js +4 -1
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -462,7 +462,10 @@ const useLocalStorage = ({ key, defaultValue, }) => {
|
|
|
462
462
|
}
|
|
463
463
|
if (newValue) {
|
|
464
464
|
localStorage.setItem(key, JSON.stringify(newValue));
|
|
465
|
-
window.dispatchEvent(new StorageEvent("storage", {
|
|
465
|
+
window.dispatchEvent(new StorageEvent("storage", {
|
|
466
|
+
key,
|
|
467
|
+
newValue: JSON.stringify(newValue),
|
|
468
|
+
}));
|
|
466
469
|
}
|
|
467
470
|
};
|
|
468
471
|
return [value, userSetValue];
|
|
@@ -466,8 +466,6 @@ class CodeBattles(
|
|
|
466
466
|
if step_element is not None:
|
|
467
467
|
step_element.onclick = create_proxy(lambda _: self._step())
|
|
468
468
|
|
|
469
|
-
self._initialized = True
|
|
470
|
-
|
|
471
469
|
def _initialize_simulation(
|
|
472
470
|
self, player_codes: List[str], seed: Optional[int] = None
|
|
473
471
|
):
|
|
@@ -530,13 +528,15 @@ class CodeBattles(
|
|
|
530
528
|
|
|
531
529
|
def _run_local_simulation(self):
|
|
532
530
|
command = sys.argv[1]
|
|
531
|
+
output_file = None
|
|
533
532
|
decisions = []
|
|
534
533
|
if command == "simulate":
|
|
535
534
|
seed = None if sys.argv[2] == "None" else int(sys.argv[2])
|
|
536
|
-
|
|
537
|
-
self.
|
|
535
|
+
output_file = None if sys.argv[3] == "None" else sys.argv[3]
|
|
536
|
+
self.map = sys.argv[4]
|
|
537
|
+
self.player_names = sys.argv[5].split("-")
|
|
538
538
|
player_codes = []
|
|
539
|
-
for filename in sys.argv[
|
|
539
|
+
for filename in sys.argv[6:]:
|
|
540
540
|
with open(filename, "r") as f:
|
|
541
541
|
player_codes.append(f.read())
|
|
542
542
|
elif command == "simulate-from-file":
|
|
@@ -556,15 +556,23 @@ class CodeBattles(
|
|
|
556
556
|
self.verbose = False
|
|
557
557
|
self._initialize_simulation(player_codes, seed)
|
|
558
558
|
|
|
559
|
+
all_logs = []
|
|
559
560
|
while not self.over:
|
|
560
561
|
print("__CODE_BATTLES_ADVANCE_STEP")
|
|
561
562
|
if len(decisions) != 0:
|
|
562
563
|
self.apply_decisions(decisions.pop(0))
|
|
563
564
|
else:
|
|
564
|
-
self.
|
|
565
|
+
self._logs = []
|
|
566
|
+
_decisions = self.make_decisions()
|
|
567
|
+
all_logs.append(self._logs)
|
|
568
|
+
self._logs = []
|
|
569
|
+
if output_file is not None:
|
|
570
|
+
self._decisions.append(_decisions)
|
|
571
|
+
self.apply_decisions(_decisions)
|
|
565
572
|
|
|
566
573
|
if not self.over:
|
|
567
574
|
self.step += 1
|
|
575
|
+
self._logs = all_logs
|
|
568
576
|
|
|
569
577
|
print("--- SIMULATION FINISHED ---")
|
|
570
578
|
print(
|
|
@@ -577,11 +585,16 @@ class CodeBattles(
|
|
|
577
585
|
if len(self.active_players) > 0
|
|
578
586
|
else None,
|
|
579
587
|
"steps": self.step,
|
|
580
|
-
"logs": self._logs,
|
|
588
|
+
"logs": [log for logs in self._logs for log in logs],
|
|
581
589
|
}
|
|
582
590
|
)
|
|
583
591
|
)
|
|
584
592
|
|
|
593
|
+
if output_file is not None:
|
|
594
|
+
simulation_str = self._get_simulation().dump()
|
|
595
|
+
with open(output_file, "w") as f:
|
|
596
|
+
f.write(simulation_str)
|
|
597
|
+
|
|
585
598
|
def _start_simulation(self, *args, **kwargs):
|
|
586
599
|
loop = asyncio.get_event_loop()
|
|
587
600
|
loop.run_until_complete(self._start_simulation_async(*args, **kwargs))
|
|
@@ -596,7 +609,7 @@ class CodeBattles(
|
|
|
596
609
|
try:
|
|
597
610
|
simulation = Simulation.load(str(contents))
|
|
598
611
|
navigate(
|
|
599
|
-
f"/simulation/{simulation.map}/{'-'.join(simulation.player_names)}"
|
|
612
|
+
f"/simulation/{simulation.map}/{'-'.join(simulation.player_names)}?seed={simulation.seed}"
|
|
600
613
|
)
|
|
601
614
|
show_alert(
|
|
602
615
|
"Loaded simulation file!",
|
|
@@ -623,8 +636,7 @@ class CodeBattles(
|
|
|
623
636
|
)
|
|
624
637
|
while document.getElementById("loader") is None:
|
|
625
638
|
await asyncio.sleep(0.01)
|
|
626
|
-
|
|
627
|
-
self._initialize()
|
|
639
|
+
self._initialize()
|
|
628
640
|
self.map = simulation.map
|
|
629
641
|
self.map_image = await download_image(
|
|
630
642
|
self.configure_map_image_url(simulation.map)
|
|
@@ -701,8 +713,7 @@ class CodeBattles(
|
|
|
701
713
|
await self.setup()
|
|
702
714
|
|
|
703
715
|
if not self.background:
|
|
704
|
-
|
|
705
|
-
self._initialize()
|
|
716
|
+
self._initialize()
|
|
706
717
|
|
|
707
718
|
# Show that loading finished
|
|
708
719
|
document.getElementById("loader").style.display = "none"
|
|
@@ -719,6 +730,18 @@ class CodeBattles(
|
|
|
719
730
|
except Exception:
|
|
720
731
|
traceback.print_exc()
|
|
721
732
|
|
|
733
|
+
def _get_simulation(self):
|
|
734
|
+
return Simulation(
|
|
735
|
+
self.map,
|
|
736
|
+
self.player_names,
|
|
737
|
+
self.__class__.__name__,
|
|
738
|
+
self.configure_version(),
|
|
739
|
+
datetime.datetime.now(),
|
|
740
|
+
self._logs,
|
|
741
|
+
self._decisions,
|
|
742
|
+
self._seed,
|
|
743
|
+
)
|
|
744
|
+
|
|
722
745
|
def _update_step(self, decisions_str: str, logs_str: str, is_over_str: str):
|
|
723
746
|
from js import window, document
|
|
724
747
|
|
|
@@ -732,16 +755,7 @@ class CodeBattles(
|
|
|
732
755
|
|
|
733
756
|
if is_over:
|
|
734
757
|
try:
|
|
735
|
-
simulation =
|
|
736
|
-
self.map,
|
|
737
|
-
self.player_names,
|
|
738
|
-
self.__class__.__name__,
|
|
739
|
-
self.configure_version(),
|
|
740
|
-
datetime.datetime.now(),
|
|
741
|
-
self._logs,
|
|
742
|
-
self._decisions,
|
|
743
|
-
self._seed,
|
|
744
|
-
)
|
|
758
|
+
simulation = self._get_simulation()
|
|
745
759
|
window.simulationToDownload = simulation.dump()
|
|
746
760
|
show_download()
|
|
747
761
|
except Exception as e:
|
|
@@ -954,5 +968,3 @@ class CodeBattles(
|
|
|
954
968
|
0,
|
|
955
969
|
)
|
|
956
970
|
)
|
|
957
|
-
else:
|
|
958
|
-
await asyncio.sleep(0.01)
|
package/dist/esm/index.js
CHANGED
|
@@ -460,7 +460,10 @@ const useLocalStorage = ({ key, defaultValue, }) => {
|
|
|
460
460
|
}
|
|
461
461
|
if (newValue) {
|
|
462
462
|
localStorage.setItem(key, JSON.stringify(newValue));
|
|
463
|
-
window.dispatchEvent(new StorageEvent("storage", {
|
|
463
|
+
window.dispatchEvent(new StorageEvent("storage", {
|
|
464
|
+
key,
|
|
465
|
+
newValue: JSON.stringify(newValue),
|
|
466
|
+
}));
|
|
464
467
|
}
|
|
465
468
|
};
|
|
466
469
|
return [value, userSetValue];
|
package/package.json
CHANGED