diff --git a/db.php b/db.php new file mode 100644 index 0000000..e96b9bb --- /dev/null +++ b/db.php @@ -0,0 +1,22 @@ +setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); + } + catch(PDOException $e){ + echo "連接失敗 : " . $e->getMessage(); + } + return $db; + } +} + +?> \ No newline at end of file diff --git a/main.py b/main.py index 5c6b91e..54cadd3 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import socket,threading,sys,random,multiprocessing,time import draw +import requests,json # Get problem MAX = 1024 class Server: @@ -87,11 +88,10 @@ class Room: startFlag = False sockList = [] # Client's sock list - problem = "apple" - def __init__(self,ip,portNum): self.ip = ip self.portNum = portNum + self.problem = self.getProblem() def start(self): # Build a room's socket to start game listensock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) @@ -113,7 +113,10 @@ class Room: sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.connect((self.ip,self.portNum)) return sock - + def getProblem(self): + jsonResponse = requests.get('https://ncnu-hungrycat.com/pyGartic.php') + jsonData = json.loads(jsonResponse.text) + return jsonData['problem'] def receiveData(self,sock): # room's socket receive MAINCLIENT'S mouse position. # And send this MOUSE position to other client. diff --git a/pyGartic.php b/pyGartic.php new file mode 100644 index 0000000..f2d1f01 --- /dev/null +++ b/pyGartic.php @@ -0,0 +1,15 @@ +prepare($sql); + $result->execute(); + + $data = $result->fetch(); + + $ans = json_encode([ + 'id' => $data[0], + 'problem' => $data[1] + ]); + echo $ans; +?> \ No newline at end of file