Skip to content

Commit

Permalink
Allow rolling cell
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Sep 7, 2024
1 parent 1ee2476 commit b1d6fc0
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 167 deletions.
8 changes: 4 additions & 4 deletions api/lib/src/event/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ final class TeamLeaveRequest extends ClientWorldEvent
}

@MappableClass()
final class RollObjectRequest extends ClientWorldEvent
with RollObjectRequestMappable {
final class CellRollRequest extends ClientWorldEvent
with CellRollRequestMappable {
final GlobalVectorDefinition cell;
final int object;
final int? object;

RollObjectRequest(this.cell, this.object);
CellRollRequest(this.cell, {this.object});
}

@MappableClass()
Expand Down
236 changes: 115 additions & 121 deletions api/lib/src/event/event.mapper.dart

Large diffs are not rendered by default.

52 changes: 31 additions & 21 deletions api/lib/src/event/process/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ bool isValidClientEvent(
switch (event) {
TeamJoinRequest() => state.info.teams.containsKey(event.team),
TeamLeaveRequest() => state.info.teams.containsKey(event.team),
RollObjectRequest() => event.object.inRange(
0,
state
.getTableOrDefault(event.cell.table)
.getCell(event.cell.location)
.objects
.length -
1),
CellRollRequest() => event.object?.inRange(
0,
state
.getTableOrDefault(event.cell.table)
.getCell(event.cell.location)
.objects
.length -
1) ??
true,
ShuffleCellRequest() => state
.getTableOrDefault(event.cell.table)
.cells
Expand Down Expand Up @@ -96,21 +97,30 @@ bool isValidClientEvent(
return (TeamJoined(channel, team), kAnyChannel);
case TeamLeaveRequest(team: final team):
return (TeamLeft(channel, team), kAnyChannel);
case RollObjectRequest():
case CellRollRequest():
final table = state.getTableOrDefault(event.cell.table);
final cell = table.cells[event.cell.location];
if (cell == null || !event.object.inRange(0, cell.objects.length - 1)) {
return null;
var cell = table.getCell(event.cell.location);
final random = Random();
GameObject roll(GameObject object) {
final figure = assetManager
.getPack(object.asset.namespace)
?.getFigure(object.asset.id);
if (figure == null || !figure.rollable) return object;
final variations = figure.variations.keys.toList();
if (variations.isEmpty) return object;
final picked = variations[random.nextInt(variations.length)];
return object.copyWith(variation: picked);
}
final objectIndex = event.object;
List<GameObject> objects;
if (objectIndex != null) {
final object = cell.objects[objectIndex];
objects = List<GameObject>.from(cell.objects)
..[objectIndex] = roll(object);
} else {
objects = cell.objects.map(roll).toList();
}
final object = cell.objects[event.object];
final figure = assetManager
.getPack(object.asset.namespace)
?.getFigure(object.asset.id);
if (figure == null || !figure.rollable) return null;
final variations = figure.variations.keys.toList();
if (variations.isEmpty) return null;
final picked = variations[Random().nextInt(variations.length)];
return (VariationChanged(event.cell, event.object, picked), kAnyChannel);
return (ObjectsChanged(event.cell, objects), kAnyChannel);
case ShuffleCellRequest():
final table = state.getTableOrDefault(event.cell.table);
final cell = table.cells[event.cell.location];
Expand Down
15 changes: 2 additions & 13 deletions api/lib/src/event/process/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ bool isValidServerEvent(ServerWorldEvent event, WorldState state) =>
event.info.packs.every((e) => event.packsSignature.containsKey(e)),
TeamJoined() => state.info.teams.containsKey(event.team),
TeamLeft() => state.info.teams.containsKey(event.team),
VariationChanged() => event.object.inRange(
0,
state
.getTableOrDefault(event.cell.table)
.getCell(event.cell.location)
.objects
.length -
1),
CellShuffled() => event.positions.every((e) => e.inRange(
0,
state
Expand Down Expand Up @@ -104,14 +96,11 @@ WorldState? processServerEvent(
allMembers[event.team] = members;
}
return state.copyWith(teamMembers: allMembers);
case VariationChanged():
case ObjectsChanged():
return state.mapTableOrDefault(event.cell.table, (table) {
final cell = table.cells[event.cell.location] ?? TableCell();
final object = cell.objects[event.object];
return table.copyWith.cells.replace(
event.cell.location,
cell.copyWith.objects.replace(
event.object, object.copyWith(variation: event.variation)));
event.cell.location, cell.copyWith(objects: event.objects));
});
case CellShuffled(positions: final positions):
return state.mapTableOrDefault(event.cell.table, (table) {
Expand Down
9 changes: 4 additions & 5 deletions api/lib/src/event/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ final class TeamLeft extends ServerWorldEvent with TeamLeftMappable {
}

@MappableClass()
final class VariationChanged extends ServerWorldEvent
with VariationChangedMappable {
final class ObjectsChanged extends ServerWorldEvent
with ObjectsChangedMappable {
final GlobalVectorDefinition cell;
final int object;
final String? variation;
final List<GameObject> objects;

VariationChanged(this.cell, this.object, this.variation);
ObjectsChanged(this.cell, this.objects);
}

@MappableClass()
Expand Down
24 changes: 24 additions & 0 deletions app/lib/board/cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,21 @@ class GameCell extends PositionComponent
game.camera.moveBy(delta);
}

bool anyRollable(ClientWorldState state) {
final assetManager = state.assetManager;
final global = toGlobalDefinition(state);
final local = global.location;
final cell = state.table.getCell(local);
if (!state.isCellVisible(global)) return false;
return cell.objects.any((object) =>
(assetManager
.getPack(object.asset.namespace)
?.getFigure(object.asset.id)
?.rollable ??
false) &&
state.isCellVisible(global));
}

@override
void onContextMenu(Vector2 position) {
game.showContextMenu(
Expand All @@ -262,6 +277,15 @@ class GameCell extends PositionComponent
onClose();
},
),
if (anyRollable(bloc.state))
ContextMenuButtonItem(
label: AppLocalizations.of(context).roll,
onPressed: () {
bloc.process(
CellRollRequest(toGlobalDefinition(bloc.state)));
onClose();
},
),
ContextMenuButtonItem(
label: AppLocalizations.of(context).remove,
onPressed: () {
Expand Down
2 changes: 1 addition & 1 deletion app/lib/board/hand/object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class GameObjectHandItem extends HandItem<(VectorDefinition, int, GameObject)> {
ContextMenuButtonItem(
label: AppLocalizations.of(context).roll,
onPressed: () {
bloc.process(RollObjectRequest(global, item.$2));
bloc.process(CellRollRequest(global, object: item.$2));
onClose();
},
),
Expand Down
4 changes: 2 additions & 2 deletions app/lib/pages/game/note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ class _GameNoteDialogState extends State<GameNoteDialog> {
listenable: _contentController,
builder: (context, _) => Markdown(
extensionSet: md.ExtensionSet(
md.ExtensionSet.gitHubFlavored.blockSyntaxes,
md.ExtensionSet.gitHubWeb.blockSyntaxes,
<md.InlineSyntax>[
md.EmojiSyntax(),
...md.ExtensionSet.gitHubFlavored.inlineSyntaxes
...md.ExtensionSet.gitHubWeb.inlineSyntaxes
],
),
data: _contentController.text)),
Expand Down

0 comments on commit b1d6fc0

Please sign in to comment.