Skip to content
This repository was archived by the owner on Feb 22, 2020. It is now read-only.

Commit 0cf8842

Browse files
authored
Merge pull request #5 from Franccisco/enigma_config
Most of enigma configs. Not much testing. Any reconfigurations should go towards master branch unless very significant.
2 parents c4b3b95 + 09976dd commit 0cf8842

File tree

6 files changed

+177
-78
lines changed

6 files changed

+177
-78
lines changed

amphibian-alchemists/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app/data/
Lines changed: 122 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,127 @@
11
from kivy.uix.screenmanager import Screen
22
from kivy.storage.jsonstore import JsonStore
3+
from kivy.app import App
4+
from requests import get
5+
from string import ascii_uppercase
6+
from random import sample
7+
from enigma.machine import EnigmaMachine
8+
import os
9+
10+
DATA_DIR = os.path.join(App.get_running_app().APP_DIR, os.path.normcase("data/gamestate.json"))
11+
12+
13+
def on_config_change():
14+
"""
15+
After you change the JSON data w/ JSONStore, call this function
16+
This should only be called for rotor or plug changes
17+
"""
18+
store = JsonStore(DATA_DIR)
19+
game_id = str(App.get_running_app().game_id)
20+
plugs = store.get(game_id)["current_state"]["plugs"]
21+
plug_settings = " ".join(x for x in plugs)
22+
App.get_running_app().machine.from_key_sheet(
23+
rotors="I II III",
24+
reflector="B",
25+
ring_settings=[1, 20, 11],
26+
plugboard_settings=plug_settings
27+
)
28+
rotors = ""
29+
for x in store.get(game_id)["current_state"]["rotors"]:
30+
if x is None:
31+
continue
32+
rotors = rotors.join(x)
33+
App.get_running_app().machine.set_display(rotors)
34+
35+
36+
def get_wiki_summary() -> str:
37+
endpoint = "https://en.wikipedia.org/w/api.php?action=query&list=random&" \
38+
"format=json&rnnamespace=0&rnlimit=1&origin=*"
39+
response = get(endpoint)
40+
title = (response.json())["query"]["random"][0]["title"]
41+
endpoint = "https://en.wikipedia.org/api/rest_v1/page/summary/"
42+
response1 = get(endpoint + title.replace(" ", "%20"))
43+
return response1.json()["extract"]
44+
45+
46+
def get_encrypted_text(text: str, rotor_settings: str, plug_settings: str) -> str:
47+
# TODO UNTESTED!!!!!!!!
48+
machine = EnigmaMachine.from_key_sheet(
49+
rotors="I II III",
50+
reflector="B",
51+
ring_settings=[1, 20, 11],
52+
plugboard_settings=plug_settings
53+
)
54+
machine.set_display(rotor_settings)
55+
return machine.process_text(text)
56+
57+
58+
def setup_new_game_settings():
59+
store = JsonStore(DATA_DIR)
60+
currrent_game_id = store.get("latest_game_id")["id"]
61+
if currrent_game_id is None:
62+
store.put("latest_game_id", id=0)
63+
else:
64+
store.put("latest_game_id", id=int(currrent_game_id) + 1)
65+
game_id = store.get("latest_game_id")["id"]
66+
plug_array = sample(ascii_uppercase, 20)
67+
plugs = []
68+
for i in range(10):
69+
plugs.append("".join(plug_array[i * 2:i * 2 + 2]))
70+
plug_settings = " ".join(x for x in plugs)
71+
rotors = sample(ascii_uppercase, 3)
72+
rotor_setting = "".join(rotors)
73+
App.get_running_app().machine.from_key_sheet(
74+
rotors="I II III",
75+
reflector="B",
76+
ring_settings=[1, 20, 11],
77+
plugboard_settings=plug_settings
78+
)
79+
App.get_running_app().machine.set_display(rotor_setting)
80+
81+
"""Storing data"""
82+
rotors.append(None)
83+
rotors.append(None)
84+
text = get_wiki_summary()
85+
store.put(
86+
game_id,
87+
ciphered_text=get_encrypted_text(text, rotor_setting, plug_settings),
88+
unciphered_text=text,
89+
encrypted_state={
90+
"reflector": "B",
91+
"rotors": rotors,
92+
"plugs": plugs
93+
},
94+
current_state={
95+
"reflector": "B",
96+
"rotors": ["A", "A", "A", None, None],
97+
"plugs": [
98+
"AB", "CD", "EF", "GH", "IJ",
99+
"KL", "MN", "OP", "QR", "ST"
100+
]
101+
},
102+
last_saved_state={
103+
"reflector": "B",
104+
"rotors": ["A", "A", "A", None, None],
105+
"plugs": [
106+
"AB", "CD", "EF", "GH", "IJ",
107+
"KL", "MN", "OP", "QR", "ST"
108+
]
109+
}
110+
)
3111

4112

5113
class GameScreen(Screen):
6-
store = JsonStore("data/sample/hello.json")
7-
store.put("blah", name="Blah", org="kivy")
8-
store.put('tshirtman', name='Gabriel', age=27)
9-
store.put('tito', name='Mathieu', age=30)
10-
print('tito is', store.get('tito')['age'])
11-
12-
# or guess the key/entry for a part of the key
13-
for item in store.find(name='Gabriel'):
14-
print('tshirtmans index key is', item[0])
15-
print('his key value pairs are', str(item[1]))
114+
"""Do we automatically assume new game or should we save?"""
115+
if not os.path.exists(DATA_DIR):
116+
store = JsonStore(DATA_DIR)
117+
store.put("latest_game_id", id=None)
118+
119+
def on_enter(self, *args):
120+
store = JsonStore(DATA_DIR)
121+
# TODO Add load game screen and select a game id by
122+
# running App.get_running_app().game_id = 0
123+
game_id = App.get_running_app().game_id
124+
if game_id is None or store.exists(str(App.get_running_app().game_id)) is False:
125+
setup_new_game_settings()
126+
else:
127+
on_config_change()

amphibian-alchemists/app/classes/game/paper.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
from kivy.uix.screenmanager import Screen
2-
from requests import get
3-
4-
5-
def get_wiki_summary() -> str:
6-
endpoint = "https://en.wikipedia.org/w/api.php?action=query&list=random&" \
7-
"format=json&rnnamespace=0&rnlimit=1&origin=*"
8-
response = get(endpoint)
9-
title = (response.json())["query"]["random"][0]["title"]
10-
endpoint = "https://en.wikipedia.org/api/rest_v1/page/summary/"
11-
response1 = get(endpoint + title.replace(" ", "%20"))
12-
return response1.json()["extract"]
132

143

154
class PaperScreen(Screen):

amphibian-alchemists/app/data/sample/gamestate.json

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,64 @@
11
{
2+
"latest_game_id": {"id": null},
23
"1": {
34
"ciphered_text": "asdfjhalskjdfhasdjf",
45
"unciphered_text": "Hello",
56
"current_output_text": "",
67
"encrypted_state": {
78
"reflector": "B",
8-
"rotors": {
9-
"1": "N",
10-
"2": "W",
11-
"3": "B",
12-
"4": null,
13-
"5": null
14-
},
15-
"plugs": {
16-
"1": "AB",
17-
"2": "CD",
18-
"3": "EF",
19-
"4": "GH",
20-
"5": "IJ",
21-
"6": "KL",
22-
"7": "MN",
23-
"8": "OP",
24-
"9": "QR",
25-
"10": "ST"
26-
}
9+
"rotors": [
10+
"N", "W", "B", null, null
11+
],
12+
"plugs": [
13+
"AB",
14+
"CD",
15+
"EF",
16+
"GH",
17+
"IJ",
18+
"KL",
19+
"MN",
20+
"OP",
21+
"QR",
22+
"ST"
23+
]
2724
},
2825
"current_state": {
2926
"rotors": {
3027
"1": "A",
3128
"2": "A",
3229
"3": "A"
3330
},
34-
"plugs": {
35-
"1": "AB",
36-
"2": "CD",
37-
"3": "EF",
38-
"4": "GH",
39-
"5": "IJ",
40-
"6": "KL",
41-
"7": "MN",
42-
"8": "OP",
43-
"9": "QR",
44-
"10": "ST"
45-
}
31+
"plugs": [
32+
"AB",
33+
"CD",
34+
"EF",
35+
"GH",
36+
"IJ",
37+
"KL",
38+
"MN",
39+
"OP",
40+
"QR",
41+
"ST"
42+
]
4643
},
4744
"last_saved_state": {
4845
"rotors": {
4946
"1": "A",
5047
"2": "A",
5148
"3": "A"
5249
},
53-
"plugs": {
54-
"1": "AB",
55-
"2": "CD",
56-
"3": "EF",
57-
"4": "GH",
58-
"5": "IJ",
59-
"6": "KL",
60-
"7": "MN",
61-
"8": "OP",
62-
"9": "QR",
63-
"10": "ST"
64-
}
50+
"plugs": [
51+
"AB",
52+
"CD",
53+
"EF",
54+
"GH",
55+
"IJ",
56+
"KL",
57+
"MN",
58+
"OP",
59+
"QR",
60+
"ST"
61+
]
6562
}
6663
},
6764
"2": {}

amphibian-alchemists/app/data/sample/hello.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

amphibian-alchemists/app/main.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from kivy.factory import Factory
44
from kivy.lang import Builder
55
from kivy.uix.screenmanager import FadeTransition, ScreenManager
6+
from enigma.machine import EnigmaMachine
7+
import os
68

79
kivy.require("1.11.1")
810

@@ -32,6 +34,18 @@ def __init__(self, **kwargs):
3234

3335

3436
class AncientTechApp(App):
37+
def __init__(self, **kwargs):
38+
super().__init__(**kwargs)
39+
self.machine = EnigmaMachine.from_key_sheet(
40+
rotors="I II III",
41+
reflector="B",
42+
ring_settings=[1, 20, 11],
43+
plugboard_settings="AV BS CG DL FU HZ IN KM OW RX"
44+
)
45+
self.machine.set_display("ABC")
46+
self.game_id = None
47+
self.APP_DIR = os.path.dirname(os.path.abspath(__file__))
48+
3549
def build(self):
3650
self.title = "ENG"
3751
self.icon = "misc/logo.png"

0 commit comments

Comments
 (0)