-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.py
More file actions
148 lines (118 loc) · 4.4 KB
/
server.py
File metadata and controls
148 lines (118 loc) · 4.4 KB
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
import socket
import pandas as pd
import csv
import os
import ast
import sys
from datetime import datetime
import time
start = time.time()
location = "null"
now = datetime.now() # current date and time
# date_time = now.strftime("%m-%d-%Y_%H-%M")
date_time = now.strftime("%m-%Y")
data_path = 'data/player_data_{}.csv'.format(date_time)
labels = ['Name', 'helmet', 'cape', 'amulet', 'weapon', 'body', 'sheild', 'Equip7', 'legs', 'Equip9',\
'gloves','boots','Equip12', 'Loc_x', 'Loc_y', 'Anim_id', 'Chat_response',\
'Overall', 'Attack', 'Defence', 'Strength', 'Hitpoints', 'Ranged', 'Prayer', 'Magic', 'Cooking', 'Woodcutting', \
'Fletching', 'Fishing', 'Firemaking', 'Crafting', 'Smithing', 'Mining', 'Herblore', 'Agility', 'Thieving', 'Slayer', \
'Farming', 'Runecrafting', 'Hunter', 'Construction', 'Location']
def writeToCSV(data):
with open(data_path, mode='w', newline='') as GE_data:
GE_writer = csv.writer(GE_data, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
GE_writer.writerow(labels) # write field names
GE_writer.writerow(data)
def appendToCSV(data):
with open(data_path, mode='a', newline='') as GE_data:
GE_writer = csv.writer(GE_data, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
GE_writer.writerow(data)
def parseRequest(request_string):
global location
try:
parsed_array = []
request_arr = request_string.split('\r\n')
request_arr[1] = ast.literal_eval(request_arr[1]) # convert equipment list to array
request_arr[2] = ast.literal_eval(request_arr[2]) # convert tuple to array
parsed_array.append(request_arr[0]) # add the name
parsed_array.extend(request_arr[1]) # add each item (should be 12)
parsed_array.append(request_arr[2][0]) # add the location tile x value
parsed_array.append(request_arr[2][1]) # add the location tile y value
parsed_array.append(request_arr[3]) # add the animation id
parsed_array.append(request_arr[4]) # add the chat response
if (request_arr[5] == '0'):
for i in range(5, 29):
parsed_array.append('0') # insert fake hiscore data, just 0 so we know it's wrong
else:
for i in range(5, 29):
parsed_array.append(request_arr[i].split(',')[1]) # get only the level for each skill
parsed_array.append(location)
print(parsed_array)
if os.path.isfile(data_path):
appendToCSV(parsed_array)
else:
writeToCSV(parsed_array)
return True
except Exception as e:
request_arr = request_string.split('\r\n')
print(request_arr)
print(len(request_arr))
print(e)
return False
def getCheckedPlayers():
player_names = ""
first_line = True
# If the file doesn't exist, return empty
if not os.path.isfile(data_path): return player_names
with open(data_path) as f:
for row in f:
if (first_line):
first_line = False
continue
player_names += row.split(',')[0] + ","
return player_names
def main():
global location, start
if(len(sys.argv) > 1):
location = sys.argv[1]
print('Location: {}'.format(location))
else:
print('Please select location based on expected skill - mining, woodcutting, none')
return
HOST = '' # Symbolic name meaning all available interfaces
PORT = 9876 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
print('Server Ready')
s.listen(1)
conn, addr = s.accept()
print ('Connected by', addr)
past_checked_players = getCheckedPlayers()
print("Restoring {} previously checked player names".format(past_checked_players.count(",")))
conn.sendall(str.encode(past_checked_players + " \r\n")) # turn it back into bytes
req_count = 0
while 1:
try:
data = conn.recv(1024)
decodedRequest = data.decode("utf-8")
if not data: break
parse_success = parseRequest(decodedRequest)
if (parse_success == True):
req_count += 1
response = "Recieved by python server!"
if (req_count > 5000):
print("Request count exceeded, stopping. Current time: {}".format(datetime.now()))
response = "OUT"
req_count = 0
if (int(time.time() - start) > 7200):
print("Two hours passed, stopping. Current time: {}".format(datetime.now()))
response = "OUT"
start = time.time()
conn.sendall(str.encode(response + " \r\n")) # turn it back into bytes
# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break
conn.close()
if __name__ == "__main__":
# past_checked_players = getCheckedPlayers()
# print(past_checked_players[0:100])
main()