forked from griffy/IronTrotter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
136 lines (102 loc) · 3.66 KB
/
server.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from twisted.internet import reactor, protocol
from twisted.internet.task import LoopingCall
from twisted.protocols.basic import LineReceiver
from stats import Stats
from update import Update
from random import choice
from sprite import UP, DOWN, LEFT, RIGHT
import pickle
from entity import is_living
import map
import entity
import pygame
class TrotterPub(LineReceiver):
def __init__(self):
self.first = True
def lineReceived(self, data):
up = pickle.loads(data)
#self.factory.glob.update(up)
if self.first == True:
self.first = False
for layer in self.factory.glob.map.layers:
for ent in layer.entities:
self.sendLine(pickle.dumps(ent.getUpdate(), 2))
#generate a player
player = entity.Entity(Stats(0,0),0, "BOB")
self.factory.glob.update(player)
self.sendLine(pickle.dumps(player.getUpdate(),2))
else:
self.factory.glob.update(up)
# send to all other clients
for t in self.factory.transports:
if t is not self.transport:
t.sendLine(pickle.dumps(up,2))
def connectionMade(self):
self.factory.transports.append(self)
class MyFactory(protocol.Factory):
def __init__(self, glob):
self.clients = []
self.protocol = TrotterPub
self.glob = glob
self.transports = []
def clientConnectionMade(self, client):
self.clients.append(client)
def clientConnectionLost(self, client):
self.clients.remove(client)
class ServerGlobals:
def __init__(self):
self.map = map.generate_map(map.map_width,map.map_height)
def update(self, up):
if isinstance(up, Update):
if is_living(up.enttype):
entity = self.map.layers[2].getById(up.idnum)
if entity is not None:
entity.stats = up.stats
else:
if up.name != "":
self.map.addPlayer(up)
def main():
"""This runs the protocol on port 8000"""
pygame.init()
screen = pygame.display.set_mode((1,1))
glob = ServerGlobals()
factory = MyFactory(glob)
reactor.listenTCP(8000,factory)
def move_enemy():
if not glob.map.layers:
return
for entity in glob.map.layers[2].entities:
moves = []
if glob.map.is_player_up(entity):
moves.append(UP)
if glob.map.is_player_down(entity):
moves.append(DOWN)
if glob.map.is_player_left(entity):
moves.append(LEFT)
if glob.map.is_player_right(entity):
moves.append(RIGHT)
if not moves:
move = choice([UP, DOWN, LEFT, RIGHT])
else:
move = choice(moves)
if move == UP:
if not glob.map.is_entity_blocked_up(entity):
entity.stats.y -= 1
elif move == DOWN:
if not glob.map.is_entity_blocked_down(entity):
entity.stats.y += 1
elif move == LEFT:
if not glob.map.is_entity_blocked_left(entity):
entity.stats.x -= 1
else:
if not glob.map.is_entity_blocked_right(entity):
entity.stats.x += 1
# send updated position information to all other clients
for t in factory.transports:
t.sendLine(pickle.dumps(entity.getUpdate(),2))
lc = LoopingCall(move_enemy)
lc.start(1)
reactor.run()
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()