Skip to content

Commit

Permalink
tui for tetris game
Browse files Browse the repository at this point in the history
  • Loading branch information
tonymorony committed Mar 27, 2019
1 parent f0bff7d commit f614203
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/tuilib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ def print_multiplayer_games_list(rpc_connection):
print("\nPlease choose R or E\n")


def rogue_newgame_singleplayer(rpc_connection):
def rogue_newgame_singleplayer(rpc_connection, is_game_a_rogue=True):
try:
new_game_txid = rpc_connection.cclib("newgame", "17", "[1]")["txid"]
print("New singleplayer training game succesfully created. txid: " + new_game_txid)
Expand Down Expand Up @@ -1084,7 +1084,10 @@ def rogue_newgame_singleplayer(rpc_connection):
game_info = rogue_game_info(rpc_connection, new_game_txid)
start_time = time.time()
while True:
subprocess.call(["cc/rogue/rogue", str(game_info["seed"]), str(game_info["gametxid"])])
if is_game_a_rogue:
subprocess.call(["cc/rogue/rogue", str(game_info["seed"]), str(game_info["gametxid"])])
else:
subprocess.call(["cc/games/tetris", str(game_info["seed"]), str(game_info["gametxid"])])
time_elapsed = time.time() - start_time
if time_elapsed > 1:
break
Expand Down
96 changes: 96 additions & 0 deletions tetris_tui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3

from lib import rpclib, tuilib
import os
import time
import sys
import platform

header = "\
_____ _ _ ______\n\
|_ _| | | (_) | _ \n\
| | ___| |_ _ __ _ ___| | | |__ _ _ __ _ __\n\
| |/ _ \ __| '__| / __| | | / _` | '_ \| '_ \\\n\
| | __/ |_| | | \__ \ |/ / (_| | |_) | |_) |\n\
\_/\___|\__|_| |_|___/___/ \__,_| .__/| .__/\n\
| | | |\n\
|_| |_|"


menuItems = [
{"Check current connection": tuilib.getinfo_tui},
{"Check mempool": tuilib.print_mempool},
{"Start singleplayer tetris game (creating, registering and starting game)": tuilib.rogue_newgame_singleplayer},
{"Exit": tuilib.exit}
]


def main():
while True:
operating_system = platform.system()
if operating_system != 'Win64' and operating_system != 'Windows':
os.system('clear')
else:
os.system('cls')
print(tuilib.colorize(header, 'pink'))
print(tuilib.colorize('TUI v0.0.3\n', 'green'))
menu_items_counter = 0
for item in menuItems:
print(tuilib.colorize("[" + str(menuItems.index(item)) + "] ", 'blue') + list(item.keys())[0])
choice = input(">> ")
try:
if int(choice) < 0:
raise ValueError
# Call the matching function
if list(menuItems[int(choice)].keys())[0] == "Exit":
list(menuItems[int(choice)].values())[0]()
elif list(menuItems[int(choice)].keys())[0] == "Start singleplayer tetris game (creating, registering and starting game)":
list(menuItems[int(choice)].values())[0](rpc_connection, False)
else:
list(menuItems[int(choice)].values())[0](rpc_connection)
except (ValueError, IndexError):
pass


if __name__ == "__main__":
while True:
chain = "GTEST"
try:
print(tuilib.colorize("Welcome to the Tetris TUI!\n"
"Please provide asset chain RPC connection details for initialization", "blue"))
rpc_connection = tuilib.def_credentials(chain)
rpclib.getinfo(rpc_connection)
# waiting until chain is in sync
while True:
have_blocks = rpclib.getinfo(rpc_connection)["blocks"]
longest_chain = rpclib.getinfo(rpc_connection)["longestchain"]
if have_blocks != longest_chain:
print(tuilib.colorize("GTEST not synced yet.", "red"))
print("Have " + str(have_blocks) + " from " + str(longest_chain) + " blocks")
time.sleep(5)
else:
print(tuilib.colorize("Chain is synced!", "green"))
break
# checking if pubkey is set and set valid if not
info = rpclib.getinfo(rpc_connection)
if "pubkey" in info.keys():
print("Pubkey is already set")
else:
valid_address = rpc_connection.getaccountaddress("")
valid_pubkey = rpc_connection.validateaddress(valid_address)["pubkey"]
rpc_connection.setpubkey(valid_pubkey)
print(tuilib.colorize("Pubkey is succesfully set!", "green"))
# copy ROGUE config to current daemon directory if it's not here
tuilib.check_if_config_is_here(rpc_connection)
except Exception:
print(tuilib.colorize("Cant connect to GTEST daemon RPC! Please check if daemon is up.", "pink"))
tuilib.exit()
else:
print(tuilib.colorize("Succesfully connected!\n", "green"))
with (open("lib/logo.txt", "r")) as logo:
for line in logo:
print(line, end='')
time.sleep(0.04)
print("\n")
break
main()

0 comments on commit f614203

Please sign in to comment.