Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pyyaml = "*"
rich = "*"
pyjwt = "*"
imaplib2 = "*"
flask = "*"
flask-socketio = "*"

[dev-packages]
autopep8 = "*"
Expand Down
18 changes: 18 additions & 0 deletions src/Config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from distutils.util import strtobool
import yaml, requests
from yaml.parser import ParserError
from rich import print
Expand All @@ -22,10 +23,27 @@ def __init__(self, configPath: str) -> None:
"""

self.accounts = {}

# Default webserver settings
self.webserver = {
"host": "0.0.0.0",
"port": 5000,
"enableWebServer": False
}
try:
configPath = self.__findConfig(configPath)
with open(configPath, "r", encoding='utf-8') as f:
config = yaml.safe_load(f)

# Process webserver settings from config.yaml
webserver = config.get("webserver")
if webserver != None:
self.webserver = {
"host": webserver["host"] if webserver["host"] != None else "0.0.0.0",
"port": int(webserver["port"] if webserver["port"] != None else "5000"),
"enableWebServer": strtobool(webserver["enablewebserver"] if webserver["enablewebserver"] != None else "False")
}
# Process accounts settings from config.yaml
accs = config.get("accounts")
for account in accs:
if "username" != accs[account]["username"]:
Expand Down
50 changes: 50 additions & 0 deletions src/WebServer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from flask_socketio import SocketIO,emit
from threading import Thread,Lock
from flask import Flask, render_template
from Stats import Stats


class WebServerThread(Thread):
def __init__(self, host, port,stats:Stats):
Thread.__init__(self)
self.host = host
self.port = port
self.app = Flask(__name__)
self.socketio = SocketIO(self.app,cors_allowed_origins="*")
self.stats = stats
self.thread = None
self.thread_lock = Lock()

def run(self):
# Default route
@self.app.route("/")
def default():
return render_template('index.html')


def handle_poro_data():
"""Sends the account data to front-end server"""
while True:
self.socketio.emit('my_response',{'stats':self.stats.accountData})
self.socketio.sleep(10)

@self.socketio.event
def connect():
with self.thread_lock:
if self.thread is None:
self.thread = self.socketio.start_background_task(handle_poro_data)
self.socketio.emit('my_response',{'stats':self.stats.accountData})

# Start the Flask web server
self.socketio.run(self.app, host=self.host, port=self.port)

class PoroWebServer(object):
def __init__(self, host, port,stats):
self.host = host
self.port = port
self.stats = stats

def start(self):
# Create and start the web server thread
web_thread = WebServerThread(self.host, self.port,self.stats)
web_thread.start()
16 changes: 14 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
from time import sleep, strftime, localtime
from Restarter import Restarter
from SharedData import SharedData

import os
from Stats import Stats
from VersionManager import VersionManager
from WebServer import PoroWebServer

CURRENT_VERSION = 1.4

Expand Down Expand Up @@ -45,18 +46,29 @@ def init() -> tuple[logging.Logger, Config]:
return log, config

def main(log: logging.Logger, config: Config):

host = config.webserver["host"]
port = config.webserver["port"]
enableWebServer = config.webserver["enableWebServer"]
farmThreads = {}
refreshLock = Lock()
locks = {"refreshLock": refreshLock}

sharedData = SharedData()
stats = Stats()

for account in config.accounts:
stats.initNewAccount(account)

restarter = Restarter(stats)

log.info(f"Starting the WebServer thread.")
if enableWebServer:
print(f"Web-server online: http://{host}:{port}/")
webServer = PoroWebServer(host,port,stats)
webServer.daemon = True
webServer.start()

log.info(f"Starting a GUI thread.")
guiThread = GuiThread(log, config, stats, locks)
guiThread.daemon = True
Expand Down
124 changes: 124 additions & 0 deletions src/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<!DOCTYPE html>
<html lang="en-us">

<head>
<meta charset="UTF-8">
<title>Capsule Farmer Evolved</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.2/socket.io.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="https://raw.githubusercontent.com/LeagueOfPoro/CapsuleFarmerEvolved/master/poro.ico">
<style>
body {
background-color: #141213;
}
table {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
td,th {
border: 1px solid #ddd;
padding: 8px;
}
tr {
color: #f2f2f2;
}
th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
color: white;
}
.center {
margin: auto;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 10px;
}
img {
width: 50%;
}
</style>
</head>

<body>
<div class="center">
<img src="https://raw.githubusercontent.com/LeagueOfPoro/CapsuleFarmerEvolved/master/.github/banner.png" alt="Capsule Farmer Evolved"/>
<table id="statsTable">
<thead>
<tr>
<th>Account</th>
<th>Status</th>
<th>Live Matches</th>
<th>Heartbeat</th>
<th>Last Drop</th>
<th>Session Drops</th>
<th>Lifetime Drops</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>

<script type="text/javascript">
var socket = io.connect('http://' + document.domain + ':' + location.port);

// Listen for the 'my_response' event
socket.on('my_response', function (data) {
var responseElement = document.getElementById('response');
displayStatsTable(data.stats);
});

// Display the stats in a table
function displayStatsTable(stats) {
var tableBody = document.querySelector('#statsTable tbody');
tableBody.innerHTML = '';

for (var username in stats) {
var statsRow = document.createElement('tr');
var userStats = stats[username];

var usernameCell = document.createElement('td');
usernameCell.innerText = username;
statsRow.appendChild(usernameCell);

var statusCell = document.createElement('td');
//force typing in js
const status = userStats.status + ""
const statusList = status.replace("[", "").split("]")
statusCell.style.color = statusList[0]
statusCell.innerText = statusList[1]
statsRow.appendChild(statusCell);

var liveMatchesCell = document.createElement('td');
liveMatchesCell.innerText = userStats.liveMatches;
statsRow.appendChild(liveMatchesCell);

var lastCheckCell = document.createElement('td');
lastCheckCell.innerText = userStats.lastCheck;
statsRow.appendChild(lastCheckCell);

var lastDropCell = document.createElement('td');
lastDropCell.innerText = userStats.lastDrop;
statsRow.appendChild(lastDropCell);

var sessionDropsCell = document.createElement('td');
sessionDropsCell.innerText = userStats.sessionDrops;
statsRow.appendChild(sessionDropsCell);

var totalDropsCell = document.createElement('td');
totalDropsCell.innerText = userStats.totalDrops;
statsRow.appendChild(totalDropsCell);



tableBody.appendChild(statsRow);
}
}
</script>
</body>

</html>