home · contact · privacy
Server/Py: Remove flush() the necessity of which I can't rationalize.
[plomrogue] / plomrogue-server.py
1 import errno, os, time
2
3 def setup_server_io(io_db):
4     """Fill IO files DB with proper file( path)s. Write process IO test string.
5
6     Ensure IO files directory at server/. Remove any old in file if found. Set
7     up new in file (io_db["file_in"]) for reading at io_db["path_in"], and new
8     out file (io_db["file_out"]) for writing at io_db["path_out"]. Start out
9     file with process hash line of format PID + " " + floated UNIX time
10     (io_db["teststring"]). Set worldstate file path io_db["path_worldstate"].
11     """
12     io_dir = "server/"
13     io_db["path_in"] = io_dir + "in"
14     io_db["path_out"] = io_dir + "out"
15     io_db["path_worldstate"] = io_dir + "worldstate"
16     io_db["teststring"] = str(os.getpid()) + " " + str(time.time())
17     os.makedirs(io_dir, exist_ok=True)
18     io_db["file_out"] = open(io_db["path_out"], "w")
19     io_db["file_out"].write(io_db["teststring"] + "\n")
20     if os.access(io_db["path_in"], os.F_OK):
21         os.remove(io_db["path_in"])
22     io_db["file_in"] = open(io_db["path_in"], "w")
23     io_db["file_in"].close()
24     io_db["file_in"] = open(io_db["path_in"], "r")
25
26 def cleanup_server_io(io_db):
27     """Close and remove all files in IO files DB."""
28     io_db["file_out"].close()
29     os.remove(io_db["path_out"])
30     io_db["file_in"].close()
31     os.remove(io_db["path_in"])
32     if "file_worldstate" in io_db:                   # This file is only set up 
33         io_db["file_worldstate"].close()             # properly when the game 
34     if os.access(io_db["path_worldstate"], os.F_OK): # world is active, which is
35         os.remove(io_db["path_worldstate"])          # not guaranteed.
36
37 io_db = {}
38 try:
39     print("DUMMY: Obey command-line arguments.")
40     print("DUMMY: Open files.")
41     setup_server_io(io_db)
42     print("DUMMY: Run game.")
43 except:
44     print("SOMETHING WENT WRONG\n")
45     raise
46 finally:
47     cleanup_server_io(io_db)
48     print("DUMMY: (Clean up C heap.)")