Set problem API make by PHP
This commit is contained in:
parent
3f8aeadb71
commit
4395108584
22
db.php
Normal file
22
db.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
class db
|
||||||
|
{
|
||||||
|
static function connect(){
|
||||||
|
$db_ip = '';
|
||||||
|
$db_user = '';
|
||||||
|
$db_password = '';
|
||||||
|
$db_select = '';
|
||||||
|
$db_charset = '';
|
||||||
|
$DSN="mysql:host=$db_ip;dbname=$db_select;charset=$db_charset";
|
||||||
|
try{
|
||||||
|
$db=new PDO($DSN,$db_user,$db_password);
|
||||||
|
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
|
||||||
|
}
|
||||||
|
catch(PDOException $e){
|
||||||
|
echo "連接失敗 : " . $e->getMessage();
|
||||||
|
}
|
||||||
|
return $db;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
9
main.py
9
main.py
@ -1,5 +1,6 @@
|
|||||||
import socket,threading,sys,random,multiprocessing,time
|
import socket,threading,sys,random,multiprocessing,time
|
||||||
import draw
|
import draw
|
||||||
|
import requests,json # Get problem
|
||||||
MAX = 1024
|
MAX = 1024
|
||||||
|
|
||||||
class Server:
|
class Server:
|
||||||
@ -87,11 +88,10 @@ class Room:
|
|||||||
startFlag = False
|
startFlag = False
|
||||||
sockList = [] # Client's sock list
|
sockList = [] # Client's sock list
|
||||||
|
|
||||||
problem = "apple"
|
|
||||||
|
|
||||||
def __init__(self,ip,portNum):
|
def __init__(self,ip,portNum):
|
||||||
self.ip = ip
|
self.ip = ip
|
||||||
self.portNum = portNum
|
self.portNum = portNum
|
||||||
|
self.problem = self.getProblem()
|
||||||
def start(self):
|
def start(self):
|
||||||
# Build a room's socket to start game
|
# Build a room's socket to start game
|
||||||
listensock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
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 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
sock.connect((self.ip,self.portNum))
|
sock.connect((self.ip,self.portNum))
|
||||||
return sock
|
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):
|
def receiveData(self,sock):
|
||||||
# room's socket receive MAINCLIENT'S mouse position.
|
# room's socket receive MAINCLIENT'S mouse position.
|
||||||
# And send this MOUSE position to other client.
|
# And send this MOUSE position to other client.
|
||||||
|
|||||||
15
pyGartic.php
Normal file
15
pyGartic.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
require('db.php');
|
||||||
|
|
||||||
|
$sql = 'SELECT * FROM `problem` WHERE 1 ORDER BY RAND() LIMIT 1';
|
||||||
|
$result = db::connect()->prepare($sql);
|
||||||
|
$result->execute();
|
||||||
|
|
||||||
|
$data = $result->fetch();
|
||||||
|
|
||||||
|
$ans = json_encode([
|
||||||
|
'id' => $data[0],
|
||||||
|
'problem' => $data[1]
|
||||||
|
]);
|
||||||
|
echo $ans;
|
||||||
|
?>
|
||||||
Loading…
Reference in New Issue
Block a user