Commit 7ede522d authored by Dennis Willers's avatar Dennis Willers 🏀

Initial commit

parents
[meta]
name = "Voting"
author = "RoboTec13"
category = "Race"
version = "1.0"
[script]
imports = ["Icons.as", "Formatting.as"]
[Setting category="Window" name="Change Voting Position"]
bool Setting_View_Change_Position = false;
[Setting category="Window" name="Position X / Position Y"]
vec2 Setting_View_Voting_Pos = vec2(Setting_View_Width-Setting_View_Voting_Size.x, 42);
int Setting_View_Width = Draw::GetWidth();
[Setting category="Window" name="Width / Height"]
vec2 Setting_View_Voting_Size = vec2(138,84);
[Setting category="Chat" name="insert chat message after click on vote"]
bool Setting_Chat_Show_Voting = true;
Net::Socket@ sock;
bool sockIsConnected = false;
bool initSocketConnection()
{
// Create a new socket.
@sock = Net::Socket();
if (!sock.Connect("willers.digital", 3201)) {
// If it failed, there was some socket error. (This is not necessarily
// a connection error!)
print("Couldn't initiate socket connection.");
return false;
}
// Wait until we are connected. This is indicated by whether we can write
// to the socket.
while (!sock.CanWrite()) {
yield();
}
sockIsConnected = true;
startnew(waitingForResponse);
return true;
}
void waitingForResponse() {
while (true) {
string line;
if (sock.ReadLine(line)) {
auto js = Json::Parse(line);
handleMessage(js);
}
yield();
}
}
void requestSocketServer(const Json::Value &in js) {
// Send raw data (as a string) to the server.
if (!sock.WriteRaw(Json::Write(js))) {
// If this fails, the socket might not be open. Something is wrong!
print("Couldn't send data.");
return;
}
}
void handleMessage(const Json::Value &in js)
{
if (Json::Write(js) != "null") {
setVotingInformation(js);
}
}
void closeSocket() {
sock.Close();
sockIsConnected = false;
}
string currentChatMessage = "";
void readLastChatMessage() {
while(true) {
if (getNetwork() !is null) {
MwFastBuffer<wstring> chatHistory = getNetwork().ChatHistoryLines;
if (chatHistory.get_Length() > 0) {
string chatMessage = chatHistory[0];
if (chatMessage != currentChatMessage && !hasVoteTimeout) {
currentChatMessage = chatMessage;
if (checkIfMessageIsByCurrentClient()) {
sendRequestIfMessageMatchForVoting();
}
}
}
yield();
}
}
}
bool checkIfMessageIsByCurrentClient() {
string user = currentChatMessage.SubStr(3,currentChatMessage.IndexOf('$>]')-3);
if (user == getName()) {
return true;
}
return false;
}
void sendRequestIfMessageMatchForVoting() {
string message = currentChatMessage.SubStr(currentChatMessage.IndexOf('$>] ')+4);
if (message == '+++') newSelectedVoteByChat(3, 100);
if (message == '++') newSelectedVoteByChat(2, 80);
if (message == '+') newSelectedVoteByChat(1, 60);
if (message == '-') newSelectedVoteByChat(4, 40);
if (message == '--') newSelectedVoteByChat(5, 20);
if (message == '---') newSelectedVoteByChat(6, 0);
}
void printVote(int vote) {
if (Setting_Chat_Show_Voting) {
CGamePlaygroundInterface@ playgroundInterface = getPlaygroundInterface();
playgroundInterface.ChatEntry = getVoteText(vote);
}
}
string getVoteText(int vote) {
string startText = "";
switch(vote) {
case 0: startText = "$F00   "; break;
case 20: startText = "$F00  "; break;
case 40: startText = "$F00 "; break;
case 60: startText = "$0F0 "; break;
case 80: startText = "$0F0  "; break;
case 100: startText = "$0F0   "; break;
}
return startText;
}
\ No newline at end of file
void RenderMenu()
{
if (UI::MenuItem("\\$ff0" + Icons::Star + "\\$z Voting", "", !hideme)) {
hideme = !hideme;
}
// add menu items to go to the right Tutorial Steps
/*if (UI::BeginMenu(ColoredString("$ff0" + Icons::Star + "$fff Voting"))) {
if (UI::MenuItem(ColoredString("$f00" + Icons::PowerOff + "$fff Hide"))) hideme = !hideme;
if (UI::MenuItem(ColoredString("$0f0" + Icons::Plus + Icons::Plus + Icons::Plus))) print("+++");
if (UI::MenuItem(ColoredString("$0f0" + Icons::Plus + Icons::Plus))) print("++");
if (UI::MenuItem(ColoredString("$0f0" + Icons::Plus))) print("+");
if (UI::MenuItem(ColoredString("$f00" + Icons::Minus + Icons::Minus + Icons::Minus))) print("---");
if (UI::MenuItem(ColoredString("$f00" + Icons::Minus + Icons::Minus))) print("--");
if (UI::MenuItem(ColoredString("$f00" + Icons::Minus))) print("-");
UI::EndMenu();
}*/
}
\ No newline at end of file
bool hasVotingInformation = false;
bool hasVoteTimeout = false;
int vote = -1;
int votes = -1;
int average = -1;
int selectedValue = -1;
int hoverIcon = 0;
string greenValue1 = "$080";
string greenValue2 = "$080";
string greenValue3 = "$080";
string redValue1 = "$800";
string redValue2 = "$800";
string redValue3 = "$800";
void Render() {
if (getRootMap() !is null && hasVotingInformation && !hideme) {
// Check Width
if (Setting_View_Voting_Pos.x <= 0) {
Setting_View_Width = Draw::GetWidth();
Setting_View_Voting_Pos = vec2(Setting_View_Width - Setting_View_Voting_Size.x, Setting_View_Voting_Pos.y);
}
UI::PushStyleColor(UI::Col::HeaderHovered, vec4(0,0,0,0));
UI::PushStyleColor(UI::Col::HeaderActive, vec4(0,0,0,0));
if (Setting_View_Change_Position) {
UI::Begin("Voting", UI::WindowFlags::NoCollapse | UI::WindowFlags::NoResize);
setVotingPosition();
}
else UI::Begin("Voting", UI::WindowFlags::NoCollapse | UI::WindowFlags::NoResize | UI::WindowFlags::NoTitleBar);
showVotes();
// Change Standard Position
UI::SetWindowPos(Setting_View_Voting_Pos);
if (Setting_View_Change_Position) UI::SetWindowSize(vec2(Setting_View_Voting_Size.x, Setting_View_Voting_Size.y + 25));
else UI::SetWindowSize(Setting_View_Voting_Size);
showAverageAndStars();
showVotingPanel();
UI::End();
UI::PopStyleColor();
UI::PopStyleColor();
}
}
void showAverageAndStars() {
UI::BeginTable("averageAndStars", 2, UI::TableFlags::SizingStretchProp);
UI::TableSetupColumn("average", UI::TableFlags::SizingStretchProp, 17.0f, 0);
UI::TableSetupColumn("stars", UI::TableFlags::SizingStretchProp, 55.0f, 1);
UI::TableNextColumn();
UI::Markdown("**" + average + "**");
UI::TableNextColumn();
UI::Text(getStars());
UI::EndTable();
}
string getStars() {
if (average <= 8) return "\\$FF0"+Icons::StarO+Icons::StarO+Icons::StarO+Icons::StarO+Icons::StarO;
else if (average <= 17) return "\\$FF0"+Icons::StarHalfO+Icons::StarO+Icons::StarO+Icons::StarO+Icons::StarO;
else if (average <= 26) return "\\$FF0"+Icons::Star+Icons::StarO+Icons::StarO+Icons::StarO+Icons::StarO;
else if (average <= 35) return "\\$FF0"+Icons::Star+Icons::StarHalfO+Icons::StarO+Icons::StarO+Icons::StarO;
else if (average <= 44) return "\\$FF0"+Icons::Star+Icons::Star+Icons::StarO+Icons::StarO+Icons::StarO;
else if (average <= 54) return "\\$FF0"+Icons::Star+Icons::Star+Icons::StarHalfO+Icons::StarO+Icons::StarO;
else if (average <= 63) return "\\$FF0"+Icons::Star+Icons::Star+Icons::Star+Icons::StarO+Icons::StarO;
else if (average <= 73) return "\\$FF0"+Icons::Star+Icons::Star+Icons::Star+Icons::StarHalfO+Icons::StarO;
else if (average <= 82) return "\\$FF0"+Icons::Star+Icons::Star+Icons::Star+Icons::Star+Icons::StarO;
else if (average <= 91) return "\\$FF0"+Icons::Star+Icons::Star+Icons::Star+Icons::Star+Icons::StarHalfO;
else return "\\$FF0"+Icons::Star+Icons::Star+Icons::Star+Icons::Star+Icons::Star;
}
void showVotingPanel() {
if(UI::IsOverlayShown()) {
UI::BeginTable("votingPannel", 6, UI::TableFlags::SizingStretchProp);
UI::TableSetupColumn("+++", UI::TableFlags::SizingStretchProp, 11.0f, 0);
UI::TableSetupColumn("++", UI::TableFlags::SizingStretchProp, 11.0f, 1);
UI::TableSetupColumn("+", UI::TableFlags::SizingStretchProp, 13.0f, 2);
UI::TableSetupColumn("---", UI::TableFlags::SizingStretchProp, 11.0f, 3);
UI::TableSetupColumn("--", UI::TableFlags::SizingStretchProp, 11.0f, 4);
UI::TableSetupColumn("-", UI::TableFlags::SizingStretchProp, 15.0f, 5);
UI::TableNextColumn();
if (UI::Selectable("\\"+redValue3 + Icons::Minus+" ", false, UI::SelectableFlags::None)) selectValue(6, 0, true);
isItemHovered(6, "$F00", "$800");
UI::TableNextColumn();
if (UI::Selectable("\\"+redValue2 + Icons::Minus+" ", false, UI::SelectableFlags::None)) selectValue(5, 20, true);
isItemHovered(5, "$F00", "$800");
UI::TableNextColumn();
if (UI::Selectable("\\"+redValue1 + Icons::Minus, false, UI::SelectableFlags::None)) selectValue(4, 40, true);
isItemHovered(4, "$F00", "$800");
UI::TableNextColumn();
if (UI::Selectable("\\"+greenValue1 + Icons::Plus, false, UI::SelectableFlags::None)) selectValue(1, 60, true);
isItemHovered(1, "$0F0", "$080");
UI::TableNextColumn();
if (UI::Selectable("\\"+greenValue2 + Icons::Plus+" ", false, UI::SelectableFlags::None)) selectValue(2, 80, true);
isItemHovered(2, "$0F0", "$080");
UI::TableNextColumn();
if(UI::Selectable("\\"+greenValue3 + Icons::Plus+" ", false, UI::SelectableFlags::None)) selectValue(3, 100, true);
isItemHovered(3, "$0F0", "$080");
UI::EndTable();
} else {
if (vote != -1) {
UI::Markdown("**Vote** \\" + getVoteText(vote));
} else {
UI::Markdown("**Vote** No Vote");
}
}
}
void showVotes() {
if (votes == 1) {
UI::Markdown(Icons::User + " **1 Vote**");
} else {
UI::Markdown(Icons::User + " **" + votes + " Votes**");
}
}
void setVotingPosition() {
if (UI::IsItemHovered()) {
Setting_View_Voting_Pos = UI::GetWindowPos();
}
}
void isItemHovered(int icon, string value1, string value2) {
if(UI::IsItemHovered()) {
if (hoverIcon != 0) setAllVotesToHoverColor();
hoverIcon = icon;
changeHoverValue(icon, value1);
}
else {
if (hoverIcon == icon) {
hoverIcon = 0;
setAllVotesToHoverColor();
setSelectedValue();
}
}
}
void setAllVotesToHoverColor() {
changeHoverValue(3, "$080");
changeHoverValue(6, "$800");
}
string getShowedIconColor() {
if (selectedValue > 0 && selectedValue < 4) return "$0F0";
if (selectedValue > 3 && selectedValue < 7) return "$F00";
return "$000";
}
void changeHoverValue(int icon, string value) {
switch(icon) {
case 3: greenValue3 = value;
case 2: greenValue2 = value;
case 1: greenValue1 = value; return;
case 6: redValue3 = value;
case 5: redValue2 = value;
case 4: redValue1 = value; return;
}
return;
}
void convertVoteToSelectedValue(int vote) {
switch(vote) {
case 0: selectedValue = 6; break;
case 20: selectedValue = 5; break;
case 40: selectedValue = 4; break;
case 60: selectedValue = 1; break;
case 80: selectedValue = 2; break;
case 100: selectedValue = 3; break;
}
setSelectedValue();
}
void setSelectedValue() {
if (selectedValue != -1) {
changeHoverValue(selectedValue, getShowedIconColor());
}
}
void newSelectedVoteByChat(int newSelectedValue, int vote) {
if (!hasVoteTimeout) {
setAllVotesToHoverColor();
selectValue(newSelectedValue, vote, false);
}
}
void selectValue(int newSelectedValue, int vote, bool isPrintVote) {
if (!hasVoteTimeout && selectedValue != newSelectedValue) {
selectedValue = newSelectedValue;
requestSocketServer(jsonBuilderSetVote(vote));
if (isPrintVote) printVote(vote);
startnew(setVoteTimeout);
}
}
void setVoteTimeout() {
hasVoteTimeout = true;
sleep(1000);
hasVoteTimeout = false;
}
\ No newline at end of file
int standardtTimeout = 840; // Server auto disconect client after 15 Minutes. Here we reload a connection after 14 Minutes.
int timeout = standardtTimeout;
void resetTimeout() {
timeout = standardtTimeout;
}
void countDownTimeout() {
while (true) {
timeout = timeout - 1;
sleep(1000);
}
}
bool isTimeoutReached() {
return timeout <= 0;
}
\ No newline at end of file
Json::Value jsonBuilderSetVote(int vote) {
auto jsonObj = Json::Object();
jsonObj["vote"] = vote;
jsonObj["mapId"] = getMapUid();
jsonObj["userId"] = getLogin();
jsonObj["name"] = getNameForUi();
return jsonObj;
}
Json::Value jsonBuilderGetMapInfo() {
auto jsonObj = Json::Object();
jsonObj["mapId"] = getMapUid();
jsonObj["userId"] = getLogin();
jsonObj["name"] = getNameForUi();
return jsonObj;
}
void setVotingInformation(const Json::Value &in js) {
if (js.HasKey('votes')) votes = js['votes'];
if (js.HasKey('average')) average = js['average'];
if (js.HasKey('vote')) {
vote = js['vote'];
convertVoteToSelectedValue(js['vote']);
}
hasVotingInformation = true;
}
\ No newline at end of file
CTrackMania@ getTmApp() {
return cast<CTrackMania>(GetApp());
}
CTrackManiaNetwork@ getNetwork() {
return cast<CTrackManiaNetwork>(getTmApp().Network);
}
CTrackManiaPlayerInfo@ getPlayerInfo() {
return getNetwork().PlayerInfo;
}
string getLogin() {
return getPlayerInfo().Login;
}
string getName() {
return getPlayerInfo().Name;
}
CGameCtnChallenge@ getRootMap() {
return getTmApp().RootMap;
}
CGameCtnChallengeInfo@ getMapInfo() {
return getRootMap().MapInfo;
}
string getMapUid() {
return getMapInfo().MapUid;
}
string getNameForUi() {
return getMapInfo().NameForUi;
}
CSmArenaClient@ getPlayground() {
return cast<CSmArenaClient>(getTmApp().CurrentPlayground);
}
CGamePlaygroundInterface@ getPlaygroundInterface() {
return cast<CGamePlaygroundInterface>(getPlayground().Interface);
}
\ No newline at end of file
#name "Voting"
#author "RoboTec13"
#category "Race"
bool hideme = false;
string currentMapUid = "";
void Main()
{
startnew(countDownTimeout);
startnew(readLastChatMessage);
while (true) {
if (getRootMap() !is null) {
if (!sockIsConnected) {
loadMapVoteInfo();
}
if (isTimeoutReached()) {
disconnectFromSocketServer();
loadMapVoteInfo();
}
if (currentMapUid != getMapUid()) {
disconnectFromSocketServer();
}
} else {
disconnectFromSocketServer();
currentMapUid = "";
}
sleep(1000);
}
}
void loadMapVoteInfo() {
if (initSocketConnection()) {
requestSocketServer(jsonBuilderGetMapInfo());
resetTimeout();
currentMapUid = getMapUid();
}
}
void disconnectFromSocketServer() {
if (sockIsConnected) {
closeSocket();
vote = -1;
votes = -1;
average = -1;
selectedValue = -1;
hoverIcon = 0;
setAllVotesToHoverColor();
hasVotingInformation = false;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment