ZontreckWebsite/lib/Packets.dart
2024-05-16 02:03:39 -07:00

311 lines
6.2 KiB
Dart

import 'dart:convert';
import 'package:libac_flutter/utils/Hashing.dart';
import 'package:zontreck/pages/OpenSim.dart';
import 'Settings.dart';
abstract class IPacket {
String encode();
HTTPMethod method();
String getType();
}
class S2CSimpleReplyPacket implements IPacket {
final bool done;
S2CSimpleReplyPacket({required this.done});
static S2CSimpleReplyPacket decode(String params) {
var map = json.decode(params);
return S2CSimpleReplyPacket(done: map['done'] as bool);
}
@override
String encode() {
return json.encode({"done": done});
}
@override
HTTPMethod method() {
return HTTPMethod.Get;
}
@override
String getType() {
return "S2CSimpleReply";
}
}
class C2SPerformSetupPacket implements IPacket {
final String PSK;
final String ClientPSK;
final String host;
final String user;
final String pass;
final String db;
C2SPerformSetupPacket(
{required this.PSK,
required this.ClientPSK,
required this.host,
required this.user,
required this.pass,
required this.db});
@override
String encode() {
return json.encode({
"psk": PSK,
"client": ClientPSK,
"host": host,
"user": user,
"pass": pass,
"db": db,
"type": getType()
});
}
static C2SPerformSetupPacket decode(String params) {
var map = json.decode(params);
return C2SPerformSetupPacket(
PSK: map['psk'] as String,
ClientPSK: map['client'] as String,
host: map['host'] as String,
user: map['user'] as String,
pass: map['pass'] as String,
db: map['db'] as String);
}
@override
HTTPMethod method() {
return HTTPMethod.Post;
}
@override
String getType() {
return "C2SPerformSetup";
}
}
class S2CPongPacket implements IPacket {
final String PSK;
final bool authorized;
final String user;
final String displayName;
final bool loggedIn;
final int totalUsers;
S2CPongPacket(
{required this.PSK,
required this.authorized,
required this.user,
required this.displayName,
required this.loggedIn,
required this.totalUsers});
@override
String encode() {
return json.encode({
"psk": PSK,
"authorized": authorized,
"type": getType(),
"login": loggedIn,
"user": user,
"display_name": displayName,
"user_count": totalUsers
});
}
@override
String getType() {
return "S2CPong";
}
@override
HTTPMethod method() {
return HTTPMethod.Get;
}
static S2CPongPacket decode(String params) {
var map = json.decode(params);
return S2CPongPacket(
PSK: map['psk'] as String,
authorized: map['authorized'] as bool,
user: map['user'] as String,
displayName: map['display_name'] as String,
loggedIn: map['login'] as bool,
totalUsers: map['user_count'] as int);
}
}
class NullPacket implements IPacket {
NullPacket();
@override
String encode() {
return json.encode({});
}
static NullPacket decode(String params) {
return NullPacket();
}
@override
String getType() {
return "NullPacket";
}
@override
HTTPMethod method() {
return HTTPMethod.Get;
}
}
class C2SPingPacket implements IPacket {
final String client;
C2SPingPacket({required this.client});
@override
HTTPMethod method() {
return HTTPMethod.Post;
}
@override
String getType() {
return "C2SPing";
}
static C2SPingPacket decode(String params) {
var map = json.decode(params);
return C2SPingPacket(client: map['client'] as String);
}
@override
String encode() {
return json.encode({"client": client});
}
}
class C2SRegisterAccountPacket implements IPacket {
final String firstName;
final String lastName;
final String passwordHash;
final String email;
final int level;
final String title;
final String clientKey;
C2SRegisterAccountPacket({
required this.firstName,
required this.lastName,
required this.passwordHash,
required this.email,
required this.level,
required this.title,
required this.clientKey,
});
@override
HTTPMethod method() {
return HTTPMethod.Post;
}
@override
String getType() {
return "C2SRegisterAccount";
}
@override
String encode() {
return json.encode({
"first": firstName,
"last": lastName,
"password": passwordHash,
"email": email,
"type": getType(),
"level": level,
"title": title,
"clientKey": clientKey,
});
}
static C2SRegisterAccountPacket decode(String params) {
var map = json.decode(params);
return C2SRegisterAccountPacket(
firstName: map['first'] as String,
lastName: map['last'] as String,
passwordHash: map['password'] as String,
email: map['email'] as String,
level: map['level'] as int,
title: map['title'] as String,
clientKey: map['clientKey'] as String);
}
}
class C2SLoginPacket implements IPacket {
final String first;
final String last;
final String password;
C2SLoginPacket(
{required this.first, required this.last, required this.password});
@override
HTTPMethod method() {
return HTTPMethod.Post;
}
@override
String getType() {
return "C2SLogin";
}
@override
String encode() {
return json.encode({
"first": first,
"last": last,
"type": getType(),
"password": Hashing.md5Hash(password)
});
}
}
class S2CLoginResponsePacket implements IPacket {
final bool loggedIn;
final String reason;
final User user;
S2CLoginResponsePacket(
{required this.loggedIn, required this.reason, required this.user});
@override
HTTPMethod method() {
return HTTPMethod.Get;
}
@override
String getType() {
return "S2CLoginResponse";
}
@override
String encode() {
return json
.encode({"type": getType(), "login": loggedIn, "reason": reason});
}
static S2CLoginResponsePacket decode(String params) {
var map = json.decode(params);
// Proceed now to constructing PODO
return S2CLoginResponsePacket(
loggedIn: map['login'] as bool,
reason: map['reason'] as String,
user: User.parseJson(json.encode(map['user'])));
}
}