-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_game.py
47 lines (39 loc) · 1.26 KB
/
populate_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'PlutosEnvy.settings')
import django
django.setup()
from game.models import Unit, City, Player
# This uses config_populate.txt to create a set of players, cities, and units while connecting all three.
# See config_populate.txt for instructions on formatting.
def populate():
p = Player()
u = Unit()
c = City()
for line in open("config_populate.txt"):
split = line.split(" ")
instruction = split[0]
if "#" in instruction:
continue
if len(split) == 1:
continue
name = ' '.join(split[1:len(split)]).rstrip("\n")
print(name)
if instruction == "P":
p = add_player(name)
elif instruction == 'C':
c = add_city(name, p)
elif instruction == 'U':
u = add_unit(name, p, c)
p.add_unit(u)
def add_unit(name, player, city):
u = Unit.objects.get_or_create(name=name, owner=player, currentCity=city)[0]
return u
def add_city(name, player):
c = City.objects.get_or_create(name=name, owner=player)[0]
return c
def add_player(name):
p = Player.objects.get_or_create(name=name)[0]
return p
if __name__ == '__main__':
print("Populating...")
populate()