code-battles 1.6.1 → 1.6.3

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.
@@ -15,6 +15,7 @@ from code_battles.utilities import (
15
15
  GameCanvas,
16
16
  console_log,
17
17
  download_image,
18
+ is_worker,
18
19
  navigate,
19
20
  set_results,
20
21
  show_alert,
@@ -381,7 +382,7 @@ class CodeBattles(
381
382
  )
382
383
  output += lines[string_file_indices[-1] + 1].strip() + "\n"
383
384
 
384
- show_alert(
385
+ self.alert(
385
386
  f"Code Exception in 'Player {player_index + 1}' API!",
386
387
  output,
387
388
  "red",
@@ -393,7 +394,7 @@ class CodeBattles(
393
394
 
394
395
  self.active_players = [p for p in self.active_players if p != player_index]
395
396
  if self.verbose:
396
- show_alert(
397
+ self.alert(
397
398
  f"{self.player_names[player_index]} was eliminated!",
398
399
  reason,
399
400
  "blue",
@@ -430,6 +431,33 @@ class CodeBattles(
430
431
  }
431
432
  )
432
433
 
434
+ def alert(
435
+ self,
436
+ title: str,
437
+ alert: str,
438
+ color: str,
439
+ icon: str,
440
+ limit_time: int = 5000,
441
+ is_code=True,
442
+ ):
443
+ """
444
+ Displays the given alert in the game UI.
445
+ """
446
+
447
+ if is_worker():
448
+ self._alerts.append(
449
+ {
450
+ "title": title,
451
+ "alert": alert,
452
+ "color": color,
453
+ "icon": icon,
454
+ "limit_time": limit_time,
455
+ "is_code": is_code,
456
+ }
457
+ )
458
+ else:
459
+ show_alert(title, alert, color, icon, limit_time, is_code)
460
+
433
461
  @web_only
434
462
  def play_sound(self, sound: str, force=False):
435
463
  """
@@ -498,6 +526,7 @@ class CodeBattles(
498
526
  if seed is None:
499
527
  seed = Random().randint(0, 2**128)
500
528
  self._logs = []
529
+ self._alerts = []
501
530
  self._decisions = []
502
531
  self._breakpoints = set()
503
532
  self._decision_index = 0
@@ -505,6 +534,7 @@ class CodeBattles(
505
534
  self.step = 0
506
535
  self.active_players = list(range(len(self.player_names)))
507
536
  self.random = Random(seed)
537
+ self.make_decisions_random = Random(self.random.randint(0, 2**128))
508
538
  self.player_randoms = [
509
539
  Random(self.random.randint(0, 2**128)) for _ in self.player_names
510
540
  ]
@@ -540,14 +570,16 @@ class CodeBattles(
540
570
  while not self.over:
541
571
  self._should_pause = False
542
572
  self._logs = []
573
+ self._alerts = []
543
574
  decisions = self.make_decisions()
544
575
  logs = self._logs
545
- self._logs = []
576
+ alerts = self._alerts
546
577
  self.apply_decisions(decisions)
547
578
 
548
579
  sync.update_step(
549
580
  base64.b64encode(decisions).decode(),
550
581
  json.dumps(logs),
582
+ json.dumps(alerts),
551
583
  "true" if self.over else "false",
552
584
  "true" if self._should_pause else "false",
553
585
  )
@@ -640,7 +672,7 @@ class CodeBattles(
640
672
  navigate(
641
673
  f"/simulation/{simulation.map}/{'-'.join(simulation.player_names)}?seed={simulation.seed}"
642
674
  )
643
- show_alert(
675
+ self.alert(
644
676
  "Loaded simulation file!",
645
677
  f"{', '.join(simulation.player_names)} competed in {simulation.map} at {simulation.timestamp}",
646
678
  "blue",
@@ -648,7 +680,7 @@ class CodeBattles(
648
680
  0,
649
681
  )
650
682
  if simulation.game != self.__class__.__name__:
651
- show_alert(
683
+ self.alert(
652
684
  "Warning: game mismatch!",
653
685
  f"Simulation file is for game {simulation.game} while the website is running {self.__class__.__name__}!",
654
686
  "yellow",
@@ -656,7 +688,7 @@ class CodeBattles(
656
688
  0,
657
689
  )
658
690
  if simulation.version != self.configure_version():
659
- show_alert(
691
+ self.alert(
660
692
  "Warning: version mismatch!",
661
693
  f"Simulation file is for version {simulation.version} while the website is running {self.configure_version()}!",
662
694
  "yellow",
@@ -788,13 +820,19 @@ class CodeBattles(
788
820
  )
789
821
 
790
822
  def _update_step(
791
- self, decisions_str: str, logs_str: str, is_over_str: str, should_pause_str: str
823
+ self,
824
+ decisions_str: str,
825
+ logs_str: str,
826
+ alerts_str: str,
827
+ is_over_str: str,
828
+ should_pause_str: str,
792
829
  ):
793
830
  from js import window, document
794
831
 
795
832
  now = time.time()
796
833
  decisions = base64.b64decode(str(decisions_str))
797
834
  logs: list = json.loads(str(logs_str))
835
+ alerts: list = json.loads(str(alerts_str))
798
836
  is_over = str(is_over_str) == "true"
799
837
  should_pause = str(should_pause_str) == "true"
800
838
 
@@ -802,6 +840,7 @@ class CodeBattles(
802
840
  self._breakpoints.add(len(self._decisions))
803
841
  self._decisions.append(decisions)
804
842
  self._logs.append(logs)
843
+ self._alerts.append(alerts)
805
844
 
806
845
  if is_over:
807
846
  try:
@@ -839,7 +878,7 @@ class CodeBattles(
839
878
  for index, api_code in enumerate(player_codes):
840
879
  if api_code != "" and api_code is not None:
841
880
  if f"class MyBot({bot_base_class_name}):" not in api_code:
842
- show_alert(
881
+ self.alert(
843
882
  f"Code Exception in 'Player {index + 1}' API!",
844
883
  f"Missing line:\nclass MyBot({bot_base_class_name}):",
845
884
  "red",
@@ -875,7 +914,7 @@ class CodeBattles(
875
914
  )
876
915
  output += lines[string_file_indices[-1] + 1].strip() + "\n"
877
916
 
878
- show_alert(
917
+ self.alert(
879
918
  f"Code Exception in 'Player {index + 1}' API!",
880
919
  output,
881
920
  "red",
@@ -922,6 +961,9 @@ class CodeBattles(
922
961
  log["text"],
923
962
  log["color"],
924
963
  )
964
+ alerts = self._alerts[self._decision_index]
965
+ for alert in alerts:
966
+ self.alert(**alert)
925
967
  self.apply_decisions(self._decisions[self._decision_index])
926
968
  self._decision_index += 1
927
969
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "code-battles",
3
- "version": "1.6.1",
3
+ "version": "1.6.3",
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",