refactoring
[r15k.git] / r15k / server / world.py
1 # -*- coding: utf-8 -*-
2
3 import random
4 import itertools
5 import networkx as nx
6 import logging
7 import uuid
8
9 from r15k.atomic.atomic import Atomic
10 import r15k.server.graph as GraphGenerator
11
12 logger = logging.getLogger('r15k.world')
13
14 node_atom = Atomic()
15 node_atom.load("old_phoenix")
16
17 TLDS = ["com", "org", "info", "nz", "fr", "en", "es", "cat", "xxx", "biz", "gouv", "onu"]
18
19 def generateNodeName():
20 return "%s.%s.%s" % (node_atom.name(), node_atom.name(), random.choice(TLDS))
21
22 name_atom = Atomic()
23 name_atom.load("handles")
24
25 def generateDeckerName():
26 return "%s %s" % (name_atom.name().capitalize(), name_atom.name().capitalize())
27
28 corp_atom = Atomic()
29 corp_atom.load("corps")
30
31 COMPANY = ["Global", "Corporation", "Consolidated", "Multinational", "GmbH", "Amalgamated", "Unlimited"]
32
33 def generateCorpName():
34 return "%s %s" % (corp_atom.name().capitalize(), random.choice(COMPANY))
35
36
37 class World:
38 def __init__(self):
39 self.seed = None
40 self.nodes = {}
41 self.corps = []
42 self.graph = None
43 self.uid = str(uuid.uuid4())
44
45 def addNode(self, node):
46 self.nodes[node.name] = node
47
48 def dump(self):
49 data = {}
50 data["uid"] = self.uid
51 data["nodes"] = []
52 data["edges"] = []
53 for n in self.graph.nodes():
54 data["nodes"].append(n.dump())
55 for a, b in self.graph.edges():
56 data["edges"].append({"from": a.name, "to": b.name})
57 return data
58
59 def restore(self, data):
60 self.uid = data["uid"]
61 self.graph = nx.Graph()
62 self.nodes = {}
63 for n in data["nodes"]:
64 node = Node()
65 node.restore(n)
66 for e in data.get("edges", []):
67 self.graph.add_edge(self.nodes[e["from"]], self.nodes[e["to"]])
68 self.graph.positions = nx.pygraphviz_layout(G, prog = 'neato')
69 return data
70
71 def create(self, criteria):
72 seed = criteria.get("seed", None) or 1
73 size = criteria.get("size", None) or 30
74 corps = criteria.get("corps", None) or 5
75
76 self.seed = seed
77 random.seed(self.seed)
78 self.corps = [generateCorpName() for _ in range(corps)]
79
80 nodes = [Node(self) for _ in range(size)]
81 self.graph = GraphGenerator.network(nodes, self.seed)
82
83 def __repr__(self):
84 return "%s(%s)" % (self.__class__.__name__, self.uid)
85
86
87 class Node:
88 CPUS = [x for x in range(100, 3000, 100)]
89 def __init__(self, world):
90 self.name = generateNodeName()
91 self.corp = random.choice(world.corps)
92 self.world = world
93 self.world.addNode(self)
94 self.cpu = random.choice(Node.CPUS)
95
96 def dump(self):
97 return {"name": self.name, "corp": self.corp, "cpu": self.cpu}
98
99 def restore(self, data):
100 self.name = data["name"]
101 self.corp = data["corp"]
102 self.cpu = data["cpu"]
103
104 def __repr__(self):
105 return self.name
106
107
108 class Decker:
109 def __init__(self):
110 self.name = generateDeckerName();
111 self.corp = generateCorpName()
112 self.stats = {}
113
114
115 if __name__ == '__main__':
116 import sys
117 W = World(1)
118 G = W.generate(500, 0)
119 print G.positions
120
121