You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.6KB

  1. import time
  2. import redis
  3. import settings
  4. from nano25519 import ed25519_oop as ed25519
  5. from jcnanolib import nano
  6. r = redis.Redis(host='localhost', port=6379, db=0)
  7. api_key = settings.api_key
  8. def follow(thefile):
  9. thefile.seek(0,2) # Go to the end of the file
  10. while True:
  11. line = thefile.readline()
  12. if not line:
  13. time.sleep(0.1) # Sleep briefly
  14. continue
  15. yield line
  16. def process_pending(account, index_pos, api_key, wallet_seed):
  17. pending = nano.get_pending(str(account))
  18. print("Process Pending: {}".format(pending))
  19. previous = nano.get_previous(str(account))
  20. print(previous)
  21. if len(pending) > 0:
  22. pending = nano.get_pending(str(account))
  23. # try:
  24. if len(previous) == 0:
  25. print("Opening Account")
  26. hash, balance = nano.open_xrb(int(index_pos), account, wallet_seed, settings.api_key)
  27. previous = nano.get_previous(str(account))
  28. else:
  29. hash, balance = nano.receive_xrb(int(index_pos), account, wallet_seed, settings.api_key)
  30. print("Reply {} {}".format(hash, balance))
  31. # except:
  32. # print("Error")
  33. else:
  34. return 0
  35. def send_reward_nano(xrb_address, raw_amount, index, api_key, dest_account):
  36. print("Sending Nano...")
  37. process_pending(xrb_address, index, api_key, settings.wallet_seed)
  38. current_balance = nano.get_account_balance(xrb_address)
  39. print(current_balance)
  40. if int(current_balance) > 0:
  41. #TODO
  42. try:
  43. return_block = nano.send_xrb(dest_account, raw_amount, xrb_address, int(index), settings.wallet_seed, settings.api_key)
  44. print(return_block)
  45. return(return_block)
  46. except:
  47. return('Error')
  48. def parse_data(username, send_amount):
  49. #first check redis db to get the nano address
  50. if r.exists(username.rstrip()) and int(send_amount) > 0:
  51. nano_address = r.get(username.rstrip()).decode('utf-8')
  52. if nano_address == 'error':
  53. return
  54. #calculate score/payout
  55. send_amount = int(send_amount)
  56. print('Sending {} to {}'.format(send_amount, nano_address))
  57. #send nano
  58. payout_raw = 100000000000000000000000000 * int(send_amount)
  59. xrb_address = settings.xrb_account
  60. return_block = send_reward_nano(xrb_address, payout_raw, 0, api_key, nano_address)
  61. score_username = 'score-{}'.format(username.rstrip())
  62. r.incrby(score_username, send_amount)
  63. else:
  64. print('User not registered')
  65. current_balance = nano.get_account_balance(settings.xrb_account)
  66. print(current_balance)
  67. process_pending(settings.xrb_account, 0, api_key, settings.wallet_seed)
  68. # Reset counter
  69. print('***RESET COUNTER***')
  70. r.set('player_count', '0')
  71. logfile = open(settings.logfile)
  72. loglines = follow(logfile)
  73. for line in loglines:
  74. print(line, end='')
  75. line_comp = line.split(' ')
  76. print(line_comp)
  77. if 'ClientConnect' in line:
  78. # Increase counter by 1
  79. r.incr('player_count')
  80. if 'ClientDisconnect' in line:
  81. # Increase counter by 1
  82. r.decr('player_count')
  83. if 'Timelimit hit' in line:
  84. # Reset counter
  85. print('***RESET COUNTER***')
  86. r.set('player_count', '0')
  87. if len(line_comp) > 3:
  88. if 'score' in line_comp[2]:
  89. print('** Score parsing {} {}: {} **'.format(line_comp[9], line_comp[10].rstrip(), line_comp[3]))
  90. parse_data(line_comp[10], line_comp[3])
  91. if 'score' in line_comp[3]:
  92. print('** Score parsing {} {}: {} **'.format(line_comp[10], line_comp[11].rstrip(), line_comp[4]))
  93. parse_data(line_comp[11], line_comp[4])