MarioWeb/IDManager.py

148 lines
4.0 KiB
Python
Raw Permalink Normal View History

import os
2022-11-03 14:07:08 +00:00
import random
import csv
2022-11-24 06:49:02 +00:00
2022-11-03 12:03:04 +00:00
class idManager():
2022-11-03 14:07:08 +00:00
levelNum = 200
typeNum = 100
2023-03-01 13:05:56 +00:00
gid = 0
2022-12-09 13:49:26 +00:00
timeMin = 1
tutorialMax = 10
2023-03-01 13:05:56 +00:00
ip_id = {}
2022-11-03 14:07:08 +00:00
ip_dic = {}
2022-11-03 14:52:38 +00:00
ip_recent = {}
2022-11-09 14:09:00 +00:00
ip_control = {}
2022-11-10 12:12:06 +00:00
ip_time = {}
2022-11-10 13:54:44 +00:00
ip_tutorial = {}
ip_type = {}
2022-11-24 06:49:02 +00:00
2022-11-03 14:07:08 +00:00
def __int__(self):
self.levelNum = 200
self.typeNum = 100
2022-12-09 13:49:26 +00:00
self.timeMin = 1
2023-03-01 13:05:56 +00:00
self.gid = 0
def iniId(self, ip):
2023-03-08 08:06:08 +00:00
self.ip_id[str(ip)] = str(ip)
2023-03-08 07:50:05 +00:00
# self.gid += 1
# self.ip_dic[ip] = str(self.gid)
# return str(self.gid)
2023-03-08 08:06:08 +00:00
return str(ip)
2023-03-01 13:05:56 +00:00
def getId(self, ip):
return self.ip_dic[ip]
2022-11-03 14:07:08 +00:00
2022-11-24 06:49:02 +00:00
def getLevels(self, ip):
2022-11-03 14:07:08 +00:00
if ip not in self.ip_dic.keys():
self.ip_dic[ip] = []
2022-11-24 06:49:02 +00:00
levels = [random.randint(1, self.levelNum), random.randint(1, self.levelNum)]
2022-11-03 14:07:08 +00:00
while levels[0] in self.ip_dic[ip]:
2022-11-24 06:49:02 +00:00
levels[0] = random.randint(1, self.levelNum)
2022-11-03 14:07:08 +00:00
self.ip_dic[ip].append(levels[0])
while levels[1] in self.ip_dic[ip]:
levels[1] = random.randint(1, self.levelNum)
self.ip_dic[ip].append(levels[1])
2022-11-24 06:49:02 +00:00
self.ip_recent[ip] = [levels[0], levels[1]]
return ["lvl" + str(levels[0]), "lvl" + str(levels[1])]
# return ["test1","test2"]
2022-11-03 14:07:08 +00:00
def getTypeLevels(self, ip):
if ip not in self.ip_type.keys():
self.ip_type[ip] = []
result = [0, 0, 0]
# [c,k,r]
tmp = random.randint(0, self.typeNum - 1)
while "c" + str(tmp) in self.ip_type[ip]:
tmp = random.randint(0, self.typeNum - 1)
2023-03-01 13:05:56 +00:00
result[0] = "Collector-" + str(tmp)
self.ip_type[ip].append("c" + str(tmp))
tmp = random.randint(0, self.typeNum - 1)
while "k" + str(tmp) in self.ip_type[ip]:
tmp = random.randint(0, self.typeNum - 1)
2023-03-01 13:05:56 +00:00
result[1] = "Killer-" + str(tmp)
self.ip_type[ip].append("k" + str(tmp))
tmp = random.randint(0, self.typeNum - 1)
while "r" + str(tmp) in self.ip_type[ip]:
tmp = random.randint(0, self.typeNum - 1)
2023-03-01 13:05:56 +00:00
result[2] = "Runner-" + str(tmp)
self.ip_type[ip].append("r" + str(tmp))
self.ip_recent[ip] = result
return result
2022-11-24 06:49:02 +00:00
def getRecent(self, ip):
2022-11-03 14:52:38 +00:00
return self.ip_recent[ip]
2022-11-24 06:49:02 +00:00
def getLevel(self, ip):
2022-11-24 07:44:32 +00:00
if ip not in self.ip_dic.keys():
self.ip_dic[ip] = []
2022-11-24 06:49:02 +00:00
level = random.randint(1, self.levelNum)
while level in self.ip_dic[ip]:
level = random.randint(1, self.levelNum)
self.ip_dic[ip].append(level)
self.ip_recent[ip] = level
return "lvl" + str(level)
def setControl(self, ip, content):
2022-11-09 14:09:00 +00:00
if content == "A":
self.ip_control[ip] = 0
else:
self.ip_control[ip] = 1
2022-11-24 06:49:02 +00:00
def setTimes(self, ip):
self.ip_time[ip] = 0
2023-04-03 06:53:14 +00:00
# 1 For next step
# 0 For not next step
2022-11-24 06:49:02 +00:00
def getTimes(self, ip):
2022-12-09 13:41:03 +00:00
2022-11-10 12:12:06 +00:00
if ip not in self.ip_time.keys():
2022-12-09 13:41:03 +00:00
self.ip_time[ip] = 0
if self.ip_time[ip] >= self.timeMin:
return 1
2022-11-10 12:12:06 +00:00
else:
2022-12-09 13:41:03 +00:00
return 0
2022-11-24 06:49:02 +00:00
def addTimes(self, ip):
2022-11-10 12:12:06 +00:00
if ip not in self.ip_time.keys():
self.ip_time[ip] = 0
2022-11-24 06:49:02 +00:00
self.ip_time[ip] = self.ip_time[ip] + 1
2022-11-10 12:12:06 +00:00
return self.ip_time[ip]
2022-11-09 14:09:00 +00:00
2022-11-24 06:49:02 +00:00
def addTutorial(self, ip):
2022-11-10 13:54:44 +00:00
if ip not in self.ip_tutorial.keys():
self.ip_tutorial[ip] = 1
return 1
else:
2022-11-24 06:49:02 +00:00
if self.ip_tutorial[ip] > self.tutorialMax:
2022-11-10 13:54:44 +00:00
return 1
else:
self.ip_tutorial[ip] = self.ip_tutorial[ip] + 1
return self.ip_tutorial[ip]
2022-11-24 06:49:02 +00:00
def hasNextTutorial(self, ip):
2022-11-10 13:54:44 +00:00
if self.ip_tutorial[ip] == self.tutorialMax:
return 0
else:
return 1
2022-11-24 06:49:02 +00:00
def getControl(self, ip):
2022-11-09 14:09:00 +00:00
return self.ip_control[ip]
2022-11-24 06:49:02 +00:00
def write_csv(self, path, data):
# 确保目录存在
os.makedirs(os.path.dirname(path), exist_ok=True)
2022-11-15 05:46:21 +00:00
with open(path, 'a+', newline='') as f:
2022-11-03 14:07:08 +00:00
csv_write = csv.writer(f)
2022-11-24 06:49:02 +00:00
csv_write.writerow([*data, ''])