1
Fork 0
lorchess/randomize.py

33 lines
1.2 KiB
Python
Raw Permalink Normal View History

2013-08-26 14:53:22 +04:00
#!/usr/bin/python2
# -*- coding: utf-8 -*-
2013-08-26 21:12:19 +04:00
import random, time
2013-08-26 14:53:22 +04:00
def write_user(username, table_size):
2013-08-26 21:12:19 +04:00
repeater = False
2013-08-26 14:53:22 +04:00
random_seed = random.choice(range(1, table_size))
2013-08-26 21:12:19 +04:00
tfile = open('table.txt', 'r+a')
2013-08-26 14:53:22 +04:00
tabledata = tfile.readlines()
for line in tabledata:
if line.split(' | ')[0] == str(random_seed):
# repeat from begin
2013-08-26 21:12:19 +04:00
repeater = True
2013-08-26 14:53:22 +04:00
write_user(username, table_size)
# if we haven't matching number, write das line
2013-08-26 21:12:19 +04:00
if repeater == False:
tfile.write("%i | %s\n" % (random_seed, username))
2013-08-26 14:53:22 +04:00
def check_file(username, table_size):
try:
tablefile = open('table.txt', 'r')
write_user(username, table_size)
except IOError:
tablefile = open('table.txt', 'w')
2013-08-26 21:12:19 +04:00
tablefile.write("Жеребьевка участников LORChess.\nРазмер таблицы участников: %s.\nДанный файл является неизменяемым и создаваемым один раз.\nUNIX timestamp: %i\n========\n\n" % (table_size - 1, int(time.time())))
2013-08-26 14:53:22 +04:00
tablefile.close()
write_user(username, table_size)
nickname = raw_input("Введите имя игрока: ")
check_file(nickname, 17)