Skip to content

Commit

Permalink
crowd control WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Die4Ever committed May 8, 2022
1 parent 02bb139 commit 9117f3b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
29 changes: 29 additions & 0 deletions RCTRandoCC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using CrowdControl.Common;
using CrowdControl.Games.Packs;
using ConnectorType = CrowdControl.Common.ConnectorType;

public class RCTRando : SimpleTCPPack
{
public override string Host { get; } = "0.0.0.0";

public override ushort Port { get; } = 43385;

public RCTRando(IPlayer player, Func<CrowdControlBlock, bool> responseHandler, Action<object> statusUpdateHandler) : base(player, responseHandler, statusUpdateHandler) { }

public override Game Game { get; } = new Game(141, "RollerCoaster Tycoon Randomizer", "RCTRando", "PC", ConnectorType.SimpleTCPConnector);

public override List<Effect> Effects => new List<Effect>
{
//General Effects
new Effect("Something nice", "nice"),
new Effect("Something mean", "mean")
};

//Slider ranges need to be defined
/*public override List<ItemType> ItemTypes => new List<ItemType>(new[]
{
new ItemType("Money", "money1000", ItemType.Subtype.Slider, "{\"min\":1,\"max\":1000}")
});*/
}
97 changes: 97 additions & 0 deletions src/crowdcontrol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@

let cc_sock: Socket = null;
let cc_good: boolean = false;

function cc_onError(hadError: boolean) {
console.log('error in Crowd Control connection, attempting to reconnect');
if(cc_good) {
park.postMessage(
{type: 'blank', text: 'error in Crowd Control connection, attempting to reconnect'} as ParkMessageDesc
);
}
cc_good = false;
cc_connect();
}

function cc_onClose(hadError: boolean) {
console.log('Crowd Control connection closed, attempting to reconnect');
if(cc_good) {
park.postMessage(
{type: 'blank', text: 'Crowd Control connection closed, attempting to reconnect'} as ParkMessageDesc
);
}
cc_good = false;
cc_connect();
}

function cc_onData(message: string) {
let data: Object = null;
let resp: Object = null;

try {
// chop off the null-terminator
while(message[message.length-1] == '\0')
message = message.substring(0, message.length-1);
data = JSON.parse(message);
console.log("Crowd Control received data: ", data);
} catch(e) {
printException('error parsing Crowd Control request JSON: ' + message, e);
}

try {
resp = cc_req(data);
} catch(e) {
printException('error handling Crowd Control request: ' + message, e);
}

try {
let r: string = JSON.stringify(resp) + '\0';
console.log(message, r.length, r);
cc_sock.end(r);
cc_connect();
} catch(e) {
printException('error sending Crowd Control response to: ' + message, e);
}
}

function cc_connect() {
if(cc_sock) {
cc_sock.off('error', cc_onError);
cc_sock.off('close', cc_onClose);
cc_sock.destroy(null);
}

cc_sock = network.createSocket();

cc_sock.connect(43385, '127.0.0.1', function() {
console.log('Crowd Control connected!');
if(!cc_good) {
park.postMessage(
{type: 'blank', text: 'Crowd Control connected!'} as ParkMessageDesc
);
}
cc_good = true;
});
cc_sock.setNoDelay(true);

cc_sock.on('error', cc_onError);
cc_sock.on('close', cc_onClose);
cc_sock.on('data', cc_onData);
}

function init_crowdcontrol() {
cc_connect();
}

function cc_req(data) {
const Success = 0;
const Failed = 1;
const NotAvail = 2;
const TempFail = 3;

park.postMessage(
{type: 'blank', text: data.viewer + ' used ' + data.code} as ParkMessageDesc
);

return { id: data.id, status: Success };
}
2 changes: 2 additions & 0 deletions src/rctrando_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ function _main() {
else {
newGame();
}

init_crowdcontrol();
console.log(rando_name+" v"+rando_version+" finished startup\n ");
}

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"src/gui.ts",
"src/rctrando.ts",
"src/tests.ts",
"src/ridetypes.ts"
"src/ridetypes.ts",
"src/crowdcontrol.ts"
]
}

0 comments on commit 9117f3b

Please sign in to comment.