Example #1
0
    updateActivityInMention(kind: TimelineActivityKind, status: Tweet, from: TwitterUser): [List<Item>, number] {
        const status_id = status.id;
        const index = this.mention.findIndex(item => {
            if (item instanceof TimelineActivity) {
                return item.kind === kind && item.status.id === status_id;
            } else {
                return false;
            }
        });

        const next_focus_index =
            this.kind === 'mention' && (index === -1 || index < this.focus_index) ?
                this.nextFocusIndex(this.mention.size + 1) : this.focus_index;

        if (index === -1) {
            return [this.mention.unshift(new TimelineActivity(kind, status, [from])), next_focus_index];
        } else {
            const will_updated = this.mention.get(index);
            if (will_updated instanceof TimelineActivity) {
                const updated = will_updated.update(status, from);
                return [this.mention.delete(index).unshift(updated), next_focus_index];
            } else {
                log.error('Invalid activity for update:', will_updated);
                return [this.mention, next_focus_index];
            }
        }
    }
Example #2
0
function findVerticalCollisions(timelineDuration: number, stacks: List<List<Record<Annotation>>>, stackStartIndex: number, annotations: List<Record<Annotation>>) {
  const collisions: {annotation: Record<Annotation>, index: number, annotationStackIndex: number}[] = []
  let spread = Set(annotations)
  for(let i = stackStartIndex; i < stacks.size; i++) {
    const stack = stacks.get(i)!
    const spreadList = spread.toList()
    const withInsertions = stack.concat(spreadList).sort(recordSort)
    const insertionIndices = spreadList.map(a => {
      return withInsertions.findIndex(wi => {
        return wi.get('id', null) === a.get('id', null)
      })
    })
    const hCollisions = findHorizontalCollisions(timelineDuration, withInsertions, insertionIndices, true)
    spread = spread.union(hCollisions.map(({annotation}) => annotation))
    // Get actual indices of horiz. collisions
    stack.forEach((annotation, annotationIndex) => {
      const isCollision = hCollisions.find(hColl => {
        return hColl.annotation.get('id', null) === annotation.get('id', null)
      }) !== undefined
      if(isCollision) {
        collisions.push({annotation, index: annotationIndex, annotationStackIndex: i})
      }
    })
  }
  return collisions
}
 private getDesiredPrice(): number {
   return this.integerifyCash(
     this.lines
       .get(0) // First line contains the desiredPrice
       .match(/(?:\$)(\S*)/)[1] // Match non-space characters after dollar sign
   );
 }
Example #4
0
function serializeMapItemList(list: List<MapItem>): string[] {
  const result: string[] = []
  for (let row = 0; row < FIELD_BLOCK_SIZE; row += 1) {
    const array: string[] = []
    for (let col = 0; col < FIELD_BLOCK_SIZE; col += 1) {
      const { type, hex } = list.get(row * FIELD_BLOCK_SIZE + col)
      if (type === 'B') {
        if (hex > 0) {
          array.push('B' + hex.toString(16))
        } else {
          array.push('X')
        }
      } else if (type === 'E') {
        array.push('E')
      } else if (type === 'R') {
        array.push('R')
      } else if (type === 'S') {
        array.push('S')
      } else if (type === 'T') {
        if (hex > 0) {
          array.push('T' + hex.toString(16))
        } else {
          array.push('X')
        }
      } else if (type === 'F') {
        array.push('F')
      } else {
        array.push('X')
      }
    }
    result.push(array.map(s => s.padEnd(3)).join(''))
  }
  return result
}
Example #5
0
 indices.forEach(i => {
   // Collision to right
   n = 1
   nextIndex = i+n
   while(nextIndex < list.size && !isInIndices(nextIndex) && recordHasCollision(list.get(i)!, list.get(nextIndex)!, timelineDuration)) {
     collisions.push({annotation: list.get(nextIndex)!, index: nextIndex})
     nextIndex = i+(++n)
   }
   // Collision to left
   n = 1
   nextIndex = i-n
   while(nextIndex >= 0 && !isInIndices(nextIndex) && recordHasCollision(list.get(i)!, list.get(nextIndex)!, timelineDuration)) {
     collisions.push({annotation: list.get(nextIndex)!, index: nextIndex})
     nextIndex = i-(++n)
   }
 })
Example #6
0
function fitOptimized(timelineDuration: number, annotationStacks: List<List<Record<Annotation>>>, annotations: List<Record<Annotation>>) {
  const fittedStacks: Array<List<Record<Annotation>>> = []
  let rest = annotations
  let stackCounter = 0
  while(rest.size > 0) {
    const stack = stackCounter < annotationStacks.size ? annotationStacks.get(stackCounter)! : List([])
    let updatedStack = stack
    const collisions = []
    const justFitted = []
    for(let j = 0; j < rest.size; j++) {
      const annotation = rest.get(j)!
      // TODO: instead of concat+sort+binarysearch, just insert sorted
      const withInsertion = updatedStack.push(annotation).sort(recordSort)
      const insertionIndex = binarySearch(withInsertion, withInsertion.size, (list, k) => {
        return list.getIn([k, 'utc_timestamp'])
      }, annotation.get('utc_timestamp', null))

      if(findHorizontalCollisions(timelineDuration, withInsertion, List([insertionIndex])).length > 0) {
        collisions.push(annotation)
      } else {
        justFitted.push(annotation)
        updatedStack = withInsertion
      }
    }
    fittedStacks.push(List(justFitted.sort(recordSort)))
    rest = List(collisions)
    stackCounter++
  }

  return List(fittedStacks)
}
Example #7
0
function findFirstNonEmptyIncome(incomes: List<Income>): number {
  for (let i = 0; i < incomes.size; ++i) {
    let income = incomes.get(i);
    if (income.spentForeignAmount.lessThan(income.foreignAmount)) {
      return i;
    }
  }
  return incomes.size - 1;
}
Example #8
0
export function moveInList<T>(list: List<T>, itemIndex: number, insertPoint: number): List<T> {
  var n = list.size;
  if (itemIndex < 0 || itemIndex >= n) throw new Error('itemIndex out of range');
  if (insertPoint < 0 || insertPoint > n) throw new Error('insertPoint out of range');
  var newArray: T[] = [];
  list.forEach((value, i) => {
    if (i === insertPoint) newArray.push(list.get(itemIndex));
    if (i !== itemIndex) newArray.push(value);
  });
  if (n === insertPoint) newArray.push(list.get(itemIndex));
  return List(newArray);
}
Example #9
0
function focusPreviousCellEditor(
  state: NotebookModel,
  action: actionTypes.FocusPreviousCellEditor
): RecordOf<DocumentRecordProps> {
  const cellOrder: List<CellId> = state.getIn(["notebook", "cellOrder"]);
  const curIndex = cellOrder.findIndex(
    (id: CellId) => id === action.payload.id
  );
  const nextIndex = Math.max(0, curIndex - 1);

  return state.set("editorFocused", cellOrder.get(nextIndex));
}
Example #10
0
function isTankCollidedWithRivers(rivers: List<boolean>, tankTarget: Rect, threshhold: number) {
  for (const t of IndexHelper.iter('river', tankTarget)) {
    if (rivers.get(t)) {
      const subject = IndexHelper.getRect('river', t)
      // 因为要考虑threshhold, 所以仍然要调用testCollide来判断是否相撞
      if (testCollide(subject, tankTarget, threshhold)) {
        return true
      }
    }
  }
  return false
}