-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalgame.py
executable file
·243 lines (206 loc) · 7.89 KB
/
localgame.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import argparse
import random
import os
import cherrypy
import string
import json
import threading
import time
import math
import random
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import WebSocket
from ws4py.messaging import TextMessage
# TestSprite['x'] -> Position
# TestSprite['y'] -> Position
# TestSprite['dx'] > Speed
# TestSprite['dy'] > Speed
# TestSprite['sprite'] > Name of creature
# 'health'
# 'isboolet'
# 'timeleft' (IF bullet)
# 'owner' (IF bullet)
AllSprites = {}
booletsize = 120
eggsize = 120
StartTime = 0.0
TimeNow = 0.0
LastTickTime = 0.0
DeltaTime = 0.0
TimeSinceStart = 0.0
CanvasWidth = 4200
CanvasHeight = 1800
TimeSinceEgg = 0
def TickEvent():
global StartTime, LastTickTime, AllSprites, TimeSinceEgg
threading.Timer(0.1, TickEvent).start()
TimeNow = time.time()
DeltaTime = TimeNow - LastTickTime
LastTickTime = TimeNow
TimeSinceStart = TimeNow - StartTime
# TestSprite = {}
# TestSprite['x'] = math.sin( TimeSinceStart ) * 200 + 200;
# TestSprite['y'] = math.cos( TimeSinceStart ) * 200 + 200;
# TestSprite['dx'] = math.cos( TimeSinceStart );
# TestSprite['dy'] = math.sin( TimeSinceStart );
# TestSprite['sprite'] = 'Lig';
# AllSprites['testsprite'] = TestSprite
UpdateAllSprites(AllSprites, DeltaTime)
TimeSinceEgg += DeltaTime;
if( TimeSinceEgg > 2.0 ):
SpawnEgg()
TimeSinceEgg = 0
def sanitize(someString):
#sanitize only if its a string
newName = someString
if (type(someString) == type("string")):
#and then only if its longer than 15 char
if (len(str((someString))) >15):
newName = str(someString)[0:15].replace("&", "").replace('"', "").replace("<", "").replace(">", "")
else:
#otherwise only sanitize the input
newName = str(someString).replacereplace("&", "").replace('"', "").replace("<", "").replace(">", "")
return newName
def StartGame():
global StartTime, LastTickTime
StartTime = time.time()
LastTickTime = StartTime
TickEvent()
def SpawnEgg():
f = random.random()*100000.0;
xStart = random.randrange(0, CanvasWidth, 1)
yStart = random.randrange(0, CanvasHeight, 1)
AllSprites[f] = { 'sprite':'egg', 'isboolet': False, 'isegg': True, 'timeleft':20, 'x': xStart, 'y': yStart, 'dx': 0, 'dy': 0 };
def Respawn( pid, species ):
xStart = random.randrange(0, CanvasWidth, 1)
yStart = random.randrange(0, CanvasHeight, 1)
if not pid in AllSprites:
AllSprites[pid] = {'x': xStart, 'y': yStart, 'dx': 0, 'dy': 0, 'health': 100, 'sprite':species }
else:
AllSprites[pid]['x'] = xStart;
AllSprites[pid]['y'] = yStart;
AllSprites[pid]['dx'] = 0;
AllSprites[pid]['dy'] = 0;
AllSprites[pid]['health'] = 100;
AllSprites[pid]['sprite'] = species;
def UpdateAllSprites(AllSprites, DeltaTime):
elements = [];
players = [];
boolets = [];
eggs = [];
# #rename spriteName if its too long and to protect against js injection
# for spriteName in AllSprites:
# #only shorten sprite name if its a string that the user entered
# if (type(spriteName) == type("string")):
# #and then only if its longer than 15 char
# if (len(str((spriteName))) >15):
# newName = str(spriteName)[0:15].replace("&", "&").replace('"', """).replace("<", "<").replace(">", ">")
# AllSprites[newName] = AllSprites.pop(spriteName)
# else:
# #otherwise only sanitize the input
# newName = str(spriteName).replace("&", "&").replace('"', """).replace("<", "<").replace(">", ">")
# AllSprites[newName] = AllSprites.pop(spriteName)
for spriteName in AllSprites:
elements.append( spriteName );
if( 'isboolet' in AllSprites[spriteName] and AllSprites[spriteName]['isboolet'] ):
boolets.append( spriteName )
elif( 'isegg' in AllSprites[spriteName] and AllSprites[spriteName]['isegg'] ):
eggs.append( spriteName )
else:
players.append( spriteName )
for e in elements:
spr = AllSprites[e];
spr['x'] = spr['x'] + spr['dx'] * DeltaTime
spr['y'] = spr['y'] + spr['dy'] * DeltaTime
if 'timeleft' in spr:
spr['timeleft']-=DeltaTime
if spr['timeleft'] < 0:
del AllSprites[ e ];
continue
#keep players within bounds of canvas
for pname in players:
if not pname in AllSprites:
continue
p = AllSprites[pname];
if( p['x'] < 0 ):
p['x'] = 0;
if( p['y'] < 0 ):
p['y'] = 0;
if( p['x'] > CanvasWidth ):
p['x'] = CanvasWidth;
if( p['y'] > CanvasHeight ):
p['y'] = CanvasHeight;
for ename in eggs:
if not ename in AllSprites:
continue
e = AllSprites[ename];
dist = math.sqrt((e['x']-p['x'])*(e['x']-p['x']) + (e['y']-p['y'])*(e['y']-p['y']))
if dist < eggsize and e['timeleft'] > 0.2:
p['health'] += 25
e['timeleft'] = 0.12
if p['health'] > 100:
p['health'] = 100
for bname in boolets:
if not bname in AllSprites:
continue
b = AllSprites[bname];
#if the owner of the bullet is this player, then we don't want to interact with it.
if b['owner'] == pname:
continue;
dist = math.sqrt((b['x']-p['x'])*(b['x']-p['x']) + (b['y']-p['y'])*(b['y']-p['y']))
if dist < booletsize:
p['health'] -= 1
if( p['health'] <= 0 ):
if not 'deaths' in AllSprites[pname]:
AllSprites[pname]['deaths'] = 0
AllSprites[pname]['deaths'] = AllSprites[pname]['deaths'] + 1
if not 'kills' in AllSprites[b['owner']]:
AllSprites[b['owner']]['kills'] = 0
AllSprites[b['owner']]['kills'] = AllSprites[b['owner']]['kills']+1
print b['owner'] + " killed " + pname
Respawn( pname, AllSprites[pname]['sprite'] )
def GotWebsocketData( thing, data ):
global AllSprites
try:
dats = json.loads( str(data) );
except:
return
if 'pid' in dats:
if hasattr(thing, 'pid'): #Handle renaming of players, remembering to sanitize input
newName = sanitize(dats['pid'])
AllSprites[newName] = AllSprites[thing.pid]
print "Player " + thing.pid + "changed their name to: "+ newName
del AllSprites[ thing.pid ];
else:
Respawn(dats['pid'], 'Lig' )
AllSprites[dats['pid']]['deaths'] = 0
AllSprites[dats['pid']]['kills'] = 0
thing.pid = dats['pid']
return
if not hasattr(thing, 'pid'):
return;
if not 'op' in dats:
print "No operation found for " + str(data);
if dats['op'] == 'getall':
thing.send( json.dumps( AllSprites) );
return;
elif dats['op'] == 'makeMove':
#calculate new location of the players move
ourPlayer = dats['p']
ourPlayer['x'] = ourPlayer['x'] + ourPlayer['dx']
ourPlayer['y'] = ourPlayer['y'] + ourPlayer['dy']
#update all the sprite locations
AllSprites[thing.pid]['dx'] = ourPlayer['dx']
AllSprites[thing.pid]['dy'] = ourPlayer['dy']
elif dats['op'] == 'respawn':
AllSprites[thing.pid]['deaths'] = 0
AllSprites[thing.pid]['kills'] = 0
Respawn( thing.pid, dats['spec'] )
elif dats['op'] == 'bul':
f = random.random()*100000.0;
AllSprites[f] = { 'sprite':dats['spr'], 'isboolet': True, 'timeleft': dats['time'], 'x': dats['x'], 'y': dats['y'], 'dx': dats['dx'], 'dy': dats['dy'], 'owner':thing.pid };
print AllSprites
#print "You are: " + thing.pid
#for each in dats:
# print each + ' = ' + str( dats[each] )
#thing.send( "response" );