Created
April 2, 2020 21:41
-
-
Save lunhg/9c3fda6072ef835e129e996e9747b7d6 to your computer and use it in GitHub Desktop.
CLI for consult iching's hexagrams and its mutations with a simulation of coin method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from optparse import OptionParser | |
import os | |
import datetime | |
import hashlib | |
import base58 | |
import binascii | |
import hmac | |
# PROGRAMA PRINCIPAL | |
PROG = "iching" | |
VERSION = "0.0.1" | |
description = "simula uma consulta de IChing pelo método da moeda " | |
parser = OptionParser(usage='usage: %prog [OPTIONS, [ARGS]]', | |
version='%s %s' % (PROG, VERSION), | |
description=description) | |
parser.add_option("-V", "--verbose", action="store_true", help="mostra dados de forma verborrágica") | |
parser.add_option("-f", "--nonce-file", action=None, help="arquivo que irá memorizar NONCE") | |
def genHMACsha512(strings, verbose): | |
__hmac__ = hmac.new( | |
bytearray(strings[1].encode('utf-8')), | |
strings[0].encode('utf-8'), | |
hashlib.sha512 | |
) | |
__hmac__ = __hmac__.hexdigest() | |
if(verbose): | |
print("hmacSha512: %s" % __hmac__) | |
return __hmac__ | |
def getNonce(filename, verbose): | |
__d__ = os.path.dirname(os.path.realpath(__file__)) | |
__f__ = os.path.join(__d__, filename) | |
nonce = -1 | |
if(verbose): | |
print("Opening %s" % __f__) | |
with open(__f__, mode='r', encoding='utf-8') as f: | |
nonce = (f.read()).split("NONCE=")[1] | |
if(verbose): | |
print("NONCE: %s" % nonce) | |
return nonce | |
def setNonce(filename, nonce, verbose): | |
__d__ = os.path.dirname(os.path.realpath(__file__)) | |
__f__ = os.path.join(__d__, filename) | |
if(verbose): | |
print("Opening %s" % __f__) | |
with open(__f__, mode='w', encoding='utf-8') as f: | |
f.write("NONCE=%s" % nonce) | |
if(verbose): | |
print("Setting NONCE to: %s" % nonce) | |
def genSha256(verbose): | |
key = binascii.hexlify(os.urandom(32)).decode() | |
if(verbose): | |
print("key: %s" % key) | |
__hash__ = hashlib.sha256(key.encode('utf8')).hexdigest() | |
if(verbose): | |
print("hash: %s" % __hash__) | |
return __hash__ | |
class Roll(object): | |
def __init__(self, filename, verbose=False): | |
self.verbose = verbose | |
self.filename = filename | |
self.seeds = [] | |
self.result = -1 | |
self.nonce = -1 | |
def setup(self): | |
for i in range(2): | |
self.seeds.append(genSha256(self.verbose)) | |
def run(self): | |
strings = [] | |
nonce = getNonce(self.filename, self.verbose) | |
for i in range(2): | |
strings.append("%s:%s:%s" % ( | |
nonce, | |
self.seeds[i], | |
nonce | |
)) | |
if(self.verbose): | |
for i in range(2): | |
print("String %s: %s" % (i, strings[i])) | |
__hmac__ = genHMACsha512(strings, self.verbose) | |
__dec__ = int(__hmac__[:8], 16) | |
if(self.verbose): | |
print("decimal: %s" % __dec__) | |
__roll__ = int(round(__dec__ / 429496.7295)) | |
if(self.verbose): | |
print("roll: %s" % __roll__) | |
setNonce(self.filename, int(nonce) + 1, self.verbose) | |
self.result = __roll__ | |
(options, args) = parser.parse_args() | |
class Move(object): | |
def __init__(self, filename, verbose): | |
self.filename = filename | |
self.rolls = [] | |
self.results = [] | |
self.verbose = verbose | |
def setup(self): | |
if(self.verbose): | |
print("=== Setuping Rolls ===") | |
for i in range(3): | |
if(self.verbose): | |
print("==> Roll %s" % i) | |
roll = Roll(self.filename, self.verbose) | |
roll.setup() | |
self.rolls.append(roll) | |
def run(self): | |
if(self.verbose): | |
print("=== Running rolls ===") | |
for i in range(3): | |
if(self.verbose): | |
print("==> Roll %s" % i) | |
self.rolls[i].run() | |
self.results.append(self.rolls[i].result) | |
class Game(object): | |
def __init__(self, filename, verbose): | |
self.filename = filename | |
self.verbose = verbose | |
self.moves = [] | |
self.results = [] | |
def setup(self): | |
if(self.verbose): | |
print("=== Setuping game ===") | |
for i in range(6): | |
if(self.verbose): | |
print("==> Move %s" % i) | |
move = Move(self.filename, self.verbose) | |
move.setup() | |
self.moves.append(move) | |
def run(self): | |
if(self.verbose): | |
print("=== Running game ===") | |
for i in range(6): | |
self.moves[i].run() | |
if(self.verbose): | |
print("==> Move %s" % i) | |
self.results.append(self.moves[i].results) | |
game = Game(options.nonce_file, options.verbose) | |
game.setup() | |
game.run() | |
hexagram = [] | |
if(options.verbose): | |
print("Hexagram matrix rolls: ") | |
for result in game.results: | |
line = [] | |
for r in result: | |
if r < 5000: | |
line.append(0) | |
if r > 5000: | |
line.append(1) | |
hexagram.append(line) | |
if(options.verbose): | |
print(result) | |
print("") | |
if(options.verbose): | |
print("Hexagrams matrix yin-yang:") | |
for h in hexagram: | |
print(h) | |
print("") | |
for i in range(len(hexagram)): | |
hex = hexagram[len(hexagram) - 1 - i] | |
val = 0 | |
for value in hex: | |
if (value == 0): | |
val += 2 | |
if (value == 1): | |
val += 3 | |
if (val == 6): | |
print('--- --- X') | |
if (val == 7): | |
print('--- --- O') | |
if (val == 8): | |
print('------- O') | |
if (val == 9): | |
print('------- X') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment