code-battles 1.5.3 → 1.5.5

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.
@@ -480,7 +480,9 @@ class CodeBattles(
480
480
  self.step = 0
481
481
  self.active_players = list(range(len(self.player_names)))
482
482
  self.random = Random(seed)
483
- self.player_randoms = [Random(self.random.random()) for _ in self.player_names]
483
+ self.player_randoms = [
484
+ Random(self.random.randint(0, 2**128)) for _ in self.player_names
485
+ ]
484
486
  self.state = self.create_initial_state()
485
487
  self.player_requests = [
486
488
  self.create_initial_player_requests(i)
@@ -528,13 +530,15 @@ class CodeBattles(
528
530
 
529
531
  def _run_local_simulation(self):
530
532
  command = sys.argv[1]
533
+ output_file = None
531
534
  decisions = []
532
535
  if command == "simulate":
533
536
  seed = None if sys.argv[2] == "None" else int(sys.argv[2])
534
- self.map = sys.argv[3]
535
- self.player_names = sys.argv[4].split("-")
537
+ output_file = None if sys.argv[3] == "None" else sys.argv[3]
538
+ self.map = sys.argv[4]
539
+ self.player_names = sys.argv[5].split("-")
536
540
  player_codes = []
537
- for filename in sys.argv[5:]:
541
+ for filename in sys.argv[6:]:
538
542
  with open(filename, "r") as f:
539
543
  player_codes.append(f.read())
540
544
  elif command == "simulate-from-file":
@@ -559,7 +563,10 @@ class CodeBattles(
559
563
  if len(decisions) != 0:
560
564
  self.apply_decisions(decisions.pop(0))
561
565
  else:
562
- self.apply_decisions(self.make_decisions())
566
+ _decisions = self.make_decisions()
567
+ if output_file is not None:
568
+ self._decisions.append(_decisions)
569
+ self.apply_decisions(_decisions)
563
570
 
564
571
  if not self.over:
565
572
  self.step += 1
@@ -580,6 +587,11 @@ class CodeBattles(
580
587
  )
581
588
  )
582
589
 
590
+ if output_file is not None:
591
+ simulation_str = self._get_simulation().dump()
592
+ with open(output_file, "w") as f:
593
+ f.write(simulation_str)
594
+
583
595
  def _start_simulation(self, *args, **kwargs):
584
596
  loop = asyncio.get_event_loop()
585
597
  loop.run_until_complete(self._start_simulation_async(*args, **kwargs))
@@ -717,6 +729,18 @@ class CodeBattles(
717
729
  except Exception:
718
730
  traceback.print_exc()
719
731
 
732
+ def _get_simulation(self):
733
+ return Simulation(
734
+ self.map,
735
+ self.player_names,
736
+ self.__class__.__name__,
737
+ self.configure_version(),
738
+ datetime.datetime.now(),
739
+ self._logs,
740
+ self._decisions,
741
+ self._seed,
742
+ )
743
+
720
744
  def _update_step(self, decisions_str: str, logs_str: str, is_over_str: str):
721
745
  from js import window, document
722
746
 
@@ -730,16 +754,7 @@ class CodeBattles(
730
754
 
731
755
  if is_over:
732
756
  try:
733
- simulation = Simulation(
734
- self.map,
735
- self.player_names,
736
- self.__class__.__name__,
737
- self.configure_version(),
738
- datetime.datetime.now(),
739
- self._logs,
740
- self._decisions,
741
- self._seed,
742
- )
757
+ simulation = self._get_simulation()
743
758
  window.simulationToDownload = simulation.dump()
744
759
  show_download()
745
760
  except Exception as e:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "code-battles",
3
- "version": "1.5.3",
3
+ "version": "1.5.5",
4
4
  "description": "A library for building interactive competitive coding battles",
5
5
  "repository": "https://github.com/noamzaks/code-battles",
6
6
  "homepage": "https://code-battles.readthedocs.org",