private void trackHandler(APICmd cmd, Action <CmdId, object> handler) { var seqNr = unusedSequenceNumbers.Dequeue(); actionTracker[seqNr] = handler; GameAPI.Game_Request(cmd.cmd, seqNr, cmd.data); }
private Action <IdList> generateActivePlayerListHandler( AccountEffectList activeCrewAccountEffects, LedgerEffectList activePlayerAccountEffects, bool resetCrews = false) { Action <IdList> handler = playerIdList => { var incrementedFactions = new HashSet <int>(); var updatedCrewInfos = new Dictionary <int, CrewInfo>(); var count = 0; foreach (var item in playerIdList.list) { var id = new Id(item); var cmd = new APICmd(CmdId.Request_Player_Info) + id; broker.ExecuteCommand <PlayerInfo>(cmd, player => { updatedCrewInfos = UpdatedCrewManifest(updatedCrewInfos, player); var factionAccount = SafeGetAccount(AccountType.Crew, player.factionId, player); var playerAccount = SafeGetAccount(AccountType.Player, player.entityId, player); var factionExp = factionAccount.balances[ResourceType.Experience]; factionAccount.balances[ResourceType.Experience] = factionExp < player.exp ? player.exp : factionExp; if (!incrementedFactions.Contains(player.factionId)) { var factionAccountKey = Account.IdKeyFromInfo(AccountType.Crew, player.factionId); var tmpAcct = factionAccount; factionAccount = activeCrewAccountEffects.Aggregate(tmpAcct, (acc, x) => x(acc)); incrementedFactions.Add(player.factionId); } var baseLedger = new PlayerLedger(player, factionAccount, playerAccount); var adjustedLedger = activePlayerAccountEffects.Aggregate(baseLedger, (memo, effect) => effect(memo)); playerAccount = adjustedLedger.PersonalAccount; factionAccount = adjustedLedger.FactionAccount; SaveAccount(playerAccount); SaveAccount(factionAccount); normalizePlayer(adjustedLedger); count++; log(() => $"normalized {count}/{ playerIdList.list.Count}: {player.playerName}"); if (count == playerIdList.list.Count) { log(() => $"updating crews to: {Serializer.Serialize(CrewInfoTracker)}"); CrewInfoTracker = updatedCrewInfos; } }); } }; return(handler); }
public void transferBalance(int playerId, int amount, TransactionType type, Action <BalanceActionResult> callback) { var id = new Id(playerId); var cmd = new APICmd(CmdId.Request_Player_Info) + id; broker.ExecuteCommand <PlayerInfo>(cmd, (x, y) => { log(() => "****player info received"); BalanceActionResult result; var factionAccount = SafeGetAccount(AccountType.Crew, y.factionId, y); var playerAccount = SafeGetAccount(AccountType.Player, y.entityId, y); result = new BalanceActionResult() { crewBalance = factionAccount.balances[ResourceType.Points], playerBalance = playerAccount.balances[ResourceType.Points], amountRequested = amount, playerAccount = playerId, crewAccount = y.factionId, transactionType = type, playerName = y.playerName }; var requestedCrewBalance = result.crewBalance - amount; var requestedPlayerBalance = result.playerBalance + amount; if (requestedCrewBalance < 0) { result.reason = "insufficient crew balance"; result.succeeded = false; } else if (requestedPlayerBalance < 0) { result.reason = "insufficient player balance"; result.succeeded = false; } else { result.crewBalance = requestedCrewBalance; result.playerBalance = requestedPlayerBalance; result.succeeded = true; } log(() => $"***transaction: {Serializer.Serialize(result)}"); playerAccount.balances[ResourceType.Points] = result.playerBalance; SaveAccount(playerAccount); factionAccount.balances[ResourceType.Points] = result.crewBalance; SaveAccount(factionAccount); var ledger = new PlayerLedger(y, factionAccount, playerAccount); normalizePlayer(ledger); callback(result); }); }
private void getHelp(ChatInfo data, PString subcommand) { var msg = new IdMsgPrio() { msg = helpMessage, id = data.playerId, prio = 1 }; var cmd = new APICmd(CmdId.Request_ShowDialog_SinglePlayer, msg); broker.ExecuteCommand(cmd); }
private void normalizePlayerPoints(int playerId, Account playerAccount) { var infoSet = new PlayerInfoSet() { entityId = playerId, upgradePoints = playerAccount.balances[ResourceType.Points], }; var cmd = new APICmd(CmdId.Request_Player_SetPlayerInfo, infoSet); broker.ExecuteCommand(cmd); }
private Account incrementPointOnActiveFaction(Account factionAccount) { var key = int.Parse(factionAccount.id); log(() => $"*** incrementing faction points for {key}"); CrewInfo info; log(() => $"current crews: {Serializer.Serialize(CrewInfoTracker)}"); info = CrewInfoTracker.TryGetValue(key, out info) ? info : new CrewInfo(); var points = (info.crewMembers.Keys.Count + info.researchTeam.members.Count + (info.researchTeam.leader != null?1:0)); log(() => $"*** points: { points}"); log(() => $"*** members: { info.crewMembers.Keys.Count}"); log(() => $"*** cluster size: { info.researchTeam.members.Count}"); factionAccount.balances[ResourceType.Points] += points; var message = generateFactionPointMessage(info, points); if (info.crewMembers.Keys.Count == 0) { log(() => $"crew has no active members {Serializer.Serialize(info)}"); return(factionAccount); } var singlePlayerCrew = info.crewMembers.Keys.Count == 1; var singlePlayerId = info.crewMembers.Values.First().entityId; var factionId = int.Parse(factionAccount.id); var msg = new IdMsgPrio() { id = singlePlayerCrew ? singlePlayerId: factionId, msg = message, prio = 1 }; log(() => $"factionMessage: {message}"); log(() => $"crewInfo: {Serializer.Serialize(info)}"); var cmdId = singlePlayerCrew ? CmdId.Request_InGameMessage_SinglePlayer : CmdId.Request_InGameMessage_Faction; var cmd = new APICmd(cmdId, msg); broker.ExecuteCommand(cmd); info.researchTeam = new ResearchTeam(); CrewInfoTracker[key] = info; return(factionAccount); }
private void normalizePlayer(PlayerLedger ledger) { var infoSet = new PlayerInfoSet() { entityId = ledger.info.entityId, experiencePoints = ledger.FactionAccount.balances[ResourceType.Experience], upgradePoints = ledger.PersonalAccount.balances[ResourceType.Points], }; log(() => $"normalizing player: {ledger.info.playerName} with points: { infoSet.upgradePoints}"); log(() => $"factionAccount: {Serializer.Serialize(ledger.FactionAccount)} playerAccount: {Serializer.Serialize(ledger.PersonalAccount)}"); var cmd = new APICmd(CmdId.Request_Player_SetPlayerInfo, infoSet); broker.ExecuteCommand(cmd); }
private static void debugEntitySpawning(PString prefabName) { EntitySpawnInfo info = new EntitySpawnInfo { playfield = "Akua", pos = new PVector3(11.5354891F, 58.5829468F, -11.8831434F), rot = new PVector3(0F, 139.371F, 0F), name = "BA_Alien", type = 2, factionGroup = 2, factionId = 0, prefabName = "Infested-Test" }; var cmd = new APICmd(CmdId.Request_Entity_Spawn, info); broker.HandleCall(cmd); }
private void withdrawUpgradePoints(ChatInfo data, PString subcommand) { int value; value = int.TryParse(subcommand.pstr, out value) ? value : -1; if (value < 0) { return; } log(() => $"*** beginning transfer of {value}"); this.transferBalance(data.playerId, value, TransactionType.UpgradePoints, x => { log(() => "***** transfer response"); string balanceMessage; string factionMessage; if (x.succeeded) { balanceMessage = $"withdrawal successful\nyour crew has a balance of {x.crewBalance} points;\nyour personal balance is {x.playerBalance} points"; factionMessage = $"{x.playerName} has withdrawn {value} points from the crew bank"; var facmsg = new IdMsgPrio() { id = x.crewAccount, msg = factionMessage, prio = (byte)(x.succeeded ? 1 : 0) }; var cmd = new APICmd(CmdId.Request_InGameMessage_Faction, facmsg); broker.ExecuteCommand(cmd); } else { balanceMessage = $"withdrawal failed: {x.reason}"; } var msg = new IdMsgPrio() { id = data.playerId, msg = balanceMessage, prio = (byte)(x.succeeded ? 1:0) }; var outmsg = new APICmd(CmdId.Request_InGameMessage_SinglePlayer, msg); broker.ExecuteCommand(outmsg); }); }
private static void testBroker(PString pstr) { GameAPI.Console_Write($"**** executing broker test"); var cmd = new APICmd(CmdId.Request_ConsoleCommand, new Eleon.Modding.PString("SAY 'broker test'")); broker.HandleCall <object>(cmd, (x, y) => { switch (x) { case CmdId.Event_Ok: GameAPI.Console_Write("test successful"); break; case CmdId.Event_Error: GameAPI.Console_Write("test error"); break; } }); }
private void convertCredits(ChatInfo data, PString subcommand) { int value; value = int.TryParse(subcommand.pstr, out value) ? value : -1; if (value < 0) { return; } var result = convertPointsToCredits(data.playerId, value); var msg = new IdMsgPrio() { id = data.playerId, msg = $"you converted {result.amountRequested} points to credits", prio = 1 }; var outmsg = new APICmd(CmdId.Request_InGameMessage_SinglePlayer, msg); broker.ExecuteCommand(outmsg); }
private BalanceActionResult convertPointsToCredits(int playerId, int points) { var account = SafeGetAccount(AccountType.Player, playerId, null); log(() => $"*** before account: {Serializer.Serialize(account)}"); var result = new BalanceActionResult() { amountRequested = points, playerAccount = playerId, transactionType = TransactionType.Credits, playerBalance = account.balances[ResourceType.Points] }; if (result.playerBalance >= points) { var credits = points * settings.creditExchangeRate; account.balances[ResourceType.Points] -= points; result.playerBalance = account.balances[ResourceType.Points]; normalizePlayerPoints(playerId, account); SaveAccount(account); log(() => $"*** after account: {Serializer.Serialize(account)}"); var idCredits = new IdCredits() { id = playerId, credits = credits }; var cmd = new APICmd(CmdId.Request_Player_AddCredits, idCredits); broker.ExecuteCommand(cmd); result.succeeded = true; } else { result.succeeded = false; result.reason = "insuffucient point balance"; } return(result); }
private void checkCrewBalance(ChatInfo data, PString subcommand) { var playerId = new Id(data.playerId); var cmd = new APICmd(CmdId.Request_Player_Info, playerId); broker.ExecuteCommand <PlayerInfo>(cmd, (cmdId, playerInfo) => { var crewAccount = SafeGetAccount(AccountType.Crew, playerInfo.factionId, playerInfo); var crewBalance = crewAccount.balances[ResourceType.Points]; var personalAccount = SafeGetAccount(playerInfo); var personalBalance = personalAccount.balances[ResourceType.Points]; var balanceMessage = $"your crew has a balance of {crewBalance} points;\nyour personal balance is {personalBalance} points"; var msg = new IdMsgPrio() { id = playerInfo.entityId, msg = balanceMessage, prio = 1 }; var outmsg = new APICmd(CmdId.Request_InGameMessage_SinglePlayer, msg); broker.ExecuteCommand(outmsg); }); }
public void HandleCall <ResponseType>(APICmd cmd, Action <CmdId, ResponseType> handler) { Action <CmdId, object> outerHandler = (x, y) => handler(x, (ResponseType)y); trackHandler(cmd, outerHandler); }
public void HandleCall(APICmd cmd) { this.HandleCall <Object>(cmd, defaultHandler); }