Browse Source

write about compilation

Eren Yilmaz 6 years ago
parent
commit
03cb454842
8 changed files with 57 additions and 24 deletions
  1. 4 1
      .gitignore
  2. 9 0
      README.md
  3. 6 5
      client_controller.py
  4. 2 1
      game.py
  5. 9 5
      model.py
  6. 1 0
      run_client.py
  7. 1 1
      run_server.py
  8. 25 11
      util.py

+ 4 - 1
.gitignore

@@ -1,4 +1,7 @@
 .idea
 *.db
 venv
-*.db-journal
+*.db-journal
+*.spec
+build
+dist

+ 9 - 0
README.md

@@ -0,0 +1,9 @@
+# README
+
+## Compilation
+The server is intended to be run within a python 3.6 environment on the host specified in `connection.py`.
+The client is intended to be compiled with
+```
+pyinstaller run_client.py
+```
+where `pyinstaller` can be installed using pip.

+ 6 - 5
client_controller.py

@@ -1,6 +1,7 @@
-import getpass
-import inspect
 import sys
+from getpass import getpass
+from inspect import signature
+
 import connection
 from run_client import allowed_commands, fake_loading_bar
 from connection import client_request
@@ -19,7 +20,7 @@ def login(username=None, password=None):
 
     if password is None:
         if sys.stdin.isatty():
-            password = getpass.getpass('Password: ')
+            password = getpass('Password: ')
         else:
             password = input('Password: ')
 
@@ -37,7 +38,7 @@ def login(username=None, password=None):
 
 def register(username=None, password=None, game_key=''):
     if connection.session_id is not None:
-        client.fake_loading_bar('Signing out', delay=0.025)
+        fake_loading_bar('Signing out', delay=0.025)
         connection.session_id = None
 
     if username is None:
@@ -66,7 +67,7 @@ def help():
     for cmd in allowed_commands:
         this_module = sys.modules[__name__]
         method = getattr(this_module, cmd)
-        params = inspect.signature(method).parameters
+        params = signature(method).parameters
         num_args = len(params)
         if num_args > 0:
             print('`' + cmd + '`', 'takes the following', num_args, 'arguments:')

+ 2 - 1
game.py

@@ -1,2 +1,3 @@
+# coding=utf-8
 # noinspection SpellCheckingInspection
-CURRENCY_NAME = '₭ollar'
+CURRENCY_NAME = "₭ollar"

+ 9 - 5
model.py

@@ -10,8 +10,12 @@ import db_setup
 from game import CURRENCY_NAME
 from util import debug, random_chars
 
-connection: db.Connection = None
-cursor: db.Cursor = None
+# connection: db.Connection = None
+# cursor: db.Cursor = None
+connection = None  # no type annotations in python 2.7
+cursor = None
+
+
 db_name = None
 
 
@@ -431,7 +435,7 @@ def ownable_name_exists(name):
         return False
 
 
-def new_stock( timeout=60, name=None):
+def new_stock(timeout=60, name=None):
     connect()
 
     while name is None:
@@ -471,7 +475,7 @@ def new_stock( timeout=60, name=None):
     return name
 
 
-def new_stocks(timeout=60,count=1):
+def new_stocks(timeout=60, count=1):
     return [new_stock(timeout=timeout) for _ in range(count)]
 
 
@@ -598,7 +602,7 @@ def execute_orders(ownable_id):
             continue
 
         buy_order_finished = (buy_order_amount - buy_executed_amount - amount <= 0) or (
-            buyer_money - amount * price < price)
+                buyer_money - amount * price < price)
         sell_order_finished = (sell_order_amount - sell_executed_amount - amount <= 0)
 
         if price < 0 or amount <= 0:

+ 1 - 0
run_client.py

@@ -1,3 +1,4 @@
+from __future__ import print_function
 import sys
 import time
 

+ 1 - 1
run_server.py

@@ -43,4 +43,4 @@ if __name__ == '__main__':
 
 
     run(host='localhost', port=connection.port, debug=debug)
-    model.db.connection.disconnect()
+    model.connection.disconnect()

+ 25 - 11
util.py

@@ -1,21 +1,35 @@
-import csv
-import numpy
+from random import random
 
 debug = True
 chars = [str(d) for d in range(1, 10)]
 
-p = [1. for _ in chars]
-with open('letter_dist.csv', newline='') as csv_file:
-    reader = csv.reader(csv_file, delimiter=',')
-    sp = sum(p)
-    for row in reader:
-        chars.append(row[0])
-        p.append(float(row[2]))
-p = numpy.array(p) / sum(p)
+ps = [1. for _ in chars]
+letter_dist = [("E", 21912, 12.02), ("T", 16587, 9.1), ("A", 14810, 8.12), ("O", 14003, 7.68), ("I", 13318, 7.31),
+               ("N", 12666, 6.95), ("S", 11450, 6.28), ("R", 10977, 6.02), ("H", 10795, 5.92), ("D", 7874, 4.32),
+               ("L", 7253, 3.98), ("U", 5246, 2.88), ("C", 4943, 2.71), ("M", 4761, 2.61), ("F", 4200, 2.3),
+               ("Y", 3853, 2.11), ("W", 3819, 2.09), ("G", 3693, 2.03), ("P", 3316, 1.82), ("B", 2715, 1.49),
+               ("V", 2019, 1.11), ("K", 1257, 0.69), ("X", 315, 0.17), ("Q", 205, 0.11), ("J", 188, 0.1),
+               ("Z", 128, 0.07), ]
+sp = sum(ps)
+for row in letter_dist:
+    chars.append(row[0])
+    ps.append(float(row[2]))
+ps = [p / sum(ps) for p in ps]
+
+
+def choice(sequence, probabilities):
+    if sum(probabilities) != 1:
+        raise AssertionError('Probabilities must sum up to 1')
+    r = random()
+    for idx, c in enumerate(sequence):
+        r -= probabilities[idx]
+        if r < 0:
+            return c
+    raise AssertionError('Probabilities must sum up to 1')
 
 
 def random_chars(count):
-    return ''.join(numpy.random.choice(chars, p=p) for _ in range(count))
+    return ''.join(choice(chars, probabilities=ps) for _ in range(count))
 
 
 def str2bool(v):